Path.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/Path.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/ScopeExit.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 "llvm/Testing/Support/Error.h"
  24. #include "gmock/gmock.h"
  25. #include "gtest/gtest.h"
  26. #ifdef _WIN32
  27. #include "llvm/ADT/ArrayRef.h"
  28. #include "llvm/Support/Chrono.h"
  29. #include <windows.h>
  30. #include <winerror.h>
  31. #endif
  32. #ifdef LLVM_ON_UNIX
  33. #include <pwd.h>
  34. #include <sys/stat.h>
  35. #endif
  36. using namespace llvm;
  37. using namespace llvm::sys;
  38. #define ASSERT_NO_ERROR(x) \
  39. if (std::error_code ASSERT_NO_ERROR_ec = x) { \
  40. SmallString<128> MessageStorage; \
  41. raw_svector_ostream Message(MessageStorage); \
  42. Message << #x ": did not return errc::success.\n" \
  43. << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
  44. << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
  45. GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
  46. } else { \
  47. }
  48. #define ASSERT_ERROR(x) \
  49. if (!x) { \
  50. SmallString<128> MessageStorage; \
  51. raw_svector_ostream Message(MessageStorage); \
  52. Message << #x ": did not return a failure error code.\n"; \
  53. GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
  54. }
  55. namespace {
  56. struct FileDescriptorCloser {
  57. explicit FileDescriptorCloser(int FD) : FD(FD) {}
  58. ~FileDescriptorCloser() { ::close(FD); }
  59. int FD;
  60. };
  61. TEST(is_separator, Works) {
  62. EXPECT_TRUE(path::is_separator('/'));
  63. EXPECT_FALSE(path::is_separator('\0'));
  64. EXPECT_FALSE(path::is_separator('-'));
  65. EXPECT_FALSE(path::is_separator(' '));
  66. EXPECT_TRUE(path::is_separator('\\', path::Style::windows));
  67. EXPECT_FALSE(path::is_separator('\\', path::Style::posix));
  68. #ifdef _WIN32
  69. EXPECT_TRUE(path::is_separator('\\'));
  70. #else
  71. EXPECT_FALSE(path::is_separator('\\'));
  72. #endif
  73. }
  74. TEST(Support, Path) {
  75. SmallVector<StringRef, 40> paths;
  76. paths.push_back("");
  77. paths.push_back(".");
  78. paths.push_back("..");
  79. paths.push_back("foo");
  80. paths.push_back("/");
  81. paths.push_back("/foo");
  82. paths.push_back("foo/");
  83. paths.push_back("/foo/");
  84. paths.push_back("foo/bar");
  85. paths.push_back("/foo/bar");
  86. paths.push_back("//net");
  87. paths.push_back("//net/");
  88. paths.push_back("//net/foo");
  89. paths.push_back("///foo///");
  90. paths.push_back("///foo///bar");
  91. paths.push_back("/.");
  92. paths.push_back("./");
  93. paths.push_back("/..");
  94. paths.push_back("../");
  95. paths.push_back("foo/.");
  96. paths.push_back("foo/..");
  97. paths.push_back("foo/./");
  98. paths.push_back("foo/./bar");
  99. paths.push_back("foo/..");
  100. paths.push_back("foo/../");
  101. paths.push_back("foo/../bar");
  102. paths.push_back("c:");
  103. paths.push_back("c:/");
  104. paths.push_back("c:foo");
  105. paths.push_back("c:/foo");
  106. paths.push_back("c:foo/");
  107. paths.push_back("c:/foo/");
  108. paths.push_back("c:/foo/bar");
  109. paths.push_back("prn:");
  110. paths.push_back("c:\\");
  111. paths.push_back("c:foo");
  112. paths.push_back("c:\\foo");
  113. paths.push_back("c:foo\\");
  114. paths.push_back("c:\\foo\\");
  115. paths.push_back("c:\\foo/");
  116. paths.push_back("c:/foo\\bar");
  117. for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
  118. e = paths.end();
  119. i != e;
  120. ++i) {
  121. SCOPED_TRACE(*i);
  122. SmallVector<StringRef, 5> ComponentStack;
  123. for (sys::path::const_iterator ci = sys::path::begin(*i),
  124. ce = sys::path::end(*i);
  125. ci != ce;
  126. ++ci) {
  127. EXPECT_FALSE(ci->empty());
  128. ComponentStack.push_back(*ci);
  129. }
  130. SmallVector<StringRef, 5> ReverseComponentStack;
  131. for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
  132. ce = sys::path::rend(*i);
  133. ci != ce;
  134. ++ci) {
  135. EXPECT_FALSE(ci->empty());
  136. ReverseComponentStack.push_back(*ci);
  137. }
  138. std::reverse(ReverseComponentStack.begin(), ReverseComponentStack.end());
  139. EXPECT_THAT(ComponentStack, testing::ContainerEq(ReverseComponentStack));
  140. // Crash test most of the API - since we're iterating over all of our paths
  141. // here there isn't really anything reasonable to assert on in the results.
  142. (void)path::has_root_path(*i);
  143. (void)path::root_path(*i);
  144. (void)path::has_root_name(*i);
  145. (void)path::root_name(*i);
  146. (void)path::has_root_directory(*i);
  147. (void)path::root_directory(*i);
  148. (void)path::has_parent_path(*i);
  149. (void)path::parent_path(*i);
  150. (void)path::has_filename(*i);
  151. (void)path::filename(*i);
  152. (void)path::has_stem(*i);
  153. (void)path::stem(*i);
  154. (void)path::has_extension(*i);
  155. (void)path::extension(*i);
  156. (void)path::is_absolute(*i);
  157. (void)path::is_relative(*i);
  158. SmallString<128> temp_store;
  159. temp_store = *i;
  160. ASSERT_NO_ERROR(fs::make_absolute(temp_store));
  161. temp_store = *i;
  162. path::remove_filename(temp_store);
  163. temp_store = *i;
  164. path::replace_extension(temp_store, "ext");
  165. StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
  166. stem = path::stem(filename);
  167. ext = path::extension(filename);
  168. EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
  169. path::native(*i, temp_store);
  170. }
  171. {
  172. SmallString<32> Relative("foo.cpp");
  173. sys::fs::make_absolute("/root", Relative);
  174. Relative[5] = '/'; // Fix up windows paths.
  175. ASSERT_EQ("/root/foo.cpp", Relative);
  176. }
  177. {
  178. SmallString<32> Relative("foo.cpp");
  179. sys::fs::make_absolute("//root", Relative);
  180. Relative[6] = '/'; // Fix up windows paths.
  181. ASSERT_EQ("//root/foo.cpp", Relative);
  182. }
  183. }
  184. TEST(Support, FilenameParent) {
  185. EXPECT_EQ("/", path::filename("/"));
  186. EXPECT_EQ("", path::parent_path("/"));
  187. EXPECT_EQ("\\", path::filename("c:\\", path::Style::windows));
  188. EXPECT_EQ("c:", path::parent_path("c:\\", path::Style::windows));
  189. EXPECT_EQ("/", path::filename("///"));
  190. EXPECT_EQ("", path::parent_path("///"));
  191. EXPECT_EQ("\\", path::filename("c:\\\\", path::Style::windows));
  192. EXPECT_EQ("c:", path::parent_path("c:\\\\", path::Style::windows));
  193. EXPECT_EQ("bar", path::filename("/foo/bar"));
  194. EXPECT_EQ("/foo", path::parent_path("/foo/bar"));
  195. EXPECT_EQ("foo", path::filename("/foo"));
  196. EXPECT_EQ("/", path::parent_path("/foo"));
  197. EXPECT_EQ("foo", path::filename("foo"));
  198. EXPECT_EQ("", path::parent_path("foo"));
  199. EXPECT_EQ(".", path::filename("foo/"));
  200. EXPECT_EQ("foo", path::parent_path("foo/"));
  201. EXPECT_EQ("//net", path::filename("//net"));
  202. EXPECT_EQ("", path::parent_path("//net"));
  203. EXPECT_EQ("/", path::filename("//net/"));
  204. EXPECT_EQ("//net", path::parent_path("//net/"));
  205. EXPECT_EQ("foo", path::filename("//net/foo"));
  206. EXPECT_EQ("//net/", path::parent_path("//net/foo"));
  207. // These checks are just to make sure we do something reasonable with the
  208. // paths below. They are not meant to prescribe the one true interpretation of
  209. // these paths. Other decompositions (e.g. "//" -> "" + "//") are also
  210. // possible.
  211. EXPECT_EQ("/", path::filename("//"));
  212. EXPECT_EQ("", path::parent_path("//"));
  213. EXPECT_EQ("\\", path::filename("\\\\", path::Style::windows));
  214. EXPECT_EQ("", path::parent_path("\\\\", path::Style::windows));
  215. EXPECT_EQ("\\", path::filename("\\\\\\", path::Style::windows));
  216. EXPECT_EQ("", path::parent_path("\\\\\\", path::Style::windows));
  217. }
  218. static std::vector<StringRef>
  219. GetComponents(StringRef Path, path::Style S = path::Style::native) {
  220. return {path::begin(Path, S), path::end(Path)};
  221. }
  222. TEST(Support, PathIterator) {
  223. EXPECT_THAT(GetComponents("/foo"), testing::ElementsAre("/", "foo"));
  224. EXPECT_THAT(GetComponents("/"), testing::ElementsAre("/"));
  225. EXPECT_THAT(GetComponents("//"), testing::ElementsAre("/"));
  226. EXPECT_THAT(GetComponents("///"), testing::ElementsAre("/"));
  227. EXPECT_THAT(GetComponents("c/d/e/foo.txt"),
  228. testing::ElementsAre("c", "d", "e", "foo.txt"));
  229. EXPECT_THAT(GetComponents(".c/.d/../."),
  230. testing::ElementsAre(".c", ".d", "..", "."));
  231. EXPECT_THAT(GetComponents("/c/d/e/foo.txt"),
  232. testing::ElementsAre("/", "c", "d", "e", "foo.txt"));
  233. EXPECT_THAT(GetComponents("/.c/.d/../."),
  234. testing::ElementsAre("/", ".c", ".d", "..", "."));
  235. EXPECT_THAT(GetComponents("c:\\c\\e\\foo.txt", path::Style::windows),
  236. testing::ElementsAre("c:", "\\", "c", "e", "foo.txt"));
  237. EXPECT_THAT(GetComponents("//net/"), testing::ElementsAre("//net", "/"));
  238. EXPECT_THAT(GetComponents("//net/c/foo.txt"),
  239. testing::ElementsAre("//net", "/", "c", "foo.txt"));
  240. }
  241. TEST(Support, AbsolutePathIteratorEnd) {
  242. // Trailing slashes are converted to '.' unless they are part of the root path.
  243. SmallVector<std::pair<StringRef, path::Style>, 4> Paths;
  244. Paths.emplace_back("/foo/", path::Style::native);
  245. Paths.emplace_back("/foo//", path::Style::native);
  246. Paths.emplace_back("//net/foo/", path::Style::native);
  247. Paths.emplace_back("c:\\foo\\", path::Style::windows);
  248. for (auto &Path : Paths) {
  249. SCOPED_TRACE(Path.first);
  250. StringRef LastComponent = *path::rbegin(Path.first, Path.second);
  251. EXPECT_EQ(".", LastComponent);
  252. }
  253. SmallVector<std::pair<StringRef, path::Style>, 3> RootPaths;
  254. RootPaths.emplace_back("/", path::Style::native);
  255. RootPaths.emplace_back("//net/", path::Style::native);
  256. RootPaths.emplace_back("c:\\", path::Style::windows);
  257. RootPaths.emplace_back("//net//", path::Style::native);
  258. RootPaths.emplace_back("c:\\\\", path::Style::windows);
  259. for (auto &Path : RootPaths) {
  260. SCOPED_TRACE(Path.first);
  261. StringRef LastComponent = *path::rbegin(Path.first, Path.second);
  262. EXPECT_EQ(1u, LastComponent.size());
  263. EXPECT_TRUE(path::is_separator(LastComponent[0], Path.second));
  264. }
  265. }
  266. TEST(Support, HomeDirectory) {
  267. std::string expected;
  268. #ifdef _WIN32
  269. if (wchar_t const *path = ::_wgetenv(L"USERPROFILE")) {
  270. auto pathLen = ::wcslen(path);
  271. ArrayRef<char> ref{reinterpret_cast<char const *>(path),
  272. pathLen * sizeof(wchar_t)};
  273. convertUTF16ToUTF8String(ref, expected);
  274. }
  275. #else
  276. if (char const *path = ::getenv("HOME"))
  277. expected = path;
  278. #endif
  279. // Do not try to test it if we don't know what to expect.
  280. // On Windows we use something better than env vars.
  281. if (!expected.empty()) {
  282. SmallString<128> HomeDir;
  283. auto status = path::home_directory(HomeDir);
  284. EXPECT_TRUE(status);
  285. EXPECT_EQ(expected, HomeDir);
  286. }
  287. }
  288. #ifdef LLVM_ON_UNIX
  289. TEST(Support, HomeDirectoryWithNoEnv) {
  290. std::string OriginalStorage;
  291. char const *OriginalEnv = ::getenv("HOME");
  292. if (OriginalEnv) {
  293. // We're going to unset it, so make a copy and save a pointer to the copy
  294. // so that we can reset it at the end of the test.
  295. OriginalStorage = OriginalEnv;
  296. OriginalEnv = OriginalStorage.c_str();
  297. }
  298. // Don't run the test if we have nothing to compare against.
  299. struct passwd *pw = getpwuid(getuid());
  300. if (!pw || !pw->pw_dir) return;
  301. ::unsetenv("HOME");
  302. EXPECT_EQ(nullptr, ::getenv("HOME"));
  303. std::string PwDir = pw->pw_dir;
  304. SmallString<128> HomeDir;
  305. auto status = path::home_directory(HomeDir);
  306. EXPECT_TRUE(status);
  307. EXPECT_EQ(PwDir, HomeDir);
  308. // Now put the environment back to its original state (meaning that if it was
  309. // unset before, we don't reset it).
  310. if (OriginalEnv) ::setenv("HOME", OriginalEnv, 1);
  311. }
  312. #endif
  313. TEST(Support, TempDirectory) {
  314. SmallString<32> TempDir;
  315. path::system_temp_directory(false, TempDir);
  316. EXPECT_TRUE(!TempDir.empty());
  317. TempDir.clear();
  318. path::system_temp_directory(true, TempDir);
  319. EXPECT_TRUE(!TempDir.empty());
  320. }
  321. #ifdef _WIN32
  322. static std::string path2regex(std::string Path) {
  323. size_t Pos = 0;
  324. while ((Pos = Path.find('\\', Pos)) != std::string::npos) {
  325. Path.replace(Pos, 1, "\\\\");
  326. Pos += 2;
  327. }
  328. return Path;
  329. }
  330. /// Helper for running temp dir test in separated process. See below.
  331. #define EXPECT_TEMP_DIR(prepare, expected) \
  332. EXPECT_EXIT( \
  333. { \
  334. prepare; \
  335. SmallString<300> TempDir; \
  336. path::system_temp_directory(true, TempDir); \
  337. raw_os_ostream(std::cerr) << TempDir; \
  338. std::exit(0); \
  339. }, \
  340. ::testing::ExitedWithCode(0), path2regex(expected))
  341. TEST(SupportDeathTest, TempDirectoryOnWindows) {
  342. // In this test we want to check how system_temp_directory responds to
  343. // different values of specific env vars. To prevent corrupting env vars of
  344. // the current process all checks are done in separated processes.
  345. EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder");
  346. EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"),
  347. "C:\\Unix\\Path\\Seperators");
  348. EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$");
  349. EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"F:\\TrailingSep\\"), "F:\\TrailingSep");
  350. EXPECT_TEMP_DIR(
  351. _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"),
  352. "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80");
  353. // Test $TMP empty, $TEMP set.
  354. EXPECT_TEMP_DIR(
  355. {
  356. _wputenv_s(L"TMP", L"");
  357. _wputenv_s(L"TEMP", L"C:\\Valid\\Path");
  358. },
  359. "C:\\Valid\\Path");
  360. // All related env vars empty
  361. EXPECT_TEMP_DIR(
  362. {
  363. _wputenv_s(L"TMP", L"");
  364. _wputenv_s(L"TEMP", L"");
  365. _wputenv_s(L"USERPROFILE", L"");
  366. },
  367. "C:\\Temp");
  368. // Test evn var / path with 260 chars.
  369. SmallString<270> Expected{"C:\\Temp\\AB\\123456789"};
  370. while (Expected.size() < 260)
  371. Expected.append("\\DirNameWith19Charss");
  372. ASSERT_EQ(260U, Expected.size());
  373. EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str());
  374. }
  375. #endif
  376. class FileSystemTest : public testing::Test {
  377. protected:
  378. /// Unique temporary directory in which all created filesystem entities must
  379. /// be placed. It is removed at the end of each test (must be empty).
  380. SmallString<128> TestDirectory;
  381. SmallString<128> NonExistantFile;
  382. void SetUp() override {
  383. ASSERT_NO_ERROR(
  384. fs::createUniqueDirectory("file-system-test", TestDirectory));
  385. // We don't care about this specific file.
  386. errs() << "Test Directory: " << TestDirectory << '\n';
  387. errs().flush();
  388. NonExistantFile = TestDirectory;
  389. // Even though this value is hardcoded, is a 128-bit GUID, so we should be
  390. // guaranteed that this file will never exist.
  391. sys::path::append(NonExistantFile, "1B28B495C16344CB9822E588CD4C3EF0");
  392. }
  393. void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
  394. };
  395. TEST_F(FileSystemTest, Unique) {
  396. // Create a temp file.
  397. int FileDescriptor;
  398. SmallString<64> TempPath;
  399. ASSERT_NO_ERROR(
  400. fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
  401. // The same file should return an identical unique id.
  402. fs::UniqueID F1, F2;
  403. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
  404. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
  405. ASSERT_EQ(F1, F2);
  406. // Different files should return different unique ids.
  407. int FileDescriptor2;
  408. SmallString<64> TempPath2;
  409. ASSERT_NO_ERROR(
  410. fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
  411. fs::UniqueID D;
  412. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
  413. ASSERT_NE(D, F1);
  414. ::close(FileDescriptor2);
  415. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
  416. // Two paths representing the same file on disk should still provide the
  417. // same unique id. We can test this by making a hard link.
  418. ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
  419. fs::UniqueID D2;
  420. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
  421. ASSERT_EQ(D2, F1);
  422. ::close(FileDescriptor);
  423. SmallString<128> Dir1;
  424. ASSERT_NO_ERROR(
  425. fs::createUniqueDirectory("dir1", Dir1));
  426. ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
  427. ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
  428. ASSERT_EQ(F1, F2);
  429. SmallString<128> Dir2;
  430. ASSERT_NO_ERROR(
  431. fs::createUniqueDirectory("dir2", Dir2));
  432. ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
  433. ASSERT_NE(F1, F2);
  434. ASSERT_NO_ERROR(fs::remove(Dir1));
  435. ASSERT_NO_ERROR(fs::remove(Dir2));
  436. ASSERT_NO_ERROR(fs::remove(TempPath2));
  437. ASSERT_NO_ERROR(fs::remove(TempPath));
  438. }
  439. TEST_F(FileSystemTest, RealPath) {
  440. ASSERT_NO_ERROR(
  441. fs::create_directories(Twine(TestDirectory) + "/test1/test2/test3"));
  442. ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/test1/test2/test3"));
  443. SmallString<64> RealBase;
  444. SmallString<64> Expected;
  445. SmallString<64> Actual;
  446. // TestDirectory itself might be under a symlink or have been specified with
  447. // a different case than the existing temp directory. In such cases real_path
  448. // on the concatenated path will differ in the TestDirectory portion from
  449. // how we specified it. Make sure to compare against the real_path of the
  450. // TestDirectory, and not just the value of TestDirectory.
  451. ASSERT_NO_ERROR(fs::real_path(TestDirectory, RealBase));
  452. path::native(Twine(RealBase) + "/test1/test2", Expected);
  453. ASSERT_NO_ERROR(fs::real_path(
  454. Twine(TestDirectory) + "/././test1/../test1/test2/./test3/..", Actual));
  455. EXPECT_EQ(Expected, Actual);
  456. SmallString<64> HomeDir;
  457. // This can fail if $HOME is not set and getpwuid fails.
  458. bool Result = llvm::sys::path::home_directory(HomeDir);
  459. if (Result) {
  460. ASSERT_NO_ERROR(fs::real_path(HomeDir, Expected));
  461. ASSERT_NO_ERROR(fs::real_path("~", Actual, true));
  462. EXPECT_EQ(Expected, Actual);
  463. ASSERT_NO_ERROR(fs::real_path("~/", Actual, true));
  464. EXPECT_EQ(Expected, Actual);
  465. }
  466. ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
  467. }
  468. TEST_F(FileSystemTest, ExpandTilde) {
  469. SmallString<64> Expected;
  470. SmallString<64> Actual;
  471. SmallString<64> HomeDir;
  472. // This can fail if $HOME is not set and getpwuid fails.
  473. bool Result = llvm::sys::path::home_directory(HomeDir);
  474. if (Result) {
  475. fs::expand_tilde(HomeDir, Expected);
  476. fs::expand_tilde("~", Actual);
  477. EXPECT_EQ(Expected, Actual);
  478. #ifdef _WIN32
  479. Expected += "\\foo";
  480. fs::expand_tilde("~\\foo", Actual);
  481. #else
  482. Expected += "/foo";
  483. fs::expand_tilde("~/foo", Actual);
  484. #endif
  485. EXPECT_EQ(Expected, Actual);
  486. }
  487. }
  488. #ifdef LLVM_ON_UNIX
  489. TEST_F(FileSystemTest, RealPathNoReadPerm) {
  490. SmallString<64> Expanded;
  491. ASSERT_NO_ERROR(
  492. fs::create_directories(Twine(TestDirectory) + "/noreadperm"));
  493. ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/noreadperm"));
  494. fs::setPermissions(Twine(TestDirectory) + "/noreadperm", fs::no_perms);
  495. fs::setPermissions(Twine(TestDirectory) + "/noreadperm", fs::all_exe);
  496. ASSERT_NO_ERROR(fs::real_path(Twine(TestDirectory) + "/noreadperm", Expanded,
  497. false));
  498. ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/noreadperm"));
  499. }
  500. #endif
  501. TEST_F(FileSystemTest, TempFileKeepDiscard) {
  502. // We can keep then discard.
  503. auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
  504. ASSERT_TRUE((bool)TempFileOrError);
  505. fs::TempFile File = std::move(*TempFileOrError);
  506. ASSERT_EQ(-1, TempFileOrError->FD);
  507. ASSERT_FALSE((bool)File.keep(TestDirectory + "/keep"));
  508. ASSERT_FALSE((bool)File.discard());
  509. ASSERT_TRUE(fs::exists(TestDirectory + "/keep"));
  510. ASSERT_NO_ERROR(fs::remove(TestDirectory + "/keep"));
  511. }
  512. TEST_F(FileSystemTest, TempFileDiscardDiscard) {
  513. // We can discard twice.
  514. auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
  515. ASSERT_TRUE((bool)TempFileOrError);
  516. fs::TempFile File = std::move(*TempFileOrError);
  517. ASSERT_EQ(-1, TempFileOrError->FD);
  518. ASSERT_FALSE((bool)File.discard());
  519. ASSERT_FALSE((bool)File.discard());
  520. ASSERT_FALSE(fs::exists(TestDirectory + "/keep"));
  521. }
  522. TEST_F(FileSystemTest, TempFiles) {
  523. // Create a temp file.
  524. int FileDescriptor;
  525. SmallString<64> TempPath;
  526. ASSERT_NO_ERROR(
  527. fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
  528. // Make sure it exists.
  529. ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
  530. // Create another temp tile.
  531. int FD2;
  532. SmallString<64> TempPath2;
  533. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
  534. ASSERT_TRUE(TempPath2.endswith(".temp"));
  535. ASSERT_NE(TempPath.str(), TempPath2.str());
  536. fs::file_status A, B;
  537. ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
  538. ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
  539. EXPECT_FALSE(fs::equivalent(A, B));
  540. ::close(FD2);
  541. // Remove Temp2.
  542. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
  543. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
  544. ASSERT_EQ(fs::remove(Twine(TempPath2), false),
  545. errc::no_such_file_or_directory);
  546. std::error_code EC = fs::status(TempPath2.c_str(), B);
  547. EXPECT_EQ(EC, errc::no_such_file_or_directory);
  548. EXPECT_EQ(B.type(), fs::file_type::file_not_found);
  549. // Make sure Temp2 doesn't exist.
  550. ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
  551. errc::no_such_file_or_directory);
  552. SmallString<64> TempPath3;
  553. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
  554. ASSERT_FALSE(TempPath3.endswith("."));
  555. FileRemover Cleanup3(TempPath3);
  556. // Create a hard link to Temp1.
  557. ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
  558. bool equal;
  559. ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
  560. EXPECT_TRUE(equal);
  561. ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
  562. ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
  563. EXPECT_TRUE(fs::equivalent(A, B));
  564. // Remove Temp1.
  565. ::close(FileDescriptor);
  566. ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
  567. // Remove the hard link.
  568. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
  569. // Make sure Temp1 doesn't exist.
  570. ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
  571. errc::no_such_file_or_directory);
  572. #ifdef _WIN32
  573. // Path name > 260 chars should get an error.
  574. const char *Path270 =
  575. "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
  576. "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
  577. "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
  578. "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
  579. "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
  580. EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
  581. errc::invalid_argument);
  582. // Relative path < 247 chars, no problem.
  583. const char *Path216 =
  584. "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
  585. "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
  586. "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
  587. "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
  588. ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
  589. ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
  590. #endif
  591. }
  592. TEST_F(FileSystemTest, TempFileCollisions) {
  593. SmallString<128> TestDirectory;
  594. ASSERT_NO_ERROR(
  595. fs::createUniqueDirectory("CreateUniqueFileTest", TestDirectory));
  596. FileRemover Cleanup(TestDirectory);
  597. SmallString<128> Model = TestDirectory;
  598. path::append(Model, "%.tmp");
  599. SmallString<128> Path;
  600. std::vector<fs::TempFile> TempFiles;
  601. auto TryCreateTempFile = [&]() {
  602. Expected<fs::TempFile> T = fs::TempFile::create(Model);
  603. if (T) {
  604. TempFiles.push_back(std::move(*T));
  605. return true;
  606. } else {
  607. logAllUnhandledErrors(T.takeError(), errs(),
  608. "Failed to create temporary file: ");
  609. return false;
  610. }
  611. };
  612. // Our single-character template allows for 16 unique names. Check that
  613. // calling TryCreateTempFile repeatedly results in 16 successes.
  614. // Because the test depends on random numbers, it could theoretically fail.
  615. // However, the probability of this happening is tiny: with 32 calls, each
  616. // of which will retry up to 128 times, to not get a given digit we would
  617. // have to fail at least 15 + 17 * 128 = 2191 attempts. The probability of
  618. // 2191 attempts not producing a given hexadecimal digit is
  619. // (1 - 1/16) ** 2191 or 3.88e-62.
  620. int Successes = 0;
  621. for (int i = 0; i < 32; ++i)
  622. if (TryCreateTempFile()) ++Successes;
  623. EXPECT_EQ(Successes, 16);
  624. for (fs::TempFile &T : TempFiles)
  625. cantFail(T.discard());
  626. }
  627. TEST_F(FileSystemTest, CreateDir) {
  628. ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
  629. ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
  630. ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
  631. errc::file_exists);
  632. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
  633. #ifdef LLVM_ON_UNIX
  634. // Set a 0000 umask so that we can test our directory permissions.
  635. mode_t OldUmask = ::umask(0000);
  636. fs::file_status Status;
  637. ASSERT_NO_ERROR(
  638. fs::create_directory(Twine(TestDirectory) + "baz500", false,
  639. fs::perms::owner_read | fs::perms::owner_exe));
  640. ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz500", Status));
  641. ASSERT_EQ(Status.permissions() & fs::perms::all_all,
  642. fs::perms::owner_read | fs::perms::owner_exe);
  643. ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "baz777", false,
  644. fs::perms::all_all));
  645. ASSERT_NO_ERROR(fs::status(Twine(TestDirectory) + "baz777", Status));
  646. ASSERT_EQ(Status.permissions() & fs::perms::all_all, fs::perms::all_all);
  647. // Restore umask to be safe.
  648. ::umask(OldUmask);
  649. #endif
  650. #ifdef _WIN32
  651. // Prove that create_directories() can handle a pathname > 248 characters,
  652. // which is the documented limit for CreateDirectory().
  653. // (248 is MAX_PATH subtracting room for an 8.3 filename.)
  654. // Generate a directory path guaranteed to fall into that range.
  655. size_t TmpLen = TestDirectory.size();
  656. const char *OneDir = "\\123456789";
  657. size_t OneDirLen = strlen(OneDir);
  658. ASSERT_LT(OneDirLen, 12U);
  659. size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
  660. SmallString<260> LongDir(TestDirectory);
  661. for (size_t I = 0; I < NLevels; ++I)
  662. LongDir.append(OneDir);
  663. ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
  664. ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
  665. ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
  666. errc::file_exists);
  667. // Tidy up, "recursively" removing the directories.
  668. StringRef ThisDir(LongDir);
  669. for (size_t J = 0; J < NLevels; ++J) {
  670. ASSERT_NO_ERROR(fs::remove(ThisDir));
  671. ThisDir = path::parent_path(ThisDir);
  672. }
  673. // Also verify that paths with Unix separators are handled correctly.
  674. std::string LongPathWithUnixSeparators(TestDirectory.str());
  675. // Add at least one subdirectory to TestDirectory, and replace slashes with
  676. // backslashes
  677. do {
  678. LongPathWithUnixSeparators.append("/DirNameWith19Charss");
  679. } while (LongPathWithUnixSeparators.size() < 260);
  680. std::replace(LongPathWithUnixSeparators.begin(),
  681. LongPathWithUnixSeparators.end(),
  682. '\\', '/');
  683. ASSERT_NO_ERROR(fs::create_directories(Twine(LongPathWithUnixSeparators)));
  684. // cleanup
  685. ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) +
  686. "/DirNameWith19Charss"));
  687. // Similarly for a relative pathname. Need to set the current directory to
  688. // TestDirectory so that the one we create ends up in the right place.
  689. char PreviousDir[260];
  690. size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
  691. ASSERT_GT(PreviousDirLen, 0U);
  692. ASSERT_LT(PreviousDirLen, 260U);
  693. ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
  694. LongDir.clear();
  695. // Generate a relative directory name with absolute length > 248.
  696. size_t LongDirLen = 249 - TestDirectory.size();
  697. LongDir.assign(LongDirLen, 'a');
  698. ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
  699. // While we're here, prove that .. and . handling works in these long paths.
  700. const char *DotDotDirs = "\\..\\.\\b";
  701. LongDir.append(DotDotDirs);
  702. ASSERT_NO_ERROR(fs::create_directory("b"));
  703. ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
  704. // And clean up.
  705. ASSERT_NO_ERROR(fs::remove("b"));
  706. ASSERT_NO_ERROR(fs::remove(
  707. Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
  708. ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
  709. #endif
  710. }
  711. TEST_F(FileSystemTest, DirectoryIteration) {
  712. std::error_code ec;
  713. for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
  714. ASSERT_NO_ERROR(ec);
  715. // Create a known hierarchy to recurse over.
  716. ASSERT_NO_ERROR(
  717. fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
  718. ASSERT_NO_ERROR(
  719. fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
  720. ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
  721. "/recursive/dontlookhere/da1"));
  722. ASSERT_NO_ERROR(
  723. fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
  724. ASSERT_NO_ERROR(
  725. fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
  726. typedef std::vector<std::string> v_t;
  727. v_t visited;
  728. for (fs::recursive_directory_iterator i(Twine(TestDirectory)
  729. + "/recursive", ec), e; i != e; i.increment(ec)){
  730. ASSERT_NO_ERROR(ec);
  731. if (path::filename(i->path()) == "p1") {
  732. i.pop();
  733. // FIXME: recursive_directory_iterator should be more robust.
  734. if (i == e) break;
  735. }
  736. if (path::filename(i->path()) == "dontlookhere")
  737. i.no_push();
  738. visited.push_back(path::filename(i->path()));
  739. }
  740. v_t::const_iterator a0 = find(visited, "a0");
  741. v_t::const_iterator aa1 = find(visited, "aa1");
  742. v_t::const_iterator ab1 = find(visited, "ab1");
  743. v_t::const_iterator dontlookhere = find(visited, "dontlookhere");
  744. v_t::const_iterator da1 = find(visited, "da1");
  745. v_t::const_iterator z0 = find(visited, "z0");
  746. v_t::const_iterator za1 = find(visited, "za1");
  747. v_t::const_iterator pop = find(visited, "pop");
  748. v_t::const_iterator p1 = find(visited, "p1");
  749. // Make sure that each path was visited correctly.
  750. ASSERT_NE(a0, visited.end());
  751. ASSERT_NE(aa1, visited.end());
  752. ASSERT_NE(ab1, visited.end());
  753. ASSERT_NE(dontlookhere, visited.end());
  754. ASSERT_EQ(da1, visited.end()); // Not visited.
  755. ASSERT_NE(z0, visited.end());
  756. ASSERT_NE(za1, visited.end());
  757. ASSERT_NE(pop, visited.end());
  758. ASSERT_EQ(p1, visited.end()); // Not visited.
  759. // Make sure that parents were visited before children. No other ordering
  760. // guarantees can be made across siblings.
  761. ASSERT_LT(a0, aa1);
  762. ASSERT_LT(a0, ab1);
  763. ASSERT_LT(z0, za1);
  764. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
  765. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
  766. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
  767. ASSERT_NO_ERROR(
  768. fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
  769. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
  770. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
  771. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
  772. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
  773. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
  774. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
  775. // Test recursive_directory_iterator level()
  776. ASSERT_NO_ERROR(
  777. fs::create_directories(Twine(TestDirectory) + "/reclevel/a/b/c"));
  778. fs::recursive_directory_iterator I(Twine(TestDirectory) + "/reclevel", ec), E;
  779. for (int l = 0; I != E; I.increment(ec), ++l) {
  780. ASSERT_NO_ERROR(ec);
  781. EXPECT_EQ(I.level(), l);
  782. }
  783. EXPECT_EQ(I, E);
  784. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b/c"));
  785. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a/b"));
  786. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel/a"));
  787. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/reclevel"));
  788. }
  789. #ifdef LLVM_ON_UNIX
  790. TEST_F(FileSystemTest, BrokenSymlinkDirectoryIteration) {
  791. // Create a known hierarchy to recurse over.
  792. ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + "/symlink"));
  793. ASSERT_NO_ERROR(
  794. fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/a"));
  795. ASSERT_NO_ERROR(
  796. fs::create_directories(Twine(TestDirectory) + "/symlink/b/bb"));
  797. ASSERT_NO_ERROR(
  798. fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/ba"));
  799. ASSERT_NO_ERROR(
  800. fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/b/bc"));
  801. ASSERT_NO_ERROR(
  802. fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/c"));
  803. ASSERT_NO_ERROR(
  804. fs::create_directories(Twine(TestDirectory) + "/symlink/d/dd/ddd"));
  805. ASSERT_NO_ERROR(fs::create_link(Twine(TestDirectory) + "/symlink/d/dd",
  806. Twine(TestDirectory) + "/symlink/d/da"));
  807. ASSERT_NO_ERROR(
  808. fs::create_link("no_such_file", Twine(TestDirectory) + "/symlink/e"));
  809. typedef std::vector<std::string> v_t;
  810. v_t VisitedNonBrokenSymlinks;
  811. v_t VisitedBrokenSymlinks;
  812. std::error_code ec;
  813. using testing::UnorderedElementsAre;
  814. using testing::UnorderedElementsAreArray;
  815. // Broken symbol links are expected to throw an error.
  816. for (fs::directory_iterator i(Twine(TestDirectory) + "/symlink", ec), e;
  817. i != e; i.increment(ec)) {
  818. ASSERT_NO_ERROR(ec);
  819. if (i->status().getError() ==
  820. std::make_error_code(std::errc::no_such_file_or_directory)) {
  821. VisitedBrokenSymlinks.push_back(path::filename(i->path()));
  822. continue;
  823. }
  824. VisitedNonBrokenSymlinks.push_back(path::filename(i->path()));
  825. }
  826. EXPECT_THAT(VisitedNonBrokenSymlinks, UnorderedElementsAre("b", "d"));
  827. VisitedNonBrokenSymlinks.clear();
  828. EXPECT_THAT(VisitedBrokenSymlinks, UnorderedElementsAre("a", "c", "e"));
  829. VisitedBrokenSymlinks.clear();
  830. // Broken symbol links are expected to throw an error.
  831. for (fs::recursive_directory_iterator i(
  832. Twine(TestDirectory) + "/symlink", ec), e; i != e; i.increment(ec)) {
  833. ASSERT_NO_ERROR(ec);
  834. if (i->status().getError() ==
  835. std::make_error_code(std::errc::no_such_file_or_directory)) {
  836. VisitedBrokenSymlinks.push_back(path::filename(i->path()));
  837. continue;
  838. }
  839. VisitedNonBrokenSymlinks.push_back(path::filename(i->path()));
  840. }
  841. EXPECT_THAT(VisitedNonBrokenSymlinks,
  842. UnorderedElementsAre("b", "bb", "d", "da", "dd", "ddd", "ddd"));
  843. VisitedNonBrokenSymlinks.clear();
  844. EXPECT_THAT(VisitedBrokenSymlinks,
  845. UnorderedElementsAre("a", "ba", "bc", "c", "e"));
  846. VisitedBrokenSymlinks.clear();
  847. for (fs::recursive_directory_iterator i(
  848. Twine(TestDirectory) + "/symlink", ec, /*follow_symlinks=*/false), e;
  849. i != e; i.increment(ec)) {
  850. ASSERT_NO_ERROR(ec);
  851. if (i->status().getError() ==
  852. std::make_error_code(std::errc::no_such_file_or_directory)) {
  853. VisitedBrokenSymlinks.push_back(path::filename(i->path()));
  854. continue;
  855. }
  856. VisitedNonBrokenSymlinks.push_back(path::filename(i->path()));
  857. }
  858. EXPECT_THAT(VisitedNonBrokenSymlinks,
  859. UnorderedElementsAreArray({"a", "b", "ba", "bb", "bc", "c", "d",
  860. "da", "dd", "ddd", "e"}));
  861. VisitedNonBrokenSymlinks.clear();
  862. EXPECT_THAT(VisitedBrokenSymlinks, UnorderedElementsAre());
  863. VisitedBrokenSymlinks.clear();
  864. ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/symlink"));
  865. }
  866. #endif
  867. TEST_F(FileSystemTest, Remove) {
  868. SmallString<64> BaseDir;
  869. SmallString<64> Paths[4];
  870. int fds[4];
  871. ASSERT_NO_ERROR(fs::createUniqueDirectory("fs_remove", BaseDir));
  872. ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/baz"));
  873. ASSERT_NO_ERROR(fs::create_directories(Twine(BaseDir) + "/foo/bar/buzz"));
  874. ASSERT_NO_ERROR(fs::createUniqueFile(
  875. Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[0], Paths[0]));
  876. ASSERT_NO_ERROR(fs::createUniqueFile(
  877. Twine(BaseDir) + "/foo/bar/baz/%%%%%%.tmp", fds[1], Paths[1]));
  878. ASSERT_NO_ERROR(fs::createUniqueFile(
  879. Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[2], Paths[2]));
  880. ASSERT_NO_ERROR(fs::createUniqueFile(
  881. Twine(BaseDir) + "/foo/bar/buzz/%%%%%%.tmp", fds[3], Paths[3]));
  882. for (int fd : fds)
  883. ::close(fd);
  884. EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/baz"));
  885. EXPECT_TRUE(fs::exists(Twine(BaseDir) + "/foo/bar/buzz"));
  886. EXPECT_TRUE(fs::exists(Paths[0]));
  887. EXPECT_TRUE(fs::exists(Paths[1]));
  888. EXPECT_TRUE(fs::exists(Paths[2]));
  889. EXPECT_TRUE(fs::exists(Paths[3]));
  890. ASSERT_NO_ERROR(fs::remove_directories("D:/footest"));
  891. ASSERT_NO_ERROR(fs::remove_directories(BaseDir));
  892. ASSERT_FALSE(fs::exists(BaseDir));
  893. }
  894. #ifdef _WIN32
  895. TEST_F(FileSystemTest, CarriageReturn) {
  896. SmallString<128> FilePathname(TestDirectory);
  897. std::error_code EC;
  898. path::append(FilePathname, "test");
  899. {
  900. raw_fd_ostream File(FilePathname, EC, sys::fs::OF_Text);
  901. ASSERT_NO_ERROR(EC);
  902. File << '\n';
  903. }
  904. {
  905. auto Buf = MemoryBuffer::getFile(FilePathname.str());
  906. EXPECT_TRUE((bool)Buf);
  907. EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
  908. }
  909. {
  910. raw_fd_ostream File(FilePathname, EC, sys::fs::OF_None);
  911. ASSERT_NO_ERROR(EC);
  912. File << '\n';
  913. }
  914. {
  915. auto Buf = MemoryBuffer::getFile(FilePathname.str());
  916. EXPECT_TRUE((bool)Buf);
  917. EXPECT_EQ(Buf.get()->getBuffer(), "\n");
  918. }
  919. ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
  920. }
  921. #endif
  922. TEST_F(FileSystemTest, Resize) {
  923. int FD;
  924. SmallString<64> TempPath;
  925. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
  926. ASSERT_NO_ERROR(fs::resize_file(FD, 123));
  927. fs::file_status Status;
  928. ASSERT_NO_ERROR(fs::status(FD, Status));
  929. ASSERT_EQ(Status.getSize(), 123U);
  930. ::close(FD);
  931. ASSERT_NO_ERROR(fs::remove(TempPath));
  932. }
  933. TEST_F(FileSystemTest, MD5) {
  934. int FD;
  935. SmallString<64> TempPath;
  936. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
  937. StringRef Data("abcdefghijklmnopqrstuvwxyz");
  938. ASSERT_EQ(write(FD, Data.data(), Data.size()), static_cast<ssize_t>(Data.size()));
  939. lseek(FD, 0, SEEK_SET);
  940. auto Hash = fs::md5_contents(FD);
  941. ::close(FD);
  942. ASSERT_NO_ERROR(Hash.getError());
  943. EXPECT_STREQ("c3fcd3d76192e4007dfb496cca67e13b", Hash->digest().c_str());
  944. }
  945. TEST_F(FileSystemTest, FileMapping) {
  946. // Create a temp file.
  947. int FileDescriptor;
  948. SmallString<64> TempPath;
  949. ASSERT_NO_ERROR(
  950. fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
  951. unsigned Size = 4096;
  952. ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
  953. // Map in temp file and add some content
  954. std::error_code EC;
  955. StringRef Val("hello there");
  956. {
  957. fs::mapped_file_region mfr(fs::convertFDToNativeFile(FileDescriptor),
  958. fs::mapped_file_region::readwrite, Size, 0, EC);
  959. ASSERT_NO_ERROR(EC);
  960. std::copy(Val.begin(), Val.end(), mfr.data());
  961. // Explicitly add a 0.
  962. mfr.data()[Val.size()] = 0;
  963. // Unmap temp file
  964. }
  965. ASSERT_EQ(close(FileDescriptor), 0);
  966. // Map it back in read-only
  967. {
  968. int FD;
  969. EC = fs::openFileForRead(Twine(TempPath), FD);
  970. ASSERT_NO_ERROR(EC);
  971. fs::mapped_file_region mfr(fs::convertFDToNativeFile(FD),
  972. fs::mapped_file_region::readonly, Size, 0, EC);
  973. ASSERT_NO_ERROR(EC);
  974. // Verify content
  975. EXPECT_EQ(StringRef(mfr.const_data()), Val);
  976. // Unmap temp file
  977. fs::mapped_file_region m(fs::convertFDToNativeFile(FD),
  978. fs::mapped_file_region::readonly, Size, 0, EC);
  979. ASSERT_NO_ERROR(EC);
  980. ASSERT_EQ(close(FD), 0);
  981. }
  982. ASSERT_NO_ERROR(fs::remove(TempPath));
  983. }
  984. TEST(Support, NormalizePath) {
  985. using TestTuple = std::tuple<const char *, const char *, const char *>;
  986. std::vector<TestTuple> Tests;
  987. Tests.emplace_back("a", "a", "a");
  988. Tests.emplace_back("a/b", "a\\b", "a/b");
  989. Tests.emplace_back("a\\b", "a\\b", "a/b");
  990. Tests.emplace_back("a\\\\b", "a\\\\b", "a\\\\b");
  991. Tests.emplace_back("\\a", "\\a", "/a");
  992. Tests.emplace_back("a\\", "a\\", "a/");
  993. for (auto &T : Tests) {
  994. SmallString<64> Win(std::get<0>(T));
  995. SmallString<64> Posix(Win);
  996. path::native(Win, path::Style::windows);
  997. path::native(Posix, path::Style::posix);
  998. EXPECT_EQ(std::get<1>(T), Win);
  999. EXPECT_EQ(std::get<2>(T), Posix);
  1000. }
  1001. #if defined(_WIN32)
  1002. SmallString<64> PathHome;
  1003. path::home_directory(PathHome);
  1004. const char *Path7a = "~/aaa";
  1005. SmallString<64> Path7(Path7a);
  1006. path::native(Path7);
  1007. EXPECT_TRUE(Path7.endswith("\\aaa"));
  1008. EXPECT_TRUE(Path7.startswith(PathHome));
  1009. EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1));
  1010. const char *Path8a = "~";
  1011. SmallString<64> Path8(Path8a);
  1012. path::native(Path8);
  1013. EXPECT_EQ(Path8, PathHome);
  1014. const char *Path9a = "~aaa";
  1015. SmallString<64> Path9(Path9a);
  1016. path::native(Path9);
  1017. EXPECT_EQ(Path9, "~aaa");
  1018. const char *Path10a = "aaa/~/b";
  1019. SmallString<64> Path10(Path10a);
  1020. path::native(Path10);
  1021. EXPECT_EQ(Path10, "aaa\\~\\b");
  1022. #endif
  1023. }
  1024. TEST(Support, RemoveLeadingDotSlash) {
  1025. StringRef Path1("././/foolz/wat");
  1026. StringRef Path2("./////");
  1027. Path1 = path::remove_leading_dotslash(Path1);
  1028. EXPECT_EQ(Path1, "foolz/wat");
  1029. Path2 = path::remove_leading_dotslash(Path2);
  1030. EXPECT_EQ(Path2, "");
  1031. }
  1032. static std::string remove_dots(StringRef path, bool remove_dot_dot,
  1033. path::Style style) {
  1034. SmallString<256> buffer(path);
  1035. path::remove_dots(buffer, remove_dot_dot, style);
  1036. return buffer.str();
  1037. }
  1038. TEST(Support, RemoveDots) {
  1039. EXPECT_EQ("foolz\\wat",
  1040. remove_dots(".\\.\\\\foolz\\wat", false, path::Style::windows));
  1041. EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false, path::Style::windows));
  1042. EXPECT_EQ("a\\..\\b\\c",
  1043. remove_dots(".\\a\\..\\b\\c", false, path::Style::windows));
  1044. EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true, path::Style::windows));
  1045. EXPECT_EQ("c", remove_dots(".\\.\\c", true, path::Style::windows));
  1046. EXPECT_EQ("..\\a\\c",
  1047. remove_dots("..\\a\\b\\..\\c", true, path::Style::windows));
  1048. EXPECT_EQ("..\\..\\a\\c",
  1049. remove_dots("..\\..\\a\\b\\..\\c", true, path::Style::windows));
  1050. SmallString<64> Path1(".\\.\\c");
  1051. EXPECT_TRUE(path::remove_dots(Path1, true, path::Style::windows));
  1052. EXPECT_EQ("c", Path1);
  1053. EXPECT_EQ("foolz/wat",
  1054. remove_dots("././/foolz/wat", false, path::Style::posix));
  1055. EXPECT_EQ("", remove_dots("./////", false, path::Style::posix));
  1056. EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false, path::Style::posix));
  1057. EXPECT_EQ("b/c", remove_dots("./a/../b/c", true, path::Style::posix));
  1058. EXPECT_EQ("c", remove_dots("././c", true, path::Style::posix));
  1059. EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true, path::Style::posix));
  1060. EXPECT_EQ("../../a/c",
  1061. remove_dots("../../a/b/../c", true, path::Style::posix));
  1062. EXPECT_EQ("/a/c", remove_dots("/../../a/c", true, path::Style::posix));
  1063. EXPECT_EQ("/a/c",
  1064. remove_dots("/../a/b//../././/c", true, path::Style::posix));
  1065. SmallString<64> Path2("././c");
  1066. EXPECT_TRUE(path::remove_dots(Path2, true, path::Style::posix));
  1067. EXPECT_EQ("c", Path2);
  1068. }
  1069. TEST(Support, ReplacePathPrefix) {
  1070. SmallString<64> Path1("/foo");
  1071. SmallString<64> Path2("/old/foo");
  1072. SmallString<64> OldPrefix("/old");
  1073. SmallString<64> NewPrefix("/new");
  1074. SmallString<64> NewPrefix2("/longernew");
  1075. SmallString<64> EmptyPrefix("");
  1076. SmallString<64> Path = Path1;
  1077. path::replace_path_prefix(Path, OldPrefix, NewPrefix);
  1078. EXPECT_EQ(Path, "/foo");
  1079. Path = Path2;
  1080. path::replace_path_prefix(Path, OldPrefix, NewPrefix);
  1081. EXPECT_EQ(Path, "/new/foo");
  1082. Path = Path2;
  1083. path::replace_path_prefix(Path, OldPrefix, NewPrefix2);
  1084. EXPECT_EQ(Path, "/longernew/foo");
  1085. Path = Path1;
  1086. path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
  1087. EXPECT_EQ(Path, "/new/foo");
  1088. Path = Path2;
  1089. path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
  1090. EXPECT_EQ(Path, "/foo");
  1091. }
  1092. TEST_F(FileSystemTest, OpenFileForRead) {
  1093. // Create a temp file.
  1094. int FileDescriptor;
  1095. SmallString<64> TempPath;
  1096. ASSERT_NO_ERROR(
  1097. fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
  1098. FileRemover Cleanup(TempPath);
  1099. // Make sure it exists.
  1100. ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
  1101. // Open the file for read
  1102. int FileDescriptor2;
  1103. SmallString<64> ResultPath;
  1104. ASSERT_NO_ERROR(fs::openFileForRead(Twine(TempPath), FileDescriptor2,
  1105. fs::OF_None, &ResultPath))
  1106. // If we succeeded, check that the paths are the same (modulo case):
  1107. if (!ResultPath.empty()) {
  1108. // The paths returned by createTemporaryFile and getPathFromOpenFD
  1109. // should reference the same file on disk.
  1110. fs::UniqueID D1, D2;
  1111. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
  1112. ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
  1113. ASSERT_EQ(D1, D2);
  1114. }
  1115. ::close(FileDescriptor);
  1116. ::close(FileDescriptor2);
  1117. #ifdef _WIN32
  1118. // Since Windows Vista, file access time is not updated by default.
  1119. // This is instead updated manually by openFileForRead.
  1120. // https://blogs.technet.microsoft.com/filecab/2006/11/07/disabling-last-access-time-in-windows-vista-to-improve-ntfs-performance/
  1121. // This part of the unit test is Windows specific as the updating of
  1122. // access times can be disabled on Linux using /etc/fstab.
  1123. // Set access time to UNIX epoch.
  1124. ASSERT_NO_ERROR(sys::fs::openFileForWrite(Twine(TempPath), FileDescriptor,
  1125. fs::CD_OpenExisting));
  1126. TimePoint<> Epoch(std::chrono::milliseconds(0));
  1127. ASSERT_NO_ERROR(fs::setLastAccessAndModificationTime(FileDescriptor, Epoch));
  1128. ::close(FileDescriptor);
  1129. // Open the file and ensure access time is updated, when forced.
  1130. ASSERT_NO_ERROR(fs::openFileForRead(Twine(TempPath), FileDescriptor,
  1131. fs::OF_UpdateAtime, &ResultPath));
  1132. sys::fs::file_status Status;
  1133. ASSERT_NO_ERROR(sys::fs::status(FileDescriptor, Status));
  1134. auto FileAccessTime = Status.getLastAccessedTime();
  1135. ASSERT_NE(Epoch, FileAccessTime);
  1136. ::close(FileDescriptor);
  1137. // Ideally this test would include a case when ATime is not forced to update,
  1138. // however the expected behaviour will differ depending on the configuration
  1139. // of the Windows file system.
  1140. #endif
  1141. }
  1142. static void createFileWithData(const Twine &Path, bool ShouldExistBefore,
  1143. fs::CreationDisposition Disp, StringRef Data) {
  1144. int FD;
  1145. ASSERT_EQ(ShouldExistBefore, fs::exists(Path));
  1146. ASSERT_NO_ERROR(fs::openFileForWrite(Path, FD, Disp));
  1147. FileDescriptorCloser Closer(FD);
  1148. ASSERT_TRUE(fs::exists(Path));
  1149. ASSERT_EQ(Data.size(), (size_t)write(FD, Data.data(), Data.size()));
  1150. }
  1151. static void verifyFileContents(const Twine &Path, StringRef Contents) {
  1152. auto Buffer = MemoryBuffer::getFile(Path);
  1153. ASSERT_TRUE((bool)Buffer);
  1154. StringRef Data = Buffer.get()->getBuffer();
  1155. ASSERT_EQ(Data, Contents);
  1156. }
  1157. TEST_F(FileSystemTest, CreateNew) {
  1158. int FD;
  1159. Optional<FileDescriptorCloser> Closer;
  1160. // Succeeds if the file does not exist.
  1161. ASSERT_FALSE(fs::exists(NonExistantFile));
  1162. ASSERT_NO_ERROR(fs::openFileForWrite(NonExistantFile, FD, fs::CD_CreateNew));
  1163. ASSERT_TRUE(fs::exists(NonExistantFile));
  1164. FileRemover Cleanup(NonExistantFile);
  1165. Closer.emplace(FD);
  1166. // And creates a file of size 0.
  1167. sys::fs::file_status Status;
  1168. ASSERT_NO_ERROR(sys::fs::status(FD, Status));
  1169. EXPECT_EQ(0ULL, Status.getSize());
  1170. // Close this first, before trying to re-open the file.
  1171. Closer.reset();
  1172. // But fails if the file does exist.
  1173. ASSERT_ERROR(fs::openFileForWrite(NonExistantFile, FD, fs::CD_CreateNew));
  1174. }
  1175. TEST_F(FileSystemTest, CreateAlways) {
  1176. int FD;
  1177. Optional<FileDescriptorCloser> Closer;
  1178. // Succeeds if the file does not exist.
  1179. ASSERT_FALSE(fs::exists(NonExistantFile));
  1180. ASSERT_NO_ERROR(
  1181. fs::openFileForWrite(NonExistantFile, FD, fs::CD_CreateAlways));
  1182. Closer.emplace(FD);
  1183. ASSERT_TRUE(fs::exists(NonExistantFile));
  1184. FileRemover Cleanup(NonExistantFile);
  1185. // And creates a file of size 0.
  1186. uint64_t FileSize;
  1187. ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile, FileSize));
  1188. ASSERT_EQ(0ULL, FileSize);
  1189. // If we write some data to it re-create it with CreateAlways, it succeeds and
  1190. // truncates to 0 bytes.
  1191. ASSERT_EQ(4, write(FD, "Test", 4));
  1192. Closer.reset();
  1193. ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile, FileSize));
  1194. ASSERT_EQ(4ULL, FileSize);
  1195. ASSERT_NO_ERROR(
  1196. fs::openFileForWrite(NonExistantFile, FD, fs::CD_CreateAlways));
  1197. Closer.emplace(FD);
  1198. ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile, FileSize));
  1199. ASSERT_EQ(0ULL, FileSize);
  1200. }
  1201. TEST_F(FileSystemTest, OpenExisting) {
  1202. int FD;
  1203. // Fails if the file does not exist.
  1204. ASSERT_FALSE(fs::exists(NonExistantFile));
  1205. ASSERT_ERROR(fs::openFileForWrite(NonExistantFile, FD, fs::CD_OpenExisting));
  1206. ASSERT_FALSE(fs::exists(NonExistantFile));
  1207. // Make a dummy file now so that we can try again when the file does exist.
  1208. createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "Fizz");
  1209. FileRemover Cleanup(NonExistantFile);
  1210. uint64_t FileSize;
  1211. ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile, FileSize));
  1212. ASSERT_EQ(4ULL, FileSize);
  1213. // If we re-create it with different data, it overwrites rather than
  1214. // appending.
  1215. createFileWithData(NonExistantFile, true, fs::CD_OpenExisting, "Buzz");
  1216. verifyFileContents(NonExistantFile, "Buzz");
  1217. }
  1218. TEST_F(FileSystemTest, OpenAlways) {
  1219. // Succeeds if the file does not exist.
  1220. createFileWithData(NonExistantFile, false, fs::CD_OpenAlways, "Fizz");
  1221. FileRemover Cleanup(NonExistantFile);
  1222. uint64_t FileSize;
  1223. ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile, FileSize));
  1224. ASSERT_EQ(4ULL, FileSize);
  1225. // Now re-open it and write again, verifying the contents get over-written.
  1226. createFileWithData(NonExistantFile, true, fs::CD_OpenAlways, "Bu");
  1227. verifyFileContents(NonExistantFile, "Buzz");
  1228. }
  1229. TEST_F(FileSystemTest, AppendSetsCorrectFileOffset) {
  1230. fs::CreationDisposition Disps[] = {fs::CD_CreateAlways, fs::CD_OpenAlways,
  1231. fs::CD_OpenExisting};
  1232. // Write some data and re-open it with every possible disposition (this is a
  1233. // hack that shouldn't work, but is left for compatibility. OF_Append
  1234. // overrides
  1235. // the specified disposition.
  1236. for (fs::CreationDisposition Disp : Disps) {
  1237. int FD;
  1238. Optional<FileDescriptorCloser> Closer;
  1239. createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "Fizz");
  1240. FileRemover Cleanup(NonExistantFile);
  1241. uint64_t FileSize;
  1242. ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile, FileSize));
  1243. ASSERT_EQ(4ULL, FileSize);
  1244. ASSERT_NO_ERROR(
  1245. fs::openFileForWrite(NonExistantFile, FD, Disp, fs::OF_Append));
  1246. Closer.emplace(FD);
  1247. ASSERT_NO_ERROR(sys::fs::file_size(NonExistantFile, FileSize));
  1248. ASSERT_EQ(4ULL, FileSize);
  1249. ASSERT_EQ(4, write(FD, "Buzz", 4));
  1250. Closer.reset();
  1251. verifyFileContents(NonExistantFile, "FizzBuzz");
  1252. }
  1253. }
  1254. static void verifyRead(int FD, StringRef Data, bool ShouldSucceed) {
  1255. std::vector<char> Buffer;
  1256. Buffer.resize(Data.size());
  1257. int Result = ::read(FD, Buffer.data(), Buffer.size());
  1258. if (ShouldSucceed) {
  1259. ASSERT_EQ((size_t)Result, Data.size());
  1260. ASSERT_EQ(Data, StringRef(Buffer.data(), Buffer.size()));
  1261. } else {
  1262. ASSERT_EQ(-1, Result);
  1263. ASSERT_EQ(EBADF, errno);
  1264. }
  1265. }
  1266. static void verifyWrite(int FD, StringRef Data, bool ShouldSucceed) {
  1267. int Result = ::write(FD, Data.data(), Data.size());
  1268. if (ShouldSucceed)
  1269. ASSERT_EQ((size_t)Result, Data.size());
  1270. else {
  1271. ASSERT_EQ(-1, Result);
  1272. ASSERT_EQ(EBADF, errno);
  1273. }
  1274. }
  1275. TEST_F(FileSystemTest, ReadOnlyFileCantWrite) {
  1276. createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "Fizz");
  1277. FileRemover Cleanup(NonExistantFile);
  1278. int FD;
  1279. ASSERT_NO_ERROR(fs::openFileForRead(NonExistantFile, FD));
  1280. FileDescriptorCloser Closer(FD);
  1281. verifyWrite(FD, "Buzz", false);
  1282. verifyRead(FD, "Fizz", true);
  1283. }
  1284. TEST_F(FileSystemTest, WriteOnlyFileCantRead) {
  1285. createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "Fizz");
  1286. FileRemover Cleanup(NonExistantFile);
  1287. int FD;
  1288. ASSERT_NO_ERROR(
  1289. fs::openFileForWrite(NonExistantFile, FD, fs::CD_OpenExisting));
  1290. FileDescriptorCloser Closer(FD);
  1291. verifyRead(FD, "Fizz", false);
  1292. verifyWrite(FD, "Buzz", true);
  1293. }
  1294. TEST_F(FileSystemTest, ReadWriteFileCanReadOrWrite) {
  1295. createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "Fizz");
  1296. FileRemover Cleanup(NonExistantFile);
  1297. int FD;
  1298. ASSERT_NO_ERROR(fs::openFileForReadWrite(NonExistantFile, FD,
  1299. fs::CD_OpenExisting, fs::OF_None));
  1300. FileDescriptorCloser Closer(FD);
  1301. verifyRead(FD, "Fizz", true);
  1302. verifyWrite(FD, "Buzz", true);
  1303. }
  1304. TEST_F(FileSystemTest, readNativeFile) {
  1305. createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "01234");
  1306. FileRemover Cleanup(NonExistantFile);
  1307. const auto &Read = [&](size_t ToRead) -> Expected<std::string> {
  1308. std::string Buf(ToRead, '?');
  1309. Expected<fs::file_t> FD = fs::openNativeFileForRead(NonExistantFile);
  1310. if (!FD)
  1311. return FD.takeError();
  1312. auto Close = make_scope_exit([&] { fs::closeFile(*FD); });
  1313. if (Expected<size_t> BytesRead = fs::readNativeFile(
  1314. *FD, makeMutableArrayRef(&*Buf.begin(), Buf.size())))
  1315. return Buf.substr(0, *BytesRead);
  1316. else
  1317. return BytesRead.takeError();
  1318. };
  1319. EXPECT_THAT_EXPECTED(Read(5), HasValue("01234"));
  1320. EXPECT_THAT_EXPECTED(Read(3), HasValue("012"));
  1321. EXPECT_THAT_EXPECTED(Read(6), HasValue("01234"));
  1322. }
  1323. TEST_F(FileSystemTest, readNativeFileSlice) {
  1324. createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "01234");
  1325. FileRemover Cleanup(NonExistantFile);
  1326. Expected<fs::file_t> FD = fs::openNativeFileForRead(NonExistantFile);
  1327. ASSERT_THAT_EXPECTED(FD, Succeeded());
  1328. auto Close = make_scope_exit([&] { fs::closeFile(*FD); });
  1329. const auto &Read = [&](size_t Offset,
  1330. size_t ToRead) -> Expected<std::string> {
  1331. std::string Buf(ToRead, '?');
  1332. if (Expected<size_t> BytesRead = fs::readNativeFileSlice(
  1333. *FD, makeMutableArrayRef(&*Buf.begin(), Buf.size()), Offset))
  1334. return Buf.substr(0, *BytesRead);
  1335. else
  1336. return BytesRead.takeError();
  1337. };
  1338. EXPECT_THAT_EXPECTED(Read(0, 5), HasValue("01234"));
  1339. EXPECT_THAT_EXPECTED(Read(0, 3), HasValue("012"));
  1340. EXPECT_THAT_EXPECTED(Read(2, 3), HasValue("234"));
  1341. EXPECT_THAT_EXPECTED(Read(0, 6), HasValue("01234"));
  1342. EXPECT_THAT_EXPECTED(Read(2, 6), HasValue("234"));
  1343. EXPECT_THAT_EXPECTED(Read(5, 5), HasValue(""));
  1344. }
  1345. TEST_F(FileSystemTest, is_local) {
  1346. bool TestDirectoryIsLocal;
  1347. ASSERT_NO_ERROR(fs::is_local(TestDirectory, TestDirectoryIsLocal));
  1348. EXPECT_EQ(TestDirectoryIsLocal, fs::is_local(TestDirectory));
  1349. int FD;
  1350. SmallString<128> TempPath;
  1351. ASSERT_NO_ERROR(
  1352. fs::createUniqueFile(Twine(TestDirectory) + "/temp", FD, TempPath));
  1353. FileRemover Cleanup(TempPath);
  1354. // Make sure it exists.
  1355. ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
  1356. bool TempFileIsLocal;
  1357. ASSERT_NO_ERROR(fs::is_local(FD, TempFileIsLocal));
  1358. EXPECT_EQ(TempFileIsLocal, fs::is_local(FD));
  1359. ::close(FD);
  1360. // Expect that the file and its parent directory are equally local or equally
  1361. // remote.
  1362. EXPECT_EQ(TestDirectoryIsLocal, TempFileIsLocal);
  1363. }
  1364. TEST_F(FileSystemTest, getUmask) {
  1365. #ifdef _WIN32
  1366. EXPECT_EQ(fs::getUmask(), 0U) << "Should always be 0 on Windows.";
  1367. #else
  1368. unsigned OldMask = ::umask(0022);
  1369. unsigned CurrentMask = fs::getUmask();
  1370. EXPECT_EQ(CurrentMask, 0022U)
  1371. << "getUmask() didn't return previously set umask()";
  1372. EXPECT_EQ(::umask(OldMask), 0022U) << "getUmask() may have changed umask()";
  1373. #endif
  1374. }
  1375. TEST_F(FileSystemTest, RespectUmask) {
  1376. #ifndef _WIN32
  1377. unsigned OldMask = ::umask(0022);
  1378. int FD;
  1379. SmallString<128> TempPath;
  1380. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
  1381. fs::perms AllRWE = static_cast<fs::perms>(0777);
  1382. ASSERT_NO_ERROR(fs::setPermissions(TempPath, AllRWE));
  1383. ErrorOr<fs::perms> Perms = fs::getPermissions(TempPath);
  1384. ASSERT_TRUE(!!Perms);
  1385. EXPECT_EQ(Perms.get(), AllRWE) << "Should have ignored umask by default";
  1386. ASSERT_NO_ERROR(fs::setPermissions(TempPath, AllRWE));
  1387. Perms = fs::getPermissions(TempPath);
  1388. ASSERT_TRUE(!!Perms);
  1389. EXPECT_EQ(Perms.get(), AllRWE) << "Should have ignored umask";
  1390. ASSERT_NO_ERROR(
  1391. fs::setPermissions(FD, static_cast<fs::perms>(AllRWE & ~fs::getUmask())));
  1392. Perms = fs::getPermissions(TempPath);
  1393. ASSERT_TRUE(!!Perms);
  1394. EXPECT_EQ(Perms.get(), static_cast<fs::perms>(0755))
  1395. << "Did not respect umask";
  1396. (void)::umask(0057);
  1397. ASSERT_NO_ERROR(
  1398. fs::setPermissions(FD, static_cast<fs::perms>(AllRWE & ~fs::getUmask())));
  1399. Perms = fs::getPermissions(TempPath);
  1400. ASSERT_TRUE(!!Perms);
  1401. EXPECT_EQ(Perms.get(), static_cast<fs::perms>(0720))
  1402. << "Did not respect umask";
  1403. (void)::umask(OldMask);
  1404. (void)::close(FD);
  1405. #endif
  1406. }
  1407. TEST_F(FileSystemTest, set_current_path) {
  1408. SmallString<128> path;
  1409. ASSERT_NO_ERROR(fs::current_path(path));
  1410. ASSERT_NE(TestDirectory, path);
  1411. struct RestorePath {
  1412. SmallString<128> path;
  1413. RestorePath(const SmallString<128> &path) : path(path) {}
  1414. ~RestorePath() { fs::set_current_path(path); }
  1415. } restore_path(path);
  1416. ASSERT_NO_ERROR(fs::set_current_path(TestDirectory));
  1417. ASSERT_NO_ERROR(fs::current_path(path));
  1418. fs::UniqueID D1, D2;
  1419. ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory, D1));
  1420. ASSERT_NO_ERROR(fs::getUniqueID(path, D2));
  1421. ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path;
  1422. }
  1423. TEST_F(FileSystemTest, permissions) {
  1424. int FD;
  1425. SmallString<64> TempPath;
  1426. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
  1427. FileRemover Cleanup(TempPath);
  1428. // Make sure it exists.
  1429. ASSERT_TRUE(fs::exists(Twine(TempPath)));
  1430. auto CheckPermissions = [&](fs::perms Expected) {
  1431. ErrorOr<fs::perms> Actual = fs::getPermissions(TempPath);
  1432. return Actual && *Actual == Expected;
  1433. };
  1434. std::error_code NoError;
  1435. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all), NoError);
  1436. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1437. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::all_exe), NoError);
  1438. EXPECT_TRUE(CheckPermissions(fs::all_read | fs::all_exe));
  1439. #if defined(_WIN32)
  1440. fs::perms ReadOnly = fs::all_read | fs::all_exe;
  1441. EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
  1442. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1443. EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
  1444. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1445. EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
  1446. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1447. EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
  1448. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1449. EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
  1450. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1451. EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
  1452. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1453. EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
  1454. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1455. EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
  1456. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1457. EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
  1458. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1459. EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
  1460. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1461. EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
  1462. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1463. EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
  1464. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1465. EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
  1466. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1467. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
  1468. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1469. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
  1470. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1471. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
  1472. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1473. EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
  1474. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1475. EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
  1476. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1477. EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
  1478. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1479. EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
  1480. fs::set_gid_on_exe |
  1481. fs::sticky_bit),
  1482. NoError);
  1483. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1484. EXPECT_EQ(fs::setPermissions(TempPath, ReadOnly | fs::set_uid_on_exe |
  1485. fs::set_gid_on_exe |
  1486. fs::sticky_bit),
  1487. NoError);
  1488. EXPECT_TRUE(CheckPermissions(ReadOnly));
  1489. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms), NoError);
  1490. EXPECT_TRUE(CheckPermissions(fs::all_all));
  1491. #else
  1492. EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
  1493. EXPECT_TRUE(CheckPermissions(fs::no_perms));
  1494. EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
  1495. EXPECT_TRUE(CheckPermissions(fs::owner_read));
  1496. EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
  1497. EXPECT_TRUE(CheckPermissions(fs::owner_write));
  1498. EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
  1499. EXPECT_TRUE(CheckPermissions(fs::owner_exe));
  1500. EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
  1501. EXPECT_TRUE(CheckPermissions(fs::owner_all));
  1502. EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
  1503. EXPECT_TRUE(CheckPermissions(fs::group_read));
  1504. EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
  1505. EXPECT_TRUE(CheckPermissions(fs::group_write));
  1506. EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
  1507. EXPECT_TRUE(CheckPermissions(fs::group_exe));
  1508. EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
  1509. EXPECT_TRUE(CheckPermissions(fs::group_all));
  1510. EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
  1511. EXPECT_TRUE(CheckPermissions(fs::others_read));
  1512. EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
  1513. EXPECT_TRUE(CheckPermissions(fs::others_write));
  1514. EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
  1515. EXPECT_TRUE(CheckPermissions(fs::others_exe));
  1516. EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
  1517. EXPECT_TRUE(CheckPermissions(fs::others_all));
  1518. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
  1519. EXPECT_TRUE(CheckPermissions(fs::all_read));
  1520. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
  1521. EXPECT_TRUE(CheckPermissions(fs::all_write));
  1522. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
  1523. EXPECT_TRUE(CheckPermissions(fs::all_exe));
  1524. EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
  1525. EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe));
  1526. EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
  1527. EXPECT_TRUE(CheckPermissions(fs::set_gid_on_exe));
  1528. // Modern BSDs require root to set the sticky bit on files.
  1529. // AIX and Solaris without root will mask off (i.e., lose) the sticky bit
  1530. // on files.
  1531. #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && \
  1532. !defined(_AIX) && !(defined(__sun__) && defined(__svr4__))
  1533. EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
  1534. EXPECT_TRUE(CheckPermissions(fs::sticky_bit));
  1535. EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
  1536. fs::set_gid_on_exe |
  1537. fs::sticky_bit),
  1538. NoError);
  1539. EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe | fs::set_gid_on_exe |
  1540. fs::sticky_bit));
  1541. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::set_uid_on_exe |
  1542. fs::set_gid_on_exe |
  1543. fs::sticky_bit),
  1544. NoError);
  1545. EXPECT_TRUE(CheckPermissions(fs::all_read | fs::set_uid_on_exe |
  1546. fs::set_gid_on_exe | fs::sticky_bit));
  1547. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms), NoError);
  1548. EXPECT_TRUE(CheckPermissions(fs::all_perms));
  1549. #endif // !FreeBSD && !NetBSD && !OpenBSD && !AIX
  1550. EXPECT_EQ(fs::setPermissions(TempPath, fs::all_perms & ~fs::sticky_bit),
  1551. NoError);
  1552. EXPECT_TRUE(CheckPermissions(fs::all_perms & ~fs::sticky_bit));
  1553. #endif
  1554. }
  1555. } // anonymous namespace