Skip to content

Commit beccee0

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 a6b988f commit beccee0

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
@@ -6,6 +6,7 @@
66
#include <sddl.h>
77
#include <conio.h>
88
#include <wchar.h>
9+
#include <winioctl.h>
910
#include "../strbuf.h"
1011
#include "../run-command.h"
1112
#include "../abspath.h"
@@ -2758,6 +2759,103 @@ int link(const char *oldpath, const char *newpath)
27582759
return 0;
27592760
}
27602761

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