Skip to content

Commit bbdff29

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 b5ce5a5 commit bbdff29

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"
@@ -2901,6 +2902,103 @@ int link(const char *oldpath, const char *newpath)
29012902
return 0;
29022903
}
29032904

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