Skip to content

Commit fed86af

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 dd9be0d commit fed86af

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"
@@ -2003,6 +2004,106 @@ int link(const char *oldpath, const char *newpath)
20032004
return 0;
20042005
}
20052006

2007+
#ifndef _WINNT_H
2008+
/*
2009+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2010+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2011+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2012+
*/
2013+
typedef struct _REPARSE_DATA_BUFFER {
2014+
DWORD ReparseTag;
2015+
WORD ReparseDataLength;
2016+
WORD Reserved;
2017+
_ANONYMOUS_UNION union {
2018+
struct {
2019+
WORD SubstituteNameOffset;
2020+
WORD SubstituteNameLength;
2021+
WORD PrintNameOffset;
2022+
WORD PrintNameLength;
2023+
ULONG Flags;
2024+
WCHAR PathBuffer[1];
2025+
} SymbolicLinkReparseBuffer;
2026+
struct {
2027+
WORD SubstituteNameOffset;
2028+
WORD SubstituteNameLength;
2029+
WORD PrintNameOffset;
2030+
WORD PrintNameLength;
2031+
WCHAR PathBuffer[1];
2032+
} MountPointReparseBuffer;
2033+
struct {
2034+
BYTE DataBuffer[1];
2035+
} GenericReparseBuffer;
2036+
} DUMMYUNIONNAME;
2037+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
2038+
#endif
2039+
2040+
int readlink(const char *path, char *buf, size_t bufsiz)
2041+
{
2042+
HANDLE handle;
2043+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2044+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
2045+
DWORD dummy;
2046+
char tmpbuf[MAX_LONG_PATH];
2047+
int len;
2048+
2049+
/* fail if symlinks are disabled */
2050+
if (!has_symlinks) {
2051+
errno = ENOSYS;
2052+
return -1;
2053+
}
2054+
2055+
if (xutftowcs_long_path(wpath, path) < 0)
2056+
return -1;
2057+
2058+
/* read reparse point data */
2059+
handle = CreateFileW(wpath, 0,
2060+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2061+
OPEN_EXISTING,
2062+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2063+
if (handle == INVALID_HANDLE_VALUE) {
2064+
errno = err_win_to_posix(GetLastError());
2065+
return -1;
2066+
}
2067+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
2068+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
2069+
errno = err_win_to_posix(GetLastError());
2070+
CloseHandle(handle);
2071+
return -1;
2072+
}
2073+
CloseHandle(handle);
2074+
2075+
/* get target path for symlinks or mount points (aka 'junctions') */
2076+
switch (b->ReparseTag) {
2077+
case IO_REPARSE_TAG_SYMLINK:
2078+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
2079+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
2080+
*(WCHAR*) (((char*) wbuf)
2081+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
2082+
break;
2083+
case IO_REPARSE_TAG_MOUNT_POINT:
2084+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
2085+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
2086+
*(WCHAR*) (((char*) wbuf)
2087+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
2088+
break;
2089+
default:
2090+
errno = EINVAL;
2091+
return -1;
2092+
}
2093+
2094+
/*
2095+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
2096+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
2097+
* condition. There is no conversion function that produces invalid UTF-8,
2098+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
2099+
* the requested number of bytes (including '\0' for robustness).
2100+
*/
2101+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2102+
return -1;
2103+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
2104+
return min(bufsiz, len);
2105+
}
2106+
20062107
pid_t waitpid(pid_t pid, int *status, int options)
20072108
{
20082109
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)