Skip to content

Commit e568585

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 a66dc57 commit e568585

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
@@ -7,6 +7,7 @@
77
#include <sddl.h>
88
#include <conio.h>
99
#include <wchar.h>
10+
#include <winioctl.h>
1011
#include "../strbuf.h"
1112
#include "../run-command.h"
1213
#include "../abspath.h"
@@ -2990,6 +2991,103 @@ int link(const char *oldpath, const char *newpath)
29902991
return 0;
29912992
}
29922993

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