1//===- llvm/Support/Windows/Path.inc - Windows Path Impl --------*- C++ -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9// This file implements the Windows specific implementation of the Path API. 10// 11//===----------------------------------------------------------------------===// 12 13//===----------------------------------------------------------------------===// 14//=== WARNING: Implementation here must contain only generic Windows code that 15//=== is guaranteed to work on *all* Windows variants. 16//===----------------------------------------------------------------------===// 17 18#include "llvm/ADT/STLExtras.h" 19#include "llvm/Support/ConvertUTF.h" 20#include "llvm/Support/WindowsError.h" 21#include <fcntl.h> 22#include <sys/stat.h> 23#include <sys/types.h> 24 25// These two headers must be included last, and make sure shlobj is required 26// after Windows.h to make sure it picks up our definition of _WIN32_WINNT 27#include "llvm/Support/Windows/WindowsSupport.h" 28#include <shellapi.h> 29#include <shlobj.h> 30 31#undef max 32 33// MinGW doesn't define this. 34#ifndef _ERRNO_T_DEFINED 35#define _ERRNO_T_DEFINED 36typedef int errno_t; 37#endif 38 39#ifdef _MSC_VER 40#pragma comment(lib, "advapi32.lib") // This provides CryptAcquireContextW. 41#pragma comment(lib, "ole32.lib") // This provides CoTaskMemFree 42#endif 43 44using namespace llvm; 45 46using llvm::sys::windows::CurCPToUTF16; 47using llvm::sys::windows::UTF16ToUTF8; 48using llvm::sys::windows::UTF8ToUTF16; 49using llvm::sys::windows::widenPath; 50 51static bool is_separator(const wchar_t value) { 52 switch (value) { 53 case L'\\': 54 case L'/': 55 return true; 56 default: 57 return false; 58 } 59} 60 61namespace llvm { 62namespace sys { 63namespace windows { 64 65// Convert a UTF-8 path to UTF-16. Also, if the absolute equivalent of the path 66// is longer than the limit that the Win32 Unicode File API can tolerate, make 67// it an absolute normalized path prefixed by '\\?\'. 68std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16, 69 size_t MaxPathLen) { 70 assert(MaxPathLen <= MAX_PATH); 71 72 // Several operations would convert Path8 to SmallString; more efficient to do 73 // it once up front. 74 SmallString<MAX_PATH> Path8Str; 75 Path8.toVector(Path8Str); 76 77 // If the path is a long path, mangled into forward slashes, normalize 78 // back to backslashes here. 79 if (Path8Str.starts_with("//?/")) 80 llvm::sys::path::native(Path8Str, path::Style::windows_backslash); 81 82 if (std::error_code EC = UTF8ToUTF16(Path8Str, Path16)) 83 return EC; 84 85 const bool IsAbsolute = llvm::sys::path::is_absolute(Path8); 86 size_t CurPathLen; 87 if (IsAbsolute) 88 CurPathLen = 0; // No contribution from current_path needed. 89 else { 90 CurPathLen = ::GetCurrentDirectoryW( 91 0, NULL); // Returns the size including the null terminator. 92 if (CurPathLen == 0) 93 return mapWindowsError(::GetLastError()); 94 } 95 96 const char *const LongPathPrefix = "\\\\?\\"; 97 98 if ((Path16.size() + CurPathLen) < MaxPathLen || 99 Path8Str.starts_with(LongPathPrefix)) 100 return std::error_code(); 101 102 if (!IsAbsolute) { 103 if (std::error_code EC = llvm::sys::fs::make_absolute(Path8Str)) 104 return EC; 105 } 106 107 // Remove '.' and '..' because long paths treat these as real path components. 108 // Explicitly use the backslash form here, as we're prepending the \\?\ 109 // prefix. 110 llvm::sys::path::native(Path8Str, path::Style::windows); 111 llvm::sys::path::remove_dots(Path8Str, true, path::Style::windows); 112 113 const StringRef RootName = llvm::sys::path::root_name(Path8Str); 114 assert(!RootName.empty() && 115 "Root name cannot be empty for an absolute path!"); 116 117 SmallString<2 * MAX_PATH> FullPath(LongPathPrefix); 118 if (RootName[1] != ':') { // Check if UNC. 119 FullPath.append("UNC\\"); 120 FullPath.append(Path8Str.begin() + 2, Path8Str.end()); 121 } else { 122 FullPath.append(Path8Str); 123 } 124 125 return UTF8ToUTF16(FullPath, Path16); 126} 127 128} // end namespace windows 129 130namespace fs { 131 132const file_t kInvalidFile = INVALID_HANDLE_VALUE; 133 134std::string getMainExecutable(const char *argv0, void *MainExecAddr) { 135 SmallVector<wchar_t, MAX_PATH> PathName; 136 PathName.resize_for_overwrite(PathName.capacity()); 137 DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.size()); 138 139 // A zero return value indicates a failure other than insufficient space. 140 if (Size == 0) 141 return ""; 142 143 // Insufficient space is determined by a return value equal to the size of 144 // the buffer passed in. 145 if (Size == PathName.capacity()) 146 return ""; 147 148 // On success, GetModuleFileNameW returns the number of characters written to 149 // the buffer not including the NULL terminator. 150 PathName.truncate(Size); 151 152 // Convert the result from UTF-16 to UTF-8. 153 SmallVector<char, MAX_PATH> PathNameUTF8; 154 if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8)) 155 return ""; 156 157 llvm::sys::path::make_preferred(PathNameUTF8); 158 159 SmallString<256> RealPath; 160 sys::fs::real_path(PathNameUTF8, RealPath); 161 if (RealPath.size()) 162 return std::string(RealPath); 163 return std::string(PathNameUTF8.data()); 164} 165 166UniqueID file_status::getUniqueID() const { 167 return UniqueID(VolumeSerialNumber, PathHash); 168} 169 170ErrorOr<space_info> disk_space(const Twine &Path) { 171 ULARGE_INTEGER Avail, Total, Free; 172 if (!::GetDiskFreeSpaceExA(Path.str().c_str(), &Avail, &Total, &Free)) 173 return mapWindowsError(::GetLastError()); 174 space_info SpaceInfo; 175 SpaceInfo.capacity = 176 (static_cast<uint64_t>(Total.HighPart) << 32) + Total.LowPart; 177 SpaceInfo.free = (static_cast<uint64_t>(Free.HighPart) << 32) + Free.LowPart; 178 SpaceInfo.available = 179 (static_cast<uint64_t>(Avail.HighPart) << 32) + Avail.LowPart; 180 return SpaceInfo; 181} 182 183TimePoint<> basic_file_status::getLastAccessedTime() const { 184 FILETIME Time; 185 Time.dwLowDateTime = LastAccessedTimeLow; 186 Time.dwHighDateTime = LastAccessedTimeHigh; 187 return toTimePoint(Time); 188} 189 190TimePoint<> basic_file_status::getLastModificationTime() const { 191 FILETIME Time; 192 Time.dwLowDateTime = LastWriteTimeLow; 193 Time.dwHighDateTime = LastWriteTimeHigh; 194 return toTimePoint(Time); 195} 196 197uint32_t file_status::getLinkCount() const { return NumLinks; } 198 199std::error_code current_path(SmallVectorImpl<char> &result) { 200 SmallVector<wchar_t, MAX_PATH> cur_path; 201 DWORD len = MAX_PATH; 202 203 do { 204 cur_path.resize_for_overwrite(len); 205 len = ::GetCurrentDirectoryW(cur_path.size(), cur_path.data()); 206 207 // A zero return value indicates a failure other than insufficient space. 208 if (len == 0) 209 return mapWindowsError(::GetLastError()); 210 211 // If there's insufficient space, the len returned is larger than the len 212 // given. 213 } while (len > cur_path.size()); 214 215 // On success, GetCurrentDirectoryW returns the number of characters not 216 // including the null-terminator. 217 cur_path.truncate(len); 218 219 if (std::error_code EC = 220 UTF16ToUTF8(cur_path.begin(), cur_path.size(), result)) 221 return EC; 222 223 llvm::sys::path::make_preferred(result); 224 return std::error_code(); 225} 226 227std::error_code set_current_path(const Twine &path) { 228 // Convert to utf-16. 229 SmallVector<wchar_t, 128> wide_path; 230 if (std::error_code ec = widenPath(path, wide_path)) 231 return ec; 232 233 if (!::SetCurrentDirectoryW(wide_path.begin())) 234 return mapWindowsError(::GetLastError()); 235 236 return std::error_code(); 237} 238 239std::error_code create_directory(const Twine &path, bool IgnoreExisting, 240 perms Perms) { 241 SmallVector<wchar_t, 128> path_utf16; 242 243 // CreateDirectoryW has a lower maximum path length as it must leave room for 244 // an 8.3 filename. 245 if (std::error_code ec = widenPath(path, path_utf16, MAX_PATH - 12)) 246 return ec; 247 248 if (!::CreateDirectoryW(path_utf16.begin(), NULL)) { 249 DWORD LastError = ::GetLastError(); 250 if (LastError != ERROR_ALREADY_EXISTS || !IgnoreExisting) 251 return mapWindowsError(LastError); 252 } 253 254 return std::error_code(); 255} 256 257// We can't use symbolic links for windows. 258std::error_code create_link(const Twine &to, const Twine &from) { 259 // Convert to utf-16. 260 SmallVector<wchar_t, 128> wide_from; 261 SmallVector<wchar_t, 128> wide_to; 262 if (std::error_code ec = widenPath(from, wide_from)) 263 return ec; 264 if (std::error_code ec = widenPath(to, wide_to)) 265 return ec; 266 267 if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL)) 268 return mapWindowsError(::GetLastError()); 269 270 return std::error_code(); 271} 272 273std::error_code create_hard_link(const Twine &to, const Twine &from) { 274 return create_link(to, from); 275} 276 277std::error_code remove(const Twine &path, bool IgnoreNonExisting) { 278 SmallVector<wchar_t, 128> path_utf16; 279 280 if (std::error_code ec = widenPath(path, path_utf16)) 281 return ec; 282 283 // We don't know whether this is a file or a directory, and remove() can 284 // accept both. The usual way to delete a file or directory is to use one of 285 // the DeleteFile or RemoveDirectory functions, but that requires you to know 286 // which one it is. We could stat() the file to determine that, but that would 287 // cost us additional system calls, which can be slow in a directory 288 // containing a large number of files. So instead we call CreateFile directly. 289 // The important part is the FILE_FLAG_DELETE_ON_CLOSE flag, which causes the 290 // file to be deleted once it is closed. We also use the flags 291 // FILE_FLAG_BACKUP_SEMANTICS (which allows us to open directories), and 292 // FILE_FLAG_OPEN_REPARSE_POINT (don't follow symlinks). 293 ScopedFileHandle h(::CreateFileW( 294 c_str(path_utf16), DELETE, 295 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 296 OPEN_EXISTING, 297 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS | 298 FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_DELETE_ON_CLOSE, 299 NULL)); 300 if (!h) { 301 std::error_code EC = mapWindowsError(::GetLastError()); 302 if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting) 303 return EC; 304 } 305 306 return std::error_code(); 307} 308 309static std::error_code is_local_internal(SmallVectorImpl<wchar_t> &Path, 310 bool &Result) { 311 SmallVector<wchar_t, 128> VolumePath; 312 size_t Len = 128; 313 while (true) { 314 VolumePath.resize(Len); 315 BOOL Success = 316 ::GetVolumePathNameW(Path.data(), VolumePath.data(), VolumePath.size()); 317 318 if (Success) 319 break; 320 321 DWORD Err = ::GetLastError(); 322 if (Err != ERROR_INSUFFICIENT_BUFFER) 323 return mapWindowsError(Err); 324 325 Len *= 2; 326 } 327 // If the output buffer has exactly enough space for the path name, but not 328 // the null terminator, it will leave the output unterminated. Push a null 329 // terminator onto the end to ensure that this never happens. 330 VolumePath.push_back(L'\0'); 331 VolumePath.truncate(wcslen(VolumePath.data())); 332 const wchar_t *P = VolumePath.data(); 333 334 UINT Type = ::GetDriveTypeW(P); 335 switch (Type) { 336 case DRIVE_FIXED: 337 Result = true; 338 return std::error_code(); 339 case DRIVE_REMOTE: 340 case DRIVE_CDROM: 341 case DRIVE_RAMDISK: 342 case DRIVE_REMOVABLE: 343 Result = false; 344 return std::error_code(); 345 default: 346 return make_error_code(errc::no_such_file_or_directory); 347 } 348 llvm_unreachable("Unreachable!"); 349} 350 351std::error_code is_local(const Twine &path, bool &result) { 352 if (!llvm::sys::fs::exists(path) || !llvm::sys::path::has_root_path(path)) 353 return make_error_code(errc::no_such_file_or_directory); 354 355 SmallString<128> Storage; 356 StringRef P = path.toStringRef(Storage); 357 358 // Convert to utf-16. 359 SmallVector<wchar_t, 128> WidePath; 360 if (std::error_code ec = widenPath(P, WidePath)) 361 return ec; 362 return is_local_internal(WidePath, result); 363} 364 365static std::error_code realPathFromHandle(HANDLE H, 366 SmallVectorImpl<wchar_t> &Buffer, 367 DWORD flags = VOLUME_NAME_DOS) { 368 Buffer.resize_for_overwrite(Buffer.capacity()); 369 DWORD CountChars = ::GetFinalPathNameByHandleW( 370 H, Buffer.begin(), Buffer.capacity(), FILE_NAME_NORMALIZED | flags); 371 if (CountChars && CountChars >= Buffer.capacity()) { 372 // The buffer wasn't big enough, try again. In this case the return value 373 // *does* indicate the size of the null terminator. 374 Buffer.resize_for_overwrite(CountChars); 375 CountChars = ::GetFinalPathNameByHandleW(H, Buffer.begin(), Buffer.size(), 376 FILE_NAME_NORMALIZED | flags); 377 } 378 Buffer.truncate(CountChars); 379 if (CountChars == 0) 380 return mapWindowsError(GetLastError()); 381 return std::error_code(); 382} 383 384static std::error_code realPathFromHandle(HANDLE H, 385 SmallVectorImpl<char> &RealPath) { 386 RealPath.clear(); 387 SmallVector<wchar_t, MAX_PATH> Buffer; 388 if (std::error_code EC = realPathFromHandle(H, Buffer)) 389 return EC; 390 391 // Strip the \\?\ prefix. We don't want it ending up in output, and such 392 // paths don't get canonicalized by file APIs. 393 wchar_t *Data = Buffer.data(); 394 DWORD CountChars = Buffer.size(); 395 if (CountChars >= 8 && ::memcmp(Data, L"\\\\?\\UNC\\", 16) == 0) { 396 // Convert \\?\UNC\foo\bar to \\foo\bar 397 CountChars -= 6; 398 Data += 6; 399 Data[0] = '\\'; 400 } else if (CountChars >= 4 && ::memcmp(Data, L"\\\\?\\", 8) == 0) { 401 // Convert \\?\c:\foo to c:\foo 402 CountChars -= 4; 403 Data += 4; 404 } 405 406 // Convert the result from UTF-16 to UTF-8. 407 if (std::error_code EC = UTF16ToUTF8(Data, CountChars, RealPath)) 408 return EC; 409 410 llvm::sys::path::make_preferred(RealPath); 411 return std::error_code(); 412} 413 414std::error_code is_local(int FD, bool &Result) { 415 SmallVector<wchar_t, 128> FinalPath; 416 HANDLE Handle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); 417 418 if (std::error_code EC = realPathFromHandle(Handle, FinalPath)) 419 return EC; 420 421 return is_local_internal(FinalPath, Result); 422} 423 424static std::error_code setDeleteDisposition(HANDLE Handle, bool Delete) { 425 // Clear the FILE_DISPOSITION_INFO flag first, before checking if it's a 426 // network file. On Windows 7 the function realPathFromHandle() below fails 427 // if the FILE_DISPOSITION_INFO flag was already set to 'DeleteFile = true' by 428 // a prior call. 429 FILE_DISPOSITION_INFO Disposition; 430 Disposition.DeleteFile = false; 431 if (!SetFileInformationByHandle(Handle, FileDispositionInfo, &Disposition, 432 sizeof(Disposition))) 433 return mapWindowsError(::GetLastError()); 434 if (!Delete) 435 return std::error_code(); 436 437 // Check if the file is on a network (non-local) drive. If so, don't 438 // continue when DeleteFile is true, since it prevents opening the file for 439 // writes. 440 SmallVector<wchar_t, 128> FinalPath; 441 if (std::error_code EC = realPathFromHandle(Handle, FinalPath)) 442 return EC; 443 444 bool IsLocal; 445 if (std::error_code EC = is_local_internal(FinalPath, IsLocal)) 446 return EC; 447 448 if (!IsLocal) 449 return errc::not_supported; 450 451 // The file is on a local drive, we can safely set FILE_DISPOSITION_INFO's 452 // flag. 453 Disposition.DeleteFile = true; 454 if (!SetFileInformationByHandle(Handle, FileDispositionInfo, &Disposition, 455 sizeof(Disposition))) 456 return mapWindowsError(::GetLastError()); 457 return std::error_code(); 458} 459 460static std::error_code rename_internal(HANDLE FromHandle, const Twine &To, 461 bool ReplaceIfExists) { 462 SmallVector<wchar_t, 0> ToWide; 463 if (auto EC = widenPath(To, ToWide)) 464 return EC; 465 466 std::vector<char> RenameInfoBuf(sizeof(FILE_RENAME_INFO) - sizeof(wchar_t) + 467 (ToWide.size() * sizeof(wchar_t))); 468 FILE_RENAME_INFO &RenameInfo = 469 *reinterpret_cast<FILE_RENAME_INFO *>(RenameInfoBuf.data()); 470 RenameInfo.ReplaceIfExists = ReplaceIfExists; 471 RenameInfo.RootDirectory = 0; 472 RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t); 473 std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]); 474 475 SetLastError(ERROR_SUCCESS); 476 if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo, 477 RenameInfoBuf.size())) { 478 unsigned Error = GetLastError(); 479 if (Error == ERROR_SUCCESS) 480 Error = ERROR_CALL_NOT_IMPLEMENTED; // Wine doesn't always set error code. 481 return mapWindowsError(Error); 482 } 483 484 return std::error_code(); 485} 486 487static std::error_code rename_handle(HANDLE FromHandle, const Twine &To) { 488 SmallVector<wchar_t, 128> WideTo; 489 if (std::error_code EC = widenPath(To, WideTo)) 490 return EC; 491 492 // We normally expect this loop to succeed after a few iterations. If it 493 // requires more than 200 tries, it's more likely that the failures are due to 494 // a true error, so stop trying. 495 for (unsigned Retry = 0; Retry != 200; ++Retry) { 496 auto EC = rename_internal(FromHandle, To, true); 497 498 if (EC == 499 std::error_code(ERROR_CALL_NOT_IMPLEMENTED, std::system_category())) { 500 // Wine doesn't support SetFileInformationByHandle in rename_internal. 501 // Fall back to MoveFileEx. 502 SmallVector<wchar_t, MAX_PATH> WideFrom; 503 if (std::error_code EC2 = realPathFromHandle(FromHandle, WideFrom)) 504 return EC2; 505 if (::MoveFileExW(WideFrom.begin(), WideTo.begin(), 506 MOVEFILE_REPLACE_EXISTING)) 507 return std::error_code(); 508 return mapWindowsError(GetLastError()); 509 } 510 511 if (!EC || EC != errc::permission_denied) 512 return EC; 513 514 // The destination file probably exists and is currently open in another 515 // process, either because the file was opened without FILE_SHARE_DELETE or 516 // it is mapped into memory (e.g. using MemoryBuffer). Rename it in order to 517 // move it out of the way of the source file. Use FILE_FLAG_DELETE_ON_CLOSE 518 // to arrange for the destination file to be deleted when the other process 519 // closes it. 520 ScopedFileHandle ToHandle( 521 ::CreateFileW(WideTo.begin(), GENERIC_READ | DELETE, 522 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 523 NULL, OPEN_EXISTING, 524 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)); 525 if (!ToHandle) { 526 auto EC = mapWindowsError(GetLastError()); 527 // Another process might have raced with us and moved the existing file 528 // out of the way before we had a chance to open it. If that happens, try 529 // to rename the source file again. 530 if (EC == errc::no_such_file_or_directory) 531 continue; 532 return EC; 533 } 534 535 BY_HANDLE_FILE_INFORMATION FI; 536 if (!GetFileInformationByHandle(ToHandle, &FI)) 537 return mapWindowsError(GetLastError()); 538 539 // Try to find a unique new name for the destination file. 540 for (unsigned UniqueId = 0; UniqueId != 200; ++UniqueId) { 541 std::string TmpFilename = (To + ".tmp" + utostr(UniqueId)).str(); 542 if (auto EC = rename_internal(ToHandle, TmpFilename, false)) { 543 if (EC == errc::file_exists || EC == errc::permission_denied) { 544 // Again, another process might have raced with us and moved the file 545 // before we could move it. Check whether this is the case, as it 546 // might have caused the permission denied error. If that was the 547 // case, we don't need to move it ourselves. 548 ScopedFileHandle ToHandle2(::CreateFileW( 549 WideTo.begin(), 0, 550 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 551 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); 552 if (!ToHandle2) { 553 auto EC = mapWindowsError(GetLastError()); 554 if (EC == errc::no_such_file_or_directory) 555 break; 556 return EC; 557 } 558 BY_HANDLE_FILE_INFORMATION FI2; 559 if (!GetFileInformationByHandle(ToHandle2, &FI2)) 560 return mapWindowsError(GetLastError()); 561 if (FI.nFileIndexHigh != FI2.nFileIndexHigh || 562 FI.nFileIndexLow != FI2.nFileIndexLow || 563 FI.dwVolumeSerialNumber != FI2.dwVolumeSerialNumber) 564 break; 565 continue; 566 } 567 return EC; 568 } 569 break; 570 } 571 572 // Okay, the old destination file has probably been moved out of the way at 573 // this point, so try to rename the source file again. Still, another 574 // process might have raced with us to create and open the destination 575 // file, so we need to keep doing this until we succeed. 576 } 577 578 // The most likely root cause. 579 return errc::permission_denied; 580} 581 582std::error_code rename(const Twine &From, const Twine &To) { 583 // Convert to utf-16. 584 SmallVector<wchar_t, 128> WideFrom; 585 if (std::error_code EC = widenPath(From, WideFrom)) 586 return EC; 587 588 ScopedFileHandle FromHandle; 589 // Retry this a few times to defeat badly behaved file system scanners. 590 for (unsigned Retry = 0; Retry != 200; ++Retry) { 591 if (Retry != 0) 592 ::Sleep(10); 593 FromHandle = 594 ::CreateFileW(WideFrom.begin(), GENERIC_READ | DELETE, 595 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 596 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 597 if (FromHandle) 598 break; 599 600 // We don't want to loop if the file doesn't exist. 601 auto EC = mapWindowsError(GetLastError()); 602 if (EC == errc::no_such_file_or_directory) 603 return EC; 604 } 605 if (!FromHandle) 606 return mapWindowsError(GetLastError()); 607 608 return rename_handle(FromHandle, To); 609} 610 611std::error_code resize_file(int FD, uint64_t Size) { 612#ifdef HAVE__CHSIZE_S 613 errno_t error = ::_chsize_s(FD, Size); 614#else 615 errno_t error = ::_chsize(FD, Size); 616#endif 617 return std::error_code(error, std::generic_category()); 618} 619 620std::error_code access(const Twine &Path, AccessMode Mode) { 621 SmallVector<wchar_t, 128> PathUtf16; 622 623 if (std::error_code EC = widenPath(Path, PathUtf16)) 624 return EC; 625 626 DWORD Attributes = ::GetFileAttributesW(PathUtf16.begin()); 627 628 if (Attributes == INVALID_FILE_ATTRIBUTES) { 629 // Avoid returning unexpected error codes when querying for existence. 630 if (Mode == AccessMode::Exist) 631 return errc::no_such_file_or_directory; 632 633 // See if the file didn't actually exist. 634 DWORD LastError = ::GetLastError(); 635 if (LastError != ERROR_FILE_NOT_FOUND && LastError != ERROR_PATH_NOT_FOUND) 636 return mapWindowsError(LastError); 637 return errc::no_such_file_or_directory; 638 } 639 640 if (Mode == AccessMode::Write && (Attributes & FILE_ATTRIBUTE_READONLY)) 641 return errc::permission_denied; 642 643 if (Mode == AccessMode::Execute && (Attributes & FILE_ATTRIBUTE_DIRECTORY)) 644 return errc::permission_denied; 645 646 return std::error_code(); 647} 648 649bool can_execute(const Twine &Path) { 650 return !access(Path, AccessMode::Execute) || 651 !access(Path + ".exe", AccessMode::Execute); 652} 653 654bool equivalent(file_status A, file_status B) { 655 assert(status_known(A) && status_known(B)); 656 return A.getUniqueID() == B.getUniqueID(); 657} 658 659std::error_code equivalent(const Twine &A, const Twine &B, bool &result) { 660 file_status fsA, fsB; 661 if (std::error_code ec = status(A, fsA)) 662 return ec; 663 if (std::error_code ec = status(B, fsB)) 664 return ec; 665 result = equivalent(fsA, fsB); 666 return std::error_code(); 667} 668 669static bool isReservedName(StringRef path) { 670 // This list of reserved names comes from MSDN, at: 671 // http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx 672 static const char *const sReservedNames[] = { 673 "nul", "con", "prn", "aux", "com1", "com2", "com3", "com4", 674 "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", 675 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9"}; 676 677 // First, check to see if this is a device namespace, which always 678 // starts with \\.\, since device namespaces are not legal file paths. 679 if (path.starts_with("\\\\.\\")) 680 return true; 681 682 // Then compare against the list of ancient reserved names. 683 for (size_t i = 0; i < std::size(sReservedNames); ++i) { 684 if (path.equals_insensitive(sReservedNames[i])) 685 return true; 686 } 687 688 // The path isn't what we consider reserved. 689 return false; 690} 691 692static file_type file_type_from_attrs(DWORD Attrs) { 693 return (Attrs & FILE_ATTRIBUTE_DIRECTORY) ? file_type::directory_file 694 : file_type::regular_file; 695} 696 697static perms perms_from_attrs(DWORD Attrs) { 698 return (Attrs & FILE_ATTRIBUTE_READONLY) ? (all_read | all_exe) : all_all; 699} 700 701static std::error_code getStatus(HANDLE FileHandle, file_status &Result) { 702 SmallVector<wchar_t, MAX_PATH> ntPath; 703 if (FileHandle == INVALID_HANDLE_VALUE) 704 goto handle_status_error; 705 706 switch (::GetFileType(FileHandle)) { 707 default: 708 llvm_unreachable("Don't know anything about this file type"); 709 case FILE_TYPE_UNKNOWN: { 710 DWORD Err = ::GetLastError(); 711 if (Err != NO_ERROR) 712 return mapWindowsError(Err); 713 Result = file_status(file_type::type_unknown); 714 return std::error_code(); 715 } 716 case FILE_TYPE_DISK: 717 break; 718 case FILE_TYPE_CHAR: 719 Result = file_status(file_type::character_file); 720 return std::error_code(); 721 case FILE_TYPE_PIPE: 722 Result = file_status(file_type::fifo_file); 723 return std::error_code(); 724 } 725 726 BY_HANDLE_FILE_INFORMATION Info; 727 if (!::GetFileInformationByHandle(FileHandle, &Info)) 728 goto handle_status_error; 729 730 // File indices aren't necessarily stable after closing the file handle; 731 // instead hash a canonicalized path. 732 // 733 // For getting a canonical path to the file, call GetFinalPathNameByHandleW 734 // with VOLUME_NAME_NT. We don't really care exactly what the path looks 735 // like here, as long as it is canonical (e.g. doesn't differentiate between 736 // whether a file was referred to with upper/lower case names originally). 737 // The default format with VOLUME_NAME_DOS doesn't work with all file system 738 // drivers, such as ImDisk. (See 739 // https://github.com/rust-lang/rust/pull/86447.) 740 uint64_t PathHash; 741 if (std::error_code EC = 742 realPathFromHandle(FileHandle, ntPath, VOLUME_NAME_NT)) { 743 // If realPathFromHandle failed, fall back on the fields 744 // nFileIndex{High,Low} instead. They're not necessarily stable on all file 745 // systems as they're only documented as being unique/stable as long as the 746 // file handle is open - but they're a decent fallback if we couldn't get 747 // the canonical path. 748 PathHash = (static_cast<uint64_t>(Info.nFileIndexHigh) << 32ULL) | 749 static_cast<uint64_t>(Info.nFileIndexLow); 750 } else { 751 PathHash = hash_combine_range(ntPath); 752 } 753 754 Result = file_status( 755 file_type_from_attrs(Info.dwFileAttributes), 756 perms_from_attrs(Info.dwFileAttributes), Info.nNumberOfLinks, 757 Info.ftLastAccessTime.dwHighDateTime, Info.ftLastAccessTime.dwLowDateTime, 758 Info.ftLastWriteTime.dwHighDateTime, Info.ftLastWriteTime.dwLowDateTime, 759 Info.dwVolumeSerialNumber, Info.nFileSizeHigh, Info.nFileSizeLow, 760 PathHash); 761 return std::error_code(); 762 763handle_status_error: 764 std::error_code Err = mapLastWindowsError(); 765 if (Err == std::errc::no_such_file_or_directory) 766 Result = file_status(file_type::file_not_found); 767 else if (Err == std::errc::permission_denied) 768 Result = file_status(file_type::type_unknown); 769 else 770 Result = file_status(file_type::status_error); 771 return Err; 772} 773 774std::error_code status(const Twine &path, file_status &result, bool Follow) { 775 SmallString<128> path_storage; 776 SmallVector<wchar_t, 128> path_utf16; 777 778 StringRef path8 = path.toStringRef(path_storage); 779 if (isReservedName(path8)) { 780 result = file_status(file_type::character_file); 781 return std::error_code(); 782 } 783 784 if (std::error_code ec = widenPath(path8, path_utf16)) 785 return ec; 786 787 DWORD Flags = FILE_FLAG_BACKUP_SEMANTICS; 788 if (!Follow) { 789 DWORD attr = ::GetFileAttributesW(path_utf16.begin()); 790 if (attr == INVALID_FILE_ATTRIBUTES) 791 return getStatus(INVALID_HANDLE_VALUE, result); 792 793 // Handle reparse points. 794 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) 795 Flags |= FILE_FLAG_OPEN_REPARSE_POINT; 796 } 797 798 ScopedFileHandle h( 799 ::CreateFileW(path_utf16.begin(), 0, // Attributes only. 800 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 801 NULL, OPEN_EXISTING, Flags, 0)); 802 if (!h) 803 return getStatus(INVALID_HANDLE_VALUE, result); 804 805 return getStatus(h, result); 806} 807 808std::error_code status(int FD, file_status &Result) { 809 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); 810 return getStatus(FileHandle, Result); 811} 812 813std::error_code status(file_t FileHandle, file_status &Result) { 814 return getStatus(FileHandle, Result); 815} 816 817unsigned getUmask() { return 0; } 818 819std::error_code setPermissions(const Twine &Path, perms Permissions) { 820 SmallVector<wchar_t, 128> PathUTF16; 821 if (std::error_code EC = widenPath(Path, PathUTF16)) 822 return EC; 823 824 DWORD Attributes = ::GetFileAttributesW(PathUTF16.begin()); 825 if (Attributes == INVALID_FILE_ATTRIBUTES) 826 return mapWindowsError(GetLastError()); 827 828 // There are many Windows file attributes that are not to do with the file 829 // permissions (e.g. FILE_ATTRIBUTE_HIDDEN). We need to be careful to preserve 830 // them. 831 if (Permissions & all_write) { 832 Attributes &= ~FILE_ATTRIBUTE_READONLY; 833 if (Attributes == 0) 834 // FILE_ATTRIBUTE_NORMAL indicates no other attributes are set. 835 Attributes |= FILE_ATTRIBUTE_NORMAL; 836 } else { 837 Attributes |= FILE_ATTRIBUTE_READONLY; 838 // FILE_ATTRIBUTE_NORMAL is not compatible with any other attributes, so 839 // remove it, if it is present. 840 Attributes &= ~FILE_ATTRIBUTE_NORMAL; 841 } 842 843 if (!::SetFileAttributesW(PathUTF16.begin(), Attributes)) 844 return mapWindowsError(GetLastError()); 845 846 return std::error_code(); 847} 848 849std::error_code setPermissions(int FD, perms Permissions) { 850 // FIXME Not implemented. 851 return std::make_error_code(std::errc::not_supported); 852} 853 854std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime, 855 TimePoint<> ModificationTime) { 856 FILETIME AccessFT = toFILETIME(AccessTime); 857 FILETIME ModifyFT = toFILETIME(ModificationTime); 858 HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); 859 if (!SetFileTime(FileHandle, NULL, &AccessFT, &ModifyFT)) 860 return mapWindowsError(::GetLastError()); 861 return std::error_code(); 862} 863 864std::error_code mapped_file_region::init(sys::fs::file_t OrigFileHandle, 865 uint64_t Offset, mapmode Mode) { 866 this->Mode = Mode; 867 if (OrigFileHandle == INVALID_HANDLE_VALUE) 868 return make_error_code(errc::bad_file_descriptor); 869 870 DWORD flprotect; 871 switch (Mode) { 872 case readonly: 873 flprotect = PAGE_READONLY; 874 break; 875 case readwrite: 876 flprotect = PAGE_READWRITE; 877 break; 878 case priv: 879 flprotect = PAGE_WRITECOPY; 880 break; 881 } 882 883 HANDLE FileMappingHandle = ::CreateFileMappingW(OrigFileHandle, 0, flprotect, 884 Hi_32(Size), Lo_32(Size), 0); 885 if (FileMappingHandle == NULL) { 886 std::error_code ec = mapWindowsError(GetLastError()); 887 return ec; 888 } 889 890 DWORD dwDesiredAccess; 891 switch (Mode) { 892 case readonly: 893 dwDesiredAccess = FILE_MAP_READ; 894 break; 895 case readwrite: 896 dwDesiredAccess = FILE_MAP_WRITE; 897 break; 898 case priv: 899 dwDesiredAccess = FILE_MAP_COPY; 900 break; 901 } 902 Mapping = ::MapViewOfFile(FileMappingHandle, dwDesiredAccess, Offset >> 32, 903 Offset & 0xffffffff, Size); 904 if (Mapping == NULL) { 905 std::error_code ec = mapWindowsError(GetLastError()); 906 ::CloseHandle(FileMappingHandle); 907 return ec; 908 } 909 910 if (Size == 0) { 911 MEMORY_BASIC_INFORMATION mbi; 912 SIZE_T Result = VirtualQuery(Mapping, &mbi, sizeof(mbi)); 913 if (Result == 0) { 914 std::error_code ec = mapWindowsError(GetLastError()); 915 ::UnmapViewOfFile(Mapping); 916 ::CloseHandle(FileMappingHandle); 917 return ec; 918 } 919 Size = mbi.RegionSize; 920 } 921 922 // Close the file mapping handle, as it's kept alive by the file mapping. But 923 // neither the file mapping nor the file mapping handle keep the file handle 924 // alive, so we need to keep a reference to the file in case all other handles 925 // are closed and the file is deleted, which may cause invalid data to be read 926 // from the file. 927 ::CloseHandle(FileMappingHandle); 928 if (!::DuplicateHandle(::GetCurrentProcess(), OrigFileHandle, 929 ::GetCurrentProcess(), &FileHandle, 0, 0, 930 DUPLICATE_SAME_ACCESS)) { 931 std::error_code ec = mapWindowsError(GetLastError()); 932 ::UnmapViewOfFile(Mapping); 933 return ec; 934 } 935 936 return std::error_code(); 937} 938 939mapped_file_region::mapped_file_region(sys::fs::file_t fd, mapmode mode, 940 size_t length, uint64_t offset, 941 std::error_code &ec) 942 : Size(length) { 943 ec = init(fd, offset, mode); 944 if (ec) 945 copyFrom(mapped_file_region()); 946} 947 948static bool hasFlushBufferKernelBug() { 949 static bool Ret{GetWindowsOSVersion() < llvm::VersionTuple(10, 0, 0, 17763)}; 950 return Ret; 951} 952 953static bool isEXE(StringRef Magic) { 954 static const char PEMagic[] = {'P', 'E', '\0', '\0'}; 955 if (Magic.starts_with(StringRef("MZ")) && Magic.size() >= 0x3c + 4) { 956 uint32_t off = read32le(Magic.data() + 0x3c); 957 // PE/COFF file, either EXE or DLL. 958 if (Magic.substr(off).starts_with(StringRef(PEMagic, sizeof(PEMagic)))) 959 return true; 960 } 961 return false; 962} 963 964void mapped_file_region::unmapImpl() { 965 if (Mapping) { 966 967 bool Exe = isEXE(StringRef((char *)Mapping, Size)); 968 969 ::UnmapViewOfFile(Mapping); 970 971 if (Mode == mapmode::readwrite) { 972 bool DoFlush = Exe && hasFlushBufferKernelBug(); 973 // There is a Windows kernel bug, the exact trigger conditions of which 974 // are not well understood. When triggered, dirty pages are not properly 975 // flushed and subsequent process's attempts to read a file can return 976 // invalid data. Calling FlushFileBuffers on the write handle is 977 // sufficient to ensure that this bug is not triggered. 978 // The bug only occurs when writing an executable and executing it right 979 // after, under high I/O pressure. 980 if (!DoFlush) { 981 // Separately, on VirtualBox Shared Folder mounts, writes via memory 982 // maps always end up unflushed (regardless of version of Windows), 983 // unless flushed with this explicit call, if they are renamed with 984 // SetFileInformationByHandle(FileRenameInfo) before closing the output 985 // handle. 986 // 987 // As the flushing is quite expensive, use a heuristic to limit the 988 // cases where we do the flushing. Only do the flushing if we aren't 989 // sure we are on a local file system. 990 bool IsLocal = false; 991 SmallVector<wchar_t, 128> FinalPath; 992 if (!realPathFromHandle(FileHandle, FinalPath)) { 993 // Not checking the return value here - if the check fails, assume the 994 // file isn't local. 995 is_local_internal(FinalPath, IsLocal); 996 } 997 DoFlush = !IsLocal; 998 } 999 if (DoFlush) 1000 ::FlushFileBuffers(FileHandle); 1001 } 1002 1003 ::CloseHandle(FileHandle); 1004 } 1005} 1006 1007void mapped_file_region::dontNeedImpl() {} 1008 1009int mapped_file_region::alignment() { 1010 SYSTEM_INFO SysInfo; 1011 ::GetSystemInfo(&SysInfo); 1012 return SysInfo.dwAllocationGranularity; 1013} 1014 1015static basic_file_status status_from_find_data(WIN32_FIND_DATAW *FindData) { 1016 return basic_file_status(file_type_from_attrs(FindData->dwFileAttributes), 1017 perms_from_attrs(FindData->dwFileAttributes), 1018 FindData->ftLastAccessTime.dwHighDateTime, 1019 FindData->ftLastAccessTime.dwLowDateTime, 1020 FindData->ftLastWriteTime.dwHighDateTime, 1021 FindData->ftLastWriteTime.dwLowDateTime, 1022 FindData->nFileSizeHigh, FindData->nFileSizeLow); 1023} 1024 1025std::error_code detail::directory_iterator_construct(detail::DirIterState &IT, 1026 StringRef Path, 1027 bool FollowSymlinks) { 1028 SmallVector<wchar_t, 128> PathUTF16; 1029 1030 if (std::error_code EC = widenPath(Path, PathUTF16)) 1031 return EC; 1032 1033 // Convert path to the format that Windows is happy with. 1034 size_t PathUTF16Len = PathUTF16.size(); 1035 if (PathUTF16Len > 0 && !is_separator(PathUTF16[PathUTF16Len - 1]) && 1036 PathUTF16[PathUTF16Len - 1] != L':') { 1037 PathUTF16.push_back(L'\\'); 1038 PathUTF16.push_back(L'*'); 1039 } else { 1040 PathUTF16.push_back(L'*'); 1041 } 1042 1043 // Get the first directory entry. 1044 WIN32_FIND_DATAW FirstFind; 1045 ScopedFindHandle FindHandle(::FindFirstFileExW( 1046 c_str(PathUTF16), FindExInfoBasic, &FirstFind, FindExSearchNameMatch, 1047 NULL, FIND_FIRST_EX_LARGE_FETCH)); 1048 if (!FindHandle) 1049 return mapWindowsError(::GetLastError()); 1050 1051 size_t FilenameLen = ::wcslen(FirstFind.cFileName); 1052 while ((FilenameLen == 1 && FirstFind.cFileName[0] == L'.') || 1053 (FilenameLen == 2 && FirstFind.cFileName[0] == L'.' && 1054 FirstFind.cFileName[1] == L'.')) 1055 if (!::FindNextFileW(FindHandle, &FirstFind)) { 1056 DWORD LastError = ::GetLastError(); 1057 // Check for end. 1058 if (LastError == ERROR_NO_MORE_FILES) 1059 return detail::directory_iterator_destruct(IT); 1060 return mapWindowsError(LastError); 1061 } else { 1062 FilenameLen = ::wcslen(FirstFind.cFileName); 1063 } 1064 1065 // Construct the current directory entry. 1066 SmallString<128> DirectoryEntryNameUTF8; 1067 if (std::error_code EC = 1068 UTF16ToUTF8(FirstFind.cFileName, ::wcslen(FirstFind.cFileName), 1069 DirectoryEntryNameUTF8)) 1070 return EC; 1071 1072 IT.IterationHandle = intptr_t(FindHandle.take()); 1073 SmallString<128> DirectoryEntryPath(Path); 1074 path::append(DirectoryEntryPath, DirectoryEntryNameUTF8); 1075 IT.CurrentEntry = 1076 directory_entry(DirectoryEntryPath, FollowSymlinks, 1077 file_type_from_attrs(FirstFind.dwFileAttributes), 1078 status_from_find_data(&FirstFind)); 1079 1080 return std::error_code(); 1081} 1082 1083std::error_code detail::directory_iterator_destruct(detail::DirIterState &IT) { 1084 if (IT.IterationHandle != 0) 1085 // Closes the handle if it's valid. 1086 ScopedFindHandle close(HANDLE(IT.IterationHandle)); 1087 IT.IterationHandle = 0; 1088 IT.CurrentEntry = directory_entry(); 1089 return std::error_code(); 1090} 1091 1092std::error_code detail::directory_iterator_increment(detail::DirIterState &IT) { 1093 WIN32_FIND_DATAW FindData; 1094 if (!::FindNextFileW(HANDLE(IT.IterationHandle), &FindData)) { 1095 DWORD LastError = ::GetLastError(); 1096 // Check for end. 1097 if (LastError == ERROR_NO_MORE_FILES) 1098 return detail::directory_iterator_destruct(IT); 1099 return mapWindowsError(LastError); 1100 } 1101 1102 size_t FilenameLen = ::wcslen(FindData.cFileName); 1103 if ((FilenameLen == 1 && FindData.cFileName[0] == L'.') || 1104 (FilenameLen == 2 && FindData.cFileName[0] == L'.' && 1105 FindData.cFileName[1] == L'.')) 1106 return directory_iterator_increment(IT); 1107 1108 SmallString<128> DirectoryEntryPathUTF8; 1109 if (std::error_code EC = 1110 UTF16ToUTF8(FindData.cFileName, ::wcslen(FindData.cFileName), 1111 DirectoryEntryPathUTF8)) 1112 return EC; 1113 1114 IT.CurrentEntry.replace_filename( 1115 Twine(DirectoryEntryPathUTF8), 1116 file_type_from_attrs(FindData.dwFileAttributes), 1117 status_from_find_data(&FindData)); 1118 return std::error_code(); 1119} 1120 1121ErrorOr<basic_file_status> directory_entry::status() const { return Status; } 1122 1123static std::error_code nativeFileToFd(Expected<HANDLE> H, int &ResultFD, 1124 OpenFlags Flags) { 1125 int CrtOpenFlags = 0; 1126 if (Flags & OF_Append) 1127 CrtOpenFlags |= _O_APPEND; 1128 1129 if (Flags & OF_CRLF) { 1130 assert(Flags & OF_Text && "Flags set OF_CRLF without OF_Text"); 1131 CrtOpenFlags |= _O_TEXT; 1132 } 1133 1134 ResultFD = -1; 1135 if (!H) 1136 return errorToErrorCode(H.takeError()); 1137 1138 ResultFD = ::_open_osfhandle(intptr_t(*H), CrtOpenFlags); 1139 if (ResultFD == -1) { 1140 ::CloseHandle(*H); 1141 return mapWindowsError(ERROR_INVALID_HANDLE); 1142 } 1143 return std::error_code(); 1144} 1145 1146static DWORD nativeDisposition(CreationDisposition Disp, OpenFlags Flags) { 1147 // This is a compatibility hack. Really we should respect the creation 1148 // disposition, but a lot of old code relied on the implicit assumption that 1149 // OF_Append implied it would open an existing file. Since the disposition is 1150 // now explicit and defaults to CD_CreateAlways, this assumption would cause 1151 // any usage of OF_Append to append to a new file, even if the file already 1152 // existed. A better solution might have two new creation dispositions: 1153 // CD_AppendAlways and CD_AppendNew. This would also address the problem of 1154 // OF_Append being used on a read-only descriptor, which doesn't make sense. 1155 if (Flags & OF_Append) 1156 return OPEN_ALWAYS; 1157 1158 switch (Disp) { 1159 case CD_CreateAlways: 1160 return CREATE_ALWAYS; 1161 case CD_CreateNew: 1162 return CREATE_NEW; 1163 case CD_OpenAlways: 1164 return OPEN_ALWAYS; 1165 case CD_OpenExisting: 1166 return OPEN_EXISTING; 1167 } 1168 llvm_unreachable("unreachable!"); 1169} 1170 1171static DWORD nativeAccess(FileAccess Access, OpenFlags Flags) { 1172 DWORD Result = 0; 1173 if (Access & FA_Read) 1174 Result |= GENERIC_READ; 1175 if (Access & FA_Write) 1176 Result |= GENERIC_WRITE; 1177 if (Flags & OF_Delete) 1178 Result |= DELETE; 1179 if (Flags & OF_UpdateAtime) 1180 Result |= FILE_WRITE_ATTRIBUTES; 1181 return Result; 1182} 1183 1184static std::error_code openNativeFileInternal(const Twine &Name, 1185 file_t &ResultFile, DWORD Disp, 1186 DWORD Access, DWORD Flags, 1187 bool Inherit = false) { 1188 SmallVector<wchar_t, 128> PathUTF16; 1189 if (std::error_code EC = widenPath(Name, PathUTF16)) 1190 return EC; 1191 1192 SECURITY_ATTRIBUTES SA; 1193 SA.nLength = sizeof(SA); 1194 SA.lpSecurityDescriptor = nullptr; 1195 SA.bInheritHandle = Inherit; 1196 1197 HANDLE H = 1198 ::CreateFileW(PathUTF16.begin(), Access, 1199 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, &SA, 1200 Disp, Flags, NULL); 1201 if (H == INVALID_HANDLE_VALUE) { 1202 DWORD LastError = ::GetLastError(); 1203 std::error_code EC = mapWindowsError(LastError); 1204 // Provide a better error message when trying to open directories. 1205 // This only runs if we failed to open the file, so there is probably 1206 // no performances issues. 1207 if (LastError != ERROR_ACCESS_DENIED) 1208 return EC; 1209 if (is_directory(Name)) 1210 return make_error_code(errc::is_a_directory); 1211 return EC; 1212 } 1213 ResultFile = H; 1214 return std::error_code(); 1215} 1216 1217Expected<file_t> openNativeFile(const Twine &Name, CreationDisposition Disp, 1218 FileAccess Access, OpenFlags Flags, 1219 unsigned Mode) { 1220 // Verify that we don't have both "append" and "excl". 1221 assert((!(Disp == CD_CreateNew) || !(Flags & OF_Append)) && 1222 "Cannot specify both 'CreateNew' and 'Append' file creation flags!"); 1223 1224 DWORD NativeDisp = nativeDisposition(Disp, Flags); 1225 DWORD NativeAccess = nativeAccess(Access, Flags); 1226 1227 bool Inherit = false; 1228 if (Flags & OF_ChildInherit) 1229 Inherit = true; 1230 1231 file_t Result; 1232 std::error_code EC = openNativeFileInternal( 1233 Name, Result, NativeDisp, NativeAccess, FILE_ATTRIBUTE_NORMAL, Inherit); 1234 if (EC) 1235 return errorCodeToError(EC); 1236 1237 if (Flags & OF_UpdateAtime) { 1238 FILETIME FileTime; 1239 SYSTEMTIME SystemTime; 1240 GetSystemTime(&SystemTime); 1241 if (SystemTimeToFileTime(&SystemTime, &FileTime) == 0 || 1242 SetFileTime(Result, NULL, &FileTime, NULL) == 0) { 1243 DWORD LastError = ::GetLastError(); 1244 ::CloseHandle(Result); 1245 return errorCodeToError(mapWindowsError(LastError)); 1246 } 1247 } 1248 1249 return Result; 1250} 1251 1252std::error_code openFile(const Twine &Name, int &ResultFD, 1253 CreationDisposition Disp, FileAccess Access, 1254 OpenFlags Flags, unsigned int Mode) { 1255 Expected<file_t> Result = openNativeFile(Name, Disp, Access, Flags); 1256 if (!Result) 1257 return errorToErrorCode(Result.takeError()); 1258 1259 return nativeFileToFd(*Result, ResultFD, Flags); 1260} 1261 1262static std::error_code directoryRealPath(const Twine &Name, 1263 SmallVectorImpl<char> &RealPath) { 1264 file_t File; 1265 std::error_code EC = openNativeFileInternal( 1266 Name, File, OPEN_EXISTING, GENERIC_READ, FILE_FLAG_BACKUP_SEMANTICS); 1267 if (EC) 1268 return EC; 1269 1270 EC = realPathFromHandle(File, RealPath); 1271 ::CloseHandle(File); 1272 return EC; 1273} 1274 1275std::error_code openFileForRead(const Twine &Name, int &ResultFD, 1276 OpenFlags Flags, 1277 SmallVectorImpl<char> *RealPath) { 1278 Expected<HANDLE> NativeFile = openNativeFileForRead(Name, Flags, RealPath); 1279 return nativeFileToFd(std::move(NativeFile), ResultFD, OF_None); 1280} 1281 1282Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags, 1283 SmallVectorImpl<char> *RealPath) { 1284 Expected<file_t> Result = 1285 openNativeFile(Name, CD_OpenExisting, FA_Read, Flags); 1286 1287 // Fetch the real name of the file, if the user asked 1288 if (Result && RealPath) 1289 realPathFromHandle(*Result, *RealPath); 1290 1291 return Result; 1292} 1293 1294file_t convertFDToNativeFile(int FD) { 1295 return reinterpret_cast<HANDLE>(::_get_osfhandle(FD)); 1296} 1297 1298file_t getStdinHandle() { return ::GetStdHandle(STD_INPUT_HANDLE); } 1299file_t getStdoutHandle() { return ::GetStdHandle(STD_OUTPUT_HANDLE); } 1300file_t getStderrHandle() { return ::GetStdHandle(STD_ERROR_HANDLE); } 1301 1302Expected<size_t> readNativeFileImpl(file_t FileHandle, 1303 MutableArrayRef<char> Buf, 1304 OVERLAPPED *Overlap) { 1305 // ReadFile can only read 2GB at a time. The caller should check the number of 1306 // bytes and read in a loop until termination. 1307 DWORD BytesToRead = 1308 std::min(size_t(std::numeric_limits<DWORD>::max()), Buf.size()); 1309 DWORD BytesRead = 0; 1310 if (::ReadFile(FileHandle, Buf.data(), BytesToRead, &BytesRead, Overlap)) 1311 return BytesRead; 1312 DWORD Err = ::GetLastError(); 1313 // EOF is not an error. 1314 if (Err == ERROR_BROKEN_PIPE || Err == ERROR_HANDLE_EOF) 1315 return BytesRead; 1316 return errorCodeToError(mapWindowsError(Err)); 1317} 1318 1319Expected<size_t> readNativeFile(file_t FileHandle, MutableArrayRef<char> Buf) { 1320 return readNativeFileImpl(FileHandle, Buf, /*Overlap=*/nullptr); 1321} 1322 1323Expected<size_t> readNativeFileSlice(file_t FileHandle, 1324 MutableArrayRef<char> Buf, 1325 uint64_t Offset) { 1326 OVERLAPPED Overlapped = {}; 1327 Overlapped.Offset = uint32_t(Offset); 1328 Overlapped.OffsetHigh = uint32_t(Offset >> 32); 1329 return readNativeFileImpl(FileHandle, Buf, &Overlapped); 1330} 1331 1332std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout) { 1333 DWORD Flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY; 1334 OVERLAPPED OV = {}; 1335 file_t File = convertFDToNativeFile(FD); 1336 auto Start = std::chrono::steady_clock::now(); 1337 auto End = Start + Timeout; 1338 do { 1339 if (::LockFileEx(File, Flags, 0, MAXDWORD, MAXDWORD, &OV)) 1340 return std::error_code(); 1341 DWORD Error = ::GetLastError(); 1342 if (Error == ERROR_LOCK_VIOLATION) { 1343 ::Sleep(1); 1344 continue; 1345 } 1346 return mapWindowsError(Error); 1347 } while (std::chrono::steady_clock::now() < End); 1348 return mapWindowsError(ERROR_LOCK_VIOLATION); 1349} 1350 1351std::error_code lockFile(int FD) { 1352 DWORD Flags = LOCKFILE_EXCLUSIVE_LOCK; 1353 OVERLAPPED OV = {}; 1354 file_t File = convertFDToNativeFile(FD); 1355 if (::LockFileEx(File, Flags, 0, MAXDWORD, MAXDWORD, &OV)) 1356 return std::error_code(); 1357 DWORD Error = ::GetLastError(); 1358 return mapWindowsError(Error); 1359} 1360 1361std::error_code unlockFile(int FD) { 1362 OVERLAPPED OV = {}; 1363 file_t File = convertFDToNativeFile(FD); 1364 if (::UnlockFileEx(File, 0, MAXDWORD, MAXDWORD, &OV)) 1365 return std::error_code(); 1366 return mapWindowsError(::GetLastError()); 1367} 1368 1369std::error_code closeFile(file_t &F) { 1370 file_t TmpF = F; 1371 F = kInvalidFile; 1372 if (!::CloseHandle(TmpF)) 1373 return mapWindowsError(::GetLastError()); 1374 return std::error_code(); 1375} 1376 1377std::error_code remove_directories(const Twine &path, bool IgnoreErrors) { 1378 SmallString<128> NativePath; 1379 llvm::sys::path::native(path, NativePath, path::Style::windows_backslash); 1380 // Convert to utf-16. 1381 SmallVector<wchar_t, 128> Path16; 1382 std::error_code EC = widenPath(NativePath, Path16); 1383 if (EC && !IgnoreErrors) 1384 return EC; 1385 1386 // SHFileOperation() accepts a list of paths, and so must be double null- 1387 // terminated to indicate the end of the list. The buffer is already null 1388 // terminated, but since that null character is not considered part of the 1389 // vector's size, pushing another one will just consume that byte. So we 1390 // need to push 2 null terminators. 1391 Path16.push_back(0); 1392 Path16.push_back(0); 1393 1394 HRESULT HR; 1395 do { 1396 HR = 1397 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); 1398 if (FAILED(HR)) 1399 break; 1400 auto Uninitialize = make_scope_exit([] { CoUninitialize(); }); 1401 IFileOperation *FileOp = NULL; 1402 HR = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, 1403 IID_PPV_ARGS(&FileOp)); 1404 if (FAILED(HR)) 1405 break; 1406 auto FileOpRelease = make_scope_exit([&FileOp] { FileOp->Release(); }); 1407 HR = FileOp->SetOperationFlags(FOF_NO_UI | FOFX_NOCOPYHOOKS); 1408 if (FAILED(HR)) 1409 break; 1410 PIDLIST_ABSOLUTE PIDL = ILCreateFromPathW(Path16.data()); 1411 auto FreePIDL = make_scope_exit([&PIDL] { ILFree(PIDL); }); 1412 IShellItem *ShItem = NULL; 1413 HR = SHCreateItemFromIDList(PIDL, IID_PPV_ARGS(&ShItem)); 1414 if (FAILED(HR)) 1415 break; 1416 auto ShItemRelease = make_scope_exit([&ShItem] { ShItem->Release(); }); 1417 HR = FileOp->DeleteItem(ShItem, NULL); 1418 if (FAILED(HR)) 1419 break; 1420 HR = FileOp->PerformOperations(); 1421 } while (false); 1422 if (FAILED(HR) && !IgnoreErrors) 1423 return mapWindowsError(HRESULT_CODE(HR)); 1424 return std::error_code(); 1425} 1426 1427static void expandTildeExpr(SmallVectorImpl<char> &Path) { 1428 // Path does not begin with a tilde expression. 1429 if (Path.empty() || Path[0] != '~') 1430 return; 1431 1432 StringRef PathStr(Path.begin(), Path.size()); 1433 PathStr = PathStr.drop_front(); 1434 StringRef Expr = 1435 PathStr.take_until([](char c) { return path::is_separator(c); }); 1436 1437 if (!Expr.empty()) { 1438 // This is probably a ~username/ expression. Don't support this on Windows. 1439 return; 1440 } 1441 1442 SmallString<128> HomeDir; 1443 if (!path::home_directory(HomeDir)) { 1444 // For some reason we couldn't get the home directory. Just exit. 1445 return; 1446 } 1447 1448 // Overwrite the first character and insert the rest. 1449 Path[0] = HomeDir[0]; 1450 Path.insert(Path.begin() + 1, HomeDir.begin() + 1, HomeDir.end()); 1451} 1452 1453void expand_tilde(const Twine &path, SmallVectorImpl<char> &dest) { 1454 dest.clear(); 1455 if (path.isTriviallyEmpty()) 1456 return; 1457 1458 path.toVector(dest); 1459 expandTildeExpr(dest); 1460 1461 return; 1462} 1463 1464std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest, 1465 bool expand_tilde) { 1466 dest.clear(); 1467 if (path.isTriviallyEmpty()) 1468 return std::error_code(); 1469 1470 if (expand_tilde) { 1471 SmallString<128> Storage; 1472 path.toVector(Storage); 1473 expandTildeExpr(Storage); 1474 return real_path(Storage, dest, false); 1475 } 1476 1477 if (is_directory(path)) 1478 return directoryRealPath(path, dest); 1479 1480 int fd; 1481 if (std::error_code EC = 1482 llvm::sys::fs::openFileForRead(path, fd, OF_None, &dest)) 1483 return EC; 1484 ::close(fd); 1485 return std::error_code(); 1486} 1487 1488} // end namespace fs 1489 1490namespace path { 1491static bool getKnownFolderPath(KNOWNFOLDERID folderId, 1492 SmallVectorImpl<char> &result) { 1493 wchar_t *path = nullptr; 1494 if (::SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, nullptr, &path) != S_OK) 1495 return false; 1496 1497 bool ok = !UTF16ToUTF8(path, ::wcslen(path), result); 1498 ::CoTaskMemFree(path); 1499 if (ok) 1500 llvm::sys::path::make_preferred(result); 1501 return ok; 1502} 1503 1504bool home_directory(SmallVectorImpl<char> &result) { 1505 return getKnownFolderPath(FOLDERID_Profile, result); 1506} 1507 1508bool user_config_directory(SmallVectorImpl<char> &result) { 1509 // Either local or roaming appdata may be suitable in some cases, depending 1510 // on the data. Local is more conservative, Roaming may not always be correct. 1511 return getKnownFolderPath(FOLDERID_LocalAppData, result); 1512} 1513 1514bool cache_directory(SmallVectorImpl<char> &result) { 1515 return getKnownFolderPath(FOLDERID_LocalAppData, result); 1516} 1517 1518static bool getTempDirEnvVar(const wchar_t *Var, SmallVectorImpl<char> &Res) { 1519 SmallVector<wchar_t, 1024> Buf; 1520 size_t Size = 1024; 1521 do { 1522 Buf.resize_for_overwrite(Size); 1523 Size = GetEnvironmentVariableW(Var, Buf.data(), Buf.size()); 1524 if (Size == 0) 1525 return false; 1526 1527 // Try again with larger buffer. 1528 } while (Size > Buf.size()); 1529 Buf.truncate(Size); 1530 1531 return !windows::UTF16ToUTF8(Buf.data(), Size, Res); 1532} 1533 1534static bool getTempDirEnvVar(SmallVectorImpl<char> &Res) { 1535 const wchar_t *EnvironmentVariables[] = {L"TMP", L"TEMP", L"USERPROFILE"}; 1536 for (auto *Env : EnvironmentVariables) { 1537 if (getTempDirEnvVar(Env, Res)) 1538 return true; 1539 } 1540 return false; 1541} 1542 1543void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) { 1544 (void)ErasedOnReboot; 1545 Result.clear(); 1546 1547 // Check whether the temporary directory is specified by an environment var. 1548 // This matches GetTempPath logic to some degree. GetTempPath is not used 1549 // directly as it cannot handle evn var longer than 130 chars on Windows 7 1550 // (fixed on Windows 8). 1551 if (getTempDirEnvVar(Result)) { 1552 assert(!Result.empty() && "Unexpected empty path"); 1553 native(Result); // Some Unix-like shells use Unix path separator in $TMP. 1554 fs::make_absolute(Result); // Make it absolute if not already. 1555 return; 1556 } 1557 1558 // Fall back to a system default. 1559 const char *DefaultResult = "C:\\Temp"; 1560 Result.append(DefaultResult, DefaultResult + strlen(DefaultResult)); 1561 llvm::sys::path::make_preferred(Result); 1562} 1563} // end namespace path 1564 1565namespace windows { 1566std::error_code CodePageToUTF16(unsigned codepage, llvm::StringRef original, 1567 llvm::SmallVectorImpl<wchar_t> &utf16) { 1568 if (!original.empty()) { 1569 int len = 1570 ::MultiByteToWideChar(codepage, MB_ERR_INVALID_CHARS, original.begin(), 1571 original.size(), utf16.begin(), 0); 1572 1573 if (len == 0) { 1574 return mapWindowsError(::GetLastError()); 1575 } 1576 1577 utf16.reserve(len + 1); 1578 utf16.resize_for_overwrite(len); 1579 1580 len = 1581 ::MultiByteToWideChar(codepage, MB_ERR_INVALID_CHARS, original.begin(), 1582 original.size(), utf16.begin(), utf16.size()); 1583 1584 if (len == 0) { 1585 return mapWindowsError(::GetLastError()); 1586 } 1587 } 1588 1589 // Make utf16 null terminated. 1590 utf16.push_back(0); 1591 utf16.pop_back(); 1592 1593 return std::error_code(); 1594} 1595 1596std::error_code UTF8ToUTF16(llvm::StringRef utf8, 1597 llvm::SmallVectorImpl<wchar_t> &utf16) { 1598 return CodePageToUTF16(CP_UTF8, utf8, utf16); 1599} 1600 1601std::error_code CurCPToUTF16(llvm::StringRef curcp, 1602 llvm::SmallVectorImpl<wchar_t> &utf16) { 1603 return CodePageToUTF16(CP_ACP, curcp, utf16); 1604} 1605 1606static std::error_code UTF16ToCodePage(unsigned codepage, const wchar_t *utf16, 1607 size_t utf16_len, 1608 llvm::SmallVectorImpl<char> &converted) { 1609 if (utf16_len) { 1610 // Get length. 1611 int len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, 1612 converted.begin(), 0, NULL, NULL); 1613 1614 if (len == 0) { 1615 return mapWindowsError(::GetLastError()); 1616 } 1617 1618 converted.reserve(len + 1); 1619 converted.resize_for_overwrite(len); 1620 1621 // Now do the actual conversion. 1622 len = ::WideCharToMultiByte(codepage, 0, utf16, utf16_len, converted.data(), 1623 converted.size(), NULL, NULL); 1624 1625 if (len == 0) { 1626 return mapWindowsError(::GetLastError()); 1627 } 1628 } 1629 1630 // Make the new string null terminated. 1631 converted.push_back(0); 1632 converted.pop_back(); 1633 1634 return std::error_code(); 1635} 1636 1637std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, 1638 llvm::SmallVectorImpl<char> &utf8) { 1639 return UTF16ToCodePage(CP_UTF8, utf16, utf16_len, utf8); 1640} 1641 1642std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len, 1643 llvm::SmallVectorImpl<char> &curcp) { 1644 return UTF16ToCodePage(CP_ACP, utf16, utf16_len, curcp); 1645} 1646 1647} // end namespace windows 1648} // end namespace sys 1649} // end namespace llvm 1650