1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric // 10fe6060f1SDimitry Andric // POSIX-like portability helper functions. 11fe6060f1SDimitry Andric // 12fe6060f1SDimitry Andric // These generally behave like the proper posix functions, with these 13fe6060f1SDimitry Andric // exceptions: 14fe6060f1SDimitry Andric // On Windows, they take paths in wchar_t* form, instead of char* form. 15fe6060f1SDimitry Andric // The symlink() function is split into two frontends, symlink_file() 16fe6060f1SDimitry Andric // and symlink_dir(). 17fe6060f1SDimitry Andric // 18fe6060f1SDimitry Andric // These are provided within an anonymous namespace within the detail 19fe6060f1SDimitry Andric // namespace - callers need to include this header and call them as 20fe6060f1SDimitry Andric // detail::function(), regardless of platform. 21fe6060f1SDimitry Andric // 22fe6060f1SDimitry Andric 23fe6060f1SDimitry Andric #ifndef POSIX_COMPAT_H 24fe6060f1SDimitry Andric #define POSIX_COMPAT_H 25fe6060f1SDimitry Andric 26*81ad6265SDimitry Andric #include <__assert> 27*81ad6265SDimitry Andric #include <filesystem> 28fe6060f1SDimitry Andric 29fe6060f1SDimitry Andric #include "filesystem_common.h" 30fe6060f1SDimitry Andric 31fe6060f1SDimitry Andric #if defined(_LIBCPP_WIN32API) 32fe6060f1SDimitry Andric # define WIN32_LEAN_AND_MEAN 33fe6060f1SDimitry Andric # define NOMINMAX 34fe6060f1SDimitry Andric # include <windows.h> 35fe6060f1SDimitry Andric # include <io.h> 36fe6060f1SDimitry Andric # include <winioctl.h> 37fe6060f1SDimitry Andric #else 38fe6060f1SDimitry Andric # include <unistd.h> 39fe6060f1SDimitry Andric # include <sys/stat.h> 40fe6060f1SDimitry Andric # include <sys/statvfs.h> 41fe6060f1SDimitry Andric #endif 42fe6060f1SDimitry Andric #include <time.h> 43fe6060f1SDimitry Andric 44fe6060f1SDimitry Andric #if defined(_LIBCPP_WIN32API) 45fe6060f1SDimitry Andric // This struct isn't defined in the normal Windows SDK, but only in the 46fe6060f1SDimitry Andric // Windows Driver Kit. 47fe6060f1SDimitry Andric struct LIBCPP_REPARSE_DATA_BUFFER { 48fe6060f1SDimitry Andric unsigned long ReparseTag; 49fe6060f1SDimitry Andric unsigned short ReparseDataLength; 50fe6060f1SDimitry Andric unsigned short Reserved; 51fe6060f1SDimitry Andric union { 52fe6060f1SDimitry Andric struct { 53fe6060f1SDimitry Andric unsigned short SubstituteNameOffset; 54fe6060f1SDimitry Andric unsigned short SubstituteNameLength; 55fe6060f1SDimitry Andric unsigned short PrintNameOffset; 56fe6060f1SDimitry Andric unsigned short PrintNameLength; 57fe6060f1SDimitry Andric unsigned long Flags; 58fe6060f1SDimitry Andric wchar_t PathBuffer[1]; 59fe6060f1SDimitry Andric } SymbolicLinkReparseBuffer; 60fe6060f1SDimitry Andric struct { 61fe6060f1SDimitry Andric unsigned short SubstituteNameOffset; 62fe6060f1SDimitry Andric unsigned short SubstituteNameLength; 63fe6060f1SDimitry Andric unsigned short PrintNameOffset; 64fe6060f1SDimitry Andric unsigned short PrintNameLength; 65fe6060f1SDimitry Andric wchar_t PathBuffer[1]; 66fe6060f1SDimitry Andric } MountPointReparseBuffer; 67fe6060f1SDimitry Andric struct { 68fe6060f1SDimitry Andric unsigned char DataBuffer[1]; 69fe6060f1SDimitry Andric } GenericReparseBuffer; 70fe6060f1SDimitry Andric }; 71fe6060f1SDimitry Andric }; 72fe6060f1SDimitry Andric #endif 73fe6060f1SDimitry Andric 74fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM 75fe6060f1SDimitry Andric 76fe6060f1SDimitry Andric namespace detail { 77fe6060f1SDimitry Andric namespace { 78fe6060f1SDimitry Andric 79fe6060f1SDimitry Andric #if defined(_LIBCPP_WIN32API) 80fe6060f1SDimitry Andric 81fe6060f1SDimitry Andric // Various C runtime header sets provide more or less of these. As we 82fe6060f1SDimitry Andric // provide our own implementation, undef all potential defines from the 83fe6060f1SDimitry Andric // C runtime headers and provide a complete set of macros of our own. 84fe6060f1SDimitry Andric 85fe6060f1SDimitry Andric #undef _S_IFMT 86fe6060f1SDimitry Andric #undef _S_IFDIR 87fe6060f1SDimitry Andric #undef _S_IFCHR 88fe6060f1SDimitry Andric #undef _S_IFIFO 89fe6060f1SDimitry Andric #undef _S_IFREG 90fe6060f1SDimitry Andric #undef _S_IFBLK 91fe6060f1SDimitry Andric #undef _S_IFLNK 92fe6060f1SDimitry Andric #undef _S_IFSOCK 93fe6060f1SDimitry Andric 94fe6060f1SDimitry Andric #define _S_IFMT 0xF000 95fe6060f1SDimitry Andric #define _S_IFDIR 0x4000 96fe6060f1SDimitry Andric #define _S_IFCHR 0x2000 97fe6060f1SDimitry Andric #define _S_IFIFO 0x1000 98fe6060f1SDimitry Andric #define _S_IFREG 0x8000 99fe6060f1SDimitry Andric #define _S_IFBLK 0x6000 100fe6060f1SDimitry Andric #define _S_IFLNK 0xA000 101fe6060f1SDimitry Andric #define _S_IFSOCK 0xC000 102fe6060f1SDimitry Andric 103fe6060f1SDimitry Andric #undef S_ISDIR 104fe6060f1SDimitry Andric #undef S_ISFIFO 105fe6060f1SDimitry Andric #undef S_ISCHR 106fe6060f1SDimitry Andric #undef S_ISREG 107fe6060f1SDimitry Andric #undef S_ISLNK 108fe6060f1SDimitry Andric #undef S_ISBLK 109fe6060f1SDimitry Andric #undef S_ISSOCK 110fe6060f1SDimitry Andric 111fe6060f1SDimitry Andric #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR) 112fe6060f1SDimitry Andric #define S_ISCHR(m) (((m) & _S_IFMT) == _S_IFCHR) 113fe6060f1SDimitry Andric #define S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO) 114fe6060f1SDimitry Andric #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) 115fe6060f1SDimitry Andric #define S_ISBLK(m) (((m) & _S_IFMT) == _S_IFBLK) 116fe6060f1SDimitry Andric #define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK) 117fe6060f1SDimitry Andric #define S_ISSOCK(m) (((m) & _S_IFMT) == _S_IFSOCK) 118fe6060f1SDimitry Andric 119fe6060f1SDimitry Andric #define O_NONBLOCK 0 120fe6060f1SDimitry Andric 121fe6060f1SDimitry Andric 122fe6060f1SDimitry Andric // There were 369 years and 89 leap days from the Windows epoch 123fe6060f1SDimitry Andric // (1601) to the Unix epoch (1970). 124fe6060f1SDimitry Andric #define FILE_TIME_OFFSET_SECS (uint64_t(369 * 365 + 89) * (24 * 60 * 60)) 125fe6060f1SDimitry Andric 126fe6060f1SDimitry Andric TimeSpec filetime_to_timespec(LARGE_INTEGER li) { 127fe6060f1SDimitry Andric TimeSpec ret; 128fe6060f1SDimitry Andric ret.tv_sec = li.QuadPart / 10000000 - FILE_TIME_OFFSET_SECS; 129fe6060f1SDimitry Andric ret.tv_nsec = (li.QuadPart % 10000000) * 100; 130fe6060f1SDimitry Andric return ret; 131fe6060f1SDimitry Andric } 132fe6060f1SDimitry Andric 133fe6060f1SDimitry Andric TimeSpec filetime_to_timespec(FILETIME ft) { 134fe6060f1SDimitry Andric LARGE_INTEGER li; 135fe6060f1SDimitry Andric li.LowPart = ft.dwLowDateTime; 136fe6060f1SDimitry Andric li.HighPart = ft.dwHighDateTime; 137fe6060f1SDimitry Andric return filetime_to_timespec(li); 138fe6060f1SDimitry Andric } 139fe6060f1SDimitry Andric 140fe6060f1SDimitry Andric FILETIME timespec_to_filetime(TimeSpec ts) { 141fe6060f1SDimitry Andric LARGE_INTEGER li; 142fe6060f1SDimitry Andric li.QuadPart = 143fe6060f1SDimitry Andric ts.tv_nsec / 100 + (ts.tv_sec + FILE_TIME_OFFSET_SECS) * 10000000; 144fe6060f1SDimitry Andric FILETIME ft; 145fe6060f1SDimitry Andric ft.dwLowDateTime = li.LowPart; 146fe6060f1SDimitry Andric ft.dwHighDateTime = li.HighPart; 147fe6060f1SDimitry Andric return ft; 148fe6060f1SDimitry Andric } 149fe6060f1SDimitry Andric 150fe6060f1SDimitry Andric int set_errno(int e = GetLastError()) { 151fe6060f1SDimitry Andric errno = static_cast<int>(__win_err_to_errc(e)); 152fe6060f1SDimitry Andric return -1; 153fe6060f1SDimitry Andric } 154fe6060f1SDimitry Andric 155fe6060f1SDimitry Andric class WinHandle { 156fe6060f1SDimitry Andric public: 157fe6060f1SDimitry Andric WinHandle(const wchar_t *p, DWORD access, DWORD flags) { 158fe6060f1SDimitry Andric h = CreateFileW( 159fe6060f1SDimitry Andric p, access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 160fe6060f1SDimitry Andric nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | flags, nullptr); 161fe6060f1SDimitry Andric } 162fe6060f1SDimitry Andric ~WinHandle() { 163fe6060f1SDimitry Andric if (h != INVALID_HANDLE_VALUE) 164fe6060f1SDimitry Andric CloseHandle(h); 165fe6060f1SDimitry Andric } 166fe6060f1SDimitry Andric operator HANDLE() const { return h; } 167fe6060f1SDimitry Andric operator bool() const { return h != INVALID_HANDLE_VALUE; } 168fe6060f1SDimitry Andric 169fe6060f1SDimitry Andric private: 170fe6060f1SDimitry Andric HANDLE h; 171fe6060f1SDimitry Andric }; 172fe6060f1SDimitry Andric 173fe6060f1SDimitry Andric int stat_handle(HANDLE h, StatT *buf) { 174fe6060f1SDimitry Andric FILE_BASIC_INFO basic; 175fe6060f1SDimitry Andric if (!GetFileInformationByHandleEx(h, FileBasicInfo, &basic, sizeof(basic))) 176fe6060f1SDimitry Andric return set_errno(); 177fe6060f1SDimitry Andric memset(buf, 0, sizeof(*buf)); 178fe6060f1SDimitry Andric buf->st_mtim = filetime_to_timespec(basic.LastWriteTime); 179fe6060f1SDimitry Andric buf->st_atim = filetime_to_timespec(basic.LastAccessTime); 180fe6060f1SDimitry Andric buf->st_mode = 0555; // Read-only 181fe6060f1SDimitry Andric if (!(basic.FileAttributes & FILE_ATTRIBUTE_READONLY)) 182fe6060f1SDimitry Andric buf->st_mode |= 0222; // Write 183fe6060f1SDimitry Andric if (basic.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) { 184fe6060f1SDimitry Andric buf->st_mode |= _S_IFDIR; 185fe6060f1SDimitry Andric } else { 186fe6060f1SDimitry Andric buf->st_mode |= _S_IFREG; 187fe6060f1SDimitry Andric } 188fe6060f1SDimitry Andric if (basic.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { 189fe6060f1SDimitry Andric FILE_ATTRIBUTE_TAG_INFO tag; 190fe6060f1SDimitry Andric if (!GetFileInformationByHandleEx(h, FileAttributeTagInfo, &tag, 191fe6060f1SDimitry Andric sizeof(tag))) 192fe6060f1SDimitry Andric return set_errno(); 193fe6060f1SDimitry Andric if (tag.ReparseTag == IO_REPARSE_TAG_SYMLINK) 194fe6060f1SDimitry Andric buf->st_mode = (buf->st_mode & ~_S_IFMT) | _S_IFLNK; 195fe6060f1SDimitry Andric } 196fe6060f1SDimitry Andric FILE_STANDARD_INFO standard; 197fe6060f1SDimitry Andric if (!GetFileInformationByHandleEx(h, FileStandardInfo, &standard, 198fe6060f1SDimitry Andric sizeof(standard))) 199fe6060f1SDimitry Andric return set_errno(); 200fe6060f1SDimitry Andric buf->st_nlink = standard.NumberOfLinks; 201fe6060f1SDimitry Andric buf->st_size = standard.EndOfFile.QuadPart; 202fe6060f1SDimitry Andric BY_HANDLE_FILE_INFORMATION info; 203fe6060f1SDimitry Andric if (!GetFileInformationByHandle(h, &info)) 204fe6060f1SDimitry Andric return set_errno(); 205fe6060f1SDimitry Andric buf->st_dev = info.dwVolumeSerialNumber; 206fe6060f1SDimitry Andric memcpy(&buf->st_ino.id[0], &info.nFileIndexHigh, 4); 207fe6060f1SDimitry Andric memcpy(&buf->st_ino.id[4], &info.nFileIndexLow, 4); 208fe6060f1SDimitry Andric return 0; 209fe6060f1SDimitry Andric } 210fe6060f1SDimitry Andric 211fe6060f1SDimitry Andric int stat_file(const wchar_t *path, StatT *buf, DWORD flags) { 212fe6060f1SDimitry Andric WinHandle h(path, FILE_READ_ATTRIBUTES, flags); 213fe6060f1SDimitry Andric if (!h) 214fe6060f1SDimitry Andric return set_errno(); 215fe6060f1SDimitry Andric int ret = stat_handle(h, buf); 216fe6060f1SDimitry Andric return ret; 217fe6060f1SDimitry Andric } 218fe6060f1SDimitry Andric 219fe6060f1SDimitry Andric int stat(const wchar_t *path, StatT *buf) { return stat_file(path, buf, 0); } 220fe6060f1SDimitry Andric 221fe6060f1SDimitry Andric int lstat(const wchar_t *path, StatT *buf) { 222fe6060f1SDimitry Andric return stat_file(path, buf, FILE_FLAG_OPEN_REPARSE_POINT); 223fe6060f1SDimitry Andric } 224fe6060f1SDimitry Andric 225fe6060f1SDimitry Andric int fstat(int fd, StatT *buf) { 226fe6060f1SDimitry Andric HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); 227fe6060f1SDimitry Andric return stat_handle(h, buf); 228fe6060f1SDimitry Andric } 229fe6060f1SDimitry Andric 230fe6060f1SDimitry Andric int mkdir(const wchar_t *path, int permissions) { 231fe6060f1SDimitry Andric (void)permissions; 232fe6060f1SDimitry Andric return _wmkdir(path); 233fe6060f1SDimitry Andric } 234fe6060f1SDimitry Andric 235fe6060f1SDimitry Andric int symlink_file_dir(const wchar_t *oldname, const wchar_t *newname, 236fe6060f1SDimitry Andric bool is_dir) { 237fe6060f1SDimitry Andric path dest(oldname); 238fe6060f1SDimitry Andric dest.make_preferred(); 239fe6060f1SDimitry Andric oldname = dest.c_str(); 240fe6060f1SDimitry Andric DWORD flags = is_dir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0; 241fe6060f1SDimitry Andric if (CreateSymbolicLinkW(newname, oldname, 242fe6060f1SDimitry Andric flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) 243fe6060f1SDimitry Andric return 0; 244fe6060f1SDimitry Andric int e = GetLastError(); 245fe6060f1SDimitry Andric if (e != ERROR_INVALID_PARAMETER) 246fe6060f1SDimitry Andric return set_errno(e); 247fe6060f1SDimitry Andric if (CreateSymbolicLinkW(newname, oldname, flags)) 248fe6060f1SDimitry Andric return 0; 249fe6060f1SDimitry Andric return set_errno(); 250fe6060f1SDimitry Andric } 251fe6060f1SDimitry Andric 252fe6060f1SDimitry Andric int symlink_file(const wchar_t *oldname, const wchar_t *newname) { 253fe6060f1SDimitry Andric return symlink_file_dir(oldname, newname, false); 254fe6060f1SDimitry Andric } 255fe6060f1SDimitry Andric 256fe6060f1SDimitry Andric int symlink_dir(const wchar_t *oldname, const wchar_t *newname) { 257fe6060f1SDimitry Andric return symlink_file_dir(oldname, newname, true); 258fe6060f1SDimitry Andric } 259fe6060f1SDimitry Andric 260fe6060f1SDimitry Andric int link(const wchar_t *oldname, const wchar_t *newname) { 261fe6060f1SDimitry Andric if (CreateHardLinkW(newname, oldname, nullptr)) 262fe6060f1SDimitry Andric return 0; 263fe6060f1SDimitry Andric return set_errno(); 264fe6060f1SDimitry Andric } 265fe6060f1SDimitry Andric 266fe6060f1SDimitry Andric int remove(const wchar_t *path) { 267fe6060f1SDimitry Andric detail::WinHandle h(path, DELETE, FILE_FLAG_OPEN_REPARSE_POINT); 268fe6060f1SDimitry Andric if (!h) 269fe6060f1SDimitry Andric return set_errno(); 270fe6060f1SDimitry Andric FILE_DISPOSITION_INFO info; 271fe6060f1SDimitry Andric info.DeleteFile = TRUE; 272fe6060f1SDimitry Andric if (!SetFileInformationByHandle(h, FileDispositionInfo, &info, sizeof(info))) 273fe6060f1SDimitry Andric return set_errno(); 274fe6060f1SDimitry Andric return 0; 275fe6060f1SDimitry Andric } 276fe6060f1SDimitry Andric 277fe6060f1SDimitry Andric int truncate_handle(HANDLE h, off_t length) { 278fe6060f1SDimitry Andric LARGE_INTEGER size_param; 279fe6060f1SDimitry Andric size_param.QuadPart = length; 280fe6060f1SDimitry Andric if (!SetFilePointerEx(h, size_param, 0, FILE_BEGIN)) 281fe6060f1SDimitry Andric return set_errno(); 282fe6060f1SDimitry Andric if (!SetEndOfFile(h)) 283fe6060f1SDimitry Andric return set_errno(); 284fe6060f1SDimitry Andric return 0; 285fe6060f1SDimitry Andric } 286fe6060f1SDimitry Andric 287fe6060f1SDimitry Andric int ftruncate(int fd, off_t length) { 288fe6060f1SDimitry Andric HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); 289fe6060f1SDimitry Andric return truncate_handle(h, length); 290fe6060f1SDimitry Andric } 291fe6060f1SDimitry Andric 292fe6060f1SDimitry Andric int truncate(const wchar_t *path, off_t length) { 293fe6060f1SDimitry Andric detail::WinHandle h(path, GENERIC_WRITE, 0); 294fe6060f1SDimitry Andric if (!h) 295fe6060f1SDimitry Andric return set_errno(); 296fe6060f1SDimitry Andric return truncate_handle(h, length); 297fe6060f1SDimitry Andric } 298fe6060f1SDimitry Andric 299fe6060f1SDimitry Andric int rename(const wchar_t *from, const wchar_t *to) { 300fe6060f1SDimitry Andric if (!(MoveFileExW(from, to, 301fe6060f1SDimitry Andric MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | 302fe6060f1SDimitry Andric MOVEFILE_WRITE_THROUGH))) 303fe6060f1SDimitry Andric return set_errno(); 304fe6060f1SDimitry Andric return 0; 305fe6060f1SDimitry Andric } 306fe6060f1SDimitry Andric 307fe6060f1SDimitry Andric template <class... Args> int open(const wchar_t *filename, Args... args) { 308fe6060f1SDimitry Andric return _wopen(filename, args...); 309fe6060f1SDimitry Andric } 310fe6060f1SDimitry Andric int close(int fd) { return _close(fd); } 311fe6060f1SDimitry Andric int chdir(const wchar_t *path) { return _wchdir(path); } 312fe6060f1SDimitry Andric 313fe6060f1SDimitry Andric struct StatVFS { 314fe6060f1SDimitry Andric uint64_t f_frsize; 315fe6060f1SDimitry Andric uint64_t f_blocks; 316fe6060f1SDimitry Andric uint64_t f_bfree; 317fe6060f1SDimitry Andric uint64_t f_bavail; 318fe6060f1SDimitry Andric }; 319fe6060f1SDimitry Andric 320fe6060f1SDimitry Andric int statvfs(const wchar_t *p, StatVFS *buf) { 321fe6060f1SDimitry Andric path dir = p; 322fe6060f1SDimitry Andric while (true) { 323fe6060f1SDimitry Andric error_code local_ec; 324fe6060f1SDimitry Andric const file_status st = status(dir, local_ec); 325fe6060f1SDimitry Andric if (!exists(st) || is_directory(st)) 326fe6060f1SDimitry Andric break; 327fe6060f1SDimitry Andric path parent = dir.parent_path(); 328fe6060f1SDimitry Andric if (parent == dir) { 329fe6060f1SDimitry Andric errno = ENOENT; 330fe6060f1SDimitry Andric return -1; 331fe6060f1SDimitry Andric } 332fe6060f1SDimitry Andric dir = parent; 333fe6060f1SDimitry Andric } 334fe6060f1SDimitry Andric ULARGE_INTEGER free_bytes_available_to_caller, total_number_of_bytes, 335fe6060f1SDimitry Andric total_number_of_free_bytes; 336fe6060f1SDimitry Andric if (!GetDiskFreeSpaceExW(dir.c_str(), &free_bytes_available_to_caller, 337fe6060f1SDimitry Andric &total_number_of_bytes, &total_number_of_free_bytes)) 338fe6060f1SDimitry Andric return set_errno(); 339fe6060f1SDimitry Andric buf->f_frsize = 1; 340fe6060f1SDimitry Andric buf->f_blocks = total_number_of_bytes.QuadPart; 341fe6060f1SDimitry Andric buf->f_bfree = total_number_of_free_bytes.QuadPart; 342fe6060f1SDimitry Andric buf->f_bavail = free_bytes_available_to_caller.QuadPart; 343fe6060f1SDimitry Andric return 0; 344fe6060f1SDimitry Andric } 345fe6060f1SDimitry Andric 346fe6060f1SDimitry Andric wchar_t *getcwd(wchar_t *buff, size_t size) { return _wgetcwd(buff, size); } 347fe6060f1SDimitry Andric 348fe6060f1SDimitry Andric wchar_t *realpath(const wchar_t *path, wchar_t *resolved_name) { 349fe6060f1SDimitry Andric // Only expected to be used with us allocating the buffer. 350fe6060f1SDimitry Andric _LIBCPP_ASSERT(resolved_name == nullptr, 351fe6060f1SDimitry Andric "Windows realpath() assumes a null resolved_name"); 352fe6060f1SDimitry Andric 353fe6060f1SDimitry Andric WinHandle h(path, FILE_READ_ATTRIBUTES, 0); 354fe6060f1SDimitry Andric if (!h) { 355fe6060f1SDimitry Andric set_errno(); 356fe6060f1SDimitry Andric return nullptr; 357fe6060f1SDimitry Andric } 358fe6060f1SDimitry Andric size_t buff_size = MAX_PATH + 10; 359fe6060f1SDimitry Andric std::unique_ptr<wchar_t, decltype(&::free)> buff( 360fe6060f1SDimitry Andric static_cast<wchar_t *>(malloc(buff_size * sizeof(wchar_t))), &::free); 361fe6060f1SDimitry Andric DWORD retval = GetFinalPathNameByHandleW( 362fe6060f1SDimitry Andric h, buff.get(), buff_size, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); 363fe6060f1SDimitry Andric if (retval > buff_size) { 364fe6060f1SDimitry Andric buff_size = retval; 365fe6060f1SDimitry Andric buff.reset(static_cast<wchar_t *>(malloc(buff_size * sizeof(wchar_t)))); 366fe6060f1SDimitry Andric retval = GetFinalPathNameByHandleW(h, buff.get(), buff_size, 367fe6060f1SDimitry Andric FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); 368fe6060f1SDimitry Andric } 369fe6060f1SDimitry Andric if (!retval) { 370fe6060f1SDimitry Andric set_errno(); 371fe6060f1SDimitry Andric return nullptr; 372fe6060f1SDimitry Andric } 373fe6060f1SDimitry Andric wchar_t *ptr = buff.get(); 374fe6060f1SDimitry Andric if (!wcsncmp(ptr, L"\\\\?\\", 4)) { 375fe6060f1SDimitry Andric if (ptr[5] == ':') { // \\?\X: -> X: 376fe6060f1SDimitry Andric memmove(&ptr[0], &ptr[4], (wcslen(&ptr[4]) + 1) * sizeof(wchar_t)); 377fe6060f1SDimitry Andric } else if (!wcsncmp(&ptr[4], L"UNC\\", 4)) { // \\?\UNC\server -> \\server 378fe6060f1SDimitry Andric wcscpy(&ptr[0], L"\\\\"); 379fe6060f1SDimitry Andric memmove(&ptr[2], &ptr[8], (wcslen(&ptr[8]) + 1) * sizeof(wchar_t)); 380fe6060f1SDimitry Andric } 381fe6060f1SDimitry Andric } 382fe6060f1SDimitry Andric return buff.release(); 383fe6060f1SDimitry Andric } 384fe6060f1SDimitry Andric 385fe6060f1SDimitry Andric #define AT_FDCWD -1 386fe6060f1SDimitry Andric #define AT_SYMLINK_NOFOLLOW 1 387fe6060f1SDimitry Andric using ModeT = int; 388fe6060f1SDimitry Andric 389fe6060f1SDimitry Andric int fchmod_handle(HANDLE h, int perms) { 390fe6060f1SDimitry Andric FILE_BASIC_INFO basic; 391fe6060f1SDimitry Andric if (!GetFileInformationByHandleEx(h, FileBasicInfo, &basic, sizeof(basic))) 392fe6060f1SDimitry Andric return set_errno(); 393fe6060f1SDimitry Andric DWORD orig_attributes = basic.FileAttributes; 394fe6060f1SDimitry Andric basic.FileAttributes &= ~FILE_ATTRIBUTE_READONLY; 395fe6060f1SDimitry Andric if ((perms & 0222) == 0) 396fe6060f1SDimitry Andric basic.FileAttributes |= FILE_ATTRIBUTE_READONLY; 397fe6060f1SDimitry Andric if (basic.FileAttributes != orig_attributes && 398fe6060f1SDimitry Andric !SetFileInformationByHandle(h, FileBasicInfo, &basic, sizeof(basic))) 399fe6060f1SDimitry Andric return set_errno(); 400fe6060f1SDimitry Andric return 0; 401fe6060f1SDimitry Andric } 402fe6060f1SDimitry Andric 403fe6060f1SDimitry Andric int fchmodat(int fd, const wchar_t *path, int perms, int flag) { 404fe6060f1SDimitry Andric DWORD attributes = GetFileAttributesW(path); 405fe6060f1SDimitry Andric if (attributes == INVALID_FILE_ATTRIBUTES) 406fe6060f1SDimitry Andric return set_errno(); 407fe6060f1SDimitry Andric if (attributes & FILE_ATTRIBUTE_REPARSE_POINT && 408fe6060f1SDimitry Andric !(flag & AT_SYMLINK_NOFOLLOW)) { 409fe6060f1SDimitry Andric // If the file is a symlink, and we are supposed to operate on the target 410fe6060f1SDimitry Andric // of the symlink, we need to open a handle to it, without the 411fe6060f1SDimitry Andric // FILE_FLAG_OPEN_REPARSE_POINT flag, to open the destination of the 412fe6060f1SDimitry Andric // symlink, and operate on it via the handle. 413fe6060f1SDimitry Andric detail::WinHandle h(path, FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, 0); 414fe6060f1SDimitry Andric if (!h) 415fe6060f1SDimitry Andric return set_errno(); 416fe6060f1SDimitry Andric return fchmod_handle(h, perms); 417fe6060f1SDimitry Andric } else { 418fe6060f1SDimitry Andric // For a non-symlink, or if operating on the symlink itself instead of 419fe6060f1SDimitry Andric // its target, we can use SetFileAttributesW, saving a few calls. 420fe6060f1SDimitry Andric DWORD orig_attributes = attributes; 421fe6060f1SDimitry Andric attributes &= ~FILE_ATTRIBUTE_READONLY; 422fe6060f1SDimitry Andric if ((perms & 0222) == 0) 423fe6060f1SDimitry Andric attributes |= FILE_ATTRIBUTE_READONLY; 424fe6060f1SDimitry Andric if (attributes != orig_attributes && !SetFileAttributesW(path, attributes)) 425fe6060f1SDimitry Andric return set_errno(); 426fe6060f1SDimitry Andric } 427fe6060f1SDimitry Andric return 0; 428fe6060f1SDimitry Andric } 429fe6060f1SDimitry Andric 430fe6060f1SDimitry Andric int fchmod(int fd, int perms) { 431fe6060f1SDimitry Andric HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); 432fe6060f1SDimitry Andric return fchmod_handle(h, perms); 433fe6060f1SDimitry Andric } 434fe6060f1SDimitry Andric 435fe6060f1SDimitry Andric #define MAX_SYMLINK_SIZE MAXIMUM_REPARSE_DATA_BUFFER_SIZE 436fe6060f1SDimitry Andric using SSizeT = ::int64_t; 437fe6060f1SDimitry Andric 438fe6060f1SDimitry Andric SSizeT readlink(const wchar_t *path, wchar_t *ret_buf, size_t bufsize) { 439fe6060f1SDimitry Andric uint8_t buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; 440fe6060f1SDimitry Andric detail::WinHandle h(path, FILE_READ_ATTRIBUTES, FILE_FLAG_OPEN_REPARSE_POINT); 441fe6060f1SDimitry Andric if (!h) 442fe6060f1SDimitry Andric return set_errno(); 443fe6060f1SDimitry Andric DWORD out; 444fe6060f1SDimitry Andric if (!DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, nullptr, 0, buf, sizeof(buf), 445fe6060f1SDimitry Andric &out, 0)) 446fe6060f1SDimitry Andric return set_errno(); 447fe6060f1SDimitry Andric const auto *reparse = reinterpret_cast<LIBCPP_REPARSE_DATA_BUFFER *>(buf); 448fe6060f1SDimitry Andric size_t path_buf_offset = offsetof(LIBCPP_REPARSE_DATA_BUFFER, 449fe6060f1SDimitry Andric SymbolicLinkReparseBuffer.PathBuffer[0]); 450fe6060f1SDimitry Andric if (out < path_buf_offset) { 451fe6060f1SDimitry Andric errno = EINVAL; 452fe6060f1SDimitry Andric return -1; 453fe6060f1SDimitry Andric } 454fe6060f1SDimitry Andric if (reparse->ReparseTag != IO_REPARSE_TAG_SYMLINK) { 455fe6060f1SDimitry Andric errno = EINVAL; 456fe6060f1SDimitry Andric return -1; 457fe6060f1SDimitry Andric } 458fe6060f1SDimitry Andric const auto &symlink = reparse->SymbolicLinkReparseBuffer; 459fe6060f1SDimitry Andric unsigned short name_offset, name_length; 460fe6060f1SDimitry Andric if (symlink.PrintNameLength == 0) { 461fe6060f1SDimitry Andric name_offset = symlink.SubstituteNameOffset; 462fe6060f1SDimitry Andric name_length = symlink.SubstituteNameLength; 463fe6060f1SDimitry Andric } else { 464fe6060f1SDimitry Andric name_offset = symlink.PrintNameOffset; 465fe6060f1SDimitry Andric name_length = symlink.PrintNameLength; 466fe6060f1SDimitry Andric } 467fe6060f1SDimitry Andric // name_offset/length are expressed in bytes, not in wchar_t 468fe6060f1SDimitry Andric if (path_buf_offset + name_offset + name_length > out) { 469fe6060f1SDimitry Andric errno = EINVAL; 470fe6060f1SDimitry Andric return -1; 471fe6060f1SDimitry Andric } 472fe6060f1SDimitry Andric if (name_length / sizeof(wchar_t) > bufsize) { 473fe6060f1SDimitry Andric errno = ENOMEM; 474fe6060f1SDimitry Andric return -1; 475fe6060f1SDimitry Andric } 476fe6060f1SDimitry Andric memcpy(ret_buf, &symlink.PathBuffer[name_offset / sizeof(wchar_t)], 477fe6060f1SDimitry Andric name_length); 478fe6060f1SDimitry Andric return name_length / sizeof(wchar_t); 479fe6060f1SDimitry Andric } 480fe6060f1SDimitry Andric 481fe6060f1SDimitry Andric #else 482fe6060f1SDimitry Andric int symlink_file(const char *oldname, const char *newname) { 483fe6060f1SDimitry Andric return ::symlink(oldname, newname); 484fe6060f1SDimitry Andric } 485fe6060f1SDimitry Andric int symlink_dir(const char *oldname, const char *newname) { 486fe6060f1SDimitry Andric return ::symlink(oldname, newname); 487fe6060f1SDimitry Andric } 488fe6060f1SDimitry Andric using ::chdir; 489fe6060f1SDimitry Andric using ::close; 490fe6060f1SDimitry Andric using ::fchmod; 491fe6060f1SDimitry Andric #if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD) 492fe6060f1SDimitry Andric using ::fchmodat; 493fe6060f1SDimitry Andric #endif 494fe6060f1SDimitry Andric using ::fstat; 495fe6060f1SDimitry Andric using ::ftruncate; 496fe6060f1SDimitry Andric using ::getcwd; 497fe6060f1SDimitry Andric using ::link; 498fe6060f1SDimitry Andric using ::lstat; 499fe6060f1SDimitry Andric using ::mkdir; 500fe6060f1SDimitry Andric using ::open; 501fe6060f1SDimitry Andric using ::readlink; 502fe6060f1SDimitry Andric using ::realpath; 503fe6060f1SDimitry Andric using ::remove; 504fe6060f1SDimitry Andric using ::rename; 505fe6060f1SDimitry Andric using ::stat; 506fe6060f1SDimitry Andric using ::statvfs; 507fe6060f1SDimitry Andric using ::truncate; 508fe6060f1SDimitry Andric 509fe6060f1SDimitry Andric #define O_BINARY 0 510fe6060f1SDimitry Andric 511fe6060f1SDimitry Andric using StatVFS = struct statvfs; 512fe6060f1SDimitry Andric using ModeT = ::mode_t; 513fe6060f1SDimitry Andric using SSizeT = ::ssize_t; 514fe6060f1SDimitry Andric 515fe6060f1SDimitry Andric #endif 516fe6060f1SDimitry Andric 517fe6060f1SDimitry Andric } // namespace 518fe6060f1SDimitry Andric } // end namespace detail 519fe6060f1SDimitry Andric 520fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_FILESYSTEM 521fe6060f1SDimitry Andric 522fe6060f1SDimitry Andric #endif // POSIX_COMPAT_H 523