Skip to content

Commit 4f2f66e

Browse files
kblees마누엘
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 299852c commit 4f2f66e

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

compat/mingw.c

Lines changed: 98 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,103 @@ int link(const char *oldpath, const char *newpath)
20032004
return 0;
20042005
}
20052006

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