Skip to content

Commit 4496a88

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 3691dbd commit 4496a88

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
@@ -4,6 +4,7 @@
44
#include <wchar.h>
55
#include <aclapi.h>
66
#include <sddl.h>
7+
#include <winioctl.h>
78
#include "../strbuf.h"
89
#include "../run-command.h"
910
#include "../cache.h"
@@ -2226,6 +2227,103 @@ int link(const char *oldpath, const char *newpath)
22262227
return 0;
22272228
}
22282229

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