|
4 | 4 | #include <sddl.h>
|
5 | 5 | #include <conio.h>
|
6 | 6 | #include <wchar.h>
|
| 7 | +#include <winioctl.h> |
7 | 8 | #include "../strbuf.h"
|
8 | 9 | #include "../run-command.h"
|
9 | 10 | #include "../cache.h"
|
@@ -2715,6 +2716,103 @@ int link(const char *oldpath, const char *newpath)
|
2715 | 2716 | return 0;
|
2716 | 2717 | }
|
2717 | 2718 |
|
| 2719 | +#ifndef _WINNT_H |
| 2720 | +/* |
| 2721 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2722 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2723 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2724 | + */ |
| 2725 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2726 | + DWORD ReparseTag; |
| 2727 | + WORD ReparseDataLength; |
| 2728 | + WORD Reserved; |
| 2729 | +#ifndef _MSC_VER |
| 2730 | + _ANONYMOUS_UNION |
| 2731 | +#endif |
| 2732 | + union { |
| 2733 | + struct { |
| 2734 | + WORD SubstituteNameOffset; |
| 2735 | + WORD SubstituteNameLength; |
| 2736 | + WORD PrintNameOffset; |
| 2737 | + WORD PrintNameLength; |
| 2738 | + ULONG Flags; |
| 2739 | + WCHAR PathBuffer[1]; |
| 2740 | + } SymbolicLinkReparseBuffer; |
| 2741 | + struct { |
| 2742 | + WORD SubstituteNameOffset; |
| 2743 | + WORD SubstituteNameLength; |
| 2744 | + WORD PrintNameOffset; |
| 2745 | + WORD PrintNameLength; |
| 2746 | + WCHAR PathBuffer[1]; |
| 2747 | + } MountPointReparseBuffer; |
| 2748 | + struct { |
| 2749 | + BYTE DataBuffer[1]; |
| 2750 | + } GenericReparseBuffer; |
| 2751 | + } DUMMYUNIONNAME; |
| 2752 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2753 | +#endif |
| 2754 | + |
| 2755 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2756 | +{ |
| 2757 | + HANDLE handle; |
| 2758 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2759 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2760 | + DWORD dummy; |
| 2761 | + char tmpbuf[MAX_LONG_PATH]; |
| 2762 | + int len; |
| 2763 | + |
| 2764 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2765 | + return -1; |
| 2766 | + |
| 2767 | + /* read reparse point data */ |
| 2768 | + handle = CreateFileW(wpath, 0, |
| 2769 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2770 | + OPEN_EXISTING, |
| 2771 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2772 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2773 | + errno = err_win_to_posix(GetLastError()); |
| 2774 | + return -1; |
| 2775 | + } |
| 2776 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2777 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2778 | + errno = err_win_to_posix(GetLastError()); |
| 2779 | + CloseHandle(handle); |
| 2780 | + return -1; |
| 2781 | + } |
| 2782 | + CloseHandle(handle); |
| 2783 | + |
| 2784 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2785 | + switch (b->ReparseTag) { |
| 2786 | + case IO_REPARSE_TAG_SYMLINK: |
| 2787 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2788 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2789 | + *(WCHAR*) (((char*) wbuf) |
| 2790 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2791 | + break; |
| 2792 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2793 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2794 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2795 | + *(WCHAR*) (((char*) wbuf) |
| 2796 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2797 | + break; |
| 2798 | + default: |
| 2799 | + errno = EINVAL; |
| 2800 | + return -1; |
| 2801 | + } |
| 2802 | + |
| 2803 | + /* |
| 2804 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2805 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2806 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2807 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2808 | + * the requested number of bytes (including '\0' for robustness). |
| 2809 | + */ |
| 2810 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2811 | + return -1; |
| 2812 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2813 | + return min(bufsiz, len); |
| 2814 | +} |
| 2815 | + |
2718 | 2816 | pid_t waitpid(pid_t pid, int *status, int options)
|
2719 | 2817 | {
|
2720 | 2818 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments