|
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"
|
@@ -2531,6 +2532,103 @@ int link(const char *oldpath, const char *newpath)
|
2531 | 2532 | return 0;
|
2532 | 2533 | }
|
2533 | 2534 |
|
| 2535 | +#ifndef _WINNT_H |
| 2536 | +/* |
| 2537 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2538 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2539 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2540 | + */ |
| 2541 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2542 | + DWORD ReparseTag; |
| 2543 | + WORD ReparseDataLength; |
| 2544 | + WORD Reserved; |
| 2545 | +#ifndef _MSC_VER |
| 2546 | + _ANONYMOUS_UNION |
| 2547 | +#endif |
| 2548 | + union { |
| 2549 | + struct { |
| 2550 | + WORD SubstituteNameOffset; |
| 2551 | + WORD SubstituteNameLength; |
| 2552 | + WORD PrintNameOffset; |
| 2553 | + WORD PrintNameLength; |
| 2554 | + ULONG Flags; |
| 2555 | + WCHAR PathBuffer[1]; |
| 2556 | + } SymbolicLinkReparseBuffer; |
| 2557 | + struct { |
| 2558 | + WORD SubstituteNameOffset; |
| 2559 | + WORD SubstituteNameLength; |
| 2560 | + WORD PrintNameOffset; |
| 2561 | + WORD PrintNameLength; |
| 2562 | + WCHAR PathBuffer[1]; |
| 2563 | + } MountPointReparseBuffer; |
| 2564 | + struct { |
| 2565 | + BYTE DataBuffer[1]; |
| 2566 | + } GenericReparseBuffer; |
| 2567 | + } DUMMYUNIONNAME; |
| 2568 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2569 | +#endif |
| 2570 | + |
| 2571 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2572 | +{ |
| 2573 | + HANDLE handle; |
| 2574 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2575 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2576 | + DWORD dummy; |
| 2577 | + char tmpbuf[MAX_LONG_PATH]; |
| 2578 | + int len; |
| 2579 | + |
| 2580 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2581 | + return -1; |
| 2582 | + |
| 2583 | + /* read reparse point data */ |
| 2584 | + handle = CreateFileW(wpath, 0, |
| 2585 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2586 | + OPEN_EXISTING, |
| 2587 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2588 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2589 | + errno = err_win_to_posix(GetLastError()); |
| 2590 | + return -1; |
| 2591 | + } |
| 2592 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2593 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2594 | + errno = err_win_to_posix(GetLastError()); |
| 2595 | + CloseHandle(handle); |
| 2596 | + return -1; |
| 2597 | + } |
| 2598 | + CloseHandle(handle); |
| 2599 | + |
| 2600 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2601 | + switch (b->ReparseTag) { |
| 2602 | + case IO_REPARSE_TAG_SYMLINK: |
| 2603 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2604 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2605 | + *(WCHAR*) (((char*) wbuf) |
| 2606 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2607 | + break; |
| 2608 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2609 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2610 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2611 | + *(WCHAR*) (((char*) wbuf) |
| 2612 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2613 | + break; |
| 2614 | + default: |
| 2615 | + errno = EINVAL; |
| 2616 | + return -1; |
| 2617 | + } |
| 2618 | + |
| 2619 | + /* |
| 2620 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2621 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2622 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2623 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2624 | + * the requested number of bytes (including '\0' for robustness). |
| 2625 | + */ |
| 2626 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2627 | + return -1; |
| 2628 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2629 | + return min(bufsiz, len); |
| 2630 | +} |
| 2631 | + |
2534 | 2632 | pid_t waitpid(pid_t pid, int *status, int options)
|
2535 | 2633 | {
|
2536 | 2634 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments