1 //===-- Path.cpp - Implement OS Path Concept ------------------------------===// 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 operating system Path API. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Support/Path.h" 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/Config/config.h" 16 #include "llvm/Config/llvm-config.h" 17 #include "llvm/Support/Endian.h" 18 #include "llvm/Support/Errc.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/FileSystem.h" 21 #include "llvm/Support/Process.h" 22 #include "llvm/Support/Signals.h" 23 #include <cctype> 24 #include <cstring> 25 26 #if !defined(_MSC_VER) && !defined(__MINGW32__) 27 #include <unistd.h> 28 #else 29 #include <io.h> 30 #endif 31 32 using namespace llvm; 33 using namespace llvm::support::endian; 34 35 namespace { 36 using llvm::StringRef; 37 using llvm::sys::path::is_separator; 38 using llvm::sys::path::Style; 39 40 inline Style real_style(Style style) { 41 if (style != Style::native) 42 return style; 43 if (is_style_posix(style)) 44 return Style::posix; 45 return LLVM_WINDOWS_PREFER_FORWARD_SLASH ? Style::windows_slash 46 : Style::windows_backslash; 47 } 48 49 inline const char *separators(Style style) { 50 if (is_style_windows(style)) 51 return "\\/"; 52 return "/"; 53 } 54 55 inline char preferred_separator(Style style) { 56 if (real_style(style) == Style::windows) 57 return '\\'; 58 return '/'; 59 } 60 61 StringRef find_first_component(StringRef path, Style style) { 62 // Look for this first component in the following order. 63 // * empty (in this case we return an empty string) 64 // * either C: or {//,\\}net. 65 // * {/,\} 66 // * {file,directory}name 67 68 if (path.empty()) 69 return path; 70 71 if (is_style_windows(style)) { 72 // C: 73 if (path.size() >= 2 && 74 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':') 75 return path.substr(0, 2); 76 } 77 78 // //net 79 if ((path.size() > 2) && is_separator(path[0], style) && 80 path[0] == path[1] && !is_separator(path[2], style)) { 81 // Find the next directory separator. 82 size_t end = path.find_first_of(separators(style), 2); 83 return path.substr(0, end); 84 } 85 86 // {/,\} 87 if (is_separator(path[0], style)) 88 return path.substr(0, 1); 89 90 // * {file,directory}name 91 size_t end = path.find_first_of(separators(style)); 92 return path.substr(0, end); 93 } 94 95 // Returns the first character of the filename in str. For paths ending in 96 // '/', it returns the position of the '/'. 97 size_t filename_pos(StringRef str, Style style) { 98 if (str.size() > 0 && is_separator(str[str.size() - 1], style)) 99 return str.size() - 1; 100 101 size_t pos = str.find_last_of(separators(style), str.size() - 1); 102 103 if (is_style_windows(style)) { 104 if (pos == StringRef::npos) 105 pos = str.find_last_of(':', str.size() - 2); 106 } 107 108 if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style))) 109 return 0; 110 111 return pos + 1; 112 } 113 114 // Returns the position of the root directory in str. If there is no root 115 // directory in str, it returns StringRef::npos. 116 size_t root_dir_start(StringRef str, Style style) { 117 // case "c:/" 118 if (is_style_windows(style)) { 119 if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style)) 120 return 2; 121 } 122 123 // case "//net" 124 if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] && 125 !is_separator(str[2], style)) { 126 return str.find_first_of(separators(style), 2); 127 } 128 129 // case "/" 130 if (str.size() > 0 && is_separator(str[0], style)) 131 return 0; 132 133 return StringRef::npos; 134 } 135 136 // Returns the position past the end of the "parent path" of path. The parent 137 // path will not end in '/', unless the parent is the root directory. If the 138 // path has no parent, 0 is returned. 139 size_t parent_path_end(StringRef path, Style style) { 140 size_t end_pos = filename_pos(path, style); 141 142 bool filename_was_sep = 143 path.size() > 0 && is_separator(path[end_pos], style); 144 145 // Skip separators until we reach root dir (or the start of the string). 146 size_t root_dir_pos = root_dir_start(path, style); 147 while (end_pos > 0 && 148 (root_dir_pos == StringRef::npos || end_pos > root_dir_pos) && 149 is_separator(path[end_pos - 1], style)) 150 --end_pos; 151 152 if (end_pos == root_dir_pos && !filename_was_sep) { 153 // We've reached the root dir and the input path was *not* ending in a 154 // sequence of slashes. Include the root dir in the parent path. 155 return root_dir_pos + 1; 156 } 157 158 // Otherwise, just include before the last slash. 159 return end_pos; 160 } 161 } // end unnamed namespace 162 163 enum FSEntity { 164 FS_Dir, 165 FS_File, 166 FS_Name 167 }; 168 169 static std::error_code 170 createUniqueEntity(const Twine &Model, int &ResultFD, 171 SmallVectorImpl<char> &ResultPath, bool MakeAbsolute, 172 FSEntity Type, sys::fs::OpenFlags Flags = sys::fs::OF_None, 173 unsigned Mode = 0) { 174 175 // Limit the number of attempts we make, so that we don't infinite loop. E.g. 176 // "permission denied" could be for a specific file (so we retry with a 177 // different name) or for the whole directory (retry would always fail). 178 // Checking which is racy, so we try a number of times, then give up. 179 std::error_code EC; 180 for (int Retries = 128; Retries > 0; --Retries) { 181 sys::fs::createUniquePath(Model, ResultPath, MakeAbsolute); 182 // Try to open + create the file. 183 switch (Type) { 184 case FS_File: { 185 EC = sys::fs::openFileForReadWrite(Twine(ResultPath.begin()), ResultFD, 186 sys::fs::CD_CreateNew, Flags, Mode); 187 if (EC) { 188 // errc::permission_denied happens on Windows when we try to open a file 189 // that has been marked for deletion. 190 if (EC == errc::file_exists || EC == errc::permission_denied) 191 continue; 192 return EC; 193 } 194 195 return std::error_code(); 196 } 197 198 case FS_Name: { 199 EC = sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist); 200 if (EC == errc::no_such_file_or_directory) 201 return std::error_code(); 202 if (EC) 203 return EC; 204 continue; 205 } 206 207 case FS_Dir: { 208 EC = sys::fs::create_directory(ResultPath.begin(), false); 209 if (EC) { 210 if (EC == errc::file_exists) 211 continue; 212 return EC; 213 } 214 return std::error_code(); 215 } 216 } 217 llvm_unreachable("Invalid Type"); 218 } 219 return EC; 220 } 221 222 namespace llvm { 223 namespace sys { 224 namespace path { 225 226 const_iterator begin(StringRef path, Style style) { 227 const_iterator i; 228 i.Path = path; 229 i.Component = find_first_component(path, style); 230 i.Position = 0; 231 i.S = style; 232 return i; 233 } 234 235 const_iterator end(StringRef path) { 236 const_iterator i; 237 i.Path = path; 238 i.Position = path.size(); 239 return i; 240 } 241 242 const_iterator &const_iterator::operator++() { 243 assert(Position < Path.size() && "Tried to increment past end!"); 244 245 // Increment Position to past the current component 246 Position += Component.size(); 247 248 // Check for end. 249 if (Position == Path.size()) { 250 Component = StringRef(); 251 return *this; 252 } 253 254 // Both POSIX and Windows treat paths that begin with exactly two separators 255 // specially. 256 bool was_net = Component.size() > 2 && is_separator(Component[0], S) && 257 Component[1] == Component[0] && !is_separator(Component[2], S); 258 259 // Handle separators. 260 if (is_separator(Path[Position], S)) { 261 // Root dir. 262 if (was_net || 263 // c:/ 264 (is_style_windows(S) && Component.endswith(":"))) { 265 Component = Path.substr(Position, 1); 266 return *this; 267 } 268 269 // Skip extra separators. 270 while (Position != Path.size() && is_separator(Path[Position], S)) { 271 ++Position; 272 } 273 274 // Treat trailing '/' as a '.', unless it is the root dir. 275 if (Position == Path.size() && Component != "/") { 276 --Position; 277 Component = "."; 278 return *this; 279 } 280 } 281 282 // Find next component. 283 size_t end_pos = Path.find_first_of(separators(S), Position); 284 Component = Path.slice(Position, end_pos); 285 286 return *this; 287 } 288 289 bool const_iterator::operator==(const const_iterator &RHS) const { 290 return Path.begin() == RHS.Path.begin() && Position == RHS.Position; 291 } 292 293 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { 294 return Position - RHS.Position; 295 } 296 297 reverse_iterator rbegin(StringRef Path, Style style) { 298 reverse_iterator I; 299 I.Path = Path; 300 I.Position = Path.size(); 301 I.S = style; 302 ++I; 303 return I; 304 } 305 306 reverse_iterator rend(StringRef Path) { 307 reverse_iterator I; 308 I.Path = Path; 309 I.Component = Path.substr(0, 0); 310 I.Position = 0; 311 return I; 312 } 313 314 reverse_iterator &reverse_iterator::operator++() { 315 size_t root_dir_pos = root_dir_start(Path, S); 316 317 // Skip separators unless it's the root directory. 318 size_t end_pos = Position; 319 while (end_pos > 0 && (end_pos - 1) != root_dir_pos && 320 is_separator(Path[end_pos - 1], S)) 321 --end_pos; 322 323 // Treat trailing '/' as a '.', unless it is the root dir. 324 if (Position == Path.size() && !Path.empty() && 325 is_separator(Path.back(), S) && 326 (root_dir_pos == StringRef::npos || end_pos - 1 > root_dir_pos)) { 327 --Position; 328 Component = "."; 329 return *this; 330 } 331 332 // Find next separator. 333 size_t start_pos = filename_pos(Path.substr(0, end_pos), S); 334 Component = Path.slice(start_pos, end_pos); 335 Position = start_pos; 336 return *this; 337 } 338 339 bool reverse_iterator::operator==(const reverse_iterator &RHS) const { 340 return Path.begin() == RHS.Path.begin() && Component == RHS.Component && 341 Position == RHS.Position; 342 } 343 344 ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const { 345 return Position - RHS.Position; 346 } 347 348 StringRef root_path(StringRef path, Style style) { 349 const_iterator b = begin(path, style), pos = b, e = end(path); 350 if (b != e) { 351 bool has_net = 352 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0]; 353 bool has_drive = is_style_windows(style) && b->endswith(":"); 354 355 if (has_net || has_drive) { 356 if ((++pos != e) && is_separator((*pos)[0], style)) { 357 // {C:/,//net/}, so get the first two components. 358 return path.substr(0, b->size() + pos->size()); 359 } 360 // just {C:,//net}, return the first component. 361 return *b; 362 } 363 364 // POSIX style root directory. 365 if (is_separator((*b)[0], style)) { 366 return *b; 367 } 368 } 369 370 return StringRef(); 371 } 372 373 StringRef root_name(StringRef path, Style style) { 374 const_iterator b = begin(path, style), e = end(path); 375 if (b != e) { 376 bool has_net = 377 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0]; 378 bool has_drive = is_style_windows(style) && b->endswith(":"); 379 380 if (has_net || has_drive) { 381 // just {C:,//net}, return the first component. 382 return *b; 383 } 384 } 385 386 // No path or no name. 387 return StringRef(); 388 } 389 390 StringRef root_directory(StringRef path, Style style) { 391 const_iterator b = begin(path, style), pos = b, e = end(path); 392 if (b != e) { 393 bool has_net = 394 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0]; 395 bool has_drive = is_style_windows(style) && b->endswith(":"); 396 397 if ((has_net || has_drive) && 398 // {C:,//net}, skip to the next component. 399 (++pos != e) && is_separator((*pos)[0], style)) { 400 return *pos; 401 } 402 403 // POSIX style root directory. 404 if (!has_net && is_separator((*b)[0], style)) { 405 return *b; 406 } 407 } 408 409 // No path or no root. 410 return StringRef(); 411 } 412 413 StringRef relative_path(StringRef path, Style style) { 414 StringRef root = root_path(path, style); 415 return path.substr(root.size()); 416 } 417 418 void append(SmallVectorImpl<char> &path, Style style, const Twine &a, 419 const Twine &b, const Twine &c, const Twine &d) { 420 SmallString<32> a_storage; 421 SmallString<32> b_storage; 422 SmallString<32> c_storage; 423 SmallString<32> d_storage; 424 425 SmallVector<StringRef, 4> components; 426 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage)); 427 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage)); 428 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage)); 429 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage)); 430 431 for (auto &component : components) { 432 bool path_has_sep = 433 !path.empty() && is_separator(path[path.size() - 1], style); 434 if (path_has_sep) { 435 // Strip separators from beginning of component. 436 size_t loc = component.find_first_not_of(separators(style)); 437 StringRef c = component.substr(loc); 438 439 // Append it. 440 path.append(c.begin(), c.end()); 441 continue; 442 } 443 444 bool component_has_sep = 445 !component.empty() && is_separator(component[0], style); 446 if (!component_has_sep && 447 !(path.empty() || has_root_name(component, style))) { 448 // Add a separator. 449 path.push_back(preferred_separator(style)); 450 } 451 452 path.append(component.begin(), component.end()); 453 } 454 } 455 456 void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b, 457 const Twine &c, const Twine &d) { 458 append(path, Style::native, a, b, c, d); 459 } 460 461 void append(SmallVectorImpl<char> &path, const_iterator begin, 462 const_iterator end, Style style) { 463 for (; begin != end; ++begin) 464 path::append(path, style, *begin); 465 } 466 467 StringRef parent_path(StringRef path, Style style) { 468 size_t end_pos = parent_path_end(path, style); 469 if (end_pos == StringRef::npos) 470 return StringRef(); 471 return path.substr(0, end_pos); 472 } 473 474 void remove_filename(SmallVectorImpl<char> &path, Style style) { 475 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style); 476 if (end_pos != StringRef::npos) 477 path.set_size(end_pos); 478 } 479 480 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension, 481 Style style) { 482 StringRef p(path.begin(), path.size()); 483 SmallString<32> ext_storage; 484 StringRef ext = extension.toStringRef(ext_storage); 485 486 // Erase existing extension. 487 size_t pos = p.find_last_of('.'); 488 if (pos != StringRef::npos && pos >= filename_pos(p, style)) 489 path.set_size(pos); 490 491 // Append '.' if needed. 492 if (ext.size() > 0 && ext[0] != '.') 493 path.push_back('.'); 494 495 // Append extension. 496 path.append(ext.begin(), ext.end()); 497 } 498 499 static bool starts_with(StringRef Path, StringRef Prefix, 500 Style style = Style::native) { 501 // Windows prefix matching : case and separator insensitive 502 if (is_style_windows(style)) { 503 if (Path.size() < Prefix.size()) 504 return false; 505 for (size_t I = 0, E = Prefix.size(); I != E; ++I) { 506 bool SepPath = is_separator(Path[I], style); 507 bool SepPrefix = is_separator(Prefix[I], style); 508 if (SepPath != SepPrefix) 509 return false; 510 if (!SepPath && toLower(Path[I]) != toLower(Prefix[I])) 511 return false; 512 } 513 return true; 514 } 515 return Path.startswith(Prefix); 516 } 517 518 bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix, 519 StringRef NewPrefix, Style style) { 520 if (OldPrefix.empty() && NewPrefix.empty()) 521 return false; 522 523 StringRef OrigPath(Path.begin(), Path.size()); 524 if (!starts_with(OrigPath, OldPrefix, style)) 525 return false; 526 527 // If prefixes have the same size we can simply copy the new one over. 528 if (OldPrefix.size() == NewPrefix.size()) { 529 llvm::copy(NewPrefix, Path.begin()); 530 return true; 531 } 532 533 StringRef RelPath = OrigPath.substr(OldPrefix.size()); 534 SmallString<256> NewPath; 535 (Twine(NewPrefix) + RelPath).toVector(NewPath); 536 Path.swap(NewPath); 537 return true; 538 } 539 540 void native(const Twine &path, SmallVectorImpl<char> &result, Style style) { 541 assert((!path.isSingleStringRef() || 542 path.getSingleStringRef().data() != result.data()) && 543 "path and result are not allowed to overlap!"); 544 // Clear result. 545 result.clear(); 546 path.toVector(result); 547 native(result, style); 548 } 549 550 void native(SmallVectorImpl<char> &Path, Style style) { 551 if (Path.empty()) 552 return; 553 if (is_style_windows(style)) { 554 for (char &Ch : Path) 555 if (is_separator(Ch, style)) 556 Ch = preferred_separator(style); 557 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) { 558 SmallString<128> PathHome; 559 home_directory(PathHome); 560 PathHome.append(Path.begin() + 1, Path.end()); 561 Path = PathHome; 562 } 563 } else { 564 std::replace(Path.begin(), Path.end(), '\\', '/'); 565 } 566 } 567 568 std::string convert_to_slash(StringRef path, Style style) { 569 if (is_style_posix(style)) 570 return std::string(path); 571 572 std::string s = path.str(); 573 std::replace(s.begin(), s.end(), '\\', '/'); 574 return s; 575 } 576 577 StringRef filename(StringRef path, Style style) { return *rbegin(path, style); } 578 579 StringRef stem(StringRef path, Style style) { 580 StringRef fname = filename(path, style); 581 size_t pos = fname.find_last_of('.'); 582 if (pos == StringRef::npos) 583 return fname; 584 if ((fname.size() == 1 && fname == ".") || 585 (fname.size() == 2 && fname == "..")) 586 return fname; 587 return fname.substr(0, pos); 588 } 589 590 StringRef extension(StringRef path, Style style) { 591 StringRef fname = filename(path, style); 592 size_t pos = fname.find_last_of('.'); 593 if (pos == StringRef::npos) 594 return StringRef(); 595 if ((fname.size() == 1 && fname == ".") || 596 (fname.size() == 2 && fname == "..")) 597 return StringRef(); 598 return fname.substr(pos); 599 } 600 601 bool is_separator(char value, Style style) { 602 if (value == '/') 603 return true; 604 if (is_style_windows(style)) 605 return value == '\\'; 606 return false; 607 } 608 609 StringRef get_separator(Style style) { 610 if (real_style(style) == Style::windows) 611 return "\\"; 612 return "/"; 613 } 614 615 bool has_root_name(const Twine &path, Style style) { 616 SmallString<128> path_storage; 617 StringRef p = path.toStringRef(path_storage); 618 619 return !root_name(p, style).empty(); 620 } 621 622 bool has_root_directory(const Twine &path, Style style) { 623 SmallString<128> path_storage; 624 StringRef p = path.toStringRef(path_storage); 625 626 return !root_directory(p, style).empty(); 627 } 628 629 bool has_root_path(const Twine &path, Style style) { 630 SmallString<128> path_storage; 631 StringRef p = path.toStringRef(path_storage); 632 633 return !root_path(p, style).empty(); 634 } 635 636 bool has_relative_path(const Twine &path, Style style) { 637 SmallString<128> path_storage; 638 StringRef p = path.toStringRef(path_storage); 639 640 return !relative_path(p, style).empty(); 641 } 642 643 bool has_filename(const Twine &path, Style style) { 644 SmallString<128> path_storage; 645 StringRef p = path.toStringRef(path_storage); 646 647 return !filename(p, style).empty(); 648 } 649 650 bool has_parent_path(const Twine &path, Style style) { 651 SmallString<128> path_storage; 652 StringRef p = path.toStringRef(path_storage); 653 654 return !parent_path(p, style).empty(); 655 } 656 657 bool has_stem(const Twine &path, Style style) { 658 SmallString<128> path_storage; 659 StringRef p = path.toStringRef(path_storage); 660 661 return !stem(p, style).empty(); 662 } 663 664 bool has_extension(const Twine &path, Style style) { 665 SmallString<128> path_storage; 666 StringRef p = path.toStringRef(path_storage); 667 668 return !extension(p, style).empty(); 669 } 670 671 bool is_absolute(const Twine &path, Style style) { 672 SmallString<128> path_storage; 673 StringRef p = path.toStringRef(path_storage); 674 675 bool rootDir = has_root_directory(p, style); 676 bool rootName = is_style_posix(style) || has_root_name(p, style); 677 678 return rootDir && rootName; 679 } 680 681 bool is_absolute_gnu(const Twine &path, Style style) { 682 SmallString<128> path_storage; 683 StringRef p = path.toStringRef(path_storage); 684 685 // Handle '/' which is absolute for both Windows and POSIX systems. 686 // Handle '\\' on Windows. 687 if (!p.empty() && is_separator(p.front(), style)) 688 return true; 689 690 if (is_style_windows(style)) { 691 // Handle drive letter pattern (a character followed by ':') on Windows. 692 if (p.size() >= 2 && (p[0] && p[1] == ':')) 693 return true; 694 } 695 696 return false; 697 } 698 699 bool is_relative(const Twine &path, Style style) { 700 return !is_absolute(path, style); 701 } 702 703 StringRef remove_leading_dotslash(StringRef Path, Style style) { 704 // Remove leading "./" (or ".//" or "././" etc.) 705 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) { 706 Path = Path.substr(2); 707 while (Path.size() > 0 && is_separator(Path[0], style)) 708 Path = Path.substr(1); 709 } 710 return Path; 711 } 712 713 // Remove path traversal components ("." and "..") when possible, and 714 // canonicalize slashes. 715 bool remove_dots(SmallVectorImpl<char> &the_path, bool remove_dot_dot, 716 Style style) { 717 style = real_style(style); 718 StringRef remaining(the_path.data(), the_path.size()); 719 bool needs_change = false; 720 SmallVector<StringRef, 16> components; 721 722 // Consume the root path, if present. 723 StringRef root = path::root_path(remaining, style); 724 bool absolute = !root.empty(); 725 if (absolute) 726 remaining = remaining.drop_front(root.size()); 727 728 // Loop over path components manually. This makes it easier to detect 729 // non-preferred slashes and double separators that must be canonicalized. 730 while (!remaining.empty()) { 731 size_t next_slash = remaining.find_first_of(separators(style)); 732 if (next_slash == StringRef::npos) 733 next_slash = remaining.size(); 734 StringRef component = remaining.take_front(next_slash); 735 remaining = remaining.drop_front(next_slash); 736 737 // Eat the slash, and check if it is the preferred separator. 738 if (!remaining.empty()) { 739 needs_change |= remaining.front() != preferred_separator(style); 740 remaining = remaining.drop_front(); 741 // The path needs to be rewritten if it has a trailing slash. 742 // FIXME: This is emergent behavior that could be removed. 743 needs_change |= remaining.empty(); 744 } 745 746 // Check for path traversal components or double separators. 747 if (component.empty() || component == ".") { 748 needs_change = true; 749 } else if (remove_dot_dot && component == "..") { 750 needs_change = true; 751 // Do not allow ".." to remove the root component. If this is the 752 // beginning of a relative path, keep the ".." component. 753 if (!components.empty() && components.back() != "..") { 754 components.pop_back(); 755 } else if (!absolute) { 756 components.push_back(component); 757 } 758 } else { 759 components.push_back(component); 760 } 761 } 762 763 // Avoid rewriting the path unless we have to. 764 if (!needs_change) 765 return false; 766 767 SmallString<256> buffer = root; 768 if (!components.empty()) { 769 buffer += components[0]; 770 for (StringRef C : makeArrayRef(components).drop_front()) { 771 buffer += preferred_separator(style); 772 buffer += C; 773 } 774 } 775 the_path.swap(buffer); 776 return true; 777 } 778 779 } // end namespace path 780 781 namespace fs { 782 783 std::error_code getUniqueID(const Twine Path, UniqueID &Result) { 784 file_status Status; 785 std::error_code EC = status(Path, Status); 786 if (EC) 787 return EC; 788 Result = Status.getUniqueID(); 789 return std::error_code(); 790 } 791 792 void createUniquePath(const Twine &Model, SmallVectorImpl<char> &ResultPath, 793 bool MakeAbsolute) { 794 SmallString<128> ModelStorage; 795 Model.toVector(ModelStorage); 796 797 if (MakeAbsolute) { 798 // Make model absolute by prepending a temp directory if it's not already. 799 if (!sys::path::is_absolute(Twine(ModelStorage))) { 800 SmallString<128> TDir; 801 sys::path::system_temp_directory(true, TDir); 802 sys::path::append(TDir, Twine(ModelStorage)); 803 ModelStorage.swap(TDir); 804 } 805 } 806 807 ResultPath = ModelStorage; 808 ResultPath.push_back(0); 809 ResultPath.pop_back(); 810 811 // Replace '%' with random chars. 812 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) { 813 if (ModelStorage[i] == '%') 814 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15]; 815 } 816 } 817 818 std::error_code createUniqueFile(const Twine &Model, int &ResultFd, 819 SmallVectorImpl<char> &ResultPath, 820 OpenFlags Flags, unsigned Mode) { 821 return createUniqueEntity(Model, ResultFd, ResultPath, false, FS_File, Flags, 822 Mode); 823 } 824 825 std::error_code createUniqueFile(const Twine &Model, 826 SmallVectorImpl<char> &ResultPath, 827 unsigned Mode) { 828 int FD; 829 auto EC = createUniqueFile(Model, FD, ResultPath, OF_None, Mode); 830 if (EC) 831 return EC; 832 // FD is only needed to avoid race conditions. Close it right away. 833 close(FD); 834 return EC; 835 } 836 837 static std::error_code 838 createTemporaryFile(const Twine &Model, int &ResultFD, 839 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type, 840 sys::fs::OpenFlags Flags = sys::fs::OF_None) { 841 SmallString<128> Storage; 842 StringRef P = Model.toNullTerminatedStringRef(Storage); 843 assert(P.find_first_of(separators(Style::native)) == StringRef::npos && 844 "Model must be a simple filename."); 845 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage. 846 return createUniqueEntity(P.begin(), ResultFD, ResultPath, true, Type, Flags, 847 owner_read | owner_write); 848 } 849 850 static std::error_code 851 createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, 852 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type, 853 sys::fs::OpenFlags Flags = sys::fs::OF_None) { 854 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%."; 855 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath, 856 Type, Flags); 857 } 858 859 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, 860 int &ResultFD, 861 SmallVectorImpl<char> &ResultPath, 862 sys::fs::OpenFlags Flags) { 863 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File, 864 Flags); 865 } 866 867 std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, 868 SmallVectorImpl<char> &ResultPath, 869 sys::fs::OpenFlags Flags) { 870 int FD; 871 auto EC = createTemporaryFile(Prefix, Suffix, FD, ResultPath, Flags); 872 if (EC) 873 return EC; 874 // FD is only needed to avoid race conditions. Close it right away. 875 close(FD); 876 return EC; 877 } 878 879 // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly 880 // for consistency. We should try using mkdtemp. 881 std::error_code createUniqueDirectory(const Twine &Prefix, 882 SmallVectorImpl<char> &ResultPath) { 883 int Dummy; 884 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true, 885 FS_Dir); 886 } 887 888 std::error_code 889 getPotentiallyUniqueFileName(const Twine &Model, 890 SmallVectorImpl<char> &ResultPath) { 891 int Dummy; 892 return createUniqueEntity(Model, Dummy, ResultPath, false, FS_Name); 893 } 894 895 std::error_code 896 getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix, 897 SmallVectorImpl<char> &ResultPath) { 898 int Dummy; 899 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name); 900 } 901 902 void make_absolute(const Twine ¤t_directory, 903 SmallVectorImpl<char> &path) { 904 StringRef p(path.data(), path.size()); 905 906 bool rootDirectory = path::has_root_directory(p); 907 bool rootName = path::has_root_name(p); 908 909 // Already absolute. 910 if ((rootName || is_style_posix(Style::native)) && rootDirectory) 911 return; 912 913 // All of the following conditions will need the current directory. 914 SmallString<128> current_dir; 915 current_directory.toVector(current_dir); 916 917 // Relative path. Prepend the current directory. 918 if (!rootName && !rootDirectory) { 919 // Append path to the current directory. 920 path::append(current_dir, p); 921 // Set path to the result. 922 path.swap(current_dir); 923 return; 924 } 925 926 if (!rootName && rootDirectory) { 927 StringRef cdrn = path::root_name(current_dir); 928 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end()); 929 path::append(curDirRootName, p); 930 // Set path to the result. 931 path.swap(curDirRootName); 932 return; 933 } 934 935 if (rootName && !rootDirectory) { 936 StringRef pRootName = path::root_name(p); 937 StringRef bRootDirectory = path::root_directory(current_dir); 938 StringRef bRelativePath = path::relative_path(current_dir); 939 StringRef pRelativePath = path::relative_path(p); 940 941 SmallString<128> res; 942 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath); 943 path.swap(res); 944 return; 945 } 946 947 llvm_unreachable("All rootName and rootDirectory combinations should have " 948 "occurred above!"); 949 } 950 951 std::error_code make_absolute(SmallVectorImpl<char> &path) { 952 if (path::is_absolute(path)) 953 return {}; 954 955 SmallString<128> current_dir; 956 if (std::error_code ec = current_path(current_dir)) 957 return ec; 958 959 make_absolute(current_dir, path); 960 return {}; 961 } 962 963 std::error_code create_directories(const Twine &Path, bool IgnoreExisting, 964 perms Perms) { 965 SmallString<128> PathStorage; 966 StringRef P = Path.toStringRef(PathStorage); 967 968 // Be optimistic and try to create the directory 969 std::error_code EC = create_directory(P, IgnoreExisting, Perms); 970 // If we succeeded, or had any error other than the parent not existing, just 971 // return it. 972 if (EC != errc::no_such_file_or_directory) 973 return EC; 974 975 // We failed because of a no_such_file_or_directory, try to create the 976 // parent. 977 StringRef Parent = path::parent_path(P); 978 if (Parent.empty()) 979 return EC; 980 981 if ((EC = create_directories(Parent, IgnoreExisting, Perms))) 982 return EC; 983 984 return create_directory(P, IgnoreExisting, Perms); 985 } 986 987 static std::error_code copy_file_internal(int ReadFD, int WriteFD) { 988 const size_t BufSize = 4096; 989 char *Buf = new char[BufSize]; 990 int BytesRead = 0, BytesWritten = 0; 991 for (;;) { 992 BytesRead = read(ReadFD, Buf, BufSize); 993 if (BytesRead <= 0) 994 break; 995 while (BytesRead) { 996 BytesWritten = write(WriteFD, Buf, BytesRead); 997 if (BytesWritten < 0) 998 break; 999 BytesRead -= BytesWritten; 1000 } 1001 if (BytesWritten < 0) 1002 break; 1003 } 1004 delete[] Buf; 1005 1006 if (BytesRead < 0 || BytesWritten < 0) 1007 return std::error_code(errno, std::generic_category()); 1008 return std::error_code(); 1009 } 1010 1011 #ifndef __APPLE__ 1012 std::error_code copy_file(const Twine &From, const Twine &To) { 1013 int ReadFD, WriteFD; 1014 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None)) 1015 return EC; 1016 if (std::error_code EC = 1017 openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) { 1018 close(ReadFD); 1019 return EC; 1020 } 1021 1022 std::error_code EC = copy_file_internal(ReadFD, WriteFD); 1023 1024 close(ReadFD); 1025 close(WriteFD); 1026 1027 return EC; 1028 } 1029 #endif 1030 1031 std::error_code copy_file(const Twine &From, int ToFD) { 1032 int ReadFD; 1033 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None)) 1034 return EC; 1035 1036 std::error_code EC = copy_file_internal(ReadFD, ToFD); 1037 1038 close(ReadFD); 1039 1040 return EC; 1041 } 1042 1043 ErrorOr<MD5::MD5Result> md5_contents(int FD) { 1044 MD5 Hash; 1045 1046 constexpr size_t BufSize = 4096; 1047 std::vector<uint8_t> Buf(BufSize); 1048 int BytesRead = 0; 1049 for (;;) { 1050 BytesRead = read(FD, Buf.data(), BufSize); 1051 if (BytesRead <= 0) 1052 break; 1053 Hash.update(makeArrayRef(Buf.data(), BytesRead)); 1054 } 1055 1056 if (BytesRead < 0) 1057 return std::error_code(errno, std::generic_category()); 1058 MD5::MD5Result Result; 1059 Hash.final(Result); 1060 return Result; 1061 } 1062 1063 ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) { 1064 int FD; 1065 if (auto EC = openFileForRead(Path, FD, OF_None)) 1066 return EC; 1067 1068 auto Result = md5_contents(FD); 1069 close(FD); 1070 return Result; 1071 } 1072 1073 bool exists(const basic_file_status &status) { 1074 return status_known(status) && status.type() != file_type::file_not_found; 1075 } 1076 1077 bool status_known(const basic_file_status &s) { 1078 return s.type() != file_type::status_error; 1079 } 1080 1081 file_type get_file_type(const Twine &Path, bool Follow) { 1082 file_status st; 1083 if (status(Path, st, Follow)) 1084 return file_type::status_error; 1085 return st.type(); 1086 } 1087 1088 bool is_directory(const basic_file_status &status) { 1089 return status.type() == file_type::directory_file; 1090 } 1091 1092 std::error_code is_directory(const Twine &path, bool &result) { 1093 file_status st; 1094 if (std::error_code ec = status(path, st)) 1095 return ec; 1096 result = is_directory(st); 1097 return std::error_code(); 1098 } 1099 1100 bool is_regular_file(const basic_file_status &status) { 1101 return status.type() == file_type::regular_file; 1102 } 1103 1104 std::error_code is_regular_file(const Twine &path, bool &result) { 1105 file_status st; 1106 if (std::error_code ec = status(path, st)) 1107 return ec; 1108 result = is_regular_file(st); 1109 return std::error_code(); 1110 } 1111 1112 bool is_symlink_file(const basic_file_status &status) { 1113 return status.type() == file_type::symlink_file; 1114 } 1115 1116 std::error_code is_symlink_file(const Twine &path, bool &result) { 1117 file_status st; 1118 if (std::error_code ec = status(path, st, false)) 1119 return ec; 1120 result = is_symlink_file(st); 1121 return std::error_code(); 1122 } 1123 1124 bool is_other(const basic_file_status &status) { 1125 return exists(status) && 1126 !is_regular_file(status) && 1127 !is_directory(status); 1128 } 1129 1130 std::error_code is_other(const Twine &Path, bool &Result) { 1131 file_status FileStatus; 1132 if (std::error_code EC = status(Path, FileStatus)) 1133 return EC; 1134 Result = is_other(FileStatus); 1135 return std::error_code(); 1136 } 1137 1138 void directory_entry::replace_filename(const Twine &Filename, file_type Type, 1139 basic_file_status Status) { 1140 SmallString<128> PathStr = path::parent_path(Path); 1141 path::append(PathStr, Filename); 1142 this->Path = std::string(PathStr.str()); 1143 this->Type = Type; 1144 this->Status = Status; 1145 } 1146 1147 ErrorOr<perms> getPermissions(const Twine &Path) { 1148 file_status Status; 1149 if (std::error_code EC = status(Path, Status)) 1150 return EC; 1151 1152 return Status.permissions(); 1153 } 1154 1155 size_t mapped_file_region::size() const { 1156 assert(Mapping && "Mapping failed but used anyway!"); 1157 return Size; 1158 } 1159 1160 char *mapped_file_region::data() const { 1161 assert(Mapping && "Mapping failed but used anyway!"); 1162 return reinterpret_cast<char *>(Mapping); 1163 } 1164 1165 const char *mapped_file_region::const_data() const { 1166 assert(Mapping && "Mapping failed but used anyway!"); 1167 return reinterpret_cast<const char *>(Mapping); 1168 } 1169 1170 } // end namespace fs 1171 } // end namespace sys 1172 } // end namespace llvm 1173 1174 // Include the truly platform-specific parts. 1175 #if defined(LLVM_ON_UNIX) 1176 #include "Unix/Path.inc" 1177 #endif 1178 #if defined(_WIN32) 1179 #include "Windows/Path.inc" 1180 #endif 1181 1182 namespace llvm { 1183 namespace sys { 1184 namespace fs { 1185 TempFile::TempFile(StringRef Name, int FD) 1186 : TmpName(std::string(Name)), FD(FD) {} 1187 TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); } 1188 TempFile &TempFile::operator=(TempFile &&Other) { 1189 TmpName = std::move(Other.TmpName); 1190 FD = Other.FD; 1191 Other.Done = true; 1192 Other.FD = -1; 1193 #ifdef _WIN32 1194 RemoveOnClose = Other.RemoveOnClose; 1195 Other.RemoveOnClose = false; 1196 #endif 1197 return *this; 1198 } 1199 1200 TempFile::~TempFile() { assert(Done); } 1201 1202 Error TempFile::discard() { 1203 Done = true; 1204 if (FD != -1 && close(FD) == -1) { 1205 std::error_code EC = std::error_code(errno, std::generic_category()); 1206 return errorCodeToError(EC); 1207 } 1208 FD = -1; 1209 1210 #ifdef _WIN32 1211 // On Windows, closing will remove the file, if we set the delete 1212 // disposition. If not, remove it manually. 1213 bool Remove = RemoveOnClose; 1214 #else 1215 // Always try to remove the file. 1216 bool Remove = true; 1217 #endif 1218 std::error_code RemoveEC; 1219 if (Remove && !TmpName.empty()) { 1220 RemoveEC = fs::remove(TmpName); 1221 sys::DontRemoveFileOnSignal(TmpName); 1222 if (!RemoveEC) 1223 TmpName = ""; 1224 } else { 1225 TmpName = ""; 1226 } 1227 return errorCodeToError(RemoveEC); 1228 } 1229 1230 Error TempFile::keep(const Twine &Name) { 1231 assert(!Done); 1232 Done = true; 1233 // Always try to close and rename. 1234 #ifdef _WIN32 1235 // If we can't cancel the delete don't rename. 1236 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); 1237 std::error_code RenameEC = setDeleteDisposition(H, false); 1238 bool ShouldDelete = false; 1239 if (!RenameEC) { 1240 RenameEC = rename_handle(H, Name); 1241 // If rename failed because it's cross-device, copy instead 1242 if (RenameEC == 1243 std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) { 1244 RenameEC = copy_file(TmpName, Name); 1245 ShouldDelete = true; 1246 } 1247 } 1248 1249 // If we can't rename or copy, discard the temporary file. 1250 if (RenameEC) 1251 ShouldDelete = true; 1252 if (ShouldDelete) { 1253 if (!RemoveOnClose) 1254 setDeleteDisposition(H, true); 1255 else 1256 remove(TmpName); 1257 } 1258 #else 1259 std::error_code RenameEC = fs::rename(TmpName, Name); 1260 if (RenameEC) { 1261 // If we can't rename, try to copy to work around cross-device link issues. 1262 RenameEC = sys::fs::copy_file(TmpName, Name); 1263 // If we can't rename or copy, discard the temporary file. 1264 if (RenameEC) 1265 remove(TmpName); 1266 } 1267 #endif 1268 sys::DontRemoveFileOnSignal(TmpName); 1269 1270 if (!RenameEC) 1271 TmpName = ""; 1272 1273 if (close(FD) == -1) { 1274 std::error_code EC(errno, std::generic_category()); 1275 return errorCodeToError(EC); 1276 } 1277 FD = -1; 1278 1279 return errorCodeToError(RenameEC); 1280 } 1281 1282 Error TempFile::keep() { 1283 assert(!Done); 1284 Done = true; 1285 1286 #ifdef _WIN32 1287 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); 1288 if (std::error_code EC = setDeleteDisposition(H, false)) 1289 return errorCodeToError(EC); 1290 #endif 1291 sys::DontRemoveFileOnSignal(TmpName); 1292 1293 TmpName = ""; 1294 1295 if (close(FD) == -1) { 1296 std::error_code EC(errno, std::generic_category()); 1297 return errorCodeToError(EC); 1298 } 1299 FD = -1; 1300 1301 return Error::success(); 1302 } 1303 1304 Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode, 1305 OpenFlags ExtraFlags) { 1306 int FD; 1307 SmallString<128> ResultPath; 1308 if (std::error_code EC = 1309 createUniqueFile(Model, FD, ResultPath, OF_Delete | ExtraFlags, Mode)) 1310 return errorCodeToError(EC); 1311 1312 TempFile Ret(ResultPath, FD); 1313 #ifdef _WIN32 1314 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD)); 1315 bool SetSignalHandler = false; 1316 if (std::error_code EC = setDeleteDisposition(H, true)) { 1317 Ret.RemoveOnClose = true; 1318 SetSignalHandler = true; 1319 } 1320 #else 1321 bool SetSignalHandler = true; 1322 #endif 1323 if (SetSignalHandler && sys::RemoveFileOnSignal(ResultPath)) { 1324 // Make sure we delete the file when RemoveFileOnSignal fails. 1325 consumeError(Ret.discard()); 1326 std::error_code EC(errc::operation_not_permitted); 1327 return errorCodeToError(EC); 1328 } 1329 return std::move(Ret); 1330 } 1331 } // namespace fs 1332 1333 } // namespace sys 1334 } // namespace llvm 1335