Skip to content

Commit 72d2e97

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 1dbd59d commit 72d2e97

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"
@@ -2347,6 +2348,103 @@ int link(const char *oldpath, const char *newpath)
23472348
return 0;
23482349
}
23492350

2351+
#ifndef _WINNT_H
2352+
/*
2353+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2354+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2355+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2356+
*/
2357+
typedef struct _REPARSE_DATA_BUFFER {
2358+
DWORD ReparseTag;
2359+
WORD ReparseDataLength;
2360+
WORD Reserved;
2361+
#ifndef _MSC_VER
2362+
_ANONYMOUS_UNION
2363+
#endif
2364+
union {
2365+
struct {
2366+
WORD SubstituteNameOffset;
2367+
WORD SubstituteNameLength;
2368+
WORD PrintNameOffset;
2369+
WORD PrintNameLength;
2370+
ULONG Flags;
2371+
WCHAR PathBuffer[1];
2372+
} SymbolicLinkReparseBuffer;
2373+
struct {
2374+
WORD SubstituteNameOffset;
2375+
WORD SubstituteNameLength;
2376+
WORD PrintNameOffset;
2377+
WORD PrintNameLength;
2378+
WCHAR PathBuffer[1];
2379+
} MountPointReparseBuffer;
2380+
struct {
2381+
BYTE DataBuffer[1];
2382+
} GenericReparseBuffer;
2383+
} DUMMYUNIONNAME;
2384+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
2385+
#endif
2386+
2387+
int readlink(const char *path, char *buf, size_t bufsiz)
2388+
{
2389+
HANDLE handle;
2390+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2391+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
2392+
DWORD dummy;
2393+
char tmpbuf[MAX_LONG_PATH];
2394+
int len;
2395+
2396+
if (xutftowcs_long_path(wpath, path) < 0)
2397+
return -1;
2398+
2399+
/* read reparse point data */
2400+
handle = CreateFileW(wpath, 0,
2401+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2402+
OPEN_EXISTING,
2403+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2404+
if (handle == INVALID_HANDLE_VALUE) {
2405+
errno = err_win_to_posix(GetLastError());
2406+
return -1;
2407+
}
2408+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
2409+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
2410+
errno = err_win_to_posix(GetLastError());
2411+
CloseHandle(handle);
2412+
return -1;
2413+
}
2414+
CloseHandle(handle);
2415+
2416+
/* get target path for symlinks or mount points (aka 'junctions') */
2417+
switch (b->ReparseTag) {
2418+
case IO_REPARSE_TAG_SYMLINK:
2419+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
2420+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
2421+
*(WCHAR*) (((char*) wbuf)
2422+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
2423+
break;
2424+
case IO_REPARSE_TAG_MOUNT_POINT:
2425+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
2426+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
2427+
*(WCHAR*) (((char*) wbuf)
2428+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
2429+
break;
2430+
default:
2431+
errno = EINVAL;
2432+
return -1;
2433+
}
2434+
2435+
/*
2436+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
2437+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
2438+
* condition. There is no conversion function that produces invalid UTF-8,
2439+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
2440+
* the requested number of bytes (including '\0' for robustness).
2441+
*/
2442+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2443+
return -1;
2444+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
2445+
return min(bufsiz, len);
2446+
}
2447+
23502448
pid_t waitpid(pid_t pid, int *status, int options)
23512449
{
23522450
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)