Skip to content

Commit b5cd688

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 1aabe57 commit b5cd688

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)
@@ -2953,6 +2954,103 @@ int link(const char *oldpath, const char *newpath)
29532954
return 0;
29542955
}
29552956

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

0 commit comments

Comments
 (0)