|
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"
|
@@ -2663,6 +2664,103 @@ int link(const char *oldpath, const char *newpath)
|
2663 | 2664 | return 0;
|
2664 | 2665 | }
|
2665 | 2666 |
|
| 2667 | +#ifndef _WINNT_H |
| 2668 | +/* |
| 2669 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2670 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2671 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2672 | + */ |
| 2673 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2674 | + DWORD ReparseTag; |
| 2675 | + WORD ReparseDataLength; |
| 2676 | + WORD Reserved; |
| 2677 | +#ifndef _MSC_VER |
| 2678 | + _ANONYMOUS_UNION |
| 2679 | +#endif |
| 2680 | + union { |
| 2681 | + struct { |
| 2682 | + WORD SubstituteNameOffset; |
| 2683 | + WORD SubstituteNameLength; |
| 2684 | + WORD PrintNameOffset; |
| 2685 | + WORD PrintNameLength; |
| 2686 | + ULONG Flags; |
| 2687 | + WCHAR PathBuffer[1]; |
| 2688 | + } SymbolicLinkReparseBuffer; |
| 2689 | + struct { |
| 2690 | + WORD SubstituteNameOffset; |
| 2691 | + WORD SubstituteNameLength; |
| 2692 | + WORD PrintNameOffset; |
| 2693 | + WORD PrintNameLength; |
| 2694 | + WCHAR PathBuffer[1]; |
| 2695 | + } MountPointReparseBuffer; |
| 2696 | + struct { |
| 2697 | + BYTE DataBuffer[1]; |
| 2698 | + } GenericReparseBuffer; |
| 2699 | + } DUMMYUNIONNAME; |
| 2700 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2701 | +#endif |
| 2702 | + |
| 2703 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2704 | +{ |
| 2705 | + HANDLE handle; |
| 2706 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2707 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2708 | + DWORD dummy; |
| 2709 | + char tmpbuf[MAX_LONG_PATH]; |
| 2710 | + int len; |
| 2711 | + |
| 2712 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2713 | + return -1; |
| 2714 | + |
| 2715 | + /* read reparse point data */ |
| 2716 | + handle = CreateFileW(wpath, 0, |
| 2717 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2718 | + OPEN_EXISTING, |
| 2719 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2720 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2721 | + errno = err_win_to_posix(GetLastError()); |
| 2722 | + return -1; |
| 2723 | + } |
| 2724 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2725 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2726 | + errno = err_win_to_posix(GetLastError()); |
| 2727 | + CloseHandle(handle); |
| 2728 | + return -1; |
| 2729 | + } |
| 2730 | + CloseHandle(handle); |
| 2731 | + |
| 2732 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2733 | + switch (b->ReparseTag) { |
| 2734 | + case IO_REPARSE_TAG_SYMLINK: |
| 2735 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2736 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2737 | + *(WCHAR*) (((char*) wbuf) |
| 2738 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2739 | + break; |
| 2740 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2741 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2742 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2743 | + *(WCHAR*) (((char*) wbuf) |
| 2744 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2745 | + break; |
| 2746 | + default: |
| 2747 | + errno = EINVAL; |
| 2748 | + return -1; |
| 2749 | + } |
| 2750 | + |
| 2751 | + /* |
| 2752 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2753 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2754 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2755 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2756 | + * the requested number of bytes (including '\0' for robustness). |
| 2757 | + */ |
| 2758 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2759 | + return -1; |
| 2760 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2761 | + return min(bufsiz, len); |
| 2762 | +} |
| 2763 | + |
2666 | 2764 | pid_t waitpid(pid_t pid, int *status, int options)
|
2667 | 2765 | {
|
2668 | 2766 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments