Skip to content

Commit 28e8491

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 c5ae21b commit 28e8491

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"
@@ -2691,6 +2692,103 @@ int link(const char *oldpath, const char *newpath)
26912692
return 0;
26922693
}
26932694

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

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ struct utsname {
123123
* trivial stubs
124124
*/
125125

126-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
127-
{ errno = ENOSYS; return -1; }
128126
static inline int symlink(const char *oldpath, const char *newpath)
129127
{ errno = ENOSYS; return -1; }
130128
static inline int fchmod(int fildes, mode_t mode)
@@ -217,6 +215,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
217215
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
218216
int link(const char *oldpath, const char *newpath);
219217
int uname(struct utsname *buf);
218+
int readlink(const char *path, char *buf, size_t bufsiz);
220219

221220
/*
222221
* replacements of existing functions

0 commit comments

Comments
 (0)