Path.cpp 52 KB

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