|
5 | 5 | #include <sddl.h>
|
6 | 6 | #include <conio.h>
|
7 | 7 | #include <wchar.h>
|
| 8 | +#include <winioctl.h> |
8 | 9 | #include "../strbuf.h"
|
9 | 10 | #include "../run-command.h"
|
10 | 11 | #include "../abspath.h"
|
@@ -2752,6 +2753,103 @@ int link(const char *oldpath, const char *newpath)
|
2752 | 2753 | return 0;
|
2753 | 2754 | }
|
2754 | 2755 |
|
| 2756 | +#ifndef _WINNT_H |
| 2757 | +/* |
| 2758 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2759 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2760 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2761 | + */ |
| 2762 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2763 | + DWORD ReparseTag; |
| 2764 | + WORD ReparseDataLength; |
| 2765 | + WORD Reserved; |
| 2766 | +#ifndef _MSC_VER |
| 2767 | + _ANONYMOUS_UNION |
| 2768 | +#endif |
| 2769 | + union { |
| 2770 | + struct { |
| 2771 | + WORD SubstituteNameOffset; |
| 2772 | + WORD SubstituteNameLength; |
| 2773 | + WORD PrintNameOffset; |
| 2774 | + WORD PrintNameLength; |
| 2775 | + ULONG Flags; |
| 2776 | + WCHAR PathBuffer[1]; |
| 2777 | + } SymbolicLinkReparseBuffer; |
| 2778 | + struct { |
| 2779 | + WORD SubstituteNameOffset; |
| 2780 | + WORD SubstituteNameLength; |
| 2781 | + WORD PrintNameOffset; |
| 2782 | + WORD PrintNameLength; |
| 2783 | + WCHAR PathBuffer[1]; |
| 2784 | + } MountPointReparseBuffer; |
| 2785 | + struct { |
| 2786 | + BYTE DataBuffer[1]; |
| 2787 | + } GenericReparseBuffer; |
| 2788 | + } DUMMYUNIONNAME; |
| 2789 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2790 | +#endif |
| 2791 | + |
| 2792 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2793 | +{ |
| 2794 | + HANDLE handle; |
| 2795 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2796 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2797 | + DWORD dummy; |
| 2798 | + char tmpbuf[MAX_LONG_PATH]; |
| 2799 | + int len; |
| 2800 | + |
| 2801 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2802 | + return -1; |
| 2803 | + |
| 2804 | + /* read reparse point data */ |
| 2805 | + handle = CreateFileW(wpath, 0, |
| 2806 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2807 | + OPEN_EXISTING, |
| 2808 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2809 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2810 | + errno = err_win_to_posix(GetLastError()); |
| 2811 | + return -1; |
| 2812 | + } |
| 2813 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2814 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2815 | + errno = err_win_to_posix(GetLastError()); |
| 2816 | + CloseHandle(handle); |
| 2817 | + return -1; |
| 2818 | + } |
| 2819 | + CloseHandle(handle); |
| 2820 | + |
| 2821 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2822 | + switch (b->ReparseTag) { |
| 2823 | + case IO_REPARSE_TAG_SYMLINK: |
| 2824 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2825 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2826 | + *(WCHAR*) (((char*) wbuf) |
| 2827 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2828 | + break; |
| 2829 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2830 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2831 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2832 | + *(WCHAR*) (((char*) wbuf) |
| 2833 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2834 | + break; |
| 2835 | + default: |
| 2836 | + errno = EINVAL; |
| 2837 | + return -1; |
| 2838 | + } |
| 2839 | + |
| 2840 | + /* |
| 2841 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2842 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2843 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2844 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2845 | + * the requested number of bytes (including '\0' for robustness). |
| 2846 | + */ |
| 2847 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2848 | + return -1; |
| 2849 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2850 | + return min(bufsiz, len); |
| 2851 | +} |
| 2852 | + |
2755 | 2853 | pid_t waitpid(pid_t pid, int *status, int options)
|
2756 | 2854 | {
|
2757 | 2855 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments