Skip to content

Commit 05674f8

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 80840ef commit 05674f8

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"
@@ -2218,6 +2219,103 @@ int link(const char *oldpath, const char *newpath)
22182219
return 0;
22192220
}
22202221

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

221220
/*
222221
* replacements of existing functions

0 commit comments

Comments
 (0)