Skip to content

Commit 4e7d782

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 a5ebcee commit 4e7d782

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
@@ -2,6 +2,7 @@
22
#include "win32.h"
33
#include <conio.h>
44
#include <wchar.h>
5+
#include <winioctl.h>
56
#include "../strbuf.h"
67
#include "../run-command.h"
78
#include "../cache.h"
@@ -2661,6 +2662,103 @@ int link(const char *oldpath, const char *newpath)
26612662
return 0;
26622663
}
26632664

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