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