|
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"
|
@@ -2354,6 +2355,103 @@ int link(const char *oldpath, const char *newpath)
|
2354 | 2355 | return 0;
|
2355 | 2356 | }
|
2356 | 2357 |
|
| 2358 | +#ifndef _WINNT_H |
| 2359 | +/* |
| 2360 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2361 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2362 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2363 | + */ |
| 2364 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2365 | + DWORD ReparseTag; |
| 2366 | + WORD ReparseDataLength; |
| 2367 | + WORD Reserved; |
| 2368 | +#ifndef _MSC_VER |
| 2369 | + _ANONYMOUS_UNION |
| 2370 | +#endif |
| 2371 | + union { |
| 2372 | + struct { |
| 2373 | + WORD SubstituteNameOffset; |
| 2374 | + WORD SubstituteNameLength; |
| 2375 | + WORD PrintNameOffset; |
| 2376 | + WORD PrintNameLength; |
| 2377 | + ULONG Flags; |
| 2378 | + WCHAR PathBuffer[1]; |
| 2379 | + } SymbolicLinkReparseBuffer; |
| 2380 | + struct { |
| 2381 | + WORD SubstituteNameOffset; |
| 2382 | + WORD SubstituteNameLength; |
| 2383 | + WORD PrintNameOffset; |
| 2384 | + WORD PrintNameLength; |
| 2385 | + WCHAR PathBuffer[1]; |
| 2386 | + } MountPointReparseBuffer; |
| 2387 | + struct { |
| 2388 | + BYTE DataBuffer[1]; |
| 2389 | + } GenericReparseBuffer; |
| 2390 | + } DUMMYUNIONNAME; |
| 2391 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2392 | +#endif |
| 2393 | + |
| 2394 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2395 | +{ |
| 2396 | + HANDLE handle; |
| 2397 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2398 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2399 | + DWORD dummy; |
| 2400 | + char tmpbuf[MAX_LONG_PATH]; |
| 2401 | + int len; |
| 2402 | + |
| 2403 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2404 | + return -1; |
| 2405 | + |
| 2406 | + /* read reparse point data */ |
| 2407 | + handle = CreateFileW(wpath, 0, |
| 2408 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2409 | + OPEN_EXISTING, |
| 2410 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2411 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2412 | + errno = err_win_to_posix(GetLastError()); |
| 2413 | + return -1; |
| 2414 | + } |
| 2415 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2416 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2417 | + errno = err_win_to_posix(GetLastError()); |
| 2418 | + CloseHandle(handle); |
| 2419 | + return -1; |
| 2420 | + } |
| 2421 | + CloseHandle(handle); |
| 2422 | + |
| 2423 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2424 | + switch (b->ReparseTag) { |
| 2425 | + case IO_REPARSE_TAG_SYMLINK: |
| 2426 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2427 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2428 | + *(WCHAR*) (((char*) wbuf) |
| 2429 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2430 | + break; |
| 2431 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2432 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2433 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2434 | + *(WCHAR*) (((char*) wbuf) |
| 2435 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2436 | + break; |
| 2437 | + default: |
| 2438 | + errno = EINVAL; |
| 2439 | + return -1; |
| 2440 | + } |
| 2441 | + |
| 2442 | + /* |
| 2443 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2444 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2445 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2446 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2447 | + * the requested number of bytes (including '\0' for robustness). |
| 2448 | + */ |
| 2449 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2450 | + return -1; |
| 2451 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2452 | + return min(bufsiz, len); |
| 2453 | +} |
| 2454 | + |
2357 | 2455 | pid_t waitpid(pid_t pid, int *status, int options)
|
2358 | 2456 | {
|
2359 | 2457 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments