|
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"
|
@@ -2581,6 +2582,103 @@ int link(const char *oldpath, const char *newpath)
|
2581 | 2582 | return 0;
|
2582 | 2583 | }
|
2583 | 2584 |
|
| 2585 | +#ifndef _WINNT_H |
| 2586 | +/* |
| 2587 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2588 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2589 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2590 | + */ |
| 2591 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2592 | + DWORD ReparseTag; |
| 2593 | + WORD ReparseDataLength; |
| 2594 | + WORD Reserved; |
| 2595 | +#ifndef _MSC_VER |
| 2596 | + _ANONYMOUS_UNION |
| 2597 | +#endif |
| 2598 | + union { |
| 2599 | + struct { |
| 2600 | + WORD SubstituteNameOffset; |
| 2601 | + WORD SubstituteNameLength; |
| 2602 | + WORD PrintNameOffset; |
| 2603 | + WORD PrintNameLength; |
| 2604 | + ULONG Flags; |
| 2605 | + WCHAR PathBuffer[1]; |
| 2606 | + } SymbolicLinkReparseBuffer; |
| 2607 | + struct { |
| 2608 | + WORD SubstituteNameOffset; |
| 2609 | + WORD SubstituteNameLength; |
| 2610 | + WORD PrintNameOffset; |
| 2611 | + WORD PrintNameLength; |
| 2612 | + WCHAR PathBuffer[1]; |
| 2613 | + } MountPointReparseBuffer; |
| 2614 | + struct { |
| 2615 | + BYTE DataBuffer[1]; |
| 2616 | + } GenericReparseBuffer; |
| 2617 | + } DUMMYUNIONNAME; |
| 2618 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2619 | +#endif |
| 2620 | + |
| 2621 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2622 | +{ |
| 2623 | + HANDLE handle; |
| 2624 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2625 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2626 | + DWORD dummy; |
| 2627 | + char tmpbuf[MAX_LONG_PATH]; |
| 2628 | + int len; |
| 2629 | + |
| 2630 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2631 | + return -1; |
| 2632 | + |
| 2633 | + /* read reparse point data */ |
| 2634 | + handle = CreateFileW(wpath, 0, |
| 2635 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2636 | + OPEN_EXISTING, |
| 2637 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2638 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2639 | + errno = err_win_to_posix(GetLastError()); |
| 2640 | + return -1; |
| 2641 | + } |
| 2642 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2643 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2644 | + errno = err_win_to_posix(GetLastError()); |
| 2645 | + CloseHandle(handle); |
| 2646 | + return -1; |
| 2647 | + } |
| 2648 | + CloseHandle(handle); |
| 2649 | + |
| 2650 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2651 | + switch (b->ReparseTag) { |
| 2652 | + case IO_REPARSE_TAG_SYMLINK: |
| 2653 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2654 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2655 | + *(WCHAR*) (((char*) wbuf) |
| 2656 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2657 | + break; |
| 2658 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2659 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2660 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2661 | + *(WCHAR*) (((char*) wbuf) |
| 2662 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2663 | + break; |
| 2664 | + default: |
| 2665 | + errno = EINVAL; |
| 2666 | + return -1; |
| 2667 | + } |
| 2668 | + |
| 2669 | + /* |
| 2670 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2671 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2672 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2673 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2674 | + * the requested number of bytes (including '\0' for robustness). |
| 2675 | + */ |
| 2676 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2677 | + return -1; |
| 2678 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2679 | + return min(bufsiz, len); |
| 2680 | +} |
| 2681 | + |
2584 | 2682 | pid_t waitpid(pid_t pid, int *status, int options)
|
2585 | 2683 | {
|
2586 | 2684 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments