Skip to content

Commit 74ba5d2

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 0801424 commit 74ba5d2

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

compat/mingw.c

+98
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sddl.h>
55
#include <conio.h>
66
#include <wchar.h>
7+
#include <winioctl.h>
78
#include "../strbuf.h"
89
#include "../run-command.h"
910
#include "../cache.h"
@@ -2706,6 +2707,103 @@ int link(const char *oldpath, const char *newpath)
27062707
return 0;
27072708
}
27082709

2710+
#ifndef _WINNT_H
2711+
/*
2712+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2713+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2714+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2715+
*/
2716+
typedef struct _REPARSE_DATA_BUFFER {
2717+
DWORD ReparseTag;
2718+
WORD ReparseDataLength;
2719+
WORD Reserved;
2720+
#ifndef _MSC_VER
2721+
_ANONYMOUS_UNION
2722+
#endif
2723+
union {
2724+
struct {
2725+
WORD SubstituteNameOffset;
2726+
WORD SubstituteNameLength;
2727+
WORD PrintNameOffset;
2728+
WORD PrintNameLength;
2729+
ULONG Flags;
2730+
WCHAR PathBuffer[1];
2731+
} SymbolicLinkReparseBuffer;
2732+
struct {
2733+
WORD SubstituteNameOffset;
2734+
WORD SubstituteNameLength;
2735+
WORD PrintNameOffset;
2736+
WORD PrintNameLength;
2737+
WCHAR PathBuffer[1];
2738+
} MountPointReparseBuffer;
2739+
struct {
2740+
BYTE DataBuffer[1];
2741+
} GenericReparseBuffer;
2742+
} DUMMYUNIONNAME;
2743+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
2744+
#endif
2745+
2746+
int readlink(const char *path, char *buf, size_t bufsiz)
2747+
{
2748+
HANDLE handle;
2749+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2750+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
2751+
DWORD dummy;
2752+
char tmpbuf[MAX_LONG_PATH];
2753+
int len;
2754+
2755+
if (xutftowcs_long_path(wpath, path) < 0)
2756+
return -1;
2757+
2758+
/* read reparse point data */
2759+
handle = CreateFileW(wpath, 0,
2760+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2761+
OPEN_EXISTING,
2762+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2763+
if (handle == INVALID_HANDLE_VALUE) {
2764+
errno = err_win_to_posix(GetLastError());
2765+
return -1;
2766+
}
2767+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
2768+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
2769+
errno = err_win_to_posix(GetLastError());
2770+
CloseHandle(handle);
2771+
return -1;
2772+
}
2773+
CloseHandle(handle);
2774+
2775+
/* get target path for symlinks or mount points (aka 'junctions') */
2776+
switch (b->ReparseTag) {
2777+
case IO_REPARSE_TAG_SYMLINK:
2778+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
2779+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
2780+
*(WCHAR*) (((char*) wbuf)
2781+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
2782+
break;
2783+
case IO_REPARSE_TAG_MOUNT_POINT:
2784+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
2785+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
2786+
*(WCHAR*) (((char*) wbuf)
2787+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
2788+
break;
2789+
default:
2790+
errno = EINVAL;
2791+
return -1;
2792+
}
2793+
2794+
/*
2795+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
2796+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
2797+
* condition. There is no conversion function that produces invalid UTF-8,
2798+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
2799+
* the requested number of bytes (including '\0' for robustness).
2800+
*/
2801+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2802+
return -1;
2803+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
2804+
return min(bufsiz, len);
2805+
}
2806+
27092807
pid_t waitpid(pid_t pid, int *status, int options)
27102808
{
27112809
HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,

compat/mingw.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ struct utsname {
125125
* trivial stubs
126126
*/
127127

128-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
129-
{ errno = ENOSYS; return -1; }
130128
static inline int symlink(const char *oldpath, const char *newpath)
131129
{ errno = ENOSYS; return -1; }
132130
static inline int fchmod(int fildes, mode_t mode)
@@ -219,6 +217,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
219217
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
220218
int link(const char *oldpath, const char *newpath);
221219
int uname(struct utsname *buf);
220+
int readlink(const char *path, char *buf, size_t bufsiz);
222221

223222
/*
224223
* replacements of existing functions

0 commit comments

Comments
 (0)