Skip to content

Commit 7106d3b

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 00ad43f commit 7106d3b

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"
@@ -2078,6 +2079,106 @@ int link(const char *oldpath, const char *newpath)
20782079
return 0;
20792080
}
20802081

2082+
#ifndef _WINNT_H
2083+
/*
2084+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2085+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2086+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2087+
*/
2088+
typedef struct _REPARSE_DATA_BUFFER {
2089+
DWORD ReparseTag;
2090+
WORD ReparseDataLength;
2091+
WORD Reserved;
2092+
_ANONYMOUS_UNION union {
2093+
struct {
2094+
WORD SubstituteNameOffset;
2095+
WORD SubstituteNameLength;
2096+
WORD PrintNameOffset;
2097+
WORD PrintNameLength;
2098+
ULONG Flags;
2099+
WCHAR PathBuffer[1];
2100+
} SymbolicLinkReparseBuffer;
2101+
struct {
2102+
WORD SubstituteNameOffset;
2103+
WORD SubstituteNameLength;
2104+
WORD PrintNameOffset;
2105+
WORD PrintNameLength;
2106+
WCHAR PathBuffer[1];
2107+
} MountPointReparseBuffer;
2108+
struct {
2109+
BYTE DataBuffer[1];
2110+
} GenericReparseBuffer;
2111+
} DUMMYUNIONNAME;
2112+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
2113+
#endif
2114+
2115+
int readlink(const char *path, char *buf, size_t bufsiz)
2116+
{
2117+
HANDLE handle;
2118+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2119+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
2120+
DWORD dummy;
2121+
char tmpbuf[MAX_LONG_PATH];
2122+
int len;
2123+
2124+
/* fail if symlinks are disabled */
2125+
if (!has_symlinks) {
2126+
errno = ENOSYS;
2127+
return -1;
2128+
}
2129+
2130+
if (xutftowcs_long_path(wpath, path) < 0)
2131+
return -1;
2132+
2133+
/* read reparse point data */
2134+
handle = CreateFileW(wpath, 0,
2135+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2136+
OPEN_EXISTING,
2137+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2138+
if (handle == INVALID_HANDLE_VALUE) {
2139+
errno = err_win_to_posix(GetLastError());
2140+
return -1;
2141+
}
2142+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
2143+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
2144+
errno = err_win_to_posix(GetLastError());
2145+
CloseHandle(handle);
2146+
return -1;
2147+
}
2148+
CloseHandle(handle);
2149+
2150+
/* get target path for symlinks or mount points (aka 'junctions') */
2151+
switch (b->ReparseTag) {
2152+
case IO_REPARSE_TAG_SYMLINK:
2153+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
2154+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
2155+
*(WCHAR*) (((char*) wbuf)
2156+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
2157+
break;
2158+
case IO_REPARSE_TAG_MOUNT_POINT:
2159+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
2160+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
2161+
*(WCHAR*) (((char*) wbuf)
2162+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
2163+
break;
2164+
default:
2165+
errno = EINVAL;
2166+
return -1;
2167+
}
2168+
2169+
/*
2170+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
2171+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
2172+
* condition. There is no conversion function that produces invalid UTF-8,
2173+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
2174+
* the requested number of bytes (including '\0' for robustness).
2175+
*/
2176+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2177+
return -1;
2178+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
2179+
return min(bufsiz, len);
2180+
}
2181+
20812182
pid_t waitpid(pid_t pid, int *status, int options)
20822183
{
20832184
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)
@@ -204,6 +202,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
204202
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
205203
int link(const char *oldpath, const char *newpath);
206204
int uname(struct utsname *buf);
205+
int readlink(const char *path, char *buf, size_t bufsiz);
207206

208207
/*
209208
* replacements of existing functions

0 commit comments

Comments
 (0)