|
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"
|
@@ -2224,6 +2225,103 @@ int link(const char *oldpath, const char *newpath)
|
2224 | 2225 | return 0;
|
2225 | 2226 | }
|
2226 | 2227 |
|
| 2228 | +#ifndef _WINNT_H |
| 2229 | +/* |
| 2230 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2231 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2232 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2233 | + */ |
| 2234 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2235 | + DWORD ReparseTag; |
| 2236 | + WORD ReparseDataLength; |
| 2237 | + WORD Reserved; |
| 2238 | +#ifndef _MSC_VER |
| 2239 | + _ANONYMOUS_UNION |
| 2240 | +#endif |
| 2241 | + union { |
| 2242 | + struct { |
| 2243 | + WORD SubstituteNameOffset; |
| 2244 | + WORD SubstituteNameLength; |
| 2245 | + WORD PrintNameOffset; |
| 2246 | + WORD PrintNameLength; |
| 2247 | + ULONG Flags; |
| 2248 | + WCHAR PathBuffer[1]; |
| 2249 | + } SymbolicLinkReparseBuffer; |
| 2250 | + struct { |
| 2251 | + WORD SubstituteNameOffset; |
| 2252 | + WORD SubstituteNameLength; |
| 2253 | + WORD PrintNameOffset; |
| 2254 | + WORD PrintNameLength; |
| 2255 | + WCHAR PathBuffer[1]; |
| 2256 | + } MountPointReparseBuffer; |
| 2257 | + struct { |
| 2258 | + BYTE DataBuffer[1]; |
| 2259 | + } GenericReparseBuffer; |
| 2260 | + } DUMMYUNIONNAME; |
| 2261 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2262 | +#endif |
| 2263 | + |
| 2264 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2265 | +{ |
| 2266 | + HANDLE handle; |
| 2267 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2268 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2269 | + DWORD dummy; |
| 2270 | + char tmpbuf[MAX_LONG_PATH]; |
| 2271 | + int len; |
| 2272 | + |
| 2273 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2274 | + return -1; |
| 2275 | + |
| 2276 | + /* read reparse point data */ |
| 2277 | + handle = CreateFileW(wpath, 0, |
| 2278 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2279 | + OPEN_EXISTING, |
| 2280 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2281 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2282 | + errno = err_win_to_posix(GetLastError()); |
| 2283 | + return -1; |
| 2284 | + } |
| 2285 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2286 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2287 | + errno = err_win_to_posix(GetLastError()); |
| 2288 | + CloseHandle(handle); |
| 2289 | + return -1; |
| 2290 | + } |
| 2291 | + CloseHandle(handle); |
| 2292 | + |
| 2293 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2294 | + switch (b->ReparseTag) { |
| 2295 | + case IO_REPARSE_TAG_SYMLINK: |
| 2296 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2297 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2298 | + *(WCHAR*) (((char*) wbuf) |
| 2299 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2300 | + break; |
| 2301 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2302 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2303 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2304 | + *(WCHAR*) (((char*) wbuf) |
| 2305 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2306 | + break; |
| 2307 | + default: |
| 2308 | + errno = EINVAL; |
| 2309 | + return -1; |
| 2310 | + } |
| 2311 | + |
| 2312 | + /* |
| 2313 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2314 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2315 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2316 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2317 | + * the requested number of bytes (including '\0' for robustness). |
| 2318 | + */ |
| 2319 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2320 | + return -1; |
| 2321 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2322 | + return min(bufsiz, len); |
| 2323 | +} |
| 2324 | + |
2227 | 2325 | pid_t waitpid(pid_t pid, int *status, int options)
|
2228 | 2326 | {
|
2229 | 2327 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments