Skip to content

Commit 1996085

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 fac17e0 commit 1996085

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)
@@ -2963,6 +2964,103 @@ int link(const char *oldpath, const char *newpath)
29632964
return 0;
29642965
}
29652966

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

0 commit comments

Comments
 (0)