Skip to content

Commit e27b040

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 11901a1 commit e27b040

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"
@@ -2037,6 +2038,106 @@ int link(const char *oldpath, const char *newpath)
20372038
return 0;
20382039
}
20392040

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

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ struct itimerval {
9393
* trivial stubs
9494
*/
9595

96-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
97-
{ errno = ENOSYS; return -1; }
9896
static inline int symlink(const char *oldpath, const char *newpath)
9997
{ errno = ENOSYS; return -1; }
10098
static inline int fchmod(int fildes, mode_t mode)
@@ -182,6 +180,7 @@ struct passwd *getpwuid(uid_t uid);
182180
int setitimer(int type, struct itimerval *in, struct itimerval *out);
183181
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
184182
int link(const char *oldpath, const char *newpath);
183+
int readlink(const char *path, char *buf, size_t bufsiz);
185184

186185
/*
187186
* replacements of existing functions

0 commit comments

Comments
 (0)