|
7 | 7 | #include <sddl.h>
|
8 | 8 | #include <conio.h>
|
9 | 9 | #include <wchar.h>
|
| 10 | +#include <winioctl.h> |
10 | 11 | #include "../strbuf.h"
|
11 | 12 | #include "../run-command.h"
|
12 | 13 | #include "../abspath.h"
|
@@ -2988,6 +2989,103 @@ int link(const char *oldpath, const char *newpath)
|
2988 | 2989 | return 0;
|
2989 | 2990 | }
|
2990 | 2991 |
|
| 2992 | +#ifndef _WINNT_H |
| 2993 | +/* |
| 2994 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2995 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2996 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2997 | + */ |
| 2998 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2999 | + DWORD ReparseTag; |
| 3000 | + WORD ReparseDataLength; |
| 3001 | + WORD Reserved; |
| 3002 | +#ifndef _MSC_VER |
| 3003 | + _ANONYMOUS_UNION |
| 3004 | +#endif |
| 3005 | + union { |
| 3006 | + struct { |
| 3007 | + WORD SubstituteNameOffset; |
| 3008 | + WORD SubstituteNameLength; |
| 3009 | + WORD PrintNameOffset; |
| 3010 | + WORD PrintNameLength; |
| 3011 | + ULONG Flags; |
| 3012 | + WCHAR PathBuffer[1]; |
| 3013 | + } SymbolicLinkReparseBuffer; |
| 3014 | + struct { |
| 3015 | + WORD SubstituteNameOffset; |
| 3016 | + WORD SubstituteNameLength; |
| 3017 | + WORD PrintNameOffset; |
| 3018 | + WORD PrintNameLength; |
| 3019 | + WCHAR PathBuffer[1]; |
| 3020 | + } MountPointReparseBuffer; |
| 3021 | + struct { |
| 3022 | + BYTE DataBuffer[1]; |
| 3023 | + } GenericReparseBuffer; |
| 3024 | + } DUMMYUNIONNAME; |
| 3025 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 3026 | +#endif |
| 3027 | + |
| 3028 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 3029 | +{ |
| 3030 | + HANDLE handle; |
| 3031 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 3032 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 3033 | + DWORD dummy; |
| 3034 | + char tmpbuf[MAX_LONG_PATH]; |
| 3035 | + int len; |
| 3036 | + |
| 3037 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 3038 | + return -1; |
| 3039 | + |
| 3040 | + /* read reparse point data */ |
| 3041 | + handle = CreateFileW(wpath, 0, |
| 3042 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 3043 | + OPEN_EXISTING, |
| 3044 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 3045 | + if (handle == INVALID_HANDLE_VALUE) { |
| 3046 | + errno = err_win_to_posix(GetLastError()); |
| 3047 | + return -1; |
| 3048 | + } |
| 3049 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 3050 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 3051 | + errno = err_win_to_posix(GetLastError()); |
| 3052 | + CloseHandle(handle); |
| 3053 | + return -1; |
| 3054 | + } |
| 3055 | + CloseHandle(handle); |
| 3056 | + |
| 3057 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 3058 | + switch (b->ReparseTag) { |
| 3059 | + case IO_REPARSE_TAG_SYMLINK: |
| 3060 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 3061 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 3062 | + *(WCHAR*) (((char*) wbuf) |
| 3063 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 3064 | + break; |
| 3065 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 3066 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 3067 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 3068 | + *(WCHAR*) (((char*) wbuf) |
| 3069 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 3070 | + break; |
| 3071 | + default: |
| 3072 | + errno = EINVAL; |
| 3073 | + return -1; |
| 3074 | + } |
| 3075 | + |
| 3076 | + /* |
| 3077 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 3078 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 3079 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 3080 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 3081 | + * the requested number of bytes (including '\0' for robustness). |
| 3082 | + */ |
| 3083 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 3084 | + return -1; |
| 3085 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 3086 | + return min(bufsiz, len); |
| 3087 | +} |
| 3088 | + |
2991 | 3089 | pid_t waitpid(pid_t pid, int *status, int options)
|
2992 | 3090 | {
|
2993 | 3091 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments