Skip to content

Commit e62f604

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 8f53367 commit e62f604

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

compat/mingw.c

Lines changed: 98 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"
@@ -2202,6 +2203,103 @@ int link(const char *oldpath, const char *newpath)
22022203
return 0;
22032204
}
22042205

2206+
#ifndef _WINNT_H
2207+
/*
2208+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2209+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2210+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2211+
*/
2212+
typedef struct _REPARSE_DATA_BUFFER {
2213+
DWORD ReparseTag;
2214+
WORD ReparseDataLength;
2215+
WORD Reserved;
2216+
#ifndef _MSC_VER
2217+
_ANONYMOUS_UNION
2218+
#endif
2219+
union {
2220+
struct {
2221+
WORD SubstituteNameOffset;
2222+
WORD SubstituteNameLength;
2223+
WORD PrintNameOffset;
2224+
WORD PrintNameLength;
2225+
ULONG Flags;
2226+
WCHAR PathBuffer[1];
2227+
} SymbolicLinkReparseBuffer;
2228+
struct {
2229+
WORD SubstituteNameOffset;
2230+
WORD SubstituteNameLength;
2231+
WORD PrintNameOffset;
2232+
WORD PrintNameLength;
2233+
WCHAR PathBuffer[1];
2234+
} MountPointReparseBuffer;
2235+
struct {
2236+
BYTE DataBuffer[1];
2237+
} GenericReparseBuffer;
2238+
} DUMMYUNIONNAME;
2239+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
2240+
#endif
2241+
2242+
int readlink(const char *path, char *buf, size_t bufsiz)
2243+
{
2244+
HANDLE handle;
2245+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2246+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
2247+
DWORD dummy;
2248+
char tmpbuf[MAX_LONG_PATH];
2249+
int len;
2250+
2251+
if (xutftowcs_long_path(wpath, path) < 0)
2252+
return -1;
2253+
2254+
/* read reparse point data */
2255+
handle = CreateFileW(wpath, 0,
2256+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2257+
OPEN_EXISTING,
2258+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2259+
if (handle == INVALID_HANDLE_VALUE) {
2260+
errno = err_win_to_posix(GetLastError());
2261+
return -1;
2262+
}
2263+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
2264+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
2265+
errno = err_win_to_posix(GetLastError());
2266+
CloseHandle(handle);
2267+
return -1;
2268+
}
2269+
CloseHandle(handle);
2270+
2271+
/* get target path for symlinks or mount points (aka 'junctions') */
2272+
switch (b->ReparseTag) {
2273+
case IO_REPARSE_TAG_SYMLINK:
2274+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
2275+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
2276+
*(WCHAR*) (((char*) wbuf)
2277+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
2278+
break;
2279+
case IO_REPARSE_TAG_MOUNT_POINT:
2280+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
2281+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
2282+
*(WCHAR*) (((char*) wbuf)
2283+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
2284+
break;
2285+
default:
2286+
errno = EINVAL;
2287+
return -1;
2288+
}
2289+
2290+
/*
2291+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
2292+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
2293+
* condition. There is no conversion function that produces invalid UTF-8,
2294+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
2295+
* the requested number of bytes (including '\0' for robustness).
2296+
*/
2297+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2298+
return -1;
2299+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
2300+
return min(bufsiz, len);
2301+
}
2302+
22052303
pid_t waitpid(pid_t pid, int *status, int options)
22062304
{
22072305
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)