Skip to content

Commit afd3ef4

Browse files
kbleesGit for Windows Build Agent
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 8b58d6c commit afd3ef4

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

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

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ struct utsname {
117117
* trivial stubs
118118
*/
119119

120-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
121-
{ errno = ENOSYS; return -1; }
122120
static inline int symlink(const char *oldpath, const char *newpath)
123121
{ errno = ENOSYS; return -1; }
124122
static inline int fchmod(int fildes, mode_t mode)
@@ -212,6 +210,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
212210
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
213211
int link(const char *oldpath, const char *newpath);
214212
int uname(struct utsname *buf);
213+
int readlink(const char *path, char *buf, size_t bufsiz);
215214

216215
/*
217216
* replacements of existing functions

0 commit comments

Comments
 (0)