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