Skip to content

Commit 461ada0

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 de612a0 commit 461ada0

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"
@@ -2081,6 +2082,106 @@ int link(const char *oldpath, const char *newpath)
20812082
return 0;
20822083
}
20832084

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