|
2 | 2 | #include "win32.h"
|
3 | 3 | #include <conio.h>
|
4 | 4 | #include <wchar.h>
|
| 5 | +#include <winioctl.h> |
5 | 6 | #include "../strbuf.h"
|
6 | 7 | #include "../run-command.h"
|
7 | 8 | #include "../cache.h"
|
@@ -2038,6 +2039,106 @@ int link(const char *oldpath, const char *newpath)
|
2038 | 2039 | return 0;
|
2039 | 2040 | }
|
2040 | 2041 |
|
| 2042 | +#ifndef _WINNT_H |
| 2043 | +/* |
| 2044 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2045 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2046 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2047 | + */ |
| 2048 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2049 | + DWORD ReparseTag; |
| 2050 | + WORD ReparseDataLength; |
| 2051 | + WORD Reserved; |
| 2052 | + _ANONYMOUS_UNION union { |
| 2053 | + struct { |
| 2054 | + WORD SubstituteNameOffset; |
| 2055 | + WORD SubstituteNameLength; |
| 2056 | + WORD PrintNameOffset; |
| 2057 | + WORD PrintNameLength; |
| 2058 | + ULONG Flags; |
| 2059 | + WCHAR PathBuffer[1]; |
| 2060 | + } SymbolicLinkReparseBuffer; |
| 2061 | + struct { |
| 2062 | + WORD SubstituteNameOffset; |
| 2063 | + WORD SubstituteNameLength; |
| 2064 | + WORD PrintNameOffset; |
| 2065 | + WORD PrintNameLength; |
| 2066 | + WCHAR PathBuffer[1]; |
| 2067 | + } MountPointReparseBuffer; |
| 2068 | + struct { |
| 2069 | + BYTE DataBuffer[1]; |
| 2070 | + } GenericReparseBuffer; |
| 2071 | + } DUMMYUNIONNAME; |
| 2072 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2073 | +#endif |
| 2074 | + |
| 2075 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2076 | +{ |
| 2077 | + HANDLE handle; |
| 2078 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2079 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2080 | + DWORD dummy; |
| 2081 | + char tmpbuf[MAX_LONG_PATH]; |
| 2082 | + int len; |
| 2083 | + |
| 2084 | + /* fail if symlinks are disabled */ |
| 2085 | + if (!has_symlinks) { |
| 2086 | + errno = ENOSYS; |
| 2087 | + return -1; |
| 2088 | + } |
| 2089 | + |
| 2090 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2091 | + return -1; |
| 2092 | + |
| 2093 | + /* read reparse point data */ |
| 2094 | + handle = CreateFileW(wpath, 0, |
| 2095 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2096 | + OPEN_EXISTING, |
| 2097 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2098 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2099 | + errno = err_win_to_posix(GetLastError()); |
| 2100 | + return -1; |
| 2101 | + } |
| 2102 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2103 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2104 | + errno = err_win_to_posix(GetLastError()); |
| 2105 | + CloseHandle(handle); |
| 2106 | + return -1; |
| 2107 | + } |
| 2108 | + CloseHandle(handle); |
| 2109 | + |
| 2110 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2111 | + switch (b->ReparseTag) { |
| 2112 | + case IO_REPARSE_TAG_SYMLINK: |
| 2113 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2114 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2115 | + *(WCHAR*) (((char*) wbuf) |
| 2116 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2117 | + break; |
| 2118 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2119 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2120 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2121 | + *(WCHAR*) (((char*) wbuf) |
| 2122 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2123 | + break; |
| 2124 | + default: |
| 2125 | + errno = EINVAL; |
| 2126 | + return -1; |
| 2127 | + } |
| 2128 | + |
| 2129 | + /* |
| 2130 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2131 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2132 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2133 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2134 | + * the requested number of bytes (including '\0' for robustness). |
| 2135 | + */ |
| 2136 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2137 | + return -1; |
| 2138 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2139 | + return min(bufsiz, len); |
| 2140 | +} |
| 2141 | + |
2041 | 2142 | pid_t waitpid(pid_t pid, int *status, int options)
|
2042 | 2143 | {
|
2043 | 2144 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments