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