Skip to content

Commit c090e6a

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 e8565ba commit c090e6a

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
@@ -5,6 +5,7 @@
55
#include <sddl.h>
66
#include <conio.h>
77
#include <wchar.h>
8+
#include <winioctl.h>
89
#include "../strbuf.h"
910
#include "../run-command.h"
1011
#include "../abspath.h"
@@ -2753,6 +2754,103 @@ int link(const char *oldpath, const char *newpath)
27532754
return 0;
27542755
}
27552756

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