Skip to content

Commit 46f3778

Browse files
kbleesGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
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 031d261 commit 46f3778

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
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"
@@ -2954,6 +2955,103 @@ int link(const char *oldpath, const char *newpath)
29542955
return 0;
29552956
}
29562957

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

0 commit comments

Comments
 (0)