1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/Path.h" 11 #include "llvm/ADT/SmallVector.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/ADT/Triple.h" 14 #include "llvm/Support/ConvertUTF.h" 15 #include "llvm/Support/Errc.h" 16 #include "llvm/Support/ErrorHandling.h" 17 #include "llvm/Support/FileSystem.h" 18 #include "llvm/Support/FileUtilities.h" 19 #include "llvm/Support/Host.h" 20 #include "llvm/Support/MemoryBuffer.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include "gtest/gtest.h" 23 24 #ifdef LLVM_ON_WIN32 25 #include "llvm/ADT/ArrayRef.h" 26 #include <windows.h> 27 #include <winerror.h> 28 #endif 29 30 #ifdef LLVM_ON_UNIX 31 #include <sys/stat.h> 32 #endif 33 34 using namespace llvm; 35 using namespace llvm::sys; 36 37 #define ASSERT_NO_ERROR(x) \ 38 if (std::error_code ASSERT_NO_ERROR_ec = x) { \ 39 SmallString<128> MessageStorage; \ 40 raw_svector_ostream Message(MessageStorage); \ 41 Message << #x ": did not return errc::success.\n" \ 42 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 43 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 44 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ 45 } else { \ 46 } 47 48 namespace { 49 50 TEST(is_separator, Works) { 51 EXPECT_TRUE(path::is_separator('/')); 52 EXPECT_FALSE(path::is_separator('\0')); 53 EXPECT_FALSE(path::is_separator('-')); 54 EXPECT_FALSE(path::is_separator(' ')); 55 56 #ifdef LLVM_ON_WIN32 57 EXPECT_TRUE(path::is_separator('\\')); 58 #else 59 EXPECT_FALSE(path::is_separator('\\')); 60 #endif 61 } 62 63 TEST(Support, Path) { 64 SmallVector<StringRef, 40> paths; 65 paths.push_back(""); 66 paths.push_back("."); 67 paths.push_back(".."); 68 paths.push_back("foo"); 69 paths.push_back("/"); 70 paths.push_back("/foo"); 71 paths.push_back("foo/"); 72 paths.push_back("/foo/"); 73 paths.push_back("foo/bar"); 74 paths.push_back("/foo/bar"); 75 paths.push_back("//net"); 76 paths.push_back("//net/foo"); 77 paths.push_back("///foo///"); 78 paths.push_back("///foo///bar"); 79 paths.push_back("/."); 80 paths.push_back("./"); 81 paths.push_back("/.."); 82 paths.push_back("../"); 83 paths.push_back("foo/."); 84 paths.push_back("foo/.."); 85 paths.push_back("foo/./"); 86 paths.push_back("foo/./bar"); 87 paths.push_back("foo/.."); 88 paths.push_back("foo/../"); 89 paths.push_back("foo/../bar"); 90 paths.push_back("c:"); 91 paths.push_back("c:/"); 92 paths.push_back("c:foo"); 93 paths.push_back("c:/foo"); 94 paths.push_back("c:foo/"); 95 paths.push_back("c:/foo/"); 96 paths.push_back("c:/foo/bar"); 97 paths.push_back("prn:"); 98 paths.push_back("c:\\"); 99 paths.push_back("c:foo"); 100 paths.push_back("c:\\foo"); 101 paths.push_back("c:foo\\"); 102 paths.push_back("c:\\foo\\"); 103 paths.push_back("c:\\foo/"); 104 paths.push_back("c:/foo\\bar"); 105 106 SmallVector<StringRef, 5> ComponentStack; 107 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), 108 e = paths.end(); 109 i != e; 110 ++i) { 111 for (sys::path::const_iterator ci = sys::path::begin(*i), 112 ce = sys::path::end(*i); 113 ci != ce; 114 ++ci) { 115 ASSERT_FALSE(ci->empty()); 116 ComponentStack.push_back(*ci); 117 } 118 119 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), 120 ce = sys::path::rend(*i); 121 ci != ce; 122 ++ci) { 123 ASSERT_TRUE(*ci == ComponentStack.back()); 124 ComponentStack.pop_back(); 125 } 126 ASSERT_TRUE(ComponentStack.empty()); 127 128 // Crash test most of the API - since we're iterating over all of our paths 129 // here there isn't really anything reasonable to assert on in the results. 130 (void)path::has_root_path(*i); 131 (void)path::root_path(*i); 132 (void)path::has_root_name(*i); 133 (void)path::root_name(*i); 134 (void)path::has_root_directory(*i); 135 (void)path::root_directory(*i); 136 (void)path::has_parent_path(*i); 137 (void)path::parent_path(*i); 138 (void)path::has_filename(*i); 139 (void)path::filename(*i); 140 (void)path::has_stem(*i); 141 (void)path::stem(*i); 142 (void)path::has_extension(*i); 143 (void)path::extension(*i); 144 (void)path::is_absolute(*i); 145 (void)path::is_relative(*i); 146 147 SmallString<128> temp_store; 148 temp_store = *i; 149 ASSERT_NO_ERROR(fs::make_absolute(temp_store)); 150 temp_store = *i; 151 path::remove_filename(temp_store); 152 153 temp_store = *i; 154 path::replace_extension(temp_store, "ext"); 155 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; 156 stem = path::stem(filename); 157 ext = path::extension(filename); 158 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str()); 159 160 path::native(*i, temp_store); 161 } 162 163 SmallString<32> Relative("foo.cpp"); 164 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative)); 165 Relative[5] = '/'; // Fix up windows paths. 166 ASSERT_EQ("/root/foo.cpp", Relative); 167 } 168 169 TEST(Support, RelativePathIterator) { 170 SmallString<64> Path(StringRef("c/d/e/foo.txt")); 171 typedef SmallVector<StringRef, 4> PathComponents; 172 PathComponents ExpectedPathComponents; 173 PathComponents ActualPathComponents; 174 175 StringRef(Path).split(ExpectedPathComponents, '/'); 176 177 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 178 ++I) { 179 ActualPathComponents.push_back(*I); 180 } 181 182 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 183 184 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 185 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 186 } 187 } 188 189 TEST(Support, RelativePathDotIterator) { 190 SmallString<64> Path(StringRef(".c/.d/../.")); 191 typedef SmallVector<StringRef, 4> PathComponents; 192 PathComponents ExpectedPathComponents; 193 PathComponents ActualPathComponents; 194 195 StringRef(Path).split(ExpectedPathComponents, '/'); 196 197 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 198 ++I) { 199 ActualPathComponents.push_back(*I); 200 } 201 202 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 203 204 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 205 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 206 } 207 } 208 209 TEST(Support, AbsolutePathIterator) { 210 SmallString<64> Path(StringRef("/c/d/e/foo.txt")); 211 typedef SmallVector<StringRef, 4> PathComponents; 212 PathComponents ExpectedPathComponents; 213 PathComponents ActualPathComponents; 214 215 StringRef(Path).split(ExpectedPathComponents, '/'); 216 217 // The root path will also be a component when iterating 218 ExpectedPathComponents[0] = "/"; 219 220 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 221 ++I) { 222 ActualPathComponents.push_back(*I); 223 } 224 225 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 226 227 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 228 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 229 } 230 } 231 232 TEST(Support, AbsolutePathDotIterator) { 233 SmallString<64> Path(StringRef("/.c/.d/../.")); 234 typedef SmallVector<StringRef, 4> PathComponents; 235 PathComponents ExpectedPathComponents; 236 PathComponents ActualPathComponents; 237 238 StringRef(Path).split(ExpectedPathComponents, '/'); 239 240 // The root path will also be a component when iterating 241 ExpectedPathComponents[0] = "/"; 242 243 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 244 ++I) { 245 ActualPathComponents.push_back(*I); 246 } 247 248 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 249 250 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 251 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 252 } 253 } 254 255 #ifdef LLVM_ON_WIN32 256 TEST(Support, AbsolutePathIteratorWin32) { 257 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); 258 typedef SmallVector<StringRef, 4> PathComponents; 259 PathComponents ExpectedPathComponents; 260 PathComponents ActualPathComponents; 261 262 StringRef(Path).split(ExpectedPathComponents, "\\"); 263 264 // The root path (which comes after the drive name) will also be a component 265 // when iterating. 266 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); 267 268 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; 269 ++I) { 270 ActualPathComponents.push_back(*I); 271 } 272 273 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); 274 275 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { 276 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); 277 } 278 } 279 #endif // LLVM_ON_WIN32 280 281 TEST(Support, AbsolutePathIteratorEnd) { 282 // Trailing slashes are converted to '.' unless they are part of the root path. 283 SmallVector<StringRef, 4> Paths; 284 Paths.push_back("/foo/"); 285 Paths.push_back("/foo//"); 286 Paths.push_back("//net//"); 287 #ifdef LLVM_ON_WIN32 288 Paths.push_back("c:\\\\"); 289 #endif 290 291 for (StringRef Path : Paths) { 292 StringRef LastComponent = *path::rbegin(Path); 293 EXPECT_EQ(".", LastComponent); 294 } 295 296 SmallVector<StringRef, 3> RootPaths; 297 RootPaths.push_back("/"); 298 RootPaths.push_back("//net/"); 299 #ifdef LLVM_ON_WIN32 300 RootPaths.push_back("c:\\"); 301 #endif 302 303 for (StringRef Path : RootPaths) { 304 StringRef LastComponent = *path::rbegin(Path); 305 EXPECT_EQ(1u, LastComponent.size()); 306 EXPECT_TRUE(path::is_separator(LastComponent[0])); 307 } 308 } 309 310 TEST(Support, HomeDirectory) { 311 std::string expected; 312 #ifdef LLVM_ON_WIN32 313 if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) { 314 auto pathLen = ::wcslen(path); 315 ArrayRef<char> ref{reinterpret_cast<char const *>(path), 316 pathLen * sizeof(wchar_t)}; 317 convertUTF16ToUTF8String(ref, expected); 318 } 319 #else 320 if (char const *path = ::getenv("HOME")) 321 expected = path; 322 #endif 323 // Do not try to test it if we don't know what to expect. 324 // On Windows we use something better than env vars. 325 if (!expected.empty()) { 326 SmallString<128> HomeDir; 327 auto status = path::home_directory(HomeDir); 328 EXPECT_TRUE(status); 329 EXPECT_EQ(expected, HomeDir); 330 } 331 } 332 333 TEST(Support, UserCacheDirectory) { 334 SmallString<13> CacheDir; 335 SmallString<20> CacheDir2; 336 auto Status = path::user_cache_directory(CacheDir, ""); 337 EXPECT_TRUE(Status ^ CacheDir.empty()); 338 339 if (Status) { 340 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed 341 EXPECT_EQ(CacheDir, CacheDir2); // and return same paths 342 343 EXPECT_TRUE(path::user_cache_directory(CacheDir, "A", "B", "file.c")); 344 auto It = path::rbegin(CacheDir); 345 EXPECT_EQ("file.c", *It); 346 EXPECT_EQ("B", *++It); 347 EXPECT_EQ("A", *++It); 348 auto ParentDir = *++It; 349 350 // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0" 351 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2", 352 "\xE2\x84\xB5.0")); 353 auto It2 = path::rbegin(CacheDir2); 354 EXPECT_EQ("\xE2\x84\xB5.0", *It2); 355 EXPECT_EQ("\xCF\x80r\xC2\xB2", *++It2); 356 auto ParentDir2 = *++It2; 357 358 EXPECT_EQ(ParentDir, ParentDir2); 359 } 360 } 361 362 TEST(Support, TempDirectory) { 363 SmallString<32> TempDir; 364 path::system_temp_directory(false, TempDir); 365 EXPECT_TRUE(!TempDir.empty()); 366 TempDir.clear(); 367 path::system_temp_directory(true, TempDir); 368 EXPECT_TRUE(!TempDir.empty()); 369 } 370 371 #ifdef LLVM_ON_WIN32 372 static std::string path2regex(std::string Path) { 373 size_t Pos = 0; 374 while ((Pos = Path.find('\\', Pos)) != std::string::npos) { 375 Path.replace(Pos, 1, "\\\\"); 376 Pos += 2; 377 } 378 return Path; 379 } 380 381 /// Helper for running temp dir test in separated process. See below. 382 #define EXPECT_TEMP_DIR(prepare, expected) \ 383 EXPECT_EXIT( \ 384 { \ 385 prepare; \ 386 SmallString<300> TempDir; \ 387 path::system_temp_directory(true, TempDir); \ 388 raw_os_ostream(std::cerr) << TempDir; \ 389 std::exit(0); \ 390 }, \ 391 ::testing::ExitedWithCode(0), path2regex(expected)) 392 393 TEST(SupportDeathTest, TempDirectoryOnWindows) { 394 // In this test we want to check how system_temp_directory responds to 395 // different values of specific env vars. To prevent corrupting env vars of 396 // the current process all checks are done in separated processes. 397 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder"); 398 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"), 399 "C:\\Unix\\Path\\Seperators"); 400 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$"); 401 EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep"); 402 EXPECT_TEMP_DIR( 403 _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"), 404 "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80"); 405 406 // Test $TMP empty, $TEMP set. 407 EXPECT_TEMP_DIR( 408 { 409 _wputenv_s(L"TMP", L""); 410 _wputenv_s(L"TEMP", L"C:\\Valid\\Path"); 411 }, 412 "C:\\Valid\\Path"); 413 414 // All related env vars empty 415 EXPECT_TEMP_DIR( 416 { 417 _wputenv_s(L"TMP", L""); 418 _wputenv_s(L"TEMP", L""); 419 _wputenv_s(L"USERPROFILE", L""); 420 }, 421 "C:\\Temp"); 422 423 // Test evn var / path with 260 chars. 424 SmallString<270> Expected{"C:\\Temp\\AB\\123456789"}; 425 while (Expected.size() < 260) 426 Expected.append("\\DirNameWith19Charss"); 427 ASSERT_EQ(260U, Expected.size()); 428 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str()); 429 } 430 #endif 431 432 class FileSystemTest : public testing::Test { 433 protected: 434 /// Unique temporary directory in which all created filesystem entities must 435 /// be placed. It is removed at the end of each test (must be empty). 436 SmallString<128> TestDirectory; 437 438 void SetUp() override { 439 ASSERT_NO_ERROR( 440 fs::createUniqueDirectory("file-system-test", TestDirectory)); 441 // We don't care about this specific file. 442 errs() << "Test Directory: " << TestDirectory << '\n'; 443 errs().flush(); 444 } 445 446 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); } 447 448 SmallVector<Triple::ArchType, 4> UnsupportedArchs; 449 SmallVector<Triple::OSType, 4> UnsupportedOSs; 450 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments; 451 452 bool isUnsupportedOSOrEnvironment() { 453 Triple Host(Triple::normalize(sys::getProcessTriple())); 454 455 if (find(UnsupportedEnvironments, Host.getEnvironment()) != 456 UnsupportedEnvironments.end()) 457 return true; 458 459 if (is_contained(UnsupportedOSs, Host.getOS())) 460 return true; 461 462 if (is_contained(UnsupportedArchs, Host.getArch())) 463 return true; 464 465 return false; 466 } 467 468 FileSystemTest() { 469 UnsupportedArchs.push_back(Triple::mips); 470 UnsupportedArchs.push_back(Triple::mipsel); 471 } 472 }; 473 474 TEST_F(FileSystemTest, Unique) { 475 // Create a temp file. 476 int FileDescriptor; 477 SmallString<64> TempPath; 478 ASSERT_NO_ERROR( 479 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 480 481 // The same file should return an identical unique id. 482 fs::UniqueID F1, F2; 483 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); 484 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); 485 ASSERT_EQ(F1, F2); 486 487 // Different files should return different unique ids. 488 int FileDescriptor2; 489 SmallString<64> TempPath2; 490 ASSERT_NO_ERROR( 491 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); 492 493 fs::UniqueID D; 494 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); 495 ASSERT_NE(D, F1); 496 ::close(FileDescriptor2); 497 498 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 499 500 // Two paths representing the same file on disk should still provide the 501 // same unique id. We can test this by making a hard link. 502 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); 503 fs::UniqueID D2; 504 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); 505 ASSERT_EQ(D2, F1); 506 507 ::close(FileDescriptor); 508 509 SmallString<128> Dir1; 510 ASSERT_NO_ERROR( 511 fs::createUniqueDirectory("dir1", Dir1)); 512 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); 513 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); 514 ASSERT_EQ(F1, F2); 515 516 SmallString<128> Dir2; 517 ASSERT_NO_ERROR( 518 fs::createUniqueDirectory("dir2", Dir2)); 519 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); 520 ASSERT_NE(F1, F2); 521 ASSERT_NO_ERROR(fs::remove(Dir1)); 522 ASSERT_NO_ERROR(fs::remove(Dir2)); 523 ASSERT_NO_ERROR(fs::remove(TempPath2)); 524 ASSERT_NO_ERROR(fs::remove(TempPath)); 525 } 526 527 TEST_F(FileSystemTest, TempFiles) { 528 // Create a temp file. 529 int FileDescriptor; 530 SmallString<64> TempPath; 531 ASSERT_NO_ERROR( 532 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 533 534 // Make sure it exists. 535 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 536 537 // Create another temp tile. 538 int FD2; 539 SmallString<64> TempPath2; 540 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); 541 ASSERT_TRUE(TempPath2.endswith(".temp")); 542 ASSERT_NE(TempPath.str(), TempPath2.str()); 543 544 fs::file_status A, B; 545 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); 546 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); 547 EXPECT_FALSE(fs::equivalent(A, B)); 548 549 ::close(FD2); 550 551 // Remove Temp2. 552 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 553 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 554 ASSERT_EQ(fs::remove(Twine(TempPath2), false), 555 errc::no_such_file_or_directory); 556 557 std::error_code EC = fs::status(TempPath2.c_str(), B); 558 EXPECT_EQ(EC, errc::no_such_file_or_directory); 559 EXPECT_EQ(B.type(), fs::file_type::file_not_found); 560 561 // Make sure Temp2 doesn't exist. 562 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist), 563 errc::no_such_file_or_directory); 564 565 SmallString<64> TempPath3; 566 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); 567 ASSERT_FALSE(TempPath3.endswith(".")); 568 FileRemover Cleanup3(TempPath3); 569 570 // Create a hard link to Temp1. 571 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); 572 bool equal; 573 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); 574 EXPECT_TRUE(equal); 575 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); 576 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); 577 EXPECT_TRUE(fs::equivalent(A, B)); 578 579 // Remove Temp1. 580 ::close(FileDescriptor); 581 ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); 582 583 // Remove the hard link. 584 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); 585 586 // Make sure Temp1 doesn't exist. 587 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist), 588 errc::no_such_file_or_directory); 589 590 #ifdef LLVM_ON_WIN32 591 // Path name > 260 chars should get an error. 592 const char *Path270 = 593 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" 594 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" 595 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" 596 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" 597 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; 598 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath), 599 errc::invalid_argument); 600 // Relative path < 247 chars, no problem. 601 const char *Path216 = 602 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" 603 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" 604 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" 605 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; 606 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath)); 607 ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); 608 #endif 609 } 610 611 TEST_F(FileSystemTest, CreateDir) { 612 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); 613 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); 614 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false), 615 errc::file_exists); 616 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo")); 617 618 #ifdef LLVM_ON_UNIX 619 // Set a 0000 umask so that we can test our directory permissions. 620 mode_t OldUmask = ::umask(0000); 621 622 fs::file_status Status; 623 ASSERT_NO_ERROR( 624 fs::create_directory(Twine(TestDirectory) + "baz500", false, 625 fs::perms::owner_read | fs::perms::owner_exe)); 626 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status)); 627 ASSERT_EQ(Status.permissions() & fs::perms::all_all, 628 fs::perms::owner_read | fs::perms::owner_exe); 629 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false, 630 fs::perms::all_all)); 631 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status)); 632 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all); 633 634 // Restore umask to be safe. 635 ::umask(OldUmask); 636 #endif 637 638 #ifdef LLVM_ON_WIN32 639 // Prove that create_directories() can handle a pathname > 248 characters, 640 // which is the documented limit for CreateDirectory(). 641 // (248 is MAX_PATH subtracting room for an 8.3 filename.) 642 // Generate a directory path guaranteed to fall into that range. 643 size_t TmpLen = TestDirectory.size(); 644 const char *OneDir = "\\123456789"; 645 size_t OneDirLen = strlen(OneDir); 646 ASSERT_LT(OneDirLen, 12U); 647 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1; 648 SmallString<260> LongDir(TestDirectory); 649 for (size_t I = 0; I < NLevels; ++I) 650 LongDir.append(OneDir); 651 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir))); 652 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir))); 653 ASSERT_EQ(fs::create_directories(Twine(LongDir), false), 654 errc::file_exists); 655 // Tidy up, "recursively" removing the directories. 656 StringRef ThisDir(LongDir); 657 for (size_t J = 0; J < NLevels; ++J) { 658 ASSERT_NO_ERROR(fs::remove(ThisDir)); 659 ThisDir = path::parent_path(ThisDir); 660 } 661 662 // Similarly for a relative pathname. Need to set the current directory to 663 // TestDirectory so that the one we create ends up in the right place. 664 char PreviousDir[260]; 665 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir); 666 ASSERT_GT(PreviousDirLen, 0U); 667 ASSERT_LT(PreviousDirLen, 260U); 668 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0); 669 LongDir.clear(); 670 // Generate a relative directory name with absolute length > 248. 671 size_t LongDirLen = 249 - TestDirectory.size(); 672 LongDir.assign(LongDirLen, 'a'); 673 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir))); 674 // While we're here, prove that .. and . handling works in these long paths. 675 const char *DotDotDirs = "\\..\\.\\b"; 676 LongDir.append(DotDotDirs); 677 ASSERT_NO_ERROR(fs::create_directory("b")); 678 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists); 679 // And clean up. 680 ASSERT_NO_ERROR(fs::remove("b")); 681 ASSERT_NO_ERROR(fs::remove( 682 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs))))); 683 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0); 684 #endif 685 } 686 687 TEST_F(FileSystemTest, DirectoryIteration) { 688 std::error_code ec; 689 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) 690 ASSERT_NO_ERROR(ec); 691 692 // Create a known hierarchy to recurse over. 693 ASSERT_NO_ERROR( 694 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1")); 695 ASSERT_NO_ERROR( 696 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1")); 697 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + 698 "/recursive/dontlookhere/da1")); 699 ASSERT_NO_ERROR( 700 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1")); 701 ASSERT_NO_ERROR( 702 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1")); 703 typedef std::vector<std::string> v_t; 704 v_t visited; 705 for (fs::recursive_directory_iterator i(Twine(TestDirectory) 706 + "/recursive", ec), e; i != e; i.increment(ec)){ 707 ASSERT_NO_ERROR(ec); 708 if (path::filename(i->path()) == "p1") { 709 i.pop(); 710 // FIXME: recursive_directory_iterator should be more robust. 711 if (i == e) break; 712 } 713 if (path::filename(i->path()) == "dontlookhere") 714 i.no_push(); 715 visited.push_back(path::filename(i->path())); 716 } 717 v_t::const_iterator a0 = find(visited, "a0"); 718 v_t::const_iterator aa1 = find(visited, "aa1"); 719 v_t::const_iterator ab1 = find(visited, "ab1"); 720 v_t::const_iterator dontlookhere = find(visited, "dontlookhere"); 721 v_t::const_iterator da1 = find(visited, "da1"); 722 v_t::const_iterator z0 = find(visited, "z0"); 723 v_t::const_iterator za1 = find(visited, "za1"); 724 v_t::const_iterator pop = find(visited, "pop"); 725 v_t::const_iterator p1 = find(visited, "p1"); 726 727 // Make sure that each path was visited correctly. 728 ASSERT_NE(a0, visited.end()); 729 ASSERT_NE(aa1, visited.end()); 730 ASSERT_NE(ab1, visited.end()); 731 ASSERT_NE(dontlookhere, visited.end()); 732 ASSERT_EQ(da1, visited.end()); // Not visited. 733 ASSERT_NE(z0, visited.end()); 734 ASSERT_NE(za1, visited.end()); 735 ASSERT_NE(pop, visited.end()); 736 ASSERT_EQ(p1, visited.end()); // Not visited. 737 738 // Make sure that parents were visited before children. No other ordering 739 // guarantees can be made across siblings. 740 ASSERT_LT(a0, aa1); 741 ASSERT_LT(a0, ab1); 742 ASSERT_LT(z0, za1); 743 744 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); 745 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); 746 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); 747 ASSERT_NO_ERROR( 748 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); 749 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); 750 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); 751 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); 752 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); 753 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); 754 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); 755 756 // Test recursive_directory_iterator level() 757 ASSERT_NO_ERROR( 758 fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c")); 759 fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E; 760 for (int l = 0; I != E; I.increment(ec), ++l) { 761 ASSERT_NO_ERROR(ec); 762 EXPECT_EQ(I.level(), l); 763 } 764 EXPECT_EQ(I, E); 765 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c")); 766 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b")); 767 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a")); 768 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel")); 769 } 770 771 const char archive[] = "!<arch>\x0A"; 772 const char bitcode[] = "\xde\xc0\x17\x0b"; 773 const char coff_object[] = "\x00\x00......"; 774 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......" 775 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8"; 776 const char coff_import_library[] = "\x00\x00\xff\xff...."; 777 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 778 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 779 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00"; 780 const char macho_object[] = 781 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............"; 782 const char macho_executable[] = 783 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............"; 784 const char macho_fixed_virtual_memory_shared_lib[] = 785 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............"; 786 const char macho_core[] = 787 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............"; 788 const char macho_preload_executable[] = 789 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............"; 790 const char macho_dynamically_linked_shared_lib[] = 791 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............"; 792 const char macho_dynamic_linker[] = 793 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............"; 794 const char macho_bundle[] = 795 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............"; 796 const char macho_dsym_companion[] = 797 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............"; 798 const char macho_kext_bundle[] = 799 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............"; 800 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; 801 const char macho_dynamically_linked_shared_lib_stub[] = 802 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............"; 803 804 TEST_F(FileSystemTest, Magic) { 805 struct type { 806 const char *filename; 807 const char *magic_str; 808 size_t magic_str_len; 809 fs::file_magic magic; 810 } types[] = { 811 #define DEFINE(magic) \ 812 { #magic, magic, sizeof(magic), fs::file_magic::magic } 813 DEFINE(archive), 814 DEFINE(bitcode), 815 DEFINE(coff_object), 816 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object }, 817 DEFINE(coff_import_library), 818 DEFINE(elf_relocatable), 819 DEFINE(macho_universal_binary), 820 DEFINE(macho_object), 821 DEFINE(macho_executable), 822 DEFINE(macho_fixed_virtual_memory_shared_lib), 823 DEFINE(macho_core), 824 DEFINE(macho_preload_executable), 825 DEFINE(macho_dynamically_linked_shared_lib), 826 DEFINE(macho_dynamic_linker), 827 DEFINE(macho_bundle), 828 DEFINE(macho_dynamically_linked_shared_lib_stub), 829 DEFINE(macho_dsym_companion), 830 DEFINE(macho_kext_bundle), 831 DEFINE(windows_resource) 832 #undef DEFINE 833 }; 834 835 // Create some files filled with magic. 836 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; 837 ++i) { 838 SmallString<128> file_pathname(TestDirectory); 839 path::append(file_pathname, i->filename); 840 std::error_code EC; 841 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None); 842 ASSERT_FALSE(file.has_error()); 843 StringRef magic(i->magic_str, i->magic_str_len); 844 file << magic; 845 file.close(); 846 EXPECT_EQ(i->magic, fs::identify_magic(magic)); 847 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); 848 } 849 } 850 851 #ifdef LLVM_ON_WIN32 852 TEST_F(FileSystemTest, CarriageReturn) { 853 SmallString<128> FilePathname(TestDirectory); 854 std::error_code EC; 855 path::append(FilePathname, "test"); 856 857 { 858 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text); 859 ASSERT_NO_ERROR(EC); 860 File << '\n'; 861 } 862 { 863 auto Buf = MemoryBuffer::getFile(FilePathname.str()); 864 EXPECT_TRUE((bool)Buf); 865 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n"); 866 } 867 868 { 869 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None); 870 ASSERT_NO_ERROR(EC); 871 File << '\n'; 872 } 873 { 874 auto Buf = MemoryBuffer::getFile(FilePathname.str()); 875 EXPECT_TRUE((bool)Buf); 876 EXPECT_EQ(Buf.get()->getBuffer(), "\n"); 877 } 878 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); 879 } 880 #endif 881 882 TEST_F(FileSystemTest, Resize) { 883 int FD; 884 SmallString<64> TempPath; 885 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); 886 ASSERT_NO_ERROR(fs::resize_file(FD, 123)); 887 fs::file_status Status; 888 ASSERT_NO_ERROR(fs::status(FD, Status)); 889 ASSERT_EQ(Status.getSize(), 123U); 890 ::close(FD); 891 ASSERT_NO_ERROR(fs::remove(TempPath)); 892 } 893 894 TEST_F(FileSystemTest, FileMapping) { 895 // Create a temp file. 896 int FileDescriptor; 897 SmallString<64> TempPath; 898 ASSERT_NO_ERROR( 899 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 900 unsigned Size = 4096; 901 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size)); 902 903 // Map in temp file and add some content 904 std::error_code EC; 905 StringRef Val("hello there"); 906 { 907 fs::mapped_file_region mfr(FileDescriptor, 908 fs::mapped_file_region::readwrite, Size, 0, EC); 909 ASSERT_NO_ERROR(EC); 910 std::copy(Val.begin(), Val.end(), mfr.data()); 911 // Explicitly add a 0. 912 mfr.data()[Val.size()] = 0; 913 // Unmap temp file 914 } 915 ASSERT_EQ(close(FileDescriptor), 0); 916 917 // Map it back in read-only 918 { 919 int FD; 920 EC = fs::openFileForRead(Twine(TempPath), FD); 921 ASSERT_NO_ERROR(EC); 922 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC); 923 ASSERT_NO_ERROR(EC); 924 925 // Verify content 926 EXPECT_EQ(StringRef(mfr.const_data()), Val); 927 928 // Unmap temp file 929 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC); 930 ASSERT_NO_ERROR(EC); 931 ASSERT_EQ(close(FD), 0); 932 } 933 ASSERT_NO_ERROR(fs::remove(TempPath)); 934 } 935 936 TEST(Support, NormalizePath) { 937 #if defined(LLVM_ON_WIN32) 938 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ 939 EXPECT_EQ(path__, windows__); 940 #else 941 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ 942 EXPECT_EQ(path__, not_windows__); 943 #endif 944 945 SmallString<64> Path1("a"); 946 SmallString<64> Path2("a/b"); 947 SmallString<64> Path3("a\\b"); 948 SmallString<64> Path4("a\\\\b"); 949 SmallString<64> Path5("\\a"); 950 SmallString<64> Path6("a\\"); 951 952 path::native(Path1); 953 EXPECT_PATH_IS(Path1, "a", "a"); 954 955 path::native(Path2); 956 EXPECT_PATH_IS(Path2, "a\\b", "a/b"); 957 958 path::native(Path3); 959 EXPECT_PATH_IS(Path3, "a\\b", "a/b"); 960 961 path::native(Path4); 962 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b"); 963 964 path::native(Path5); 965 EXPECT_PATH_IS(Path5, "\\a", "/a"); 966 967 path::native(Path6); 968 EXPECT_PATH_IS(Path6, "a\\", "a/"); 969 970 #undef EXPECT_PATH_IS 971 972 #if defined(LLVM_ON_WIN32) 973 SmallString<64> PathHome; 974 path::home_directory(PathHome); 975 976 const char *Path7a = "~/aaa"; 977 SmallString<64> Path7(Path7a); 978 path::native(Path7); 979 EXPECT_TRUE(Path7.endswith("\\aaa")); 980 EXPECT_TRUE(Path7.startswith(PathHome)); 981 EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1)); 982 983 const char *Path8a = "~"; 984 SmallString<64> Path8(Path8a); 985 path::native(Path8); 986 EXPECT_EQ(Path8, PathHome); 987 988 const char *Path9a = "~aaa"; 989 SmallString<64> Path9(Path9a); 990 path::native(Path9); 991 EXPECT_EQ(Path9, "~aaa"); 992 993 const char *Path10a = "aaa/~/b"; 994 SmallString<64> Path10(Path10a); 995 path::native(Path10); 996 EXPECT_EQ(Path10, "aaa\\~\\b"); 997 #endif 998 } 999 1000 TEST(Support, RemoveLeadingDotSlash) { 1001 StringRef Path1("././/foolz/wat"); 1002 StringRef Path2("./////"); 1003 1004 Path1 = path::remove_leading_dotslash(Path1); 1005 EXPECT_EQ(Path1, "foolz/wat"); 1006 Path2 = path::remove_leading_dotslash(Path2); 1007 EXPECT_EQ(Path2, ""); 1008 } 1009 1010 static std::string remove_dots(StringRef path, 1011 bool remove_dot_dot) { 1012 SmallString<256> buffer(path); 1013 path::remove_dots(buffer, remove_dot_dot); 1014 return buffer.str(); 1015 } 1016 1017 TEST(Support, RemoveDots) { 1018 #if defined(LLVM_ON_WIN32) 1019 EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false)); 1020 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false)); 1021 1022 EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false)); 1023 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true)); 1024 EXPECT_EQ("c", remove_dots(".\\.\\c", true)); 1025 EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true)); 1026 EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true)); 1027 1028 SmallString<64> Path1(".\\.\\c"); 1029 EXPECT_TRUE(path::remove_dots(Path1, true)); 1030 EXPECT_EQ("c", Path1); 1031 #else 1032 EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false)); 1033 EXPECT_EQ("", remove_dots("./////", false)); 1034 1035 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false)); 1036 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true)); 1037 EXPECT_EQ("c", remove_dots("././c", true)); 1038 EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true)); 1039 EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true)); 1040 EXPECT_EQ("/a/c", remove_dots("/../../a/c", true)); 1041 EXPECT_EQ("/a/c", remove_dots("/../a/b//../././/c", true)); 1042 1043 SmallString<64> Path1("././c"); 1044 EXPECT_TRUE(path::remove_dots(Path1, true)); 1045 EXPECT_EQ("c", Path1); 1046 #endif 1047 } 1048 1049 TEST(Support, ReplacePathPrefix) { 1050 SmallString<64> Path1("/foo"); 1051 SmallString<64> Path2("/old/foo"); 1052 SmallString<64> OldPrefix("/old"); 1053 SmallString<64> NewPrefix("/new"); 1054 SmallString<64> NewPrefix2("/longernew"); 1055 SmallString<64> EmptyPrefix(""); 1056 1057 SmallString<64> Path = Path1; 1058 path::replace_path_prefix(Path, OldPrefix, NewPrefix); 1059 EXPECT_EQ(Path, "/foo"); 1060 Path = Path2; 1061 path::replace_path_prefix(Path, OldPrefix, NewPrefix); 1062 EXPECT_EQ(Path, "/new/foo"); 1063 Path = Path2; 1064 path::replace_path_prefix(Path, OldPrefix, NewPrefix2); 1065 EXPECT_EQ(Path, "/longernew/foo"); 1066 Path = Path1; 1067 path::replace_path_prefix(Path, EmptyPrefix, NewPrefix); 1068 EXPECT_EQ(Path, "/new/foo"); 1069 Path = Path2; 1070 path::replace_path_prefix(Path, OldPrefix, EmptyPrefix); 1071 EXPECT_EQ(Path, "/foo"); 1072 } 1073 1074 TEST_F(FileSystemTest, PathFromFD) { 1075 // Create a temp file. 1076 int FileDescriptor; 1077 SmallString<64> TempPath; 1078 ASSERT_NO_ERROR( 1079 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 1080 FileRemover Cleanup(TempPath); 1081 1082 // Make sure it exists. 1083 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1084 1085 // Try to get the path from the file descriptor 1086 SmallString<64> ResultPath; 1087 std::error_code ErrorCode = 1088 fs::getPathFromOpenFD(FileDescriptor, ResultPath); 1089 1090 // If we succeeded, check that the paths are the same (modulo case): 1091 if (!ErrorCode) { 1092 // The paths returned by createTemporaryFile and getPathFromOpenFD 1093 // should reference the same file on disk. 1094 fs::UniqueID D1, D2; 1095 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); 1096 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); 1097 ASSERT_EQ(D1, D2); 1098 } 1099 1100 ::close(FileDescriptor); 1101 } 1102 1103 TEST_F(FileSystemTest, PathFromFDWin32) { 1104 // Create a temp file. 1105 int FileDescriptor; 1106 SmallString<64> TempPath; 1107 ASSERT_NO_ERROR( 1108 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 1109 FileRemover Cleanup(TempPath); 1110 1111 // Make sure it exists. 1112 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1113 1114 SmallVector<char, 8> ResultPath; 1115 std::error_code ErrorCode = 1116 fs::getPathFromOpenFD(FileDescriptor, ResultPath); 1117 1118 if (!ErrorCode) { 1119 // Now that we know how much space is required for the path, create a path 1120 // buffer with exactly enough space (sans null terminator, which should not 1121 // be present), and call getPathFromOpenFD again to ensure that the API 1122 // properly handles exactly-sized buffers. 1123 SmallVector<char, 8> ExactSizedPath(ResultPath.size()); 1124 ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath); 1125 ResultPath = ExactSizedPath; 1126 } 1127 1128 if (!ErrorCode) { 1129 fs::UniqueID D1, D2; 1130 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); 1131 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); 1132 ASSERT_EQ(D1, D2); 1133 } 1134 ::close(FileDescriptor); 1135 } 1136 1137 TEST_F(FileSystemTest, PathFromFDUnicode) { 1138 // Create a temp file. 1139 int FileDescriptor; 1140 SmallString<64> TempPath; 1141 1142 // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0" 1143 ASSERT_NO_ERROR( 1144 fs::createTemporaryFile("\xCF\x80r\xC2\xB2", 1145 "\xE2\x84\xB5.0", FileDescriptor, TempPath)); 1146 FileRemover Cleanup(TempPath); 1147 1148 // Make sure it exists. 1149 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1150 1151 SmallVector<char, 8> ResultPath; 1152 std::error_code ErrorCode = 1153 fs::getPathFromOpenFD(FileDescriptor, ResultPath); 1154 1155 if (!ErrorCode) { 1156 fs::UniqueID D1, D2; 1157 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); 1158 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); 1159 ASSERT_EQ(D1, D2); 1160 } 1161 ::close(FileDescriptor); 1162 } 1163 1164 TEST_F(FileSystemTest, OpenFileForRead) { 1165 // Create a temp file. 1166 int FileDescriptor; 1167 SmallString<64> TempPath; 1168 ASSERT_NO_ERROR( 1169 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); 1170 FileRemover Cleanup(TempPath); 1171 1172 // Make sure it exists. 1173 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1174 1175 // Open the file for read 1176 int FileDescriptor2; 1177 SmallString<64> ResultPath; 1178 ASSERT_NO_ERROR( 1179 fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath)) 1180 1181 // If we succeeded, check that the paths are the same (modulo case): 1182 if (!ResultPath.empty()) { 1183 // The paths returned by createTemporaryFile and getPathFromOpenFD 1184 // should reference the same file on disk. 1185 fs::UniqueID D1, D2; 1186 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); 1187 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); 1188 ASSERT_EQ(D1, D2); 1189 } 1190 1191 ::close(FileDescriptor); 1192 } 1193 1194 #define CHECK_UNSUPPORTED() \ 1195 do { \ 1196 if (isUnsupportedOSOrEnvironment()) \ 1197 return; \ 1198 } while (0); \ 1199 1200 TEST_F(FileSystemTest, is_local) { 1201 CHECK_UNSUPPORTED(); 1202 1203 SmallString<128> CurrentPath; 1204 ASSERT_NO_ERROR(fs::current_path(CurrentPath)); 1205 1206 bool Result; 1207 ASSERT_NO_ERROR(fs::is_local(CurrentPath, Result)); 1208 EXPECT_TRUE(Result); 1209 EXPECT_TRUE(fs::is_local(CurrentPath)); 1210 1211 int FD; 1212 SmallString<64> TempPath; 1213 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); 1214 FileRemover Cleanup(TempPath); 1215 1216 // Make sure it exists. 1217 ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); 1218 1219 ASSERT_NO_ERROR(fs::is_local(FD, Result)); 1220 EXPECT_TRUE(Result); 1221 EXPECT_TRUE(fs::is_local(FD)); 1222 } 1223 1224 TEST_F(FileSystemTest, set_current_path) { 1225 SmallString<128> path; 1226 1227 ASSERT_NO_ERROR(fs::current_path(path)); 1228 ASSERT_NE(TestDirectory, path); 1229 1230 struct RestorePath { 1231 SmallString<128> path; 1232 RestorePath(const SmallString<128> &path) : path(path) {} 1233 ~RestorePath() { fs::set_current_path(path); } 1234 } restore_path(path); 1235 1236 ASSERT_NO_ERROR(fs::set_current_path(TestDirectory)); 1237 1238 ASSERT_NO_ERROR(fs::current_path(path)); 1239 1240 fs::UniqueID D1, D2; 1241 ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory, D1)); 1242 ASSERT_NO_ERROR(fs::getUniqueID(path, D2)); 1243 ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path; 1244 } 1245 1246 } // anonymous namespace 1247