Skip to content

Commit 692cffc

Browse files
kbleesmjcheetham
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 0b76ed8 commit 692cffc

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
@@ -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"
@@ -2724,6 +2725,103 @@ int link(const char *oldpath, const char *newpath)
27242725
return 0;
27252726
}
27262727

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

compat/mingw.h

Lines changed: 1 addition & 2 deletions
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)