Skip to content

Commit 472e9a0

Browse files
kbleesvdye
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 7219a46 commit 472e9a0

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
@@ -3,6 +3,7 @@
33
#include <aclapi.h>
44
#include <conio.h>
55
#include <wchar.h>
6+
#include <winioctl.h>
67
#include "../strbuf.h"
78
#include "../run-command.h"
89
#include "../cache.h"
@@ -2662,6 +2663,103 @@ int link(const char *oldpath, const char *newpath)
26622663
return 0;
26632664
}
26642665

2666+
#ifndef _WINNT_H
2667+
/*
2668+
* The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in
2669+
* ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define
2670+
* it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_).
2671+
*/
2672+
typedef struct _REPARSE_DATA_BUFFER {
2673+
DWORD ReparseTag;
2674+
WORD ReparseDataLength;
2675+
WORD Reserved;
2676+
#ifndef _MSC_VER
2677+
_ANONYMOUS_UNION
2678+
#endif
2679+
union {
2680+
struct {
2681+
WORD SubstituteNameOffset;
2682+
WORD SubstituteNameLength;
2683+
WORD PrintNameOffset;
2684+
WORD PrintNameLength;
2685+
ULONG Flags;
2686+
WCHAR PathBuffer[1];
2687+
} SymbolicLinkReparseBuffer;
2688+
struct {
2689+
WORD SubstituteNameOffset;
2690+
WORD SubstituteNameLength;
2691+
WORD PrintNameOffset;
2692+
WORD PrintNameLength;
2693+
WCHAR PathBuffer[1];
2694+
} MountPointReparseBuffer;
2695+
struct {
2696+
BYTE DataBuffer[1];
2697+
} GenericReparseBuffer;
2698+
} DUMMYUNIONNAME;
2699+
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
2700+
#endif
2701+
2702+
int readlink(const char *path, char *buf, size_t bufsiz)
2703+
{
2704+
HANDLE handle;
2705+
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2706+
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
2707+
DWORD dummy;
2708+
char tmpbuf[MAX_LONG_PATH];
2709+
int len;
2710+
2711+
if (xutftowcs_long_path(wpath, path) < 0)
2712+
return -1;
2713+
2714+
/* read reparse point data */
2715+
handle = CreateFileW(wpath, 0,
2716+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2717+
OPEN_EXISTING,
2718+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2719+
if (handle == INVALID_HANDLE_VALUE) {
2720+
errno = err_win_to_posix(GetLastError());
2721+
return -1;
2722+
}
2723+
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b,
2724+
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) {
2725+
errno = err_win_to_posix(GetLastError());
2726+
CloseHandle(handle);
2727+
return -1;
2728+
}
2729+
CloseHandle(handle);
2730+
2731+
/* get target path for symlinks or mount points (aka 'junctions') */
2732+
switch (b->ReparseTag) {
2733+
case IO_REPARSE_TAG_SYMLINK:
2734+
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
2735+
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
2736+
*(WCHAR*) (((char*) wbuf)
2737+
+ b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0;
2738+
break;
2739+
case IO_REPARSE_TAG_MOUNT_POINT:
2740+
wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer)
2741+
+ b->MountPointReparseBuffer.SubstituteNameOffset);
2742+
*(WCHAR*) (((char*) wbuf)
2743+
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
2744+
break;
2745+
default:
2746+
errno = EINVAL;
2747+
return -1;
2748+
}
2749+
2750+
/*
2751+
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
2752+
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
2753+
* condition. There is no conversion function that produces invalid UTF-8,
2754+
* so convert to a (hopefully large enough) temporary buffer, then memcpy
2755+
* the requested number of bytes (including '\0' for robustness).
2756+
*/
2757+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2758+
return -1;
2759+
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
2760+
return min(bufsiz, len);
2761+
}
2762+
26652763
pid_t waitpid(pid_t pid, int *status, int options)
26662764
{
26672765
HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,

compat/mingw.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ struct utsname {
123123
* trivial stubs
124124
*/
125125

126-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
127-
{ errno = ENOSYS; return -1; }
128126
static inline int symlink(const char *oldpath, const char *newpath)
129127
{ errno = ENOSYS; return -1; }
130128
static inline int fchmod(int fildes, mode_t mode)
@@ -217,6 +215,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
217215
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
218216
int link(const char *oldpath, const char *newpath);
219217
int uname(struct utsname *buf);
218+
int readlink(const char *path, char *buf, size_t bufsiz);
220219

221220
/*
222221
* replacements of existing functions

0 commit comments

Comments
 (0)