|
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"
|
@@ -2123,6 +2124,106 @@ int link(const char *oldpath, const char *newpath)
|
2123 | 2124 | return 0;
|
2124 | 2125 | }
|
2125 | 2126 |
|
| 2127 | +#ifndef _WINNT_H |
| 2128 | +/* |
| 2129 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2130 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2131 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2132 | + */ |
| 2133 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2134 | + DWORD ReparseTag; |
| 2135 | + WORD ReparseDataLength; |
| 2136 | + WORD Reserved; |
| 2137 | + _ANONYMOUS_UNION union { |
| 2138 | + struct { |
| 2139 | + WORD SubstituteNameOffset; |
| 2140 | + WORD SubstituteNameLength; |
| 2141 | + WORD PrintNameOffset; |
| 2142 | + WORD PrintNameLength; |
| 2143 | + ULONG Flags; |
| 2144 | + WCHAR PathBuffer[1]; |
| 2145 | + } SymbolicLinkReparseBuffer; |
| 2146 | + struct { |
| 2147 | + WORD SubstituteNameOffset; |
| 2148 | + WORD SubstituteNameLength; |
| 2149 | + WORD PrintNameOffset; |
| 2150 | + WORD PrintNameLength; |
| 2151 | + WCHAR PathBuffer[1]; |
| 2152 | + } MountPointReparseBuffer; |
| 2153 | + struct { |
| 2154 | + BYTE DataBuffer[1]; |
| 2155 | + } GenericReparseBuffer; |
| 2156 | + } DUMMYUNIONNAME; |
| 2157 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2158 | +#endif |
| 2159 | + |
| 2160 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2161 | +{ |
| 2162 | + HANDLE handle; |
| 2163 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2164 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2165 | + DWORD dummy; |
| 2166 | + char tmpbuf[MAX_LONG_PATH]; |
| 2167 | + int len; |
| 2168 | + |
| 2169 | + /* fail if symlinks are disabled */ |
| 2170 | + if (!has_symlinks) { |
| 2171 | + errno = ENOSYS; |
| 2172 | + return -1; |
| 2173 | + } |
| 2174 | + |
| 2175 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2176 | + return -1; |
| 2177 | + |
| 2178 | + /* read reparse point data */ |
| 2179 | + handle = CreateFileW(wpath, 0, |
| 2180 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2181 | + OPEN_EXISTING, |
| 2182 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2183 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2184 | + errno = err_win_to_posix(GetLastError()); |
| 2185 | + return -1; |
| 2186 | + } |
| 2187 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2188 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2189 | + errno = err_win_to_posix(GetLastError()); |
| 2190 | + CloseHandle(handle); |
| 2191 | + return -1; |
| 2192 | + } |
| 2193 | + CloseHandle(handle); |
| 2194 | + |
| 2195 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2196 | + switch (b->ReparseTag) { |
| 2197 | + case IO_REPARSE_TAG_SYMLINK: |
| 2198 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2199 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2200 | + *(WCHAR*) (((char*) wbuf) |
| 2201 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2202 | + break; |
| 2203 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2204 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2205 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2206 | + *(WCHAR*) (((char*) wbuf) |
| 2207 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2208 | + break; |
| 2209 | + default: |
| 2210 | + errno = EINVAL; |
| 2211 | + return -1; |
| 2212 | + } |
| 2213 | + |
| 2214 | + /* |
| 2215 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2216 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2217 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2218 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2219 | + * the requested number of bytes (including '\0' for robustness). |
| 2220 | + */ |
| 2221 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2222 | + return -1; |
| 2223 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2224 | + return min(bufsiz, len); |
| 2225 | +} |
| 2226 | + |
2126 | 2227 | pid_t waitpid(pid_t pid, int *status, int options)
|
2127 | 2228 | {
|
2128 | 2229 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments