Skip to content

Commit ab65cc1

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 ecf35f7 commit ab65cc1

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

compat/mingw-posix.h

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

124-
static inline int readlink(const char *path UNUSED, char *buf UNUSED, size_t bufsiz UNUSED)
125-
{ errno = ENOSYS; return -1; }
126124
static inline int symlink(const char *oldpath UNUSED, const char *newpath UNUSED)
127125
{ errno = ENOSYS; return -1; }
128126
static inline int fchmod(int fildes UNUSED, mode_t mode UNUSED)
@@ -197,6 +195,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
197195
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
198196
int link(const char *oldpath, const char *newpath);
199197
int uname(struct utsname *buf);
198+
int readlink(const char *path, char *buf, size_t bufsiz);
200199

201200
/*
202201
* replacements of existing functions

compat/mingw.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define SECURITY_WIN32
2525
#include <sspi.h>
2626
#include <wchar.h>
27+
#include <winioctl.h>
2728
#include <winternl.h>
2829

2930
#define STATUS_DELETE_PENDING ((NTSTATUS) 0xC0000056)
@@ -2965,6 +2966,103 @@ int link(const char *oldpath, const char *newpath)
29652966
return 0;
29662967
}
29672968

2969+
#ifndef _WINNT_H
2970+
/*
2971+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2972+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2973+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2974+
*/
2975+
typedef struct _REPARSE_DATA_BUFFER {
2976+
DWORD ReparseTag;
2977+
WORD ReparseDataLength;
2978+
WORD Reserved;
2979+
#ifndef _MSC_VER
2980+
_ANONYMOUS_UNION
2981+
#endif
2982+
union {
2983+
struct {
2984+
WORD SubstituteNameOffset;
2985+
WORD SubstituteNameLength;
2986+
WORD PrintNameOffset;
2987+
WORD PrintNameLength;
2988+
ULONG Flags;
2989+
WCHAR PathBuffer[1];
2990+
} SymbolicLinkReparseBuffer;
2991+
struct {
2992+
WORD SubstituteNameOffset;
2993+
WORD SubstituteNameLength;
2994+
WORD PrintNameOffset;
2995+
WORD PrintNameLength;
2996+
WCHAR PathBuffer[1];
2997+
} MountPointReparseBuffer;
2998+
struct {
2999+
BYTE DataBuffer[1];
3000+
} GenericReparseBuffer;
3001+
} DUMMYUNIONNAME;
3002+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
3003+
#endif
3004+
3005+
int readlink(const char *path, char *buf, size_t bufsiz)
3006+
{
3007+
HANDLE handle;
3008+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
3009+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
3010+
DWORD dummy;
3011+
char tmpbuf[MAX_LONG_PATH];
3012+
int len;
3013+
3014+
if (xutftowcs_long_path(wpath, path) < 0)
3015+
return -1;
3016+
3017+
/* read reparse point data */
3018+
handle = CreateFileW(wpath, 0,
3019+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
3020+
OPEN_EXISTING,
3021+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
3022+
if (handle == INVALID_HANDLE_VALUE) {
3023+
errno = err_win_to_posix(GetLastError());
3024+
return -1;
3025+
}
3026+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
3027+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
3028+
errno = err_win_to_posix(GetLastError());
3029+
CloseHandle(handle);
3030+
return -1;
3031+
}
3032+
CloseHandle(handle);
3033+
3034+
/* get target path for symlinks or mount points (aka 'junctions') */
3035+
switch (b->ReparseTag) {
3036+
case IO_REPARSE_TAG_SYMLINK:
3037+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
3038+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
3039+
*(WCHAR*) (((char*) wbuf)
3040+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
3041+
break;
3042+
case IO_REPARSE_TAG_MOUNT_POINT:
3043+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
3044+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
3045+
*(WCHAR*) (((char*) wbuf)
3046+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
3047+
break;
3048+
default:
3049+
errno = EINVAL;
3050+
return -1;
3051+
}
3052+
3053+
/*
3054+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
3055+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
3056+
* condition. There is no conversion function that produces invalid UTF-8,
3057+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
3058+
* the requested number of bytes (including '\0' for robustness).
3059+
*/
3060+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
3061+
return -1;
3062+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
3063+
return min(bufsiz, len);
3064+
}
3065+
29683066
pid_t waitpid(pid_t pid, int *status, int options)
29693067
{
29703068
HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,

0 commit comments

Comments
 (0)