Skip to content

Commit 2c021e9

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 72648c6 commit 2c021e9

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"
@@ -2097,6 +2098,106 @@ int link(const char *oldpath, const char *newpath)
20972098
return 0;
20982099
}
20992100

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

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ struct utsname {
110110
* trivial stubs
111111
*/
112112

113-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
114-
{ errno = ENOSYS; return -1; }
115113
static inline int symlink(const char *oldpath, const char *newpath)
116114
{ errno = ENOSYS; return -1; }
117115
static inline int fchmod(int fildes, mode_t mode)
@@ -205,6 +203,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
205203
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
206204
int link(const char *oldpath, const char *newpath);
207205
int uname(struct utsname *buf);
206+
int readlink(const char *path, char *buf, size_t bufsiz);
208207

209208
/*
210209
* replacements of existing functions

0 commit comments

Comments
 (0)