|
| 1 | +#include <string> |
| 2 | +#include <cstddef> |
| 3 | + |
| 4 | +#define WIN32_LEAN_AND_MEAN |
| 5 | +#include <windows.h> |
| 6 | +#include <winioctl.h> |
| 7 | + |
| 8 | +#include "win32_fs.h" |
| 9 | + |
| 10 | + |
| 11 | +// create type PREPARSE_DATA_BUFFER |
| 12 | +// from ntifs.h, which can only be used by drivers |
| 13 | +// typedef is copied from https://gitlab.kitware.com/utils/kwsys/-/blob/master/SystemTools.cxx |
| 14 | +// that has a BSD 3-clause license |
| 15 | +typedef struct _REPARSE_DATA_BUFFER |
| 16 | +{ |
| 17 | + ULONG ReparseTag; |
| 18 | + USHORT ReparseDataLength; |
| 19 | + USHORT Reserved; |
| 20 | + union |
| 21 | + { |
| 22 | + struct |
| 23 | + { |
| 24 | + USHORT SubstituteNameOffset; |
| 25 | + USHORT SubstituteNameLength; |
| 26 | + USHORT PrintNameOffset; |
| 27 | + USHORT PrintNameLength; |
| 28 | + ULONG Flags; |
| 29 | + WCHAR PathBuffer[1]; |
| 30 | + } SymbolicLinkReparseBuffer; |
| 31 | + struct |
| 32 | + { |
| 33 | + USHORT SubstituteNameOffset; |
| 34 | + USHORT SubstituteNameLength; |
| 35 | + USHORT PrintNameOffset; |
| 36 | + USHORT PrintNameLength; |
| 37 | + WCHAR PathBuffer[1]; |
| 38 | + } MountPointReparseBuffer; |
| 39 | + struct |
| 40 | + { |
| 41 | + UCHAR DataBuffer[1]; |
| 42 | + } GenericReparseBuffer; |
| 43 | + struct |
| 44 | + { |
| 45 | + ULONG Version; |
| 46 | + WCHAR StringList[1]; |
| 47 | + // In version 3, there are 4 NUL-terminated strings: |
| 48 | + // * Package ID |
| 49 | + // * Entry Point |
| 50 | + // * Executable Path |
| 51 | + // * Application Type |
| 52 | + } AppExecLinkReparseBuffer; |
| 53 | + } DUMMYUNIONNAME; |
| 54 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 55 | + |
| 56 | + |
| 57 | +static bool fs_win32_get_reparse_buffer(std::string path, std::byte* buffer) |
| 58 | +{ |
| 59 | +// this function is adapted from |
| 60 | +// https://gitlab.kitware.com/utils/kwsys/-/blob/master/SystemTools.cxx |
| 61 | +// that has a BSD 3-clause license |
| 62 | + |
| 63 | + const DWORD attr = GetFileAttributesA(path.data()); |
| 64 | +// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileattributesa |
| 65 | + |
| 66 | +// https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants |
| 67 | + if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_REPARSE_POINT)) |
| 68 | + return false; |
| 69 | + |
| 70 | + // Using 0 instead of GENERIC_READ as it allows reading of file attributes |
| 71 | + // even if we do not have permission to read the file itself |
| 72 | + |
| 73 | + // A reparse point may be an execution alias (Windows Store app), which |
| 74 | + // is similar to a symlink but it cannot be opened as a regular file. |
| 75 | + // We must look at the reparse point data explicitly. |
| 76 | + |
| 77 | + // FILE_ATTRIBUTE_REPARSE_POINT means: |
| 78 | + // * a file or directory that has an associated reparse point, or |
| 79 | + // * a file that is a symbolic link. |
| 80 | + HANDLE h = CreateFileA( |
| 81 | + path.data(), 0, 0, nullptr, OPEN_EXISTING, |
| 82 | + FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 83 | + |
| 84 | + if (h == INVALID_HANDLE_VALUE) |
| 85 | + return false; |
| 86 | + |
| 87 | + DWORD bytesReturned = 0; |
| 88 | + |
| 89 | + BOOL ok = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, nullptr, 0, buffer, |
| 90 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &bytesReturned, |
| 91 | + nullptr); |
| 92 | + |
| 93 | + CloseHandle(h); |
| 94 | + |
| 95 | + return ok; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +bool fs_win32_is_symlink(std::string path) |
| 100 | +{ |
| 101 | +// distinguish between Windows symbolic links and reparse points as |
| 102 | +// reparse points can be unlike symlinks. |
| 103 | +// |
| 104 | +// this function is adapted from |
| 105 | +// https://gitlab.kitware.com/utils/kwsys/-/blob/master/SystemTools.cxx |
| 106 | +// that has a BSD 3-clause license |
| 107 | + |
| 108 | + std::byte buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 109 | + // Since FILE_ATTRIBUTE_REPARSE_POINT is set this file must be |
| 110 | + // a symbolic link if it is not a reparse point. |
| 111 | + if (!fs_win32_get_reparse_buffer(path, buffer)) |
| 112 | + return GetLastError() == ERROR_NOT_A_REPARSE_POINT; |
| 113 | + |
| 114 | + ULONG reparseTag = |
| 115 | + reinterpret_cast<PREPARSE_DATA_BUFFER>(&buffer[0])->ReparseTag; |
| 116 | + |
| 117 | + return (reparseTag == IO_REPARSE_TAG_SYMLINK) || |
| 118 | + (reparseTag == IO_REPARSE_TAG_MOUNT_POINT); |
| 119 | + |
| 120 | +} |
0 commit comments