Skip to content

Commit f4b3ec8

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 b45867c commit f4b3ec8

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"
@@ -2083,6 +2084,106 @@ int link(const char *oldpath, const char *newpath)
20832084
return 0;
20842085
}
20852086

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

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ struct utsname {
117117
* trivial stubs
118118
*/
119119

120-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
121-
{ errno = ENOSYS; return -1; }
122120
static inline int symlink(const char *oldpath, const char *newpath)
123121
{ errno = ENOSYS; return -1; }
124122
static inline int fchmod(int fildes, mode_t mode)
@@ -212,6 +210,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
212210
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
213211
int link(const char *oldpath, const char *newpath);
214212
int uname(struct utsname *buf);
213+
int readlink(const char *path, char *buf, size_t bufsiz);
215214

216215
/*
217216
* replacements of existing functions

0 commit comments

Comments
 (0)