Skip to content

Commit d18741b

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 88f96d3 commit d18741b

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"
@@ -2054,6 +2055,106 @@ int link(const char *oldpath, const char *newpath)
20542055
return 0;
20552056
}
20562057

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

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ struct utsname {
101101
* trivial stubs
102102
*/
103103

104-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
105-
{ errno = ENOSYS; return -1; }
106104
static inline int symlink(const char *oldpath, const char *newpath)
107105
{ errno = ENOSYS; return -1; }
108106
static inline int fchmod(int fildes, mode_t mode)
@@ -195,6 +193,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
195193
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
196194
int link(const char *oldpath, const char *newpath);
197195
int uname(struct utsname *buf);
196+
int readlink(const char *path, char *buf, size_t bufsiz);
198197

199198
/*
200199
* replacements of existing functions

0 commit comments

Comments
 (0)