Skip to content

Commit 63128ed

Browse files
kbleesGit for Windows Build Agent
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 116b615 commit 63128ed

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
@@ -120,8 +120,6 @@ struct utsname {
120120
* trivial stubs
121121
*/
122122

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

200199
/*
201200
* 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)
@@ -2961,6 +2962,103 @@ int link(const char *oldpath, const char *newpath)
29612962
return 0;
29622963
}
29632964

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

0 commit comments

Comments
 (0)