1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Path.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/BinaryFormat/Magic.h"
14 #include "llvm/Config/llvm-config.h"
15 #include "llvm/Support/ConvertUTF.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/FileUtilities.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "gtest/gtest.h"
24 #include "gmock/gmock.h"
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/Support/Chrono.h"
39 using namespace llvm::sys
;
41 #define ASSERT_NO_ERROR(x) \
42 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
43 SmallString<128> MessageStorage; \
44 raw_svector_ostream Message(MessageStorage); \
45 Message << #x ": did not return errc::success.\n" \
46 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
47 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
48 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
52 #define ASSERT_ERROR(x) \
54 SmallString<128> MessageStorage; \
55 raw_svector_ostream Message(MessageStorage); \
56 Message << #x ": did not return a failure error code.\n"; \
57 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
62 struct FileDescriptorCloser
{
63 explicit FileDescriptorCloser(int FD
) : FD(FD
) {}
64 ~FileDescriptorCloser() { ::close(FD
); }
68 TEST(is_separator
, Works
) {
69 EXPECT_TRUE(path::is_separator('/'));
70 EXPECT_FALSE(path::is_separator('\0'));
71 EXPECT_FALSE(path::is_separator('-'));
72 EXPECT_FALSE(path::is_separator(' '));
74 EXPECT_TRUE(path::is_separator('\\', path::Style::windows
));
75 EXPECT_FALSE(path::is_separator('\\', path::Style::posix
));
78 EXPECT_TRUE(path::is_separator('\\'));
80 EXPECT_FALSE(path::is_separator('\\'));
85 SmallVector
<StringRef
, 40> paths
;
88 paths
.push_back("..");
89 paths
.push_back("foo");
91 paths
.push_back("/foo");
92 paths
.push_back("foo/");
93 paths
.push_back("/foo/");
94 paths
.push_back("foo/bar");
95 paths
.push_back("/foo/bar");
96 paths
.push_back("//net");
97 paths
.push_back("//net/");
98 paths
.push_back("//net/foo");
99 paths
.push_back("///foo///");
100 paths
.push_back("///foo///bar");
101 paths
.push_back("/.");
102 paths
.push_back("./");
103 paths
.push_back("/..");
104 paths
.push_back("../");
105 paths
.push_back("foo/.");
106 paths
.push_back("foo/..");
107 paths
.push_back("foo/./");
108 paths
.push_back("foo/./bar");
109 paths
.push_back("foo/..");
110 paths
.push_back("foo/../");
111 paths
.push_back("foo/../bar");
112 paths
.push_back("c:");
113 paths
.push_back("c:/");
114 paths
.push_back("c:foo");
115 paths
.push_back("c:/foo");
116 paths
.push_back("c:foo/");
117 paths
.push_back("c:/foo/");
118 paths
.push_back("c:/foo/bar");
119 paths
.push_back("prn:");
120 paths
.push_back("c:\\");
121 paths
.push_back("c:foo");
122 paths
.push_back("c:\\foo");
123 paths
.push_back("c:foo\\");
124 paths
.push_back("c:\\foo\\");
125 paths
.push_back("c:\\foo/");
126 paths
.push_back("c:/foo\\bar");
128 for (SmallVector
<StringRef
, 40>::const_iterator i
= paths
.begin(),
133 SmallVector
<StringRef
, 5> ComponentStack
;
134 for (sys::path::const_iterator ci
= sys::path::begin(*i
),
135 ce
= sys::path::end(*i
);
138 EXPECT_FALSE(ci
->empty());
139 ComponentStack
.push_back(*ci
);
142 SmallVector
<StringRef
, 5> ReverseComponentStack
;
143 for (sys::path::reverse_iterator ci
= sys::path::rbegin(*i
),
144 ce
= sys::path::rend(*i
);
147 EXPECT_FALSE(ci
->empty());
148 ReverseComponentStack
.push_back(*ci
);
150 std::reverse(ReverseComponentStack
.begin(), ReverseComponentStack
.end());
151 EXPECT_THAT(ComponentStack
, testing::ContainerEq(ReverseComponentStack
));
153 // Crash test most of the API - since we're iterating over all of our paths
154 // here there isn't really anything reasonable to assert on in the results.
155 (void)path::has_root_path(*i
);
156 (void)path::root_path(*i
);
157 (void)path::has_root_name(*i
);
158 (void)path::root_name(*i
);
159 (void)path::has_root_directory(*i
);
160 (void)path::root_directory(*i
);
161 (void)path::has_parent_path(*i
);
162 (void)path::parent_path(*i
);
163 (void)path::has_filename(*i
);
164 (void)path::filename(*i
);
165 (void)path::has_stem(*i
);
166 (void)path::stem(*i
);
167 (void)path::has_extension(*i
);
168 (void)path::extension(*i
);
169 (void)path::is_absolute(*i
);
170 (void)path::is_relative(*i
);
172 SmallString
<128> temp_store
;
174 ASSERT_NO_ERROR(fs::make_absolute(temp_store
));
176 path::remove_filename(temp_store
);
179 path::replace_extension(temp_store
, "ext");
180 StringRef
filename(temp_store
.begin(), temp_store
.size()), stem
, ext
;
181 stem
= path::stem(filename
);
182 ext
= path::extension(filename
);
183 EXPECT_EQ(*sys::path::rbegin(filename
), (stem
+ ext
).str());
185 path::native(*i
, temp_store
);
188 SmallString
<32> Relative("foo.cpp");
189 sys::fs::make_absolute("/root", Relative
);
190 Relative
[5] = '/'; // Fix up windows paths.
191 ASSERT_EQ("/root/foo.cpp", Relative
);
194 TEST(Support
, FilenameParent
) {
195 EXPECT_EQ("/", path::filename("/"));
196 EXPECT_EQ("", path::parent_path("/"));
198 EXPECT_EQ("\\", path::filename("c:\\", path::Style::windows
));
199 EXPECT_EQ("c:", path::parent_path("c:\\", path::Style::windows
));
201 EXPECT_EQ("/", path::filename("///"));
202 EXPECT_EQ("", path::parent_path("///"));
204 EXPECT_EQ("\\", path::filename("c:\\\\", path::Style::windows
));
205 EXPECT_EQ("c:", path::parent_path("c:\\\\", path::Style::windows
));
207 EXPECT_EQ("bar", path::filename("/foo/bar"));
208 EXPECT_EQ("/foo", path::parent_path("/foo/bar"));
210 EXPECT_EQ("foo", path::filename("/foo"));
211 EXPECT_EQ("/", path::parent_path("/foo"));
213 EXPECT_EQ("foo", path::filename("foo"));
214 EXPECT_EQ("", path::parent_path("foo"));
216 EXPECT_EQ(".", path::filename("foo/"));
217 EXPECT_EQ("foo", path::parent_path("foo/"));
219 EXPECT_EQ("//net", path::filename("//net"));
220 EXPECT_EQ("", path::parent_path("//net"));
222 EXPECT_EQ("/", path::filename("//net/"));
223 EXPECT_EQ("//net", path::parent_path("//net/"));
225 EXPECT_EQ("foo", path::filename("//net/foo"));
226 EXPECT_EQ("//net/", path::parent_path("//net/foo"));
228 // These checks are just to make sure we do something reasonable with the
229 // paths below. They are not meant to prescribe the one true interpretation of
230 // these paths. Other decompositions (e.g. "//" -> "" + "//") are also
232 EXPECT_EQ("/", path::filename("//"));
233 EXPECT_EQ("", path::parent_path("//"));
235 EXPECT_EQ("\\", path::filename("\\\\", path::Style::windows
));
236 EXPECT_EQ("", path::parent_path("\\\\", path::Style::windows
));
238 EXPECT_EQ("\\", path::filename("\\\\\\", path::Style::windows
));
239 EXPECT_EQ("", path::parent_path("\\\\\\", path::Style::windows
));
242 static std::vector
<StringRef
>
243 GetComponents(StringRef Path
, path::Style S
= path::Style::native
) {
244 return {path::begin(Path
, S
), path::end(Path
)};
247 TEST(Support
, PathIterator
) {
248 EXPECT_THAT(GetComponents("/foo"), testing::ElementsAre("/", "foo"));
249 EXPECT_THAT(GetComponents("/"), testing::ElementsAre("/"));
250 EXPECT_THAT(GetComponents("//"), testing::ElementsAre("/"));
251 EXPECT_THAT(GetComponents("///"), testing::ElementsAre("/"));
252 EXPECT_THAT(GetComponents("c/d/e/foo.txt"),
253 testing::ElementsAre("c", "d", "e", "foo.txt"));
254 EXPECT_THAT(GetComponents(".c/.d/../."),
255 testing::ElementsAre(".c", ".d", "..", "."));
256 EXPECT_THAT(GetComponents("/c/d/e/foo.txt"),
257 testing::ElementsAre("/", "c", "d", "e", "foo.txt"));
258 EXPECT_THAT(GetComponents("/.c/.d/../."),
259 testing::ElementsAre("/", ".c", ".d", "..", "."));
260 EXPECT_THAT(GetComponents("c:\\c\\e\\foo.txt", path::Style::windows
),
261 testing::ElementsAre("c:", "\\", "c", "e", "foo.txt"));
262 EXPECT_THAT(GetComponents("//net/"), testing::ElementsAre("//net", "/"));
263 EXPECT_THAT(GetComponents("//net/c/foo.txt"),
264 testing::ElementsAre("//net", "/", "c", "foo.txt"));
267 TEST(Support
, AbsolutePathIteratorEnd
) {
268 // Trailing slashes are converted to '.' unless they are part of the root path.
269 SmallVector
<std::pair
<StringRef
, path::Style
>, 4> Paths
;
270 Paths
.emplace_back("/foo/", path::Style::native
);
271 Paths
.emplace_back("/foo//", path::Style::native
);
272 Paths
.emplace_back("//net/foo/", path::Style::native
);
273 Paths
.emplace_back("c:\\foo\\", path::Style::windows
);
275 for (auto &Path
: Paths
) {
276 SCOPED_TRACE(Path
.first
);
277 StringRef LastComponent
= *path::rbegin(Path
.first
, Path
.second
);
278 EXPECT_EQ(".", LastComponent
);
281 SmallVector
<std::pair
<StringRef
, path::Style
>, 3> RootPaths
;
282 RootPaths
.emplace_back("/", path::Style::native
);
283 RootPaths
.emplace_back("//net/", path::Style::native
);
284 RootPaths
.emplace_back("c:\\", path::Style::windows
);
285 RootPaths
.emplace_back("//net//", path::Style::native
);
286 RootPaths
.emplace_back("c:\\\\", path::Style::windows
);
288 for (auto &Path
: RootPaths
) {
289 SCOPED_TRACE(Path
.first
);
290 StringRef LastComponent
= *path::rbegin(Path
.first
, Path
.second
);
291 EXPECT_EQ(1u, LastComponent
.size());
292 EXPECT_TRUE(path::is_separator(LastComponent
[0], Path
.second
));
296 TEST(Support
, HomeDirectory
) {
297 std::string expected
;
299 if (wchar_t const *path
= ::_wgetenv(L
"USERPROFILE")) {
300 auto pathLen
= ::wcslen(path
);
301 ArrayRef
<char> ref
{reinterpret_cast<char const *>(path
),
302 pathLen
* sizeof(wchar_t)};
303 convertUTF16ToUTF8String(ref
, expected
);
306 if (char const *path
= ::getenv("HOME"))
309 // Do not try to test it if we don't know what to expect.
310 // On Windows we use something better than env vars.
311 if (!expected
.empty()) {
312 SmallString
<128> HomeDir
;
313 auto status
= path::home_directory(HomeDir
);
315 EXPECT_EQ(expected
, HomeDir
);
320 TEST(Support
, HomeDirectoryWithNoEnv
) {
321 std::string OriginalStorage
;
322 char const *OriginalEnv
= ::getenv("HOME");
324 // We're going to unset it, so make a copy and save a pointer to the copy
325 // so that we can reset it at the end of the test.
326 OriginalStorage
= OriginalEnv
;
327 OriginalEnv
= OriginalStorage
.c_str();
330 // Don't run the test if we have nothing to compare against.
331 struct passwd
*pw
= getpwuid(getuid());
332 if (!pw
|| !pw
->pw_dir
) return;
335 EXPECT_EQ(nullptr, ::getenv("HOME"));
336 std::string PwDir
= pw
->pw_dir
;
338 SmallString
<128> HomeDir
;
339 auto status
= path::home_directory(HomeDir
);
341 EXPECT_EQ(PwDir
, HomeDir
);
343 // Now put the environment back to its original state (meaning that if it was
344 // unset before, we don't reset it).
345 if (OriginalEnv
) ::setenv("HOME", OriginalEnv
, 1);
349 TEST(Support
, TempDirectory
) {
350 SmallString
<32> TempDir
;
351 path::system_temp_directory(false, TempDir
);
352 EXPECT_TRUE(!TempDir
.empty());
354 path::system_temp_directory(true, TempDir
);
355 EXPECT_TRUE(!TempDir
.empty());
359 static std::string
path2regex(std::string Path
) {
361 while ((Pos
= Path
.find('\\', Pos
)) != std::string::npos
) {
362 Path
.replace(Pos
, 1, "\\\\");
368 /// Helper for running temp dir test in separated process. See below.
369 #define EXPECT_TEMP_DIR(prepare, expected) \
373 SmallString<300> TempDir; \
374 path::system_temp_directory(true, TempDir); \
375 raw_os_ostream(std::cerr) << TempDir; \
378 ::testing::ExitedWithCode(0), path2regex(expected))
380 TEST(SupportDeathTest
, TempDirectoryOnWindows
) {
381 // In this test we want to check how system_temp_directory responds to
382 // different values of specific env vars. To prevent corrupting env vars of
383 // the current process all checks are done in separated processes.
384 EXPECT_TEMP_DIR(_wputenv_s(L
"TMP", L
"C:\\OtherFolder"), "C:\\OtherFolder");
385 EXPECT_TEMP_DIR(_wputenv_s(L
"TMP", L
"C:/Unix/Path/Seperators"),
386 "C:\\Unix\\Path\\Seperators");
387 EXPECT_TEMP_DIR(_wputenv_s(L
"TMP", L
"Local Path"), ".+\\Local Path$");
388 EXPECT_TEMP_DIR(_wputenv_s(L
"TMP", L
"F:\\TrailingSep\\"), "F:\\TrailingSep");
390 _wputenv_s(L
"TMP", L
"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"),
391 "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80");
393 // Test $TMP empty, $TEMP set.
396 _wputenv_s(L
"TMP", L
"");
397 _wputenv_s(L
"TEMP", L
"C:\\Valid\\Path");
401 // All related env vars empty
404 _wputenv_s(L
"TMP", L
"");
405 _wputenv_s(L
"TEMP", L
"");
406 _wputenv_s(L
"USERPROFILE", L
"");
410 // Test evn var / path with 260 chars.
411 SmallString
<270> Expected
{"C:\\Temp\\AB\\123456789"};
412 while (Expected
.size() < 260)
413 Expected
.append("\\DirNameWith19Charss");
414 ASSERT_EQ(260U, Expected
.size());
415 EXPECT_TEMP_DIR(_putenv_s("TMP", Expected
.c_str()), Expected
.c_str());
419 class FileSystemTest
: public testing::Test
{
421 /// Unique temporary directory in which all created filesystem entities must
422 /// be placed. It is removed at the end of each test (must be empty).
423 SmallString
<128> TestDirectory
;
424 SmallString
<128> NonExistantFile
;
426 void SetUp() override
{
428 fs::createUniqueDirectory("file-system-test", TestDirectory
));
429 // We don't care about this specific file.
430 errs() << "Test Directory: " << TestDirectory
<< '\n';
432 NonExistantFile
= TestDirectory
;
434 // Even though this value is hardcoded, is a 128-bit GUID, so we should be
435 // guaranteed that this file will never exist.
436 sys::path::append(NonExistantFile
, "1B28B495C16344CB9822E588CD4C3EF0");
439 void TearDown() override
{ ASSERT_NO_ERROR(fs::remove(TestDirectory
.str())); }
442 TEST_F(FileSystemTest
, Unique
) {
443 // Create a temp file.
445 SmallString
<64> TempPath
;
447 fs::createTemporaryFile("prefix", "temp", FileDescriptor
, TempPath
));
449 // The same file should return an identical unique id.
451 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath
), F1
));
452 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath
), F2
));
455 // Different files should return different unique ids.
457 SmallString
<64> TempPath2
;
459 fs::createTemporaryFile("prefix", "temp", FileDescriptor2
, TempPath2
));
462 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2
), D
));
464 ::close(FileDescriptor2
);
466 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2
)));
468 // Two paths representing the same file on disk should still provide the
469 // same unique id. We can test this by making a hard link.
470 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath
), Twine(TempPath2
)));
472 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2
), D2
));
475 ::close(FileDescriptor
);
477 SmallString
<128> Dir1
;
479 fs::createUniqueDirectory("dir1", Dir1
));
480 ASSERT_NO_ERROR(fs::getUniqueID(Dir1
.c_str(), F1
));
481 ASSERT_NO_ERROR(fs::getUniqueID(Dir1
.c_str(), F2
));
484 SmallString
<128> Dir2
;
486 fs::createUniqueDirectory("dir2", Dir2
));
487 ASSERT_NO_ERROR(fs::getUniqueID(Dir2
.c_str(), F2
));
489 ASSERT_NO_ERROR(fs::remove(Dir1
));
490 ASSERT_NO_ERROR(fs::remove(Dir2
));
491 ASSERT_NO_ERROR(fs::remove(TempPath2
));
492 ASSERT_NO_ERROR(fs::remove(TempPath
));
495 TEST_F(FileSystemTest
, RealPath
) {
497 fs::create_directories(Twine(TestDirectory
) + "/test1/test2/test3"));
498 ASSERT_TRUE(fs::exists(Twine(TestDirectory
) + "/test1/test2/test3"));
500 SmallString
<64> RealBase
;
501 SmallString
<64> Expected
;
502 SmallString
<64> Actual
;
504 // TestDirectory itself might be under a symlink or have been specified with
505 // a different case than the existing temp directory. In such cases real_path
506 // on the concatenated path will differ in the TestDirectory portion from
507 // how we specified it. Make sure to compare against the real_path of the
508 // TestDirectory, and not just the value of TestDirectory.
509 ASSERT_NO_ERROR(fs::real_path(TestDirectory
, RealBase
));
510 path::native(Twine(RealBase
) + "/test1/test2", Expected
);
512 ASSERT_NO_ERROR(fs::real_path(
513 Twine(TestDirectory
) + "/././test1/../test1/test2/./test3/..", Actual
));
515 EXPECT_EQ(Expected
, Actual
);
517 SmallString
<64> HomeDir
;
519 // This can fail if $HOME is not set and getpwuid fails.
520 bool Result
= llvm::sys::path::home_directory(HomeDir
);
522 ASSERT_NO_ERROR(fs::real_path(HomeDir
, Expected
));
523 ASSERT_NO_ERROR(fs::real_path("~", Actual
, true));
524 EXPECT_EQ(Expected
, Actual
);
525 ASSERT_NO_ERROR(fs::real_path("~/", Actual
, true));
526 EXPECT_EQ(Expected
, Actual
);
529 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory
) + "/test1"));
532 TEST_F(FileSystemTest
, ExpandTilde
) {
533 SmallString
<64> Expected
;
534 SmallString
<64> Actual
;
535 SmallString
<64> HomeDir
;
537 // This can fail if $HOME is not set and getpwuid fails.
538 bool Result
= llvm::sys::path::home_directory(HomeDir
);
540 fs::expand_tilde(HomeDir
, Expected
);
542 fs::expand_tilde("~", Actual
);
543 EXPECT_EQ(Expected
, Actual
);
547 fs::expand_tilde("~\\foo", Actual
);
550 fs::expand_tilde("~/foo", Actual
);
553 EXPECT_EQ(Expected
, Actual
);
558 TEST_F(FileSystemTest
, RealPathNoReadPerm
) {
559 SmallString
<64> Expanded
;
562 fs::create_directories(Twine(TestDirectory
) + "/noreadperm"));
563 ASSERT_TRUE(fs::exists(Twine(TestDirectory
) + "/noreadperm"));
565 fs::setPermissions(Twine(TestDirectory
) + "/noreadperm", fs::no_perms
);
566 fs::setPermissions(Twine(TestDirectory
) + "/noreadperm", fs::all_exe
);
568 ASSERT_NO_ERROR(fs::real_path(Twine(TestDirectory
) + "/noreadperm", Expanded
,
571 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory
) + "/noreadperm"));
576 TEST_F(FileSystemTest
, TempFileKeepDiscard
) {
577 // We can keep then discard.
578 auto TempFileOrError
= fs::TempFile::create(TestDirectory
+ "/test-%%%%");
579 ASSERT_TRUE((bool)TempFileOrError
);
580 fs::TempFile File
= std::move(*TempFileOrError
);
581 ASSERT_FALSE((bool)File
.keep(TestDirectory
+ "/keep"));
582 ASSERT_FALSE((bool)File
.discard());
583 ASSERT_TRUE(fs::exists(TestDirectory
+ "/keep"));
584 ASSERT_NO_ERROR(fs::remove(TestDirectory
+ "/keep"));
587 TEST_F(FileSystemTest
, TempFileDiscardDiscard
) {
588 // We can discard twice.
589 auto TempFileOrError
= fs::TempFile::create(TestDirectory
+ "/test-%%%%");
590 ASSERT_TRUE((bool)TempFileOrError
);
591 fs::TempFile File
= std::move(*TempFileOrError
);
592 ASSERT_FALSE((bool)File
.discard());
593 ASSERT_FALSE((bool)File
.discard());
594 ASSERT_FALSE(fs::exists(TestDirectory
+ "/keep"));
597 TEST_F(FileSystemTest
, TempFiles
) {
598 // Create a temp file.
600 SmallString
<64> TempPath
;
602 fs::createTemporaryFile("prefix", "temp", FileDescriptor
, TempPath
));
604 // Make sure it exists.
605 ASSERT_TRUE(sys::fs::exists(Twine(TempPath
)));
607 // Create another temp tile.
609 SmallString
<64> TempPath2
;
610 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2
, TempPath2
));
611 ASSERT_TRUE(TempPath2
.endswith(".temp"));
612 ASSERT_NE(TempPath
.str(), TempPath2
.str());
614 fs::file_status A
, B
;
615 ASSERT_NO_ERROR(fs::status(Twine(TempPath
), A
));
616 ASSERT_NO_ERROR(fs::status(Twine(TempPath2
), B
));
617 EXPECT_FALSE(fs::equivalent(A
, B
));
622 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2
)));
623 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2
)));
624 ASSERT_EQ(fs::remove(Twine(TempPath2
), false),
625 errc::no_such_file_or_directory
);
627 std::error_code EC
= fs::status(TempPath2
.c_str(), B
);
628 EXPECT_EQ(EC
, errc::no_such_file_or_directory
);
629 EXPECT_EQ(B
.type(), fs::file_type::file_not_found
);
631 // Make sure Temp2 doesn't exist.
632 ASSERT_EQ(fs::access(Twine(TempPath2
), sys::fs::AccessMode::Exist
),
633 errc::no_such_file_or_directory
);
635 SmallString
<64> TempPath3
;
636 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3
));
637 ASSERT_FALSE(TempPath3
.endswith("."));
638 FileRemover
Cleanup3(TempPath3
);
640 // Create a hard link to Temp1.
641 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath
), Twine(TempPath2
)));
643 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath
), Twine(TempPath2
), equal
));
645 ASSERT_NO_ERROR(fs::status(Twine(TempPath
), A
));
646 ASSERT_NO_ERROR(fs::status(Twine(TempPath2
), B
));
647 EXPECT_TRUE(fs::equivalent(A
, B
));
650 ::close(FileDescriptor
);
651 ASSERT_NO_ERROR(fs::remove(Twine(TempPath
)));
653 // Remove the hard link.
654 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2
)));
656 // Make sure Temp1 doesn't exist.
657 ASSERT_EQ(fs::access(Twine(TempPath
), sys::fs::AccessMode::Exist
),
658 errc::no_such_file_or_directory
);
661 // Path name > 260 chars should get an error.
662 const char *Path270
=
663 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
664 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
665 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
666 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
667 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
668 EXPECT_EQ(fs::createUniqueFile(Path270
, FileDescriptor
, TempPath
),
669 errc::invalid_argument
);
670 // Relative path < 247 chars, no problem.
671 const char *Path216
=
672 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
673 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
674 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
675 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
676 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216
, "", TempPath
));
677 ASSERT_NO_ERROR(fs::remove(Twine(TempPath
)));
681 TEST_F(FileSystemTest
, TempFileCollisions
) {
682 SmallString
<128> TestDirectory
;
684 fs::createUniqueDirectory("CreateUniqueFileTest", TestDirectory
));
685 FileRemover
Cleanup(TestDirectory
);
686 SmallString
<128> Model
= TestDirectory
;
687 path::append(Model
, "%.tmp");
688 SmallString
<128> Path
;
689 std::vector
<fs::TempFile
> TempFiles
;
691 auto TryCreateTempFile
= [&]() {
692 Expected
<fs::TempFile
> T
= fs::TempFile::create(Model
);
694 TempFiles
.push_back(std::move(*T
));
697 logAllUnhandledErrors(T
.takeError(), errs(),
698 "Failed to create temporary file: ");
703 // We should be able to create exactly 16 temporary files.
704 for (int i
= 0; i
< 16; ++i
)
705 EXPECT_TRUE(TryCreateTempFile());
706 EXPECT_FALSE(TryCreateTempFile());
708 for (fs::TempFile
&T
: TempFiles
)
709 cantFail(T
.discard());
712 TEST_F(FileSystemTest
, CreateDir
) {
713 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory
) + "foo"));
714 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory
) + "foo"));
715 ASSERT_EQ(fs::create_directory(Twine(TestDirectory
) + "foo", false),
717 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "foo"));
720 // Set a 0000 umask so that we can test our directory permissions.
721 mode_t OldUmask
= ::umask(0000);
723 fs::file_status Status
;
725 fs::create_directory(Twine(TestDirectory
) + "baz500", false,
726 fs::perms::owner_read
| fs::perms::owner_exe
));
727 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory
) + "baz500", Status
));
728 ASSERT_EQ(Status
.permissions() & fs::perms::all_all
,
729 fs::perms::owner_read
| fs::perms::owner_exe
);
730 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory
) + "baz777", false,
731 fs::perms::all_all
));
732 ASSERT_NO_ERROR(fs::status(Twine(TestDirectory
) + "baz777", Status
));
733 ASSERT_EQ(Status
.permissions() & fs::perms::all_all
, fs::perms::all_all
);
735 // Restore umask to be safe.
740 // Prove that create_directories() can handle a pathname > 248 characters,
741 // which is the documented limit for CreateDirectory().
742 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
743 // Generate a directory path guaranteed to fall into that range.
744 size_t TmpLen
= TestDirectory
.size();
745 const char *OneDir
= "\\123456789";
746 size_t OneDirLen
= strlen(OneDir
);
747 ASSERT_LT(OneDirLen
, 12U);
748 size_t NLevels
= ((248 - TmpLen
) / OneDirLen
) + 1;
749 SmallString
<260> LongDir(TestDirectory
);
750 for (size_t I
= 0; I
< NLevels
; ++I
)
751 LongDir
.append(OneDir
);
752 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir
)));
753 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir
)));
754 ASSERT_EQ(fs::create_directories(Twine(LongDir
), false),
756 // Tidy up, "recursively" removing the directories.
757 StringRef
ThisDir(LongDir
);
758 for (size_t J
= 0; J
< NLevels
; ++J
) {
759 ASSERT_NO_ERROR(fs::remove(ThisDir
));
760 ThisDir
= path::parent_path(ThisDir
);
763 // Also verify that paths with Unix separators are handled correctly.
764 std::string
LongPathWithUnixSeparators(TestDirectory
.str());
765 // Add at least one subdirectory to TestDirectory, and replace slashes with
768 LongPathWithUnixSeparators
.append("/DirNameWith19Charss");
769 } while (LongPathWithUnixSeparators
.size() < 260);
770 std::replace(LongPathWithUnixSeparators
.begin(),
771 LongPathWithUnixSeparators
.end(),
773 ASSERT_NO_ERROR(fs::create_directories(Twine(LongPathWithUnixSeparators
)));
775 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory
) +
776 "/DirNameWith19Charss"));
778 // Similarly for a relative pathname. Need to set the current directory to
779 // TestDirectory so that the one we create ends up in the right place.
780 char PreviousDir
[260];
781 size_t PreviousDirLen
= ::GetCurrentDirectoryA(260, PreviousDir
);
782 ASSERT_GT(PreviousDirLen
, 0U);
783 ASSERT_LT(PreviousDirLen
, 260U);
784 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory
.c_str()), 0);
786 // Generate a relative directory name with absolute length > 248.
787 size_t LongDirLen
= 249 - TestDirectory
.size();
788 LongDir
.assign(LongDirLen
, 'a');
789 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir
)));
790 // While we're here, prove that .. and . handling works in these long paths.
791 const char *DotDotDirs
= "\\..\\.\\b";
792 LongDir
.append(DotDotDirs
);
793 ASSERT_NO_ERROR(fs::create_directory("b"));
794 ASSERT_EQ(fs::create_directory(Twine(LongDir
), false), errc::file_exists
);
796 ASSERT_NO_ERROR(fs::remove("b"));
797 ASSERT_NO_ERROR(fs::remove(
798 Twine(LongDir
.substr(0, LongDir
.size() - strlen(DotDotDirs
)))));
799 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir
), 0);
803 TEST_F(FileSystemTest
, DirectoryIteration
) {
805 for (fs::directory_iterator
i(".", ec
), e
; i
!= e
; i
.increment(ec
))
808 // Create a known hierarchy to recurse over.
810 fs::create_directories(Twine(TestDirectory
) + "/recursive/a0/aa1"));
812 fs::create_directories(Twine(TestDirectory
) + "/recursive/a0/ab1"));
813 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory
) +
814 "/recursive/dontlookhere/da1"));
816 fs::create_directories(Twine(TestDirectory
) + "/recursive/z0/za1"));
818 fs::create_directories(Twine(TestDirectory
) + "/recursive/pop/p1"));
819 typedef std::vector
<std::string
> v_t
;
821 for (fs::recursive_directory_iterator
i(Twine(TestDirectory
)
822 + "/recursive", ec
), e
; i
!= e
; i
.increment(ec
)){
824 if (path::filename(i
->path()) == "p1") {
826 // FIXME: recursive_directory_iterator should be more robust.
829 if (path::filename(i
->path()) == "dontlookhere")
831 visited
.push_back(path::filename(i
->path()));
833 v_t::const_iterator a0
= find(visited
, "a0");
834 v_t::const_iterator aa1
= find(visited
, "aa1");
835 v_t::const_iterator ab1
= find(visited
, "ab1");
836 v_t::const_iterator dontlookhere
= find(visited
, "dontlookhere");
837 v_t::const_iterator da1
= find(visited
, "da1");
838 v_t::const_iterator z0
= find(visited
, "z0");
839 v_t::const_iterator za1
= find(visited
, "za1");
840 v_t::const_iterator pop
= find(visited
, "pop");
841 v_t::const_iterator p1
= find(visited
, "p1");
843 // Make sure that each path was visited correctly.
844 ASSERT_NE(a0
, visited
.end());
845 ASSERT_NE(aa1
, visited
.end());
846 ASSERT_NE(ab1
, visited
.end());
847 ASSERT_NE(dontlookhere
, visited
.end());
848 ASSERT_EQ(da1
, visited
.end()); // Not visited.
849 ASSERT_NE(z0
, visited
.end());
850 ASSERT_NE(za1
, visited
.end());
851 ASSERT_NE(pop
, visited
.end());
852 ASSERT_EQ(p1
, visited
.end()); // Not visited.
854 // Make sure that parents were visited before children. No other ordering
855 // guarantees can be made across siblings.
860 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive/a0/aa1"));
861 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive/a0/ab1"));
862 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive/a0"));
864 fs::remove(Twine(TestDirectory
) + "/recursive/dontlookhere/da1"));
865 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive/dontlookhere"));
866 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive/pop/p1"));
867 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive/pop"));
868 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive/z0/za1"));
869 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive/z0"));
870 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/recursive"));
872 // Test recursive_directory_iterator level()
874 fs::create_directories(Twine(TestDirectory
) + "/reclevel/a/b/c"));
875 fs::recursive_directory_iterator
I(Twine(TestDirectory
) + "/reclevel", ec
), E
;
876 for (int l
= 0; I
!= E
; I
.increment(ec
), ++l
) {
878 EXPECT_EQ(I
.level(), l
);
881 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/reclevel/a/b/c"));
882 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/reclevel/a/b"));
883 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/reclevel/a"));
884 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
) + "/reclevel"));
888 TEST_F(FileSystemTest
, BrokenSymlinkDirectoryIteration
) {
889 // Create a known hierarchy to recurse over.
890 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory
) + "/symlink"));
892 fs::create_link("no_such_file", Twine(TestDirectory
) + "/symlink/a"));
894 fs::create_directories(Twine(TestDirectory
) + "/symlink/b/bb"));
896 fs::create_link("no_such_file", Twine(TestDirectory
) + "/symlink/b/ba"));
898 fs::create_link("no_such_file", Twine(TestDirectory
) + "/symlink/b/bc"));
900 fs::create_link("no_such_file", Twine(TestDirectory
) + "/symlink/c"));
902 fs::create_directories(Twine(TestDirectory
) + "/symlink/d/dd/ddd"));
903 ASSERT_NO_ERROR(fs::create_link(Twine(TestDirectory
) + "/symlink/d/dd",
904 Twine(TestDirectory
) + "/symlink/d/da"));
906 fs::create_link("no_such_file", Twine(TestDirectory
) + "/symlink/e"));
908 typedef std::vector
<std::string
> v_t
;
909 v_t VisitedNonBrokenSymlinks
;
910 v_t VisitedBrokenSymlinks
;
912 using testing::UnorderedElementsAre
;
913 using testing::UnorderedElementsAreArray
;
915 // Broken symbol links are expected to throw an error.
916 for (fs::directory_iterator
i(Twine(TestDirectory
) + "/symlink", ec
), e
;
917 i
!= e
; i
.increment(ec
)) {
919 if (i
->status().getError() ==
920 std::make_error_code(std::errc::no_such_file_or_directory
)) {
921 VisitedBrokenSymlinks
.push_back(path::filename(i
->path()));
924 VisitedNonBrokenSymlinks
.push_back(path::filename(i
->path()));
926 EXPECT_THAT(VisitedNonBrokenSymlinks
, UnorderedElementsAre("b", "d"));
927 VisitedNonBrokenSymlinks
.clear();
929 EXPECT_THAT(VisitedBrokenSymlinks
, UnorderedElementsAre("a", "c", "e"));
930 VisitedBrokenSymlinks
.clear();
932 // Broken symbol links are expected to throw an error.
933 for (fs::recursive_directory_iterator
i(
934 Twine(TestDirectory
) + "/symlink", ec
), e
; i
!= e
; i
.increment(ec
)) {
936 if (i
->status().getError() ==
937 std::make_error_code(std::errc::no_such_file_or_directory
)) {
938 VisitedBrokenSymlinks
.push_back(path::filename(i
->path()));
941 VisitedNonBrokenSymlinks
.push_back(path::filename(i
->path()));
943 EXPECT_THAT(VisitedNonBrokenSymlinks
,
944 UnorderedElementsAre("b", "bb", "d", "da", "dd", "ddd", "ddd"));
945 VisitedNonBrokenSymlinks
.clear();
947 EXPECT_THAT(VisitedBrokenSymlinks
,
948 UnorderedElementsAre("a", "ba", "bc", "c", "e"));
949 VisitedBrokenSymlinks
.clear();
951 for (fs::recursive_directory_iterator
i(
952 Twine(TestDirectory
) + "/symlink", ec
, /*follow_symlinks=*/false), e
;
953 i
!= e
; i
.increment(ec
)) {
955 if (i
->status().getError() ==
956 std::make_error_code(std::errc::no_such_file_or_directory
)) {
957 VisitedBrokenSymlinks
.push_back(path::filename(i
->path()));
960 VisitedNonBrokenSymlinks
.push_back(path::filename(i
->path()));
962 EXPECT_THAT(VisitedNonBrokenSymlinks
,
963 UnorderedElementsAreArray({"a", "b", "ba", "bb", "bc", "c", "d",
964 "da", "dd", "ddd", "e"}));
965 VisitedNonBrokenSymlinks
.clear();
967 EXPECT_THAT(VisitedBrokenSymlinks
, UnorderedElementsAre());
968 VisitedBrokenSymlinks
.clear();
970 ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory
) + "/symlink"));
974 TEST_F(FileSystemTest
, Remove
) {
975 SmallString
<64> BaseDir
;
976 SmallString
<64> Paths
[4];
978 ASSERT_NO_ERROR(fs::createUniqueDirectory("fs_remove", BaseDir
));
980 ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir
) + "/foo/bar/baz"));
981 ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir
) + "/foo/bar/buzz"));
982 ASSERT_NO_ERROR(fs::createUniqueFile(
983 Twine(BaseDir
) + "/foo/bar/baz/%%%%%%.tmp", fds
[0], Paths
[0]));
984 ASSERT_NO_ERROR(fs::createUniqueFile(
985 Twine(BaseDir
) + "/foo/bar/baz/%%%%%%.tmp", fds
[1], Paths
[1]));
986 ASSERT_NO_ERROR(fs::createUniqueFile(
987 Twine(BaseDir
) + "/foo/bar/buzz/%%%%%%.tmp", fds
[2], Paths
[2]));
988 ASSERT_NO_ERROR(fs::createUniqueFile(
989 Twine(BaseDir
) + "/foo/bar/buzz/%%%%%%.tmp", fds
[3], Paths
[3]));
994 EXPECT_TRUE(fs::exists(Twine(BaseDir
) + "/foo/bar/baz"));
995 EXPECT_TRUE(fs::exists(Twine(BaseDir
) + "/foo/bar/buzz"));
996 EXPECT_TRUE(fs::exists(Paths
[0]));
997 EXPECT_TRUE(fs::exists(Paths
[1]));
998 EXPECT_TRUE(fs::exists(Paths
[2]));
999 EXPECT_TRUE(fs::exists(Paths
[3]));
1001 ASSERT_NO_ERROR(fs::remove_directories("D:/footest"));
1003 ASSERT_NO_ERROR(fs::remove_directories(BaseDir
));
1004 ASSERT_FALSE(fs::exists(BaseDir
));
1008 TEST_F(FileSystemTest
, CarriageReturn
) {
1009 SmallString
<128> FilePathname(TestDirectory
);
1011 path::append(FilePathname
, "test");
1014 raw_fd_ostream
File(FilePathname
, EC
, sys::fs::F_Text
);
1015 ASSERT_NO_ERROR(EC
);
1019 auto Buf
= MemoryBuffer::getFile(FilePathname
.str());
1020 EXPECT_TRUE((bool)Buf
);
1021 EXPECT_EQ(Buf
.get()->getBuffer(), "\r\n");
1025 raw_fd_ostream
File(FilePathname
, EC
, sys::fs::F_None
);
1026 ASSERT_NO_ERROR(EC
);
1030 auto Buf
= MemoryBuffer::getFile(FilePathname
.str());
1031 EXPECT_TRUE((bool)Buf
);
1032 EXPECT_EQ(Buf
.get()->getBuffer(), "\n");
1034 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname
)));
1038 TEST_F(FileSystemTest
, Resize
) {
1040 SmallString
<64> TempPath
;
1041 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD
, TempPath
));
1042 ASSERT_NO_ERROR(fs::resize_file(FD
, 123));
1043 fs::file_status Status
;
1044 ASSERT_NO_ERROR(fs::status(FD
, Status
));
1045 ASSERT_EQ(Status
.getSize(), 123U);
1047 ASSERT_NO_ERROR(fs::remove(TempPath
));
1050 TEST_F(FileSystemTest
, MD5
) {
1052 SmallString
<64> TempPath
;
1053 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD
, TempPath
));
1054 StringRef
Data("abcdefghijklmnopqrstuvwxyz");
1055 ASSERT_EQ(write(FD
, Data
.data(), Data
.size()), static_cast<ssize_t
>(Data
.size()));
1056 lseek(FD
, 0, SEEK_SET
);
1057 auto Hash
= fs::md5_contents(FD
);
1059 ASSERT_NO_ERROR(Hash
.getError());
1061 EXPECT_STREQ("c3fcd3d76192e4007dfb496cca67e13b", Hash
->digest().c_str());
1064 TEST_F(FileSystemTest
, FileMapping
) {
1065 // Create a temp file.
1067 SmallString
<64> TempPath
;
1069 fs::createTemporaryFile("prefix", "temp", FileDescriptor
, TempPath
));
1070 unsigned Size
= 4096;
1071 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor
, Size
));
1073 // Map in temp file and add some content
1075 StringRef
Val("hello there");
1077 fs::mapped_file_region
mfr(FileDescriptor
,
1078 fs::mapped_file_region::readwrite
, Size
, 0, EC
);
1079 ASSERT_NO_ERROR(EC
);
1080 std::copy(Val
.begin(), Val
.end(), mfr
.data());
1081 // Explicitly add a 0.
1082 mfr
.data()[Val
.size()] = 0;
1085 ASSERT_EQ(close(FileDescriptor
), 0);
1087 // Map it back in read-only
1090 EC
= fs::openFileForRead(Twine(TempPath
), FD
);
1091 ASSERT_NO_ERROR(EC
);
1092 fs::mapped_file_region
mfr(FD
, fs::mapped_file_region::readonly
, Size
, 0, EC
);
1093 ASSERT_NO_ERROR(EC
);
1096 EXPECT_EQ(StringRef(mfr
.const_data()), Val
);
1099 fs::mapped_file_region
m(FD
, fs::mapped_file_region::readonly
, Size
, 0, EC
);
1100 ASSERT_NO_ERROR(EC
);
1101 ASSERT_EQ(close(FD
), 0);
1103 ASSERT_NO_ERROR(fs::remove(TempPath
));
1106 TEST(Support
, NormalizePath
) {
1107 using TestTuple
= std::tuple
<const char *, const char *, const char *>;
1108 std::vector
<TestTuple
> Tests
;
1109 Tests
.emplace_back("a", "a", "a");
1110 Tests
.emplace_back("a/b", "a\\b", "a/b");
1111 Tests
.emplace_back("a\\b", "a\\b", "a/b");
1112 Tests
.emplace_back("a\\\\b", "a\\\\b", "a\\\\b");
1113 Tests
.emplace_back("\\a", "\\a", "/a");
1114 Tests
.emplace_back("a\\", "a\\", "a/");
1116 for (auto &T
: Tests
) {
1117 SmallString
<64> Win(std::get
<0>(T
));
1118 SmallString
<64> Posix(Win
);
1119 path::native(Win
, path::Style::windows
);
1120 path::native(Posix
, path::Style::posix
);
1121 EXPECT_EQ(std::get
<1>(T
), Win
);
1122 EXPECT_EQ(std::get
<2>(T
), Posix
);
1126 SmallString
<64> PathHome
;
1127 path::home_directory(PathHome
);
1129 const char *Path7a
= "~/aaa";
1130 SmallString
<64> Path7(Path7a
);
1131 path::native(Path7
);
1132 EXPECT_TRUE(Path7
.endswith("\\aaa"));
1133 EXPECT_TRUE(Path7
.startswith(PathHome
));
1134 EXPECT_EQ(Path7
.size(), PathHome
.size() + strlen(Path7a
+ 1));
1136 const char *Path8a
= "~";
1137 SmallString
<64> Path8(Path8a
);
1138 path::native(Path8
);
1139 EXPECT_EQ(Path8
, PathHome
);
1141 const char *Path9a
= "~aaa";
1142 SmallString
<64> Path9(Path9a
);
1143 path::native(Path9
);
1144 EXPECT_EQ(Path9
, "~aaa");
1146 const char *Path10a
= "aaa/~/b";
1147 SmallString
<64> Path10(Path10a
);
1148 path::native(Path10
);
1149 EXPECT_EQ(Path10
, "aaa\\~\\b");
1153 TEST(Support
, RemoveLeadingDotSlash
) {
1154 StringRef
Path1("././/foolz/wat");
1155 StringRef
Path2("./////");
1157 Path1
= path::remove_leading_dotslash(Path1
);
1158 EXPECT_EQ(Path1
, "foolz/wat");
1159 Path2
= path::remove_leading_dotslash(Path2
);
1160 EXPECT_EQ(Path2
, "");
1163 static std::string
remove_dots(StringRef path
, bool remove_dot_dot
,
1164 path::Style style
) {
1165 SmallString
<256> buffer(path
);
1166 path::remove_dots(buffer
, remove_dot_dot
, style
);
1167 return buffer
.str();
1170 TEST(Support
, RemoveDots
) {
1171 EXPECT_EQ("foolz\\wat",
1172 remove_dots(".\\.\\\\foolz\\wat", false, path::Style::windows
));
1173 EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false, path::Style::windows
));
1175 EXPECT_EQ("a\\..\\b\\c",
1176 remove_dots(".\\a\\..\\b\\c", false, path::Style::windows
));
1177 EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true, path::Style::windows
));
1178 EXPECT_EQ("c", remove_dots(".\\.\\c", true, path::Style::windows
));
1179 EXPECT_EQ("..\\a\\c",
1180 remove_dots("..\\a\\b\\..\\c", true, path::Style::windows
));
1181 EXPECT_EQ("..\\..\\a\\c",
1182 remove_dots("..\\..\\a\\b\\..\\c", true, path::Style::windows
));
1184 SmallString
<64> Path1(".\\.\\c");
1185 EXPECT_TRUE(path::remove_dots(Path1
, true, path::Style::windows
));
1186 EXPECT_EQ("c", Path1
);
1188 EXPECT_EQ("foolz/wat",
1189 remove_dots("././/foolz/wat", false, path::Style::posix
));
1190 EXPECT_EQ("", remove_dots("./////", false, path::Style::posix
));
1192 EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false, path::Style::posix
));
1193 EXPECT_EQ("b/c", remove_dots("./a/../b/c", true, path::Style::posix
));
1194 EXPECT_EQ("c", remove_dots("././c", true, path::Style::posix
));
1195 EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true, path::Style::posix
));
1196 EXPECT_EQ("../../a/c",
1197 remove_dots("../../a/b/../c", true, path::Style::posix
));
1198 EXPECT_EQ("/a/c", remove_dots("/../../a/c", true, path::Style::posix
));
1200 remove_dots("/../a/b//../././/c", true, path::Style::posix
));
1202 SmallString
<64> Path2("././c");
1203 EXPECT_TRUE(path::remove_dots(Path2
, true, path::Style::posix
));
1204 EXPECT_EQ("c", Path2
);
1207 TEST(Support
, ReplacePathPrefix
) {
1208 SmallString
<64> Path1("/foo");
1209 SmallString
<64> Path2("/old/foo");
1210 SmallString
<64> OldPrefix("/old");
1211 SmallString
<64> NewPrefix("/new");
1212 SmallString
<64> NewPrefix2("/longernew");
1213 SmallString
<64> EmptyPrefix("");
1215 SmallString
<64> Path
= Path1
;
1216 path::replace_path_prefix(Path
, OldPrefix
, NewPrefix
);
1217 EXPECT_EQ(Path
, "/foo");
1219 path::replace_path_prefix(Path
, OldPrefix
, NewPrefix
);
1220 EXPECT_EQ(Path
, "/new/foo");
1222 path::replace_path_prefix(Path
, OldPrefix
, NewPrefix2
);
1223 EXPECT_EQ(Path
, "/longernew/foo");
1225 path::replace_path_prefix(Path
, EmptyPrefix
, NewPrefix
);
1226 EXPECT_EQ(Path
, "/new/foo");
1228 path::replace_path_prefix(Path
, OldPrefix
, EmptyPrefix
);
1229 EXPECT_EQ(Path
, "/foo");
1232 TEST_F(FileSystemTest
, OpenFileForRead
) {
1233 // Create a temp file.
1235 SmallString
<64> TempPath
;
1237 fs::createTemporaryFile("prefix", "temp", FileDescriptor
, TempPath
));
1238 FileRemover
Cleanup(TempPath
);
1240 // Make sure it exists.
1241 ASSERT_TRUE(sys::fs::exists(Twine(TempPath
)));
1243 // Open the file for read
1244 int FileDescriptor2
;
1245 SmallString
<64> ResultPath
;
1246 ASSERT_NO_ERROR(fs::openFileForRead(Twine(TempPath
), FileDescriptor2
,
1247 fs::OF_None
, &ResultPath
))
1249 // If we succeeded, check that the paths are the same (modulo case):
1250 if (!ResultPath
.empty()) {
1251 // The paths returned by createTemporaryFile and getPathFromOpenFD
1252 // should reference the same file on disk.
1253 fs::UniqueID D1
, D2
;
1254 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath
), D1
));
1255 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath
), D2
));
1258 ::close(FileDescriptor
);
1259 ::close(FileDescriptor2
);
1262 // Since Windows Vista, file access time is not updated by default.
1263 // This is instead updated manually by openFileForRead.
1264 // https://blogs.technet.microsoft.com/filecab/2006/11/07/disabling-last-access-time-in-windows-vista-to-improve-ntfs-performance/
1265 // This part of the unit test is Windows specific as the updating of
1266 // access times can be disabled on Linux using /etc/fstab.
1268 // Set access time to UNIX epoch.
1269 ASSERT_NO_ERROR(sys::fs::openFileForWrite(Twine(TempPath
), FileDescriptor
,
1270 fs::CD_OpenExisting
));
1271 TimePoint
<> Epoch(std::chrono::milliseconds(0));
1272 ASSERT_NO_ERROR(fs::setLastAccessAndModificationTime(FileDescriptor
, Epoch
));
1273 ::close(FileDescriptor
);
1275 // Open the file and ensure access time is updated, when forced.
1276 ASSERT_NO_ERROR(fs::openFileForRead(Twine(TempPath
), FileDescriptor
,
1277 fs::OF_UpdateAtime
, &ResultPath
));
1279 sys::fs::file_status Status
;
1280 ASSERT_NO_ERROR(sys::fs::status(FileDescriptor
, Status
));
1281 auto FileAccessTime
= Status
.getLastAccessedTime();
1283 ASSERT_NE(Epoch
, FileAccessTime
);
1284 ::close(FileDescriptor
);
1286 // Ideally this test would include a case when ATime is not forced to update,
1287 // however the expected behaviour will differ depending on the configuration
1288 // of the Windows file system.
1292 static void createFileWithData(const Twine
&Path
, bool ShouldExistBefore
,
1293 fs::CreationDisposition Disp
, StringRef Data
) {
1295 ASSERT_EQ(ShouldExistBefore
, fs::exists(Path
));
1296 ASSERT_NO_ERROR(fs::openFileForWrite(Path
, FD
, Disp
));
1297 FileDescriptorCloser
Closer(FD
);
1298 ASSERT_TRUE(fs::exists(Path
));
1300 ASSERT_EQ(Data
.size(), (size_t)write(FD
, Data
.data(), Data
.size()));
1303 static void verifyFileContents(const Twine
&Path
, StringRef Contents
) {
1304 auto Buffer
= MemoryBuffer::getFile(Path
);
1305 ASSERT_TRUE((bool)Buffer
);
1306 StringRef Data
= Buffer
.get()->getBuffer();
1307 ASSERT_EQ(Data
, Contents
);
1310 TEST_F(FileSystemTest
, CreateNew
) {
1312 Optional
<FileDescriptorCloser
> Closer
;
1314 // Succeeds if the file does not exist.
1315 ASSERT_FALSE(fs::exists(NonExistantFile
));
1316 ASSERT_NO_ERROR(fs::openFileForWrite(NonExistantFile
, FD
, fs::CD_CreateNew
));
1317 ASSERT_TRUE(fs::exists(NonExistantFile
));
1319 FileRemover
Cleanup(NonExistantFile
);
1322 // And creates a file of size 0.
1323 sys::fs::file_status Status
;
1324 ASSERT_NO_ERROR(sys::fs::status(FD
, Status
));
1325 EXPECT_EQ(0ULL, Status
.getSize());
1327 // Close this first, before trying to re-open the file.
1330 // But fails if the file does exist.
1331 ASSERT_ERROR(fs::openFileForWrite(NonExistantFile
, FD
, fs::CD_CreateNew
));
1334 TEST_F(FileSystemTest
, CreateAlways
) {
1336 Optional
<FileDescriptorCloser
> Closer
;
1338 // Succeeds if the file does not exist.
1339 ASSERT_FALSE(fs::exists(NonExistantFile
));
1341 fs::openFileForWrite(NonExistantFile
, FD
, fs::CD_CreateAlways
));
1345 ASSERT_TRUE(fs::exists(NonExistantFile
));
1347 FileRemover
Cleanup(NonExistantFile
);
1349 // And creates a file of size 0.
1351 ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile
, FileSize
));
1352 ASSERT_EQ(0ULL, FileSize
);
1354 // If we write some data to it re-create it with CreateAlways, it succeeds and
1355 // truncates to 0 bytes.
1356 ASSERT_EQ(4, write(FD
, "Test", 4));
1360 ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile
, FileSize
));
1361 ASSERT_EQ(4ULL, FileSize
);
1364 fs::openFileForWrite(NonExistantFile
, FD
, fs::CD_CreateAlways
));
1366 ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile
, FileSize
));
1367 ASSERT_EQ(0ULL, FileSize
);
1370 TEST_F(FileSystemTest
, OpenExisting
) {
1373 // Fails if the file does not exist.
1374 ASSERT_FALSE(fs::exists(NonExistantFile
));
1375 ASSERT_ERROR(fs::openFileForWrite(NonExistantFile
, FD
, fs::CD_OpenExisting
));
1376 ASSERT_FALSE(fs::exists(NonExistantFile
));
1378 // Make a dummy file now so that we can try again when the file does exist.
1379 createFileWithData(NonExistantFile
, false, fs::CD_CreateNew
, "Fizz");
1380 FileRemover
Cleanup(NonExistantFile
);
1382 ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile
, FileSize
));
1383 ASSERT_EQ(4ULL, FileSize
);
1385 // If we re-create it with different data, it overwrites rather than
1387 createFileWithData(NonExistantFile
, true, fs::CD_OpenExisting
, "Buzz");
1388 verifyFileContents(NonExistantFile
, "Buzz");
1391 TEST_F(FileSystemTest
, OpenAlways
) {
1392 // Succeeds if the file does not exist.
1393 createFileWithData(NonExistantFile
, false, fs::CD_OpenAlways
, "Fizz");
1394 FileRemover
Cleanup(NonExistantFile
);
1396 ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile
, FileSize
));
1397 ASSERT_EQ(4ULL, FileSize
);
1399 // Now re-open it and write again, verifying the contents get over-written.
1400 createFileWithData(NonExistantFile
, true, fs::CD_OpenAlways
, "Bu");
1401 verifyFileContents(NonExistantFile
, "Buzz");
1404 TEST_F(FileSystemTest
, AppendSetsCorrectFileOffset
) {
1405 fs::CreationDisposition Disps
[] = {fs::CD_CreateAlways
, fs::CD_OpenAlways
,
1406 fs::CD_OpenExisting
};
1408 // Write some data and re-open it with every possible disposition (this is a
1409 // hack that shouldn't work, but is left for compatibility. F_Append
1411 // the specified disposition.
1412 for (fs::CreationDisposition Disp
: Disps
) {
1414 Optional
<FileDescriptorCloser
> Closer
;
1416 createFileWithData(NonExistantFile
, false, fs::CD_CreateNew
, "Fizz");
1418 FileRemover
Cleanup(NonExistantFile
);
1421 ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile
, FileSize
));
1422 ASSERT_EQ(4ULL, FileSize
);
1424 fs::openFileForWrite(NonExistantFile
, FD
, Disp
, fs::OF_Append
));
1426 ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile
, FileSize
));
1427 ASSERT_EQ(4ULL, FileSize
);
1429 ASSERT_EQ(4, write(FD
, "Buzz", 4));
1432 verifyFileContents(NonExistantFile
, "FizzBuzz");
1436 static void verifyRead(int FD
, StringRef Data
, bool ShouldSucceed
) {
1437 std::vector
<char> Buffer
;
1438 Buffer
.resize(Data
.size());
1439 int Result
= ::read(FD
, Buffer
.data(), Buffer
.size());
1440 if (ShouldSucceed
) {
1441 ASSERT_EQ((size_t)Result
, Data
.size());
1442 ASSERT_EQ(Data
, StringRef(Buffer
.data(), Buffer
.size()));
1444 ASSERT_EQ(-1, Result
);
1445 ASSERT_EQ(EBADF
, errno
);
1449 static void verifyWrite(int FD
, StringRef Data
, bool ShouldSucceed
) {
1450 int Result
= ::write(FD
, Data
.data(), Data
.size());
1452 ASSERT_EQ((size_t)Result
, Data
.size());
1454 ASSERT_EQ(-1, Result
);
1455 ASSERT_EQ(EBADF
, errno
);
1459 TEST_F(FileSystemTest
, ReadOnlyFileCantWrite
) {
1460 createFileWithData(NonExistantFile
, false, fs::CD_CreateNew
, "Fizz");
1461 FileRemover
Cleanup(NonExistantFile
);
1464 ASSERT_NO_ERROR(fs::openFileForRead(NonExistantFile
, FD
));
1465 FileDescriptorCloser
Closer(FD
);
1467 verifyWrite(FD
, "Buzz", false);
1468 verifyRead(FD
, "Fizz", true);
1471 TEST_F(FileSystemTest
, WriteOnlyFileCantRead
) {
1472 createFileWithData(NonExistantFile
, false, fs::CD_CreateNew
, "Fizz");
1473 FileRemover
Cleanup(NonExistantFile
);
1477 fs::openFileForWrite(NonExistantFile
, FD
, fs::CD_OpenExisting
));
1478 FileDescriptorCloser
Closer(FD
);
1479 verifyRead(FD
, "Fizz", false);
1480 verifyWrite(FD
, "Buzz", true);
1483 TEST_F(FileSystemTest
, ReadWriteFileCanReadOrWrite
) {
1484 createFileWithData(NonExistantFile
, false, fs::CD_CreateNew
, "Fizz");
1485 FileRemover
Cleanup(NonExistantFile
);
1488 ASSERT_NO_ERROR(fs::openFileForReadWrite(NonExistantFile
, FD
,
1489 fs::CD_OpenExisting
, fs::OF_None
));
1490 FileDescriptorCloser
Closer(FD
);
1491 verifyRead(FD
, "Fizz", true);
1492 verifyWrite(FD
, "Buzz", true);
1495 TEST_F(FileSystemTest
, set_current_path
) {
1496 SmallString
<128> path
;
1498 ASSERT_NO_ERROR(fs::current_path(path
));
1499 ASSERT_NE(TestDirectory
, path
);
1501 struct RestorePath
{
1502 SmallString
<128> path
;
1503 RestorePath(const SmallString
<128> &path
) : path(path
) {}
1504 ~RestorePath() { fs::set_current_path(path
); }
1505 } restore_path(path
);
1507 ASSERT_NO_ERROR(fs::set_current_path(TestDirectory
));
1509 ASSERT_NO_ERROR(fs::current_path(path
));
1511 fs::UniqueID D1
, D2
;
1512 ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory
, D1
));
1513 ASSERT_NO_ERROR(fs::getUniqueID(path
, D2
));
1514 ASSERT_EQ(D1
, D2
) << "D1: " << TestDirectory
<< "\nD2: " << path
;
1517 TEST_F(FileSystemTest
, permissions
) {
1519 SmallString
<64> TempPath
;
1520 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD
, TempPath
));
1521 FileRemover
Cleanup(TempPath
);
1523 // Make sure it exists.
1524 ASSERT_TRUE(fs::exists(Twine(TempPath
)));
1526 auto CheckPermissions
= [&](fs::perms Expected
) {
1527 ErrorOr
<fs::perms
> Actual
= fs::getPermissions(TempPath
);
1528 return Actual
&& *Actual
== Expected
;
1531 std::error_code NoError
;
1532 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_all
), NoError
);
1533 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1535 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_read
| fs::all_exe
), NoError
);
1536 EXPECT_TRUE(CheckPermissions(fs::all_read
| fs::all_exe
));
1539 fs::perms ReadOnly
= fs::all_read
| fs::all_exe
;
1540 EXPECT_EQ(fs::setPermissions(TempPath
, fs::no_perms
), NoError
);
1541 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1543 EXPECT_EQ(fs::setPermissions(TempPath
, fs::owner_read
), NoError
);
1544 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1546 EXPECT_EQ(fs::setPermissions(TempPath
, fs::owner_write
), NoError
);
1547 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1549 EXPECT_EQ(fs::setPermissions(TempPath
, fs::owner_exe
), NoError
);
1550 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1552 EXPECT_EQ(fs::setPermissions(TempPath
, fs::owner_all
), NoError
);
1553 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1555 EXPECT_EQ(fs::setPermissions(TempPath
, fs::group_read
), NoError
);
1556 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1558 EXPECT_EQ(fs::setPermissions(TempPath
, fs::group_write
), NoError
);
1559 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1561 EXPECT_EQ(fs::setPermissions(TempPath
, fs::group_exe
), NoError
);
1562 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1564 EXPECT_EQ(fs::setPermissions(TempPath
, fs::group_all
), NoError
);
1565 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1567 EXPECT_EQ(fs::setPermissions(TempPath
, fs::others_read
), NoError
);
1568 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1570 EXPECT_EQ(fs::setPermissions(TempPath
, fs::others_write
), NoError
);
1571 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1573 EXPECT_EQ(fs::setPermissions(TempPath
, fs::others_exe
), NoError
);
1574 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1576 EXPECT_EQ(fs::setPermissions(TempPath
, fs::others_all
), NoError
);
1577 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1579 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_read
), NoError
);
1580 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1582 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_write
), NoError
);
1583 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1585 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_exe
), NoError
);
1586 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1588 EXPECT_EQ(fs::setPermissions(TempPath
, fs::set_uid_on_exe
), NoError
);
1589 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1591 EXPECT_EQ(fs::setPermissions(TempPath
, fs::set_gid_on_exe
), NoError
);
1592 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1594 EXPECT_EQ(fs::setPermissions(TempPath
, fs::sticky_bit
), NoError
);
1595 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1597 EXPECT_EQ(fs::setPermissions(TempPath
, fs::set_uid_on_exe
|
1598 fs::set_gid_on_exe
|
1601 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1603 EXPECT_EQ(fs::setPermissions(TempPath
, ReadOnly
| fs::set_uid_on_exe
|
1604 fs::set_gid_on_exe
|
1607 EXPECT_TRUE(CheckPermissions(ReadOnly
));
1609 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_perms
), NoError
);
1610 EXPECT_TRUE(CheckPermissions(fs::all_all
));
1612 EXPECT_EQ(fs::setPermissions(TempPath
, fs::no_perms
), NoError
);
1613 EXPECT_TRUE(CheckPermissions(fs::no_perms
));
1615 EXPECT_EQ(fs::setPermissions(TempPath
, fs::owner_read
), NoError
);
1616 EXPECT_TRUE(CheckPermissions(fs::owner_read
));
1618 EXPECT_EQ(fs::setPermissions(TempPath
, fs::owner_write
), NoError
);
1619 EXPECT_TRUE(CheckPermissions(fs::owner_write
));
1621 EXPECT_EQ(fs::setPermissions(TempPath
, fs::owner_exe
), NoError
);
1622 EXPECT_TRUE(CheckPermissions(fs::owner_exe
));
1624 EXPECT_EQ(fs::setPermissions(TempPath
, fs::owner_all
), NoError
);
1625 EXPECT_TRUE(CheckPermissions(fs::owner_all
));
1627 EXPECT_EQ(fs::setPermissions(TempPath
, fs::group_read
), NoError
);
1628 EXPECT_TRUE(CheckPermissions(fs::group_read
));
1630 EXPECT_EQ(fs::setPermissions(TempPath
, fs::group_write
), NoError
);
1631 EXPECT_TRUE(CheckPermissions(fs::group_write
));
1633 EXPECT_EQ(fs::setPermissions(TempPath
, fs::group_exe
), NoError
);
1634 EXPECT_TRUE(CheckPermissions(fs::group_exe
));
1636 EXPECT_EQ(fs::setPermissions(TempPath
, fs::group_all
), NoError
);
1637 EXPECT_TRUE(CheckPermissions(fs::group_all
));
1639 EXPECT_EQ(fs::setPermissions(TempPath
, fs::others_read
), NoError
);
1640 EXPECT_TRUE(CheckPermissions(fs::others_read
));
1642 EXPECT_EQ(fs::setPermissions(TempPath
, fs::others_write
), NoError
);
1643 EXPECT_TRUE(CheckPermissions(fs::others_write
));
1645 EXPECT_EQ(fs::setPermissions(TempPath
, fs::others_exe
), NoError
);
1646 EXPECT_TRUE(CheckPermissions(fs::others_exe
));
1648 EXPECT_EQ(fs::setPermissions(TempPath
, fs::others_all
), NoError
);
1649 EXPECT_TRUE(CheckPermissions(fs::others_all
));
1651 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_read
), NoError
);
1652 EXPECT_TRUE(CheckPermissions(fs::all_read
));
1654 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_write
), NoError
);
1655 EXPECT_TRUE(CheckPermissions(fs::all_write
));
1657 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_exe
), NoError
);
1658 EXPECT_TRUE(CheckPermissions(fs::all_exe
));
1660 EXPECT_EQ(fs::setPermissions(TempPath
, fs::set_uid_on_exe
), NoError
);
1661 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe
));
1663 EXPECT_EQ(fs::setPermissions(TempPath
, fs::set_gid_on_exe
), NoError
);
1664 EXPECT_TRUE(CheckPermissions(fs::set_gid_on_exe
));
1666 // Modern BSDs require root to set the sticky bit on files.
1667 // AIX without root will mask off (i.e., lose) the sticky bit on files.
1668 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && \
1670 EXPECT_EQ(fs::setPermissions(TempPath
, fs::sticky_bit
), NoError
);
1671 EXPECT_TRUE(CheckPermissions(fs::sticky_bit
));
1673 EXPECT_EQ(fs::setPermissions(TempPath
, fs::set_uid_on_exe
|
1674 fs::set_gid_on_exe
|
1677 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe
| fs::set_gid_on_exe
|
1680 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_read
| fs::set_uid_on_exe
|
1681 fs::set_gid_on_exe
|
1684 EXPECT_TRUE(CheckPermissions(fs::all_read
| fs::set_uid_on_exe
|
1685 fs::set_gid_on_exe
| fs::sticky_bit
));
1687 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_perms
), NoError
);
1688 EXPECT_TRUE(CheckPermissions(fs::all_perms
));
1689 #endif // !FreeBSD && !NetBSD && !OpenBSD && !AIX
1691 EXPECT_EQ(fs::setPermissions(TempPath
, fs::all_perms
& ~fs::sticky_bit
),
1693 EXPECT_TRUE(CheckPermissions(fs::all_perms
& ~fs::sticky_bit
));
1697 } // anonymous namespace