Skip to content

Commit 3e708df

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 771b29b commit 3e708df

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"
@@ -2930,6 +2931,103 @@ int link(const char *oldpath, const char *newpath)
29302931
return 0;
29312932
}
29322933

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