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