Skip to content

Commit e357424

Browse files
committed
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 92cfb95 commit e357424

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
@@ -3,6 +3,7 @@
33
#include <conio.h>
44
#include <shlobj.h>
55
#include <wchar.h>
6+
#include <winioctl.h>
67
#include "../strbuf.h"
78
#include "../run-command.h"
89
#include "../cache.h"
@@ -2011,6 +2012,103 @@ int link(const char *oldpath, const char *newpath)
20112012
return 0;
20122013
}
20132014

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