Skip to content

Commit dbef513

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 533ae66 commit dbef513

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 "../abspath.h"
@@ -2743,6 +2744,103 @@ int link(const char *oldpath, const char *newpath)
27432744
return 0;
27442745
}
27452746

2747+
#ifndef _WINNT_H
2748+
/*
2749+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2750+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2751+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2752+
*/
2753+
typedef struct _REPARSE_DATA_BUFFER {
2754+
DWORD ReparseTag;
2755+
WORD ReparseDataLength;
2756+
WORD Reserved;
2757+
#ifndef _MSC_VER
2758+
_ANONYMOUS_UNION
2759+
#endif
2760+
union {
2761+
struct {
2762+
WORD SubstituteNameOffset;
2763+
WORD SubstituteNameLength;
2764+
WORD PrintNameOffset;
2765+
WORD PrintNameLength;
2766+
ULONG Flags;
2767+
WCHAR PathBuffer[1];
2768+
} SymbolicLinkReparseBuffer;
2769+
struct {
2770+
WORD SubstituteNameOffset;
2771+
WORD SubstituteNameLength;
2772+
WORD PrintNameOffset;
2773+
WORD PrintNameLength;
2774+
WCHAR PathBuffer[1];
2775+
} MountPointReparseBuffer;
2776+
struct {
2777+
BYTE DataBuffer[1];
2778+
} GenericReparseBuffer;
2779+
} DUMMYUNIONNAME;
2780+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
2781+
#endif
2782+
2783+
int readlink(const char *path, char *buf, size_t bufsiz)
2784+
{
2785+
HANDLE handle;
2786+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2787+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
2788+
DWORD dummy;
2789+
char tmpbuf[MAX_LONG_PATH];
2790+
int len;
2791+
2792+
if (xutftowcs_long_path(wpath, path) < 0)
2793+
return -1;
2794+
2795+
/* read reparse point data */
2796+
handle = CreateFileW(wpath, 0,
2797+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2798+
OPEN_EXISTING,
2799+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2800+
if (handle == INVALID_HANDLE_VALUE) {
2801+
errno = err_win_to_posix(GetLastError());
2802+
return -1;
2803+
}
2804+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
2805+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
2806+
errno = err_win_to_posix(GetLastError());
2807+
CloseHandle(handle);
2808+
return -1;
2809+
}
2810+
CloseHandle(handle);
2811+
2812+
/* get target path for symlinks or mount points (aka 'junctions') */
2813+
switch (b->ReparseTag) {
2814+
case IO_REPARSE_TAG_SYMLINK:
2815+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
2816+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
2817+
*(WCHAR*) (((char*) wbuf)
2818+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
2819+
break;
2820+
case IO_REPARSE_TAG_MOUNT_POINT:
2821+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
2822+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
2823+
*(WCHAR*) (((char*) wbuf)
2824+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
2825+
break;
2826+
default:
2827+
errno = EINVAL;
2828+
return -1;
2829+
}
2830+
2831+
/*
2832+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
2833+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
2834+
* condition. There is no conversion function that produces invalid UTF-8,
2835+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
2836+
* the requested number of bytes (including '\0' for robustness).
2837+
*/
2838+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2839+
return -1;
2840+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
2841+
return min(bufsiz, len);
2842+
}
2843+
27462844
pid_t waitpid(pid_t pid, int *status, int options)
27472845
{
27482846
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)
@@ -222,6 +220,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
222220
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
223221
int link(const char *oldpath, const char *newpath);
224222
int uname(struct utsname *buf);
223+
int readlink(const char *path, char *buf, size_t bufsiz);
225224

226225
/*
227226
* replacements of existing functions

0 commit comments

Comments
 (0)