Path.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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/Support/Errc.h"
  11. #include "llvm/Support/ErrorHandling.h"
  12. #include "llvm/Support/FileSystem.h"
  13. #include "llvm/Support/MemoryBuffer.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include "gtest/gtest.h"
  16. #ifdef LLVM_ON_WIN32
  17. #include <windows.h>
  18. #include <winerror.h>
  19. #endif
  20. using namespace llvm;
  21. using namespace llvm::sys;
  22. #define ASSERT_NO_ERROR(x) \
  23. if (std::error_code ASSERT_NO_ERROR_ec = x) { \
  24. SmallString<128> MessageStorage; \
  25. raw_svector_ostream Message(MessageStorage); \
  26. Message << #x ": did not return errc::success.\n" \
  27. << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
  28. << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
  29. GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
  30. } else { \
  31. }
  32. namespace {
  33. TEST(is_separator, Works) {
  34. EXPECT_TRUE(path::is_separator('/'));
  35. EXPECT_FALSE(path::is_separator('\0'));
  36. EXPECT_FALSE(path::is_separator('-'));
  37. EXPECT_FALSE(path::is_separator(' '));
  38. #ifdef LLVM_ON_WIN32
  39. EXPECT_TRUE(path::is_separator('\\'));
  40. #else
  41. EXPECT_FALSE(path::is_separator('\\'));
  42. #endif
  43. }
  44. TEST(Support, Path) {
  45. SmallVector<StringRef, 40> paths;
  46. paths.push_back("");
  47. paths.push_back(".");
  48. paths.push_back("..");
  49. paths.push_back("foo");
  50. paths.push_back("/");
  51. paths.push_back("/foo");
  52. paths.push_back("foo/");
  53. paths.push_back("/foo/");
  54. paths.push_back("foo/bar");
  55. paths.push_back("/foo/bar");
  56. paths.push_back("//net");
  57. paths.push_back("//net/foo");
  58. paths.push_back("///foo///");
  59. paths.push_back("///foo///bar");
  60. paths.push_back("/.");
  61. paths.push_back("./");
  62. paths.push_back("/..");
  63. paths.push_back("../");
  64. paths.push_back("foo/.");
  65. paths.push_back("foo/..");
  66. paths.push_back("foo/./");
  67. paths.push_back("foo/./bar");
  68. paths.push_back("foo/..");
  69. paths.push_back("foo/../");
  70. paths.push_back("foo/../bar");
  71. paths.push_back("c:");
  72. paths.push_back("c:/");
  73. paths.push_back("c:foo");
  74. paths.push_back("c:/foo");
  75. paths.push_back("c:foo/");
  76. paths.push_back("c:/foo/");
  77. paths.push_back("c:/foo/bar");
  78. paths.push_back("prn:");
  79. paths.push_back("c:\\");
  80. paths.push_back("c:foo");
  81. paths.push_back("c:\\foo");
  82. paths.push_back("c:foo\\");
  83. paths.push_back("c:\\foo\\");
  84. paths.push_back("c:\\foo/");
  85. paths.push_back("c:/foo\\bar");
  86. SmallVector<StringRef, 5> ComponentStack;
  87. for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
  88. e = paths.end();
  89. i != e;
  90. ++i) {
  91. for (sys::path::const_iterator ci = sys::path::begin(*i),
  92. ce = sys::path::end(*i);
  93. ci != ce;
  94. ++ci) {
  95. ASSERT_FALSE(ci->empty());
  96. ComponentStack.push_back(*ci);
  97. }
  98. for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
  99. ce = sys::path::rend(*i);
  100. ci != ce;
  101. ++ci) {
  102. ASSERT_TRUE(*ci == ComponentStack.back());
  103. ComponentStack.pop_back();
  104. }
  105. ASSERT_TRUE(ComponentStack.empty());
  106. path::has_root_path(*i);
  107. path::root_path(*i);
  108. path::has_root_name(*i);
  109. path::root_name(*i);
  110. path::has_root_directory(*i);
  111. path::root_directory(*i);
  112. path::has_parent_path(*i);
  113. path::parent_path(*i);
  114. path::has_filename(*i);
  115. path::filename(*i);
  116. path::has_stem(*i);
  117. path::stem(*i);
  118. path::has_extension(*i);
  119. path::extension(*i);
  120. path::is_absolute(*i);
  121. path::is_relative(*i);
  122. SmallString<128> temp_store;
  123. temp_store = *i;
  124. ASSERT_NO_ERROR(fs::make_absolute(temp_store));
  125. temp_store = *i;
  126. path::remove_filename(temp_store);
  127. temp_store = *i;
  128. path::replace_extension(temp_store, "ext");
  129. StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
  130. stem = path::stem(filename);
  131. ext = path::extension(filename);
  132. EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
  133. path::native(*i, temp_store);
  134. }
  135. }
  136. TEST(Support, RelativePathIterator) {
  137. SmallString<64> Path(StringRef("c/d/e/foo.txt"));
  138. typedef SmallVector<StringRef, 4> PathComponents;
  139. PathComponents ExpectedPathComponents;
  140. PathComponents ActualPathComponents;
  141. StringRef(Path).split(ExpectedPathComponents, "/");
  142. for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
  143. ++I) {
  144. ActualPathComponents.push_back(*I);
  145. }
  146. ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
  147. for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
  148. EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
  149. }
  150. }
  151. TEST(Support, RelativePathDotIterator) {
  152. SmallString<64> Path(StringRef(".c/.d/../."));
  153. typedef SmallVector<StringRef, 4> PathComponents;
  154. PathComponents ExpectedPathComponents;
  155. PathComponents ActualPathComponents;
  156. StringRef(Path).split(ExpectedPathComponents, "/");
  157. for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
  158. ++I) {
  159. ActualPathComponents.push_back(*I);
  160. }
  161. ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
  162. for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
  163. EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
  164. }
  165. }
  166. TEST(Support, AbsolutePathIterator) {
  167. SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
  168. typedef SmallVector<StringRef, 4> PathComponents;
  169. PathComponents ExpectedPathComponents;
  170. PathComponents ActualPathComponents;
  171. StringRef(Path).split(ExpectedPathComponents, "/");
  172. // The root path will also be a component when iterating
  173. ExpectedPathComponents[0] = "/";
  174. for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
  175. ++I) {
  176. ActualPathComponents.push_back(*I);
  177. }
  178. ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
  179. for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
  180. EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
  181. }
  182. }
  183. TEST(Support, AbsolutePathDotIterator) {
  184. SmallString<64> Path(StringRef("/.c/.d/../."));
  185. typedef SmallVector<StringRef, 4> PathComponents;
  186. PathComponents ExpectedPathComponents;
  187. PathComponents ActualPathComponents;
  188. StringRef(Path).split(ExpectedPathComponents, "/");
  189. // The root path will also be a component when iterating
  190. ExpectedPathComponents[0] = "/";
  191. for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
  192. ++I) {
  193. ActualPathComponents.push_back(*I);
  194. }
  195. ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
  196. for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
  197. EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
  198. }
  199. }
  200. #ifdef LLVM_ON_WIN32
  201. TEST(Support, AbsolutePathIteratorWin32) {
  202. SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
  203. typedef SmallVector<StringRef, 4> PathComponents;
  204. PathComponents ExpectedPathComponents;
  205. PathComponents ActualPathComponents;
  206. StringRef(Path).split(ExpectedPathComponents, "\\");
  207. // The root path (which comes after the drive name) will also be a component
  208. // when iterating.
  209. ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
  210. for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
  211. ++I) {
  212. ActualPathComponents.push_back(*I);
  213. }
  214. ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
  215. for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
  216. EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
  217. }
  218. }
  219. #endif // LLVM_ON_WIN32
  220. TEST(Support, AbsolutePathIteratorEnd) {
  221. // Trailing slashes are converted to '.' unless they are part of the root path.
  222. SmallVector<StringRef, 4> Paths;
  223. Paths.push_back("/foo/");
  224. Paths.push_back("/foo//");
  225. Paths.push_back("//net//");
  226. #ifdef LLVM_ON_WIN32
  227. Paths.push_back("c:\\\\");
  228. #endif
  229. for (StringRef Path : Paths) {
  230. StringRef LastComponent = *path::rbegin(Path);
  231. EXPECT_EQ(".", LastComponent);
  232. }
  233. SmallVector<StringRef, 3> RootPaths;
  234. RootPaths.push_back("/");
  235. RootPaths.push_back("//net/");
  236. #ifdef LLVM_ON_WIN32
  237. RootPaths.push_back("c:\\");
  238. #endif
  239. for (StringRef Path : RootPaths) {
  240. StringRef LastComponent = *path::rbegin(Path);
  241. EXPECT_EQ(1u, LastComponent.size());
  242. EXPECT_TRUE(path::is_separator(LastComponent[0]));
  243. }
  244. }
  245. TEST(Support, HomeDirectory) {
  246. #ifdef LLVM_ON_UNIX
  247. // This test only makes sense on Unix if $HOME is set.
  248. if (::getenv("HOME")) {
  249. #endif
  250. SmallString<128> HomeDir;
  251. EXPECT_TRUE(path::home_directory(HomeDir));
  252. EXPECT_FALSE(HomeDir.empty());
  253. #ifdef LLVM_ON_UNIX
  254. }
  255. #endif
  256. }
  257. class FileSystemTest : public testing::Test {
  258. protected:
  259. /// Unique temporary directory in which all created filesystem entities must
  260. /// be placed. It is removed at the end of each test (must be empty).
  261. SmallString<128> TestDirectory;
  262. void SetUp() override {
  263. ASSERT_NO_ERROR(
  264. fs::createUniqueDirectory("file-system-test", TestDirectory));
  265. // We don't care about this specific file.
  266. errs() << "Test Directory: " << TestDirectory << '\n';
  267. errs().flush();
  268. }
  269. void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
  270. };
  271. TEST_F(FileSystemTest, Unique) {
  272. // Create a temp file.
  273. int FileDescriptor;
  274. SmallString<64> TempPath;
  275. ASSERT_NO_ERROR(
  276. fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
  277. // The same file should return an identical unique id.
  278. fs::UniqueID F1, F2;
  279. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
  280. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
  281. ASSERT_EQ(F1, F2);
  282. // Different files should return different unique ids.
  283. int FileDescriptor2;
  284. SmallString<64> TempPath2;
  285. ASSERT_NO_ERROR(
  286. fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
  287. fs::UniqueID D;
  288. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
  289. ASSERT_NE(D, F1);
  290. ::close(FileDescriptor2);
  291. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
  292. // Two paths representing the same file on disk should still provide the
  293. // same unique id. We can test this by making a hard link.
  294. ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
  295. fs::UniqueID D2;
  296. ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
  297. ASSERT_EQ(D2, F1);
  298. ::close(FileDescriptor);
  299. SmallString<128> Dir1;
  300. ASSERT_NO_ERROR(
  301. fs::createUniqueDirectory("dir1", Dir1));
  302. ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
  303. ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
  304. ASSERT_EQ(F1, F2);
  305. SmallString<128> Dir2;
  306. ASSERT_NO_ERROR(
  307. fs::createUniqueDirectory("dir2", Dir2));
  308. ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
  309. ASSERT_NE(F1, F2);
  310. }
  311. TEST_F(FileSystemTest, TempFiles) {
  312. // Create a temp file.
  313. int FileDescriptor;
  314. SmallString<64> TempPath;
  315. ASSERT_NO_ERROR(
  316. fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
  317. // Make sure it exists.
  318. ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
  319. // Create another temp tile.
  320. int FD2;
  321. SmallString<64> TempPath2;
  322. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
  323. ASSERT_TRUE(TempPath2.endswith(".temp"));
  324. ASSERT_NE(TempPath.str(), TempPath2.str());
  325. fs::file_status A, B;
  326. ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
  327. ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
  328. EXPECT_FALSE(fs::equivalent(A, B));
  329. ::close(FD2);
  330. // Remove Temp2.
  331. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
  332. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
  333. ASSERT_EQ(fs::remove(Twine(TempPath2), false),
  334. errc::no_such_file_or_directory);
  335. std::error_code EC = fs::status(TempPath2.c_str(), B);
  336. EXPECT_EQ(EC, errc::no_such_file_or_directory);
  337. EXPECT_EQ(B.type(), fs::file_type::file_not_found);
  338. // Make sure Temp2 doesn't exist.
  339. ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
  340. errc::no_such_file_or_directory);
  341. SmallString<64> TempPath3;
  342. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
  343. ASSERT_FALSE(TempPath3.endswith("."));
  344. // Create a hard link to Temp1.
  345. ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
  346. bool equal;
  347. ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
  348. EXPECT_TRUE(equal);
  349. ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
  350. ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
  351. EXPECT_TRUE(fs::equivalent(A, B));
  352. // Remove Temp1.
  353. ::close(FileDescriptor);
  354. ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
  355. // Remove the hard link.
  356. ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
  357. // Make sure Temp1 doesn't exist.
  358. ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
  359. errc::no_such_file_or_directory);
  360. #ifdef LLVM_ON_WIN32
  361. // Path name > 260 chars should get an error.
  362. const char *Path270 =
  363. "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
  364. "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
  365. "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
  366. "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
  367. "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
  368. EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
  369. errc::invalid_argument);
  370. // Relative path < 247 chars, no problem.
  371. const char *Path216 =
  372. "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
  373. "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
  374. "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
  375. "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
  376. ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
  377. ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
  378. #endif
  379. }
  380. TEST_F(FileSystemTest, CreateDir) {
  381. ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
  382. ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
  383. ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
  384. errc::file_exists);
  385. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
  386. #ifdef LLVM_ON_WIN32
  387. // Prove that create_directories() can handle a pathname > 248 characters,
  388. // which is the documented limit for CreateDirectory().
  389. // (248 is MAX_PATH subtracting room for an 8.3 filename.)
  390. // Generate a directory path guaranteed to fall into that range.
  391. size_t TmpLen = TestDirectory.size();
  392. const char *OneDir = "\\123456789";
  393. size_t OneDirLen = strlen(OneDir);
  394. ASSERT_LT(OneDirLen, 12U);
  395. size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
  396. SmallString<260> LongDir(TestDirectory);
  397. for (size_t I = 0; I < NLevels; ++I)
  398. LongDir.append(OneDir);
  399. ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
  400. ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
  401. ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
  402. errc::file_exists);
  403. // Tidy up, "recursively" removing the directories.
  404. StringRef ThisDir(LongDir);
  405. for (size_t J = 0; J < NLevels; ++J) {
  406. ASSERT_NO_ERROR(fs::remove(ThisDir));
  407. ThisDir = path::parent_path(ThisDir);
  408. }
  409. // Similarly for a relative pathname. Need to set the current directory to
  410. // TestDirectory so that the one we create ends up in the right place.
  411. char PreviousDir[260];
  412. size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
  413. ASSERT_GT(PreviousDirLen, 0U);
  414. ASSERT_LT(PreviousDirLen, 260U);
  415. ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
  416. LongDir.clear();
  417. // Generate a relative directory name with absolute length > 248.
  418. size_t LongDirLen = 249 - TestDirectory.size();
  419. LongDir.assign(LongDirLen, 'a');
  420. ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
  421. // While we're here, prove that .. and . handling works in these long paths.
  422. const char *DotDotDirs = "\\..\\.\\b";
  423. LongDir.append(DotDotDirs);
  424. ASSERT_NO_ERROR(fs::create_directory("b"));
  425. ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
  426. // And clean up.
  427. ASSERT_NO_ERROR(fs::remove("b"));
  428. ASSERT_NO_ERROR(fs::remove(
  429. Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
  430. ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
  431. #endif
  432. }
  433. TEST_F(FileSystemTest, DirectoryIteration) {
  434. std::error_code ec;
  435. for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
  436. ASSERT_NO_ERROR(ec);
  437. // Create a known hierarchy to recurse over.
  438. ASSERT_NO_ERROR(
  439. fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
  440. ASSERT_NO_ERROR(
  441. fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
  442. ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
  443. "/recursive/dontlookhere/da1"));
  444. ASSERT_NO_ERROR(
  445. fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
  446. ASSERT_NO_ERROR(
  447. fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
  448. typedef std::vector<std::string> v_t;
  449. v_t visited;
  450. for (fs::recursive_directory_iterator i(Twine(TestDirectory)
  451. + "/recursive", ec), e; i != e; i.increment(ec)){
  452. ASSERT_NO_ERROR(ec);
  453. if (path::filename(i->path()) == "p1") {
  454. i.pop();
  455. // FIXME: recursive_directory_iterator should be more robust.
  456. if (i == e) break;
  457. }
  458. if (path::filename(i->path()) == "dontlookhere")
  459. i.no_push();
  460. visited.push_back(path::filename(i->path()));
  461. }
  462. v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
  463. v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
  464. v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
  465. v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
  466. "dontlookhere");
  467. v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
  468. v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
  469. v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
  470. v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
  471. v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
  472. // Make sure that each path was visited correctly.
  473. ASSERT_NE(a0, visited.end());
  474. ASSERT_NE(aa1, visited.end());
  475. ASSERT_NE(ab1, visited.end());
  476. ASSERT_NE(dontlookhere, visited.end());
  477. ASSERT_EQ(da1, visited.end()); // Not visited.
  478. ASSERT_NE(z0, visited.end());
  479. ASSERT_NE(za1, visited.end());
  480. ASSERT_NE(pop, visited.end());
  481. ASSERT_EQ(p1, visited.end()); // Not visited.
  482. // Make sure that parents were visited before children. No other ordering
  483. // guarantees can be made across siblings.
  484. ASSERT_LT(a0, aa1);
  485. ASSERT_LT(a0, ab1);
  486. ASSERT_LT(z0, za1);
  487. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
  488. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
  489. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
  490. ASSERT_NO_ERROR(
  491. fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
  492. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
  493. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
  494. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
  495. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
  496. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
  497. ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
  498. }
  499. const char archive[] = "!<arch>\x0A";
  500. const char bitcode[] = "\xde\xc0\x17\x0b";
  501. const char coff_object[] = "\x00\x00......";
  502. const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
  503. "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
  504. const char coff_import_library[] = "\x00\x00\xff\xff....";
  505. const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
  506. 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  507. const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00";
  508. const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01";
  509. const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02";
  510. const char macho_fixed_virtual_memory_shared_lib[] =
  511. "\xfe\xed\xfa\xce..........\x00\x03";
  512. const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04";
  513. const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05";
  514. const char macho_dynamically_linked_shared_lib[] =
  515. "\xfe\xed\xfa\xce..........\x00\x06";
  516. const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07";
  517. const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08";
  518. const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a";
  519. const char macho_kext_bundle[] = "\xfe\xed\xfa\xce..........\x00\x0b";
  520. const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
  521. const char macho_dynamically_linked_shared_lib_stub[] =
  522. "\xfe\xed\xfa\xce..........\x00\x09";
  523. TEST_F(FileSystemTest, Magic) {
  524. struct type {
  525. const char *filename;
  526. const char *magic_str;
  527. size_t magic_str_len;
  528. fs::file_magic magic;
  529. } types[] = {
  530. #define DEFINE(magic) \
  531. { #magic, magic, sizeof(magic), fs::file_magic::magic }
  532. DEFINE(archive),
  533. DEFINE(bitcode),
  534. DEFINE(coff_object),
  535. { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
  536. DEFINE(coff_import_library),
  537. DEFINE(elf_relocatable),
  538. DEFINE(macho_universal_binary),
  539. DEFINE(macho_object),
  540. DEFINE(macho_executable),
  541. DEFINE(macho_fixed_virtual_memory_shared_lib),
  542. DEFINE(macho_core),
  543. DEFINE(macho_preload_executable),
  544. DEFINE(macho_dynamically_linked_shared_lib),
  545. DEFINE(macho_dynamic_linker),
  546. DEFINE(macho_bundle),
  547. DEFINE(macho_dynamically_linked_shared_lib_stub),
  548. DEFINE(macho_dsym_companion),
  549. DEFINE(macho_kext_bundle),
  550. DEFINE(windows_resource)
  551. #undef DEFINE
  552. };
  553. // Create some files filled with magic.
  554. for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
  555. ++i) {
  556. SmallString<128> file_pathname(TestDirectory);
  557. path::append(file_pathname, i->filename);
  558. std::error_code EC;
  559. raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
  560. ASSERT_FALSE(file.has_error());
  561. StringRef magic(i->magic_str, i->magic_str_len);
  562. file << magic;
  563. file.close();
  564. EXPECT_EQ(i->magic, fs::identify_magic(magic));
  565. ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
  566. }
  567. }
  568. #ifdef LLVM_ON_WIN32
  569. TEST_F(FileSystemTest, CarriageReturn) {
  570. SmallString<128> FilePathname(TestDirectory);
  571. std::error_code EC;
  572. path::append(FilePathname, "test");
  573. {
  574. raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
  575. ASSERT_NO_ERROR(EC);
  576. File << '\n';
  577. }
  578. {
  579. auto Buf = MemoryBuffer::getFile(FilePathname.str());
  580. EXPECT_TRUE((bool)Buf);
  581. EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
  582. }
  583. {
  584. raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
  585. ASSERT_NO_ERROR(EC);
  586. File << '\n';
  587. }
  588. {
  589. auto Buf = MemoryBuffer::getFile(FilePathname.str());
  590. EXPECT_TRUE((bool)Buf);
  591. EXPECT_EQ(Buf.get()->getBuffer(), "\n");
  592. }
  593. ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
  594. }
  595. #endif
  596. TEST_F(FileSystemTest, Resize) {
  597. int FD;
  598. SmallString<64> TempPath;
  599. ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
  600. ASSERT_NO_ERROR(fs::resize_file(FD, 123));
  601. fs::file_status Status;
  602. ASSERT_NO_ERROR(fs::status(FD, Status));
  603. ASSERT_EQ(Status.getSize(), 123U);
  604. }
  605. TEST_F(FileSystemTest, FileMapping) {
  606. // Create a temp file.
  607. int FileDescriptor;
  608. SmallString<64> TempPath;
  609. ASSERT_NO_ERROR(
  610. fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
  611. unsigned Size = 4096;
  612. ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
  613. // Map in temp file and add some content
  614. std::error_code EC;
  615. StringRef Val("hello there");
  616. {
  617. fs::mapped_file_region mfr(FileDescriptor,
  618. fs::mapped_file_region::readwrite, Size, 0, EC);
  619. ASSERT_NO_ERROR(EC);
  620. std::copy(Val.begin(), Val.end(), mfr.data());
  621. // Explicitly add a 0.
  622. mfr.data()[Val.size()] = 0;
  623. // Unmap temp file
  624. }
  625. // Map it back in read-only
  626. int FD;
  627. EC = fs::openFileForRead(Twine(TempPath), FD);
  628. ASSERT_NO_ERROR(EC);
  629. fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
  630. ASSERT_NO_ERROR(EC);
  631. // Verify content
  632. EXPECT_EQ(StringRef(mfr.const_data()), Val);
  633. // Unmap temp file
  634. fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
  635. ASSERT_NO_ERROR(EC);
  636. ASSERT_EQ(close(FD), 0);
  637. }
  638. TEST(Support, NormalizePath) {
  639. #if defined(LLVM_ON_WIN32)
  640. #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
  641. EXPECT_EQ(path__, windows__);
  642. #else
  643. #define EXPECT_PATH_IS(path__, windows__, not_windows__) \
  644. EXPECT_EQ(path__, not_windows__);
  645. #endif
  646. SmallString<64> Path1("a");
  647. SmallString<64> Path2("a/b");
  648. SmallString<64> Path3("a\\b");
  649. SmallString<64> Path4("a\\\\b");
  650. SmallString<64> Path5("\\a");
  651. SmallString<64> Path6("a\\");
  652. path::native(Path1);
  653. EXPECT_PATH_IS(Path1, "a", "a");
  654. path::native(Path2);
  655. EXPECT_PATH_IS(Path2, "a\\b", "a/b");
  656. path::native(Path3);
  657. EXPECT_PATH_IS(Path3, "a\\b", "a/b");
  658. path::native(Path4);
  659. EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
  660. path::native(Path5);
  661. EXPECT_PATH_IS(Path5, "\\a", "/a");
  662. path::native(Path6);
  663. EXPECT_PATH_IS(Path6, "a\\", "a/");
  664. #undef EXPECT_PATH_IS
  665. }
  666. } // anonymous namespace