Skip to content

Commit 822c636

Browse files
kbleesdscho
authored andcommitted
Win32: implement readlink()
Implement readlink() by reading NTFS reparse points. Works for symlinks and directory junctions. If symlinks are disabled, fail with ENOSYS. Signed-off-by: Karsten Blees <[email protected]>
1 parent d17e776 commit 822c636

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

compat/mingw.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "win32.h"
33
#include <conio.h>
44
#include <wchar.h>
5+
#include <winioctl.h>
56
#include "../strbuf.h"
67
#include "../run-command.h"
78
#include "../cache.h"
@@ -2123,6 +2124,106 @@ int link(const char *oldpath, const char *newpath)
21232124
return 0;
21242125
}
21252126

2127+
#ifndef _WINNT_H
2128+
/*
2129+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2130+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2131+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2132+
*/
2133+
typedef struct _REPARSE_DATA_BUFFER {
2134+
DWORD ReparseTag;
2135+
WORD ReparseDataLength;
2136+
WORD Reserved;
2137+
_ANONYMOUS_UNION union {
2138+
struct {
2139+
WORD SubstituteNameOffset;
2140+
WORD SubstituteNameLength;
2141+
WORD PrintNameOffset;
2142+
WORD PrintNameLength;
2143+
ULONG Flags;
2144+
WCHAR PathBuffer[1];
2145+
} SymbolicLinkReparseBuffer;
2146+
struct {
2147+
WORD SubstituteNameOffset;
2148+
WORD SubstituteNameLength;
2149+
WORD PrintNameOffset;
2150+
WORD PrintNameLength;
2151+
WCHAR PathBuffer[1];
2152+
} MountPointReparseBuffer;
2153+
struct {
2154+
BYTE DataBuffer[1];
2155+
} GenericReparseBuffer;
2156+
} DUMMYUNIONNAME;
2157+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
2158+
#endif
2159+
2160+
int readlink(const char *path, char *buf, size_t bufsiz)
2161+
{
2162+
HANDLE handle;
2163+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2164+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
2165+
DWORD dummy;
2166+
char tmpbuf[MAX_LONG_PATH];
2167+
int len;
2168+
2169+
/* fail if symlinks are disabled */
2170+
if (!has_symlinks) {
2171+
errno = ENOSYS;
2172+
return -1;
2173+
}
2174+
2175+
if (xutftowcs_long_path(wpath, path) < 0)
2176+
return -1;
2177+
2178+
/* read reparse point data */
2179+
handle = CreateFileW(wpath, 0,
2180+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2181+
OPEN_EXISTING,
2182+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2183+
if (handle == INVALID_HANDLE_VALUE) {
2184+
errno = err_win_to_posix(GetLastError());
2185+
return -1;
2186+
}
2187+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
2188+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
2189+
errno = err_win_to_posix(GetLastError());
2190+
CloseHandle(handle);
2191+
return -1;
2192+
}
2193+
CloseHandle(handle);
2194+
2195+
/* get target path for symlinks or mount points (aka 'junctions') */
2196+
switch (b->ReparseTag) {
2197+
case IO_REPARSE_TAG_SYMLINK:
2198+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
2199+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
2200+
*(WCHAR*) (((char*) wbuf)
2201+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
2202+
break;
2203+
case IO_REPARSE_TAG_MOUNT_POINT:
2204+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
2205+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
2206+
*(WCHAR*) (((char*) wbuf)
2207+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
2208+
break;
2209+
default:
2210+
errno = EINVAL;
2211+
return -1;
2212+
}
2213+
2214+
/*
2215+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
2216+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
2217+
* condition. There is no conversion function that produces invalid UTF-8,
2218+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
2219+
* the requested number of bytes (including '\0' for robustness).
2220+
*/
2221+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2222+
return -1;
2223+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
2224+
return min(bufsiz, len);
2225+
}
2226+
21262227
pid_t waitpid(pid_t pid, int *status, int options)
21272228
{
21282229
HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ struct utsname {
123123
* trivial stubs
124124
*/
125125

126-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
127-
{ errno = ENOSYS; return -1; }
128126
static inline int symlink(const char *oldpath, const char *newpath)
129127
{ errno = ENOSYS; return -1; }
130128
static inline int fchmod(int fildes, mode_t mode)
@@ -218,6 +216,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
218216
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
219217
int link(const char *oldpath, const char *newpath);
220218
int uname(struct utsname *buf);
219+
int readlink(const char *path, char *buf, size_t bufsiz);
221220

222221
/*
223222
* replacements of existing functions

0 commit comments

Comments
 (0)