1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
25 #include "llvm/ADT/ArrayRef.h"
35 using namespace llvm::sys;
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()); \
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(' '));
57 EXPECT_TRUE(path::is_separator('\\'));
59 EXPECT_FALSE(path::is_separator('\\'));
64 SmallVector<StringRef, 40> paths;
67 paths.push_back("..");
68 paths.push_back("foo");
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");
106 SmallVector<StringRef, 5> ComponentStack;
107 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
111 for (sys::path::const_iterator ci = sys::path::begin(*i),
112 ce = sys::path::end(*i);
115 ASSERT_FALSE(ci->empty());
116 ComponentStack.push_back(*ci);
119 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
120 ce = sys::path::rend(*i);
123 ASSERT_TRUE(*ci == ComponentStack.back());
124 ComponentStack.pop_back();
126 ASSERT_TRUE(ComponentStack.empty());
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);
147 SmallString<128> temp_store;
149 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
151 path::remove_filename(temp_store);
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());
160 path::native(*i, temp_store);
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);
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;
175 StringRef(Path).split(ExpectedPathComponents, '/');
177 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
179 ActualPathComponents.push_back(*I);
182 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
184 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
185 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
189 TEST(Support, RelativePathDotIterator) {
190 SmallString<64> Path(StringRef(".c/.d/../."));
191 typedef SmallVector<StringRef, 4> PathComponents;
192 PathComponents ExpectedPathComponents;
193 PathComponents ActualPathComponents;
195 StringRef(Path).split(ExpectedPathComponents, '/');
197 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
199 ActualPathComponents.push_back(*I);
202 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
204 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
205 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
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;
215 StringRef(Path).split(ExpectedPathComponents, '/');
217 // The root path will also be a component when iterating
218 ExpectedPathComponents[0] = "/";
220 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
222 ActualPathComponents.push_back(*I);
225 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
227 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
228 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
232 TEST(Support, AbsolutePathDotIterator) {
233 SmallString<64> Path(StringRef("/.c/.d/../."));
234 typedef SmallVector<StringRef, 4> PathComponents;
235 PathComponents ExpectedPathComponents;
236 PathComponents ActualPathComponents;
238 StringRef(Path).split(ExpectedPathComponents, '/');
240 // The root path will also be a component when iterating
241 ExpectedPathComponents[0] = "/";
243 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
245 ActualPathComponents.push_back(*I);
248 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
250 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
251 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
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;
262 StringRef(Path).split(ExpectedPathComponents, "\\");
264 // The root path (which comes after the drive name) will also be a component
266 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
268 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
270 ActualPathComponents.push_back(*I);
273 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
275 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
276 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
279 #endif // LLVM_ON_WIN32
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//");
288 Paths.push_back("c:\\\\");
291 for (StringRef Path : Paths) {
292 StringRef LastComponent = *path::rbegin(Path);
293 EXPECT_EQ(".", LastComponent);
296 SmallVector<StringRef, 3> RootPaths;
297 RootPaths.push_back("/");
298 RootPaths.push_back("//net/");
300 RootPaths.push_back("c:\\");
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]));
310 TEST(Support, HomeDirectory) {
311 std::string expected;
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);
320 if (char const *path = ::getenv("HOME"))
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);
329 EXPECT_EQ(expected, HomeDir);
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());
340 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "")); // should succeed
341 EXPECT_EQ(CacheDir, CacheDir2); // and return same paths
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;
350 // Test Unicode: "<user_cache_dir>/(pi)r^2/aleth.0"
351 EXPECT_TRUE(path::user_cache_directory(CacheDir2, "\xCF\x80r\xC2\xB2",
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;
358 EXPECT_EQ(ParentDir, ParentDir2);
362 TEST(Support, TempDirectory) {
363 SmallString<32> TempDir;
364 path::system_temp_directory(false, TempDir);
365 EXPECT_TRUE(!TempDir.empty());
367 path::system_temp_directory(true, TempDir);
368 EXPECT_TRUE(!TempDir.empty());
372 static std::string path2regex(std::string Path) {
374 while ((Pos = Path.find('\\', Pos)) != std::string::npos) {
375 Path.replace(Pos, 1, "\\\\");
381 /// Helper for running temp dir test in separated process. See below.
382 #define EXPECT_TEMP_DIR(prepare, expected) \
386 SmallString<300> TempDir; \
387 path::system_temp_directory(true, TempDir); \
388 raw_os_ostream(std::cerr) << TempDir; \
391 ::testing::ExitedWithCode(0), path2regex(expected))
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");
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");
406 // Test $TMP empty, $TEMP set.
409 _wputenv_s(L"TMP", L"");
410 _wputenv_s(L"TEMP", L"C:\\Valid\\Path");
414 // All related env vars empty
417 _wputenv_s(L"TMP", L"");
418 _wputenv_s(L"TEMP", L"");
419 _wputenv_s(L"USERPROFILE", L"");
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());
432 class FileSystemTest : public testing::Test {
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;
438 void SetUp() override {
440 fs::createUniqueDirectory("file-system-test", TestDirectory));
441 // We don't care about this specific file.
442 errs() << "Test Directory: " << TestDirectory << '\n';
446 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
449 TEST_F(FileSystemTest, Unique) {
450 // Create a temp file.
452 SmallString<64> TempPath;
454 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
456 // The same file should return an identical unique id.
458 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
459 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
462 // Different files should return different unique ids.
464 SmallString<64> TempPath2;
466 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
469 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
471 ::close(FileDescriptor2);
473 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
475 // Two paths representing the same file on disk should still provide the
476 // same unique id. We can test this by making a hard link.
477 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
479 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
482 ::close(FileDescriptor);
484 SmallString<128> Dir1;
486 fs::createUniqueDirectory("dir1", Dir1));
487 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
488 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
491 SmallString<128> Dir2;
493 fs::createUniqueDirectory("dir2", Dir2));
494 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
496 ASSERT_NO_ERROR(fs::remove(Dir1));
497 ASSERT_NO_ERROR(fs::remove(Dir2));
498 ASSERT_NO_ERROR(fs::remove(TempPath2));
499 ASSERT_NO_ERROR(fs::remove(TempPath));
502 TEST_F(FileSystemTest, RealPath) {
504 fs::create_directories(Twine(TestDirectory) + "/test1/test2/test3"));
505 ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/test1/test2/test3"));
507 SmallString<64> RealBase;
508 SmallString<64> Expected;
509 SmallString<64> Actual;
511 // TestDirectory itself might be under a symlink or have been specified with
512 // a different case than the existing temp directory. In such cases real_path
513 // on the concatenated path will differ in the TestDirectory portion from
514 // how we specified it. Make sure to compare against the real_path of the
515 // TestDirectory, and not just the value of TestDirectory.
516 ASSERT_NO_ERROR(fs::real_path(TestDirectory, RealBase));
517 path::native(Twine(RealBase) + "/test1/test2", Expected);
519 ASSERT_NO_ERROR(fs::real_path(
520 Twine(TestDirectory) + "/././test1/../test1/test2/./test3/..", Actual));
522 EXPECT_EQ(Expected, Actual);
524 SmallString<64> HomeDir;
525 bool Result = llvm::sys::path::home_directory(HomeDir);
527 ASSERT_NO_ERROR(fs::real_path(HomeDir, Expected));
528 ASSERT_NO_ERROR(fs::real_path("~", Actual, true));
529 EXPECT_EQ(Expected, Actual);
530 ASSERT_NO_ERROR(fs::real_path("~/", Actual, true));
531 EXPECT_EQ(Expected, Actual);
534 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
537 TEST_F(FileSystemTest, TempFiles) {
538 // Create a temp file.
540 SmallString<64> TempPath;
542 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
544 // Make sure it exists.
545 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
547 // Create another temp tile.
549 SmallString<64> TempPath2;
550 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
551 ASSERT_TRUE(TempPath2.endswith(".temp"));
552 ASSERT_NE(TempPath.str(), TempPath2.str());
554 fs::file_status A, B;
555 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
556 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
557 EXPECT_FALSE(fs::equivalent(A, B));
562 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
563 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
564 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
565 errc::no_such_file_or_directory);
567 std::error_code EC = fs::status(TempPath2.c_str(), B);
568 EXPECT_EQ(EC, errc::no_such_file_or_directory);
569 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
571 // Make sure Temp2 doesn't exist.
572 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
573 errc::no_such_file_or_directory);
575 SmallString<64> TempPath3;
576 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
577 ASSERT_FALSE(TempPath3.endswith("."));
578 FileRemover Cleanup3(TempPath3);
580 // Create a hard link to Temp1.
581 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
583 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
585 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
586 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
587 EXPECT_TRUE(fs::equivalent(A, B));
590 ::close(FileDescriptor);
591 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
593 // Remove the hard link.
594 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
596 // Make sure Temp1 doesn't exist.
597 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
598 errc::no_such_file_or_directory);
601 // Path name > 260 chars should get an error.
602 const char *Path270 =
603 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
604 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
605 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
606 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
607 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
608 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
609 errc::invalid_argument);
610 // Relative path < 247 chars, no problem.
611 const char *Path216 =
612 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
613 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
614 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
615 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
616 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
617 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
621 TEST_F(FileSystemTest, CreateDir) {
622 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
623 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
624 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
626 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
629 // Set a 0000 umask so that we can test our directory permissions.
630 mode_t OldUmask = ::umask(0000);
632 fs::file_status Status;
634 fs::create_directory(Twine(TestDirectory) + "baz500", false,
635 fs::perms::owner_read | fs::perms::owner_exe));
636 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
637 ASSERT_EQ(Status.permissions() & fs::perms::all_all,
638 fs::perms::owner_read | fs::perms::owner_exe);
639 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
640 fs::perms::all_all));
641 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
642 ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
644 // Restore umask to be safe.
649 // Prove that create_directories() can handle a pathname > 248 characters,
650 // which is the documented limit for CreateDirectory().
651 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
652 // Generate a directory path guaranteed to fall into that range.
653 size_t TmpLen = TestDirectory.size();
654 const char *OneDir = "\\123456789";
655 size_t OneDirLen = strlen(OneDir);
656 ASSERT_LT(OneDirLen, 12U);
657 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
658 SmallString<260> LongDir(TestDirectory);
659 for (size_t I = 0; I < NLevels; ++I)
660 LongDir.append(OneDir);
661 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
662 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
663 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
665 // Tidy up, "recursively" removing the directories.
666 StringRef ThisDir(LongDir);
667 for (size_t J = 0; J < NLevels; ++J) {
668 ASSERT_NO_ERROR(fs::remove(ThisDir));
669 ThisDir = path::parent_path(ThisDir);
672 // Similarly for a relative pathname. Need to set the current directory to
673 // TestDirectory so that the one we create ends up in the right place.
674 char PreviousDir[260];
675 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
676 ASSERT_GT(PreviousDirLen, 0U);
677 ASSERT_LT(PreviousDirLen, 260U);
678 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
680 // Generate a relative directory name with absolute length > 248.
681 size_t LongDirLen = 249 - TestDirectory.size();
682 LongDir.assign(LongDirLen, 'a');
683 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
684 // While we're here, prove that .. and . handling works in these long paths.
685 const char *DotDotDirs = "\\..\\.\\b";
686 LongDir.append(DotDotDirs);
687 ASSERT_NO_ERROR(fs::create_directory("b"));
688 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
690 ASSERT_NO_ERROR(fs::remove("b"));
691 ASSERT_NO_ERROR(fs::remove(
692 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
693 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
697 TEST_F(FileSystemTest, DirectoryIteration) {
699 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
702 // Create a known hierarchy to recurse over.
704 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
706 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
707 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
708 "/recursive/dontlookhere/da1"));
710 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
712 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
713 typedef std::vector<std::string> v_t;
715 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
716 + "/recursive", ec), e; i != e; i.increment(ec)){
718 if (path::filename(i->path()) == "p1") {
720 // FIXME: recursive_directory_iterator should be more robust.
723 if (path::filename(i->path()) == "dontlookhere")
725 visited.push_back(path::filename(i->path()));
727 v_t::const_iterator a0 = find(visited, "a0");
728 v_t::const_iterator aa1 = find(visited, "aa1");
729 v_t::const_iterator ab1 = find(visited, "ab1");
730 v_t::const_iterator dontlookhere = find(visited, "dontlookhere");
731 v_t::const_iterator da1 = find(visited, "da1");
732 v_t::const_iterator z0 = find(visited, "z0");
733 v_t::const_iterator za1 = find(visited, "za1");
734 v_t::const_iterator pop = find(visited, "pop");
735 v_t::const_iterator p1 = find(visited, "p1");
737 // Make sure that each path was visited correctly.
738 ASSERT_NE(a0, visited.end());
739 ASSERT_NE(aa1, visited.end());
740 ASSERT_NE(ab1, visited.end());
741 ASSERT_NE(dontlookhere, visited.end());
742 ASSERT_EQ(da1, visited.end()); // Not visited.
743 ASSERT_NE(z0, visited.end());
744 ASSERT_NE(za1, visited.end());
745 ASSERT_NE(pop, visited.end());
746 ASSERT_EQ(p1, visited.end()); // Not visited.
748 // Make sure that parents were visited before children. No other ordering
749 // guarantees can be made across siblings.
754 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
755 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
756 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
758 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
759 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
760 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
761 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
762 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
763 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
764 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
766 // Test recursive_directory_iterator level()
768 fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c"));
769 fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E;
770 for (int l = 0; I != E; I.increment(ec), ++l) {
772 EXPECT_EQ(I.level(), l);
775 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c"));
776 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b"));
777 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a"));
778 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel"));
781 TEST_F(FileSystemTest, Remove) {
782 SmallString<64> BaseDir;
783 SmallString<64> Paths[4];
785 ASSERT_NO_ERROR(fs::createUniqueDirectory("fs_remove", BaseDir));
787 ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/baz"));
788 ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/buzz"));
789 ASSERT_NO_ERROR(fs::createUniqueFile(
790 Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[0], Paths[0]));
791 ASSERT_NO_ERROR(fs::createUniqueFile(
792 Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[1], Paths[1]));
793 ASSERT_NO_ERROR(fs::createUniqueFile(
794 Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[2], Paths[2]));
795 ASSERT_NO_ERROR(fs::createUniqueFile(
796 Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[3], Paths[3]));
801 EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/baz"));
802 EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/buzz"));
803 EXPECT_TRUE(fs::exists(Paths[0]));
804 EXPECT_TRUE(fs::exists(Paths[1]));
805 EXPECT_TRUE(fs::exists(Paths[2]));
806 EXPECT_TRUE(fs::exists(Paths[3]));
808 ASSERT_NO_ERROR(fs::remove_directories("D:/footest"));
810 ASSERT_NO_ERROR(fs::remove_directories(BaseDir));
811 ASSERT_FALSE(fs::exists(BaseDir));
814 const char archive[] = "!<arch>\x0A";
815 const char bitcode[] = "\xde\xc0\x17\x0b";
816 const char coff_object[] = "\x00\x00......";
817 const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
818 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
819 const char coff_import_library[] = "\x00\x00\xff\xff....";
820 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
821 0, 0, 0, 0, 0, 0, 0, 0, 1 };
822 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
823 const char macho_object[] =
824 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
825 const char macho_executable[] =
826 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
827 const char macho_fixed_virtual_memory_shared_lib[] =
828 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
829 const char macho_core[] =
830 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
831 const char macho_preload_executable[] =
832 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
833 const char macho_dynamically_linked_shared_lib[] =
834 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
835 const char macho_dynamic_linker[] =
836 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
837 const char macho_bundle[] =
838 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
839 const char macho_dsym_companion[] =
840 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
841 const char macho_kext_bundle[] =
842 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
843 const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
844 const char macho_dynamically_linked_shared_lib_stub[] =
845 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
847 TEST_F(FileSystemTest, Magic) {
849 const char *filename;
850 const char *magic_str;
851 size_t magic_str_len;
852 fs::file_magic magic;
854 #define DEFINE(magic) \
855 { #magic, magic, sizeof(magic), fs::file_magic::magic }
859 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
860 DEFINE(coff_import_library),
861 DEFINE(elf_relocatable),
862 DEFINE(macho_universal_binary),
863 DEFINE(macho_object),
864 DEFINE(macho_executable),
865 DEFINE(macho_fixed_virtual_memory_shared_lib),
867 DEFINE(macho_preload_executable),
868 DEFINE(macho_dynamically_linked_shared_lib),
869 DEFINE(macho_dynamic_linker),
870 DEFINE(macho_bundle),
871 DEFINE(macho_dynamically_linked_shared_lib_stub),
872 DEFINE(macho_dsym_companion),
873 DEFINE(macho_kext_bundle),
874 DEFINE(windows_resource)
878 // Create some files filled with magic.
879 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
881 SmallString<128> file_pathname(TestDirectory);
882 path::append(file_pathname, i->filename);
884 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
885 ASSERT_FALSE(file.has_error());
886 StringRef magic(i->magic_str, i->magic_str_len);
889 EXPECT_EQ(i->magic, fs::identify_magic(magic));
890 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
895 TEST_F(FileSystemTest, CarriageReturn) {
896 SmallString<128> FilePathname(TestDirectory);
898 path::append(FilePathname, "test");
901 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
906 auto Buf = MemoryBuffer::getFile(FilePathname.str());
907 EXPECT_TRUE((bool)Buf);
908 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
912 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
917 auto Buf = MemoryBuffer::getFile(FilePathname.str());
918 EXPECT_TRUE((bool)Buf);
919 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
921 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
925 TEST_F(FileSystemTest, Resize) {
927 SmallString<64> TempPath;
928 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
929 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
930 fs::file_status Status;
931 ASSERT_NO_ERROR(fs::status(FD, Status));
932 ASSERT_EQ(Status.getSize(), 123U);
934 ASSERT_NO_ERROR(fs::remove(TempPath));
937 TEST_F(FileSystemTest, FileMapping) {
938 // Create a temp file.
940 SmallString<64> TempPath;
942 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
943 unsigned Size = 4096;
944 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
946 // Map in temp file and add some content
948 StringRef Val("hello there");
950 fs::mapped_file_region mfr(FileDescriptor,
951 fs::mapped_file_region::readwrite, Size, 0, EC);
953 std::copy(Val.begin(), Val.end(), mfr.data());
954 // Explicitly add a 0.
955 mfr.data()[Val.size()] = 0;
958 ASSERT_EQ(close(FileDescriptor), 0);
960 // Map it back in read-only
963 EC = fs::openFileForRead(Twine(TempPath), FD);
965 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
969 EXPECT_EQ(StringRef(mfr.const_data()), Val);
972 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
974 ASSERT_EQ(close(FD), 0);
976 ASSERT_NO_ERROR(fs::remove(TempPath));
979 TEST(Support, NormalizePath) {
980 #if defined(LLVM_ON_WIN32)
981 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
982 EXPECT_EQ(path__, windows__);
984 #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
985 EXPECT_EQ(path__, not_windows__);
988 SmallString<64> Path1("a");
989 SmallString<64> Path2("a/b");
990 SmallString<64> Path3("a\\b");
991 SmallString<64> Path4("a\\\\b");
992 SmallString<64> Path5("\\a");
993 SmallString<64> Path6("a\\");
996 EXPECT_PATH_IS(Path1, "a", "a");
999 EXPECT_PATH_IS(Path2, "a\\b", "a/b");
1001 path::native(Path3);
1002 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
1004 path::native(Path4);
1005 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
1007 path::native(Path5);
1008 EXPECT_PATH_IS(Path5, "\\a", "/a");
1010 path::native(Path6);
1011 EXPECT_PATH_IS(Path6, "a\\", "a/");
1013 #undef EXPECT_PATH_IS
1015 #if defined(LLVM_ON_WIN32)
1016 SmallString<64> PathHome;
1017 path::home_directory(PathHome);
1019 const char *Path7a = "~/aaa";
1020 SmallString<64> Path7(Path7a);
1021 path::native(Path7);
1022 EXPECT_TRUE(Path7.endswith("\\aaa"));
1023 EXPECT_TRUE(Path7.startswith(PathHome));
1024 EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1));
1026 const char *Path8a = "~";
1027 SmallString<64> Path8(Path8a);
1028 path::native(Path8);
1029 EXPECT_EQ(Path8, PathHome);
1031 const char *Path9a = "~aaa";
1032 SmallString<64> Path9(Path9a);
1033 path::native(Path9);
1034 EXPECT_EQ(Path9, "~aaa");
1036 const char *Path10a = "aaa/~/b";
1037 SmallString<64> Path10(Path10a);
1038 path::native(Path10);
1039 EXPECT_EQ(Path10, "aaa\\~\\b");
1043 TEST(Support, RemoveLeadingDotSlash) {
1044 StringRef Path1("././/foolz/wat");
1045 StringRef Path2("./////");
1047 Path1 = path::remove_leading_dotslash(Path1);
1048 EXPECT_EQ(Path1, "foolz/wat");
1049 Path2 = path::remove_leading_dotslash(Path2);
1050 EXPECT_EQ(Path2, "");
1053 static std::string remove_dots(StringRef path,
1054 bool remove_dot_dot) {
1055 SmallString<256> buffer(path);
1056 path::remove_dots(buffer, remove_dot_dot);
1057 return buffer.str();
1060 TEST(Support, RemoveDots) {
1061 #if defined(LLVM_ON_WIN32)
1062 EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false));
1063 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false));
1065 EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false));
1066 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true));
1067 EXPECT_EQ("c", remove_dots(".\\.\\c", true));
1068 EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true));
1069 EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true));
1071 SmallString<64> Path1(".\\.\\c");
1072 EXPECT_TRUE(path::remove_dots(Path1, true));
1073 EXPECT_EQ("c", Path1);
1075 EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false));
1076 EXPECT_EQ("", remove_dots("./////", false));
1078 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false));
1079 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true));
1080 EXPECT_EQ("c", remove_dots("././c", true));
1081 EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true));
1082 EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true));
1083 EXPECT_EQ("/a/c", remove_dots("/../../a/c", true));
1084 EXPECT_EQ("/a/c", remove_dots("/../a/b//../././/c", true));
1086 SmallString<64> Path1("././c");
1087 EXPECT_TRUE(path::remove_dots(Path1, true));
1088 EXPECT_EQ("c", Path1);
1092 TEST(Support, ReplacePathPrefix) {
1093 SmallString<64> Path1("/foo");
1094 SmallString<64> Path2("/old/foo");
1095 SmallString<64> OldPrefix("/old");
1096 SmallString<64> NewPrefix("/new");
1097 SmallString<64> NewPrefix2("/longernew");
1098 SmallString<64> EmptyPrefix("");
1100 SmallString<64> Path = Path1;
1101 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1102 EXPECT_EQ(Path, "/foo");
1104 path::replace_path_prefix(Path, OldPrefix, NewPrefix);
1105 EXPECT_EQ(Path, "/new/foo");
1107 path::replace_path_prefix(Path, OldPrefix, NewPrefix2);
1108 EXPECT_EQ(Path, "/longernew/foo");
1110 path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
1111 EXPECT_EQ(Path, "/new/foo");
1113 path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
1114 EXPECT_EQ(Path, "/foo");
1117 TEST_F(FileSystemTest, PathFromFD) {
1118 // Create a temp file.
1120 SmallString<64> TempPath;
1122 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1123 FileRemover Cleanup(TempPath);
1125 // Make sure it exists.
1126 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1128 // Try to get the path from the file descriptor
1129 SmallString<64> ResultPath;
1130 std::error_code ErrorCode =
1131 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1133 // If we succeeded, check that the paths are the same (modulo case):
1135 // The paths returned by createTemporaryFile and getPathFromOpenFD
1136 // should reference the same file on disk.
1137 fs::UniqueID D1, D2;
1138 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1139 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1143 ::close(FileDescriptor);
1146 TEST_F(FileSystemTest, PathFromFDWin32) {
1147 // Create a temp file.
1149 SmallString<64> TempPath;
1151 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1152 FileRemover Cleanup(TempPath);
1154 // Make sure it exists.
1155 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1157 SmallVector<char, 8> ResultPath;
1158 std::error_code ErrorCode =
1159 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1162 // Now that we know how much space is required for the path, create a path
1163 // buffer with exactly enough space (sans null terminator, which should not
1164 // be present), and call getPathFromOpenFD again to ensure that the API
1165 // properly handles exactly-sized buffers.
1166 SmallVector<char, 8> ExactSizedPath(ResultPath.size());
1167 ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath);
1168 ResultPath = ExactSizedPath;
1172 fs::UniqueID D1, D2;
1173 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1174 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1177 ::close(FileDescriptor);
1180 TEST_F(FileSystemTest, PathFromFDUnicode) {
1181 // Create a temp file.
1183 SmallString<64> TempPath;
1185 // Test Unicode: "<temp directory>/(pi)r^2<temp rand chars>.aleth.0"
1187 fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
1188 "\xE2\x84\xB5.0", FileDescriptor, TempPath));
1189 FileRemover Cleanup(TempPath);
1191 // Make sure it exists.
1192 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1194 SmallVector<char, 8> ResultPath;
1195 std::error_code ErrorCode =
1196 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1199 fs::UniqueID D1, D2;
1200 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1201 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1204 ::close(FileDescriptor);
1207 TEST_F(FileSystemTest, OpenFileForRead) {
1208 // Create a temp file.
1210 SmallString<64> TempPath;
1212 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1213 FileRemover Cleanup(TempPath);
1215 // Make sure it exists.
1216 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1218 // Open the file for read
1219 int FileDescriptor2;
1220 SmallString<64> ResultPath;
1222 fs::openFileForRead(Twine(TempPath), FileDescriptor2, &ResultPath))
1224 // If we succeeded, check that the paths are the same (modulo case):
1225 if (!ResultPath.empty()) {
1226 // The paths returned by createTemporaryFile and getPathFromOpenFD
1227 // should reference the same file on disk.
1228 fs::UniqueID D1, D2;
1229 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1230 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1234 ::close(FileDescriptor);
1237 TEST_F(FileSystemTest, set_current_path) {
1238 SmallString<128> path;
1240 ASSERT_NO_ERROR(fs::current_path(path));
1241 ASSERT_NE(TestDirectory, path);
1243 struct RestorePath {
1244 SmallString<128> path;
1245 RestorePath(const SmallString<128> &path) : path(path) {}
1246 ~RestorePath() { fs::set_current_path(path); }
1247 } restore_path(path);
1249 ASSERT_NO_ERROR(fs::set_current_path(TestDirectory));
1251 ASSERT_NO_ERROR(fs::current_path(path));
1253 fs::UniqueID D1, D2;
1254 ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory, D1));
1255 ASSERT_NO_ERROR(fs::getUniqueID(path, D2));
1256 ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path;
\r
1259 TEST_F(FileSystemTest, permissions) {
\r
1261 SmallString<64> TempPath;
\r
1262 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
\r
1263 FileRemover Cleanup(TempPath);
\r
1265 // Make sure it exists.
\r
1266 ASSERT_TRUE(fs::exists(Twine(TempPath)));
\r
1268 auto CheckPermissions = [&](fs::perms Expected) {
\r
1269 ErrorOr<fs::perms> Actual = fs::getPermissions(TempPath);
\r
1270 return Actual && *Actual == Expected;
\r
1273 std::error_code NoError;
\r
1274 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all), NoError);
\r
1275 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1277 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::all_exe), NoError);
\r
1278 EXPECT_TRUE(CheckPermissions(fs::all_read | fs::all_exe));
\r
1280 #if defined(LLVM_ON_WIN32)
\r
1281 fs::perms ReadOnly = fs::all_read | fs::all_exe;
\r
1282 EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
\r
1283 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1285 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
\r
1286 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1288 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
\r
1289 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1291 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
\r
1292 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1294 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
\r
1295 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1297 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
\r
1298 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1300 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
\r
1301 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1303 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
\r
1304 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1306 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
\r
1307 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1309 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
\r
1310 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1312 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
\r
1313 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1315 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
\r
1316 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1318 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
\r
1319 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1321 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
\r
1322 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1324 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
\r
1325 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1327 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
\r
1328 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1330 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
\r
1331 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1333 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
\r
1334 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1336 EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
\r
1337 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1339 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
\r
1340 fs::set_gid_on_exe |
\r
1343 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1345 EXPECT_EQ(fs::setPermissions(TempPath, ReadOnly | fs::set_uid_on_exe |
\r
1346 fs::set_gid_on_exe |
\r
1349 EXPECT_TRUE(CheckPermissions(ReadOnly));
\r
1351 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all | fs::set_uid_on_exe |
\r
1352 fs::set_gid_on_exe |
\r
1355 EXPECT_TRUE(CheckPermissions(fs::all_all));
\r
1357 EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
\r
1358 EXPECT_TRUE(CheckPermissions(fs::no_perms));
\r
1360 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
\r
1361 EXPECT_TRUE(CheckPermissions(fs::owner_read));
\r
1363 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
\r
1364 EXPECT_TRUE(CheckPermissions(fs::owner_write));
\r
1366 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
\r
1367 EXPECT_TRUE(CheckPermissions(fs::owner_exe));
\r
1369 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
\r
1370 EXPECT_TRUE(CheckPermissions(fs::owner_all));
\r
1372 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
\r
1373 EXPECT_TRUE(CheckPermissions(fs::group_read));
\r
1375 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
\r
1376 EXPECT_TRUE(CheckPermissions(fs::group_write));
\r
1378 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
\r
1379 EXPECT_TRUE(CheckPermissions(fs::group_exe));
\r
1381 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
\r
1382 EXPECT_TRUE(CheckPermissions(fs::group_all));
\r
1384 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
\r
1385 EXPECT_TRUE(CheckPermissions(fs::others_read));
\r
1387 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
\r
1388 EXPECT_TRUE(CheckPermissions(fs::others_write));
\r
1390 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
\r
1391 EXPECT_TRUE(CheckPermissions(fs::others_exe));
\r
1393 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
\r
1394 EXPECT_TRUE(CheckPermissions(fs::others_all));
\r
1396 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
\r
1397 EXPECT_TRUE(CheckPermissions(fs::all_read));
\r
1399 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
\r
1400 EXPECT_TRUE(CheckPermissions(fs::all_write));
\r
1402 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
\r
1403 EXPECT_TRUE(CheckPermissions(fs::all_exe));
\r
1405 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
\r
1406 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe));
\r
1408 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
\r
1409 EXPECT_TRUE(CheckPermissions(fs::set_gid_on_exe));
\r
1411 EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
\r
1412 EXPECT_TRUE(CheckPermissions(fs::sticky_bit));
\r
1414 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
\r
1415 fs::set_gid_on_exe |
\r
1418 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe | fs::set_gid_on_exe |
\r
1421 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::set_uid_on_exe |
\r
1422 fs::set_gid_on_exe |
\r
1425 EXPECT_TRUE(CheckPermissions(fs::all_read | fs::set_uid_on_exe |
\r
1426 fs::set_gid_on_exe | fs::sticky_bit));
\r
1428 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all | fs::set_uid_on_exe |
\r
1429 fs::set_gid_on_exe |
\r
1432 EXPECT_TRUE(CheckPermissions(fs::all_all | fs::set_uid_on_exe |
\r
1433 fs::set_gid_on_exe | fs::sticky_bit));
\r
1437 } // anonymous namespace
\r