VirtualFileSystemTest.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. //===- unittests/Basic/VirtualFileSystem.cpp ---------------- VFS 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 "clang/Basic/VirtualFileSystem.h"
  10. #include "llvm/Support/Errc.h"
  11. #include "llvm/Support/MemoryBuffer.h"
  12. #include "llvm/Support/Path.h"
  13. #include "llvm/Support/SourceMgr.h"
  14. #include "gtest/gtest.h"
  15. #include <map>
  16. using namespace clang;
  17. using namespace llvm;
  18. using llvm::sys::fs::UniqueID;
  19. namespace {
  20. class DummyFileSystem : public vfs::FileSystem {
  21. int FSID; // used to produce UniqueIDs
  22. int FileID; // used to produce UniqueIDs
  23. std::map<std::string, vfs::Status> FilesAndDirs;
  24. static int getNextFSID() {
  25. static int Count = 0;
  26. return Count++;
  27. }
  28. public:
  29. DummyFileSystem() : FSID(getNextFSID()), FileID(0) {}
  30. ErrorOr<vfs::Status> status(const Twine &Path) override {
  31. std::map<std::string, vfs::Status>::iterator I =
  32. FilesAndDirs.find(Path.str());
  33. if (I == FilesAndDirs.end())
  34. return make_error_code(llvm::errc::no_such_file_or_directory);
  35. return I->second;
  36. }
  37. ErrorOr<std::unique_ptr<vfs::File>>
  38. openFileForRead(const Twine &Path) override {
  39. llvm_unreachable("unimplemented");
  40. }
  41. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
  42. return std::string();
  43. }
  44. std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
  45. return std::error_code();
  46. }
  47. struct DirIterImpl : public clang::vfs::detail::DirIterImpl {
  48. std::map<std::string, vfs::Status> &FilesAndDirs;
  49. std::map<std::string, vfs::Status>::iterator I;
  50. std::string Path;
  51. bool isInPath(StringRef S) {
  52. if (Path.size() < S.size() && S.find(Path) == 0) {
  53. auto LastSep = S.find_last_of('/');
  54. if (LastSep == Path.size() || LastSep == Path.size()-1)
  55. return true;
  56. }
  57. return false;
  58. }
  59. DirIterImpl(std::map<std::string, vfs::Status> &FilesAndDirs,
  60. const Twine &_Path)
  61. : FilesAndDirs(FilesAndDirs), I(FilesAndDirs.begin()),
  62. Path(_Path.str()) {
  63. for ( ; I != FilesAndDirs.end(); ++I) {
  64. if (isInPath(I->first)) {
  65. CurrentEntry = I->second;
  66. break;
  67. }
  68. }
  69. }
  70. std::error_code increment() override {
  71. ++I;
  72. for ( ; I != FilesAndDirs.end(); ++I) {
  73. if (isInPath(I->first)) {
  74. CurrentEntry = I->second;
  75. break;
  76. }
  77. }
  78. if (I == FilesAndDirs.end())
  79. CurrentEntry = vfs::Status();
  80. return std::error_code();
  81. }
  82. };
  83. vfs::directory_iterator dir_begin(const Twine &Dir,
  84. std::error_code &EC) override {
  85. return vfs::directory_iterator(
  86. std::make_shared<DirIterImpl>(FilesAndDirs, Dir));
  87. }
  88. void addEntry(StringRef Path, const vfs::Status &Status) {
  89. FilesAndDirs[Path] = Status;
  90. }
  91. void addRegularFile(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
  92. vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0,
  93. 1024, sys::fs::file_type::regular_file, Perms);
  94. addEntry(Path, S);
  95. }
  96. void addDirectory(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
  97. vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0,
  98. 0, sys::fs::file_type::directory_file, Perms);
  99. addEntry(Path, S);
  100. }
  101. void addSymlink(StringRef Path) {
  102. vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0,
  103. 0, sys::fs::file_type::symlink_file, sys::fs::all_all);
  104. addEntry(Path, S);
  105. }
  106. };
  107. } // end anonymous namespace
  108. TEST(VirtualFileSystemTest, StatusQueries) {
  109. IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem());
  110. ErrorOr<vfs::Status> Status((std::error_code()));
  111. D->addRegularFile("/foo");
  112. Status = D->status("/foo");
  113. ASSERT_FALSE(Status.getError());
  114. EXPECT_TRUE(Status->isStatusKnown());
  115. EXPECT_FALSE(Status->isDirectory());
  116. EXPECT_TRUE(Status->isRegularFile());
  117. EXPECT_FALSE(Status->isSymlink());
  118. EXPECT_FALSE(Status->isOther());
  119. EXPECT_TRUE(Status->exists());
  120. D->addDirectory("/bar");
  121. Status = D->status("/bar");
  122. ASSERT_FALSE(Status.getError());
  123. EXPECT_TRUE(Status->isStatusKnown());
  124. EXPECT_TRUE(Status->isDirectory());
  125. EXPECT_FALSE(Status->isRegularFile());
  126. EXPECT_FALSE(Status->isSymlink());
  127. EXPECT_FALSE(Status->isOther());
  128. EXPECT_TRUE(Status->exists());
  129. D->addSymlink("/baz");
  130. Status = D->status("/baz");
  131. ASSERT_FALSE(Status.getError());
  132. EXPECT_TRUE(Status->isStatusKnown());
  133. EXPECT_FALSE(Status->isDirectory());
  134. EXPECT_FALSE(Status->isRegularFile());
  135. EXPECT_TRUE(Status->isSymlink());
  136. EXPECT_FALSE(Status->isOther());
  137. EXPECT_TRUE(Status->exists());
  138. EXPECT_TRUE(Status->equivalent(*Status));
  139. ErrorOr<vfs::Status> Status2 = D->status("/foo");
  140. ASSERT_FALSE(Status2.getError());
  141. EXPECT_FALSE(Status->equivalent(*Status2));
  142. }
  143. TEST(VirtualFileSystemTest, BaseOnlyOverlay) {
  144. IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem());
  145. ErrorOr<vfs::Status> Status((std::error_code()));
  146. EXPECT_FALSE(Status = D->status("/foo"));
  147. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(new vfs::OverlayFileSystem(D));
  148. EXPECT_FALSE(Status = O->status("/foo"));
  149. D->addRegularFile("/foo");
  150. Status = D->status("/foo");
  151. EXPECT_FALSE(Status.getError());
  152. ErrorOr<vfs::Status> Status2((std::error_code()));
  153. Status2 = O->status("/foo");
  154. EXPECT_FALSE(Status2.getError());
  155. EXPECT_TRUE(Status->equivalent(*Status2));
  156. }
  157. TEST(VirtualFileSystemTest, OverlayFiles) {
  158. IntrusiveRefCntPtr<DummyFileSystem> Base(new DummyFileSystem());
  159. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  160. IntrusiveRefCntPtr<DummyFileSystem> Top(new DummyFileSystem());
  161. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  162. new vfs::OverlayFileSystem(Base));
  163. O->pushOverlay(Middle);
  164. O->pushOverlay(Top);
  165. ErrorOr<vfs::Status> Status1((std::error_code())),
  166. Status2((std::error_code())), Status3((std::error_code())),
  167. StatusB((std::error_code())), StatusM((std::error_code())),
  168. StatusT((std::error_code()));
  169. Base->addRegularFile("/foo");
  170. StatusB = Base->status("/foo");
  171. ASSERT_FALSE(StatusB.getError());
  172. Status1 = O->status("/foo");
  173. ASSERT_FALSE(Status1.getError());
  174. Middle->addRegularFile("/foo");
  175. StatusM = Middle->status("/foo");
  176. ASSERT_FALSE(StatusM.getError());
  177. Status2 = O->status("/foo");
  178. ASSERT_FALSE(Status2.getError());
  179. Top->addRegularFile("/foo");
  180. StatusT = Top->status("/foo");
  181. ASSERT_FALSE(StatusT.getError());
  182. Status3 = O->status("/foo");
  183. ASSERT_FALSE(Status3.getError());
  184. EXPECT_TRUE(Status1->equivalent(*StatusB));
  185. EXPECT_TRUE(Status2->equivalent(*StatusM));
  186. EXPECT_TRUE(Status3->equivalent(*StatusT));
  187. EXPECT_FALSE(Status1->equivalent(*Status2));
  188. EXPECT_FALSE(Status2->equivalent(*Status3));
  189. EXPECT_FALSE(Status1->equivalent(*Status3));
  190. }
  191. TEST(VirtualFileSystemTest, OverlayDirsNonMerged) {
  192. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  193. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  194. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  195. new vfs::OverlayFileSystem(Lower));
  196. O->pushOverlay(Upper);
  197. Lower->addDirectory("/lower-only");
  198. Upper->addDirectory("/upper-only");
  199. // non-merged paths should be the same
  200. ErrorOr<vfs::Status> Status1 = Lower->status("/lower-only");
  201. ASSERT_FALSE(Status1.getError());
  202. ErrorOr<vfs::Status> Status2 = O->status("/lower-only");
  203. ASSERT_FALSE(Status2.getError());
  204. EXPECT_TRUE(Status1->equivalent(*Status2));
  205. Status1 = Upper->status("/upper-only");
  206. ASSERT_FALSE(Status1.getError());
  207. Status2 = O->status("/upper-only");
  208. ASSERT_FALSE(Status2.getError());
  209. EXPECT_TRUE(Status1->equivalent(*Status2));
  210. }
  211. TEST(VirtualFileSystemTest, MergedDirPermissions) {
  212. // merged directories get the permissions of the upper dir
  213. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  214. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  215. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  216. new vfs::OverlayFileSystem(Lower));
  217. O->pushOverlay(Upper);
  218. ErrorOr<vfs::Status> Status((std::error_code()));
  219. Lower->addDirectory("/both", sys::fs::owner_read);
  220. Upper->addDirectory("/both", sys::fs::owner_all | sys::fs::group_read);
  221. Status = O->status("/both");
  222. ASSERT_FALSE(Status.getError());
  223. EXPECT_EQ(0740, Status->getPermissions());
  224. // permissions (as usual) are not recursively applied
  225. Lower->addRegularFile("/both/foo", sys::fs::owner_read);
  226. Upper->addRegularFile("/both/bar", sys::fs::owner_write);
  227. Status = O->status("/both/foo");
  228. ASSERT_FALSE( Status.getError());
  229. EXPECT_EQ(0400, Status->getPermissions());
  230. Status = O->status("/both/bar");
  231. ASSERT_FALSE(Status.getError());
  232. EXPECT_EQ(0200, Status->getPermissions());
  233. }
  234. namespace {
  235. struct ScopedDir {
  236. SmallString<128> Path;
  237. ScopedDir(const Twine &Name, bool Unique=false) {
  238. std::error_code EC;
  239. if (Unique) {
  240. EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
  241. } else {
  242. Path = Name.str();
  243. EC = llvm::sys::fs::create_directory(Twine(Path));
  244. }
  245. if (EC)
  246. Path = "";
  247. EXPECT_FALSE(EC);
  248. }
  249. ~ScopedDir() {
  250. if (Path != "")
  251. EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
  252. }
  253. operator StringRef() { return Path.str(); }
  254. };
  255. }
  256. TEST(VirtualFileSystemTest, BasicRealFSIteration) {
  257. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/true);
  258. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  259. std::error_code EC;
  260. vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC);
  261. ASSERT_FALSE(EC);
  262. EXPECT_EQ(vfs::directory_iterator(), I); // empty directory is empty
  263. ScopedDir _a(TestDirectory+"/a");
  264. ScopedDir _ab(TestDirectory+"/a/b");
  265. ScopedDir _c(TestDirectory+"/c");
  266. ScopedDir _cd(TestDirectory+"/c/d");
  267. I = FS->dir_begin(Twine(TestDirectory), EC);
  268. ASSERT_FALSE(EC);
  269. ASSERT_NE(vfs::directory_iterator(), I);
  270. // Check either a or c, since we can't rely on the iteration order.
  271. EXPECT_TRUE(I->getName().endswith("a") || I->getName().endswith("c"));
  272. I.increment(EC);
  273. ASSERT_FALSE(EC);
  274. ASSERT_NE(vfs::directory_iterator(), I);
  275. EXPECT_TRUE(I->getName().endswith("a") || I->getName().endswith("c"));
  276. I.increment(EC);
  277. EXPECT_EQ(vfs::directory_iterator(), I);
  278. }
  279. TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) {
  280. ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/true);
  281. IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
  282. std::error_code EC;
  283. auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
  284. ASSERT_FALSE(EC);
  285. EXPECT_EQ(vfs::recursive_directory_iterator(), I); // empty directory is empty
  286. ScopedDir _a(TestDirectory+"/a");
  287. ScopedDir _ab(TestDirectory+"/a/b");
  288. ScopedDir _c(TestDirectory+"/c");
  289. ScopedDir _cd(TestDirectory+"/c/d");
  290. I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
  291. ASSERT_FALSE(EC);
  292. ASSERT_NE(vfs::recursive_directory_iterator(), I);
  293. std::vector<std::string> Contents;
  294. for (auto E = vfs::recursive_directory_iterator(); !EC && I != E;
  295. I.increment(EC)) {
  296. Contents.push_back(I->getName());
  297. }
  298. // Check contents, which may be in any order
  299. EXPECT_EQ(4U, Contents.size());
  300. int Counts[4] = { 0, 0, 0, 0 };
  301. for (const std::string &Name : Contents) {
  302. ASSERT_FALSE(Name.empty());
  303. int Index = Name[Name.size()-1] - 'a';
  304. ASSERT_TRUE(Index >= 0 && Index < 4);
  305. Counts[Index]++;
  306. }
  307. EXPECT_EQ(1, Counts[0]); // a
  308. EXPECT_EQ(1, Counts[1]); // b
  309. EXPECT_EQ(1, Counts[2]); // c
  310. EXPECT_EQ(1, Counts[3]); // d
  311. }
  312. template <typename T, size_t N>
  313. std::vector<StringRef> makeStringRefVector(const T (&Arr)[N]) {
  314. std::vector<StringRef> Vec;
  315. for (size_t i = 0; i != N; ++i)
  316. Vec.push_back(Arr[i]);
  317. return Vec;
  318. }
  319. template <typename DirIter>
  320. static void checkContents(DirIter I, ArrayRef<StringRef> Expected) {
  321. std::error_code EC;
  322. auto ExpectedIter = Expected.begin(), ExpectedEnd = Expected.end();
  323. for (DirIter E;
  324. !EC && I != E && ExpectedIter != ExpectedEnd;
  325. I.increment(EC), ++ExpectedIter)
  326. EXPECT_EQ(*ExpectedIter, I->getName());
  327. EXPECT_EQ(ExpectedEnd, ExpectedIter);
  328. EXPECT_EQ(DirIter(), I);
  329. }
  330. TEST(VirtualFileSystemTest, OverlayIteration) {
  331. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  332. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  333. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  334. new vfs::OverlayFileSystem(Lower));
  335. O->pushOverlay(Upper);
  336. std::error_code EC;
  337. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>());
  338. Lower->addRegularFile("/file1");
  339. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file1"));
  340. Upper->addRegularFile("/file2");
  341. {
  342. const char *Contents[] = {"/file2", "/file1"};
  343. checkContents(O->dir_begin("/", EC), makeStringRefVector(Contents));
  344. }
  345. Lower->addDirectory("/dir1");
  346. Lower->addRegularFile("/dir1/foo");
  347. Upper->addDirectory("/dir2");
  348. Upper->addRegularFile("/dir2/foo");
  349. checkContents(O->dir_begin("/dir2", EC), ArrayRef<StringRef>("/dir2/foo"));
  350. {
  351. const char *Contents[] = {"/dir2", "/file2", "/dir1", "/file1"};
  352. checkContents(O->dir_begin("/", EC), makeStringRefVector(Contents));
  353. }
  354. }
  355. TEST(VirtualFileSystemTest, OverlayRecursiveIteration) {
  356. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  357. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  358. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  359. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  360. new vfs::OverlayFileSystem(Lower));
  361. O->pushOverlay(Middle);
  362. O->pushOverlay(Upper);
  363. std::error_code EC;
  364. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  365. ArrayRef<StringRef>());
  366. Lower->addRegularFile("/file1");
  367. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  368. ArrayRef<StringRef>("/file1"));
  369. Upper->addDirectory("/dir");
  370. Upper->addRegularFile("/dir/file2");
  371. {
  372. const char *Contents[] = {"/dir", "/dir/file2", "/file1"};
  373. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  374. makeStringRefVector(Contents));
  375. }
  376. Lower->addDirectory("/dir1");
  377. Lower->addRegularFile("/dir1/foo");
  378. Lower->addDirectory("/dir1/a");
  379. Lower->addRegularFile("/dir1/a/b");
  380. Middle->addDirectory("/a");
  381. Middle->addDirectory("/a/b");
  382. Middle->addDirectory("/a/b/c");
  383. Middle->addRegularFile("/a/b/c/d");
  384. Middle->addRegularFile("/hiddenByUp");
  385. Upper->addDirectory("/dir2");
  386. Upper->addRegularFile("/dir2/foo");
  387. Upper->addRegularFile("/hiddenByUp");
  388. checkContents(vfs::recursive_directory_iterator(*O, "/dir2", EC),
  389. ArrayRef<StringRef>("/dir2/foo"));
  390. {
  391. const char *Contents[] = { "/dir", "/dir/file2", "/dir2", "/dir2/foo",
  392. "/hiddenByUp", "/a", "/a/b", "/a/b/c", "/a/b/c/d", "/dir1", "/dir1/a",
  393. "/dir1/a/b", "/dir1/foo", "/file1" };
  394. checkContents(vfs::recursive_directory_iterator(*O, "/", EC),
  395. makeStringRefVector(Contents));
  396. }
  397. }
  398. TEST(VirtualFileSystemTest, ThreeLevelIteration) {
  399. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  400. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  401. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  402. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  403. new vfs::OverlayFileSystem(Lower));
  404. O->pushOverlay(Middle);
  405. O->pushOverlay(Upper);
  406. std::error_code EC;
  407. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>());
  408. Middle->addRegularFile("/file2");
  409. checkContents(O->dir_begin("/", EC), ArrayRef<StringRef>("/file2"));
  410. Lower->addRegularFile("/file1");
  411. Upper->addRegularFile("/file3");
  412. {
  413. const char *Contents[] = {"/file3", "/file2", "/file1"};
  414. checkContents(O->dir_begin("/", EC), makeStringRefVector(Contents));
  415. }
  416. }
  417. TEST(VirtualFileSystemTest, HiddenInIteration) {
  418. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  419. IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
  420. IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
  421. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  422. new vfs::OverlayFileSystem(Lower));
  423. O->pushOverlay(Middle);
  424. O->pushOverlay(Upper);
  425. std::error_code EC;
  426. Lower->addRegularFile("/onlyInLow", sys::fs::owner_read);
  427. Lower->addRegularFile("/hiddenByMid", sys::fs::owner_read);
  428. Lower->addRegularFile("/hiddenByUp", sys::fs::owner_read);
  429. Middle->addRegularFile("/onlyInMid", sys::fs::owner_write);
  430. Middle->addRegularFile("/hiddenByMid", sys::fs::owner_write);
  431. Middle->addRegularFile("/hiddenByUp", sys::fs::owner_write);
  432. Upper->addRegularFile("/onlyInUp", sys::fs::owner_all);
  433. Upper->addRegularFile("/hiddenByUp", sys::fs::owner_all);
  434. {
  435. const char *Contents[] = {"/hiddenByUp", "/onlyInUp", "/hiddenByMid",
  436. "/onlyInMid", "/onlyInLow"};
  437. checkContents(O->dir_begin("/", EC), makeStringRefVector(Contents));
  438. }
  439. // Make sure we get the top-most entry
  440. {
  441. std::error_code EC;
  442. vfs::directory_iterator I = O->dir_begin("/", EC), E;
  443. for ( ; !EC && I != E; I.increment(EC))
  444. if (I->getName() == "/hiddenByUp")
  445. break;
  446. ASSERT_NE(E, I);
  447. EXPECT_EQ(sys::fs::owner_all, I->getPermissions());
  448. }
  449. {
  450. std::error_code EC;
  451. vfs::directory_iterator I = O->dir_begin("/", EC), E;
  452. for ( ; !EC && I != E; I.increment(EC))
  453. if (I->getName() == "/hiddenByMid")
  454. break;
  455. ASSERT_NE(E, I);
  456. EXPECT_EQ(sys::fs::owner_write, I->getPermissions());
  457. }
  458. }
  459. class InMemoryFileSystemTest : public ::testing::Test {
  460. protected:
  461. clang::vfs::InMemoryFileSystem FS;
  462. };
  463. TEST_F(InMemoryFileSystemTest, IsEmpty) {
  464. auto Stat = FS.status("/a");
  465. ASSERT_EQ(Stat.getError(),errc::no_such_file_or_directory) << FS.toString();
  466. Stat = FS.status("/");
  467. ASSERT_EQ(Stat.getError(), errc::no_such_file_or_directory) << FS.toString();
  468. }
  469. TEST_F(InMemoryFileSystemTest, WindowsPath) {
  470. FS.addFile("c:/windows/system128/foo.cpp", 0, MemoryBuffer::getMemBuffer(""));
  471. auto Stat = FS.status("c:");
  472. #if !defined(_WIN32)
  473. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  474. #endif
  475. Stat = FS.status("c:/windows/system128/foo.cpp");
  476. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  477. FS.addFile("d:/windows/foo.cpp", 0, MemoryBuffer::getMemBuffer(""));
  478. Stat = FS.status("d:/windows/foo.cpp");
  479. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  480. }
  481. TEST_F(InMemoryFileSystemTest, OverlayFile) {
  482. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  483. auto Stat = FS.status("/");
  484. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  485. Stat = FS.status("/.");
  486. ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
  487. Stat = FS.status("/a");
  488. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  489. ASSERT_EQ("/a", Stat->getName());
  490. }
  491. TEST_F(InMemoryFileSystemTest, OverlayFileNoOwn) {
  492. auto Buf = MemoryBuffer::getMemBuffer("a");
  493. FS.addFileNoOwn("/a", 0, Buf.get());
  494. auto Stat = FS.status("/a");
  495. ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
  496. ASSERT_EQ("/a", Stat->getName());
  497. }
  498. TEST_F(InMemoryFileSystemTest, OpenFileForRead) {
  499. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
  500. auto File = FS.openFileForRead("/a");
  501. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  502. File = FS.openFileForRead("/a"); // Open again.
  503. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  504. File = FS.openFileForRead("/././a"); // Open again.
  505. ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
  506. File = FS.openFileForRead("/");
  507. ASSERT_EQ(File.getError(), errc::invalid_argument) << FS.toString();
  508. File = FS.openFileForRead("/b");
  509. ASSERT_EQ(File.getError(), errc::no_such_file_or_directory) << FS.toString();
  510. }
  511. TEST_F(InMemoryFileSystemTest, DirectoryIteration) {
  512. FS.addFile("/a", 0, MemoryBuffer::getMemBuffer(""));
  513. FS.addFile("/b/c", 0, MemoryBuffer::getMemBuffer(""));
  514. std::error_code EC;
  515. vfs::directory_iterator I = FS.dir_begin("/", EC);
  516. ASSERT_FALSE(EC);
  517. ASSERT_EQ("/a", I->getName());
  518. I.increment(EC);
  519. ASSERT_FALSE(EC);
  520. ASSERT_EQ("/b", I->getName());
  521. I.increment(EC);
  522. ASSERT_FALSE(EC);
  523. ASSERT_EQ(vfs::directory_iterator(), I);
  524. I = FS.dir_begin("/b", EC);
  525. ASSERT_FALSE(EC);
  526. ASSERT_EQ("/b/c", I->getName());
  527. I.increment(EC);
  528. ASSERT_FALSE(EC);
  529. ASSERT_EQ(vfs::directory_iterator(), I);
  530. }
  531. // NOTE: in the tests below, we use '//root/' as our root directory, since it is
  532. // a legal *absolute* path on Windows as well as *nix.
  533. class VFSFromYAMLTest : public ::testing::Test {
  534. public:
  535. int NumDiagnostics;
  536. void SetUp() override { NumDiagnostics = 0; }
  537. static void CountingDiagHandler(const SMDiagnostic &, void *Context) {
  538. VFSFromYAMLTest *Test = static_cast<VFSFromYAMLTest *>(Context);
  539. ++Test->NumDiagnostics;
  540. }
  541. IntrusiveRefCntPtr<vfs::FileSystem>
  542. getFromYAMLRawString(StringRef Content,
  543. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS) {
  544. std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Content);
  545. return getVFSFromYAML(std::move(Buffer), CountingDiagHandler, this,
  546. ExternalFS);
  547. }
  548. IntrusiveRefCntPtr<vfs::FileSystem> getFromYAMLString(
  549. StringRef Content,
  550. IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS = new DummyFileSystem()) {
  551. std::string VersionPlusContent("{\n 'version':0,\n");
  552. VersionPlusContent += Content.slice(Content.find('{') + 1, StringRef::npos);
  553. return getFromYAMLRawString(VersionPlusContent, ExternalFS);
  554. }
  555. };
  556. TEST_F(VFSFromYAMLTest, BasicVFSFromYAML) {
  557. IntrusiveRefCntPtr<vfs::FileSystem> FS;
  558. FS = getFromYAMLString("");
  559. EXPECT_EQ(nullptr, FS.get());
  560. FS = getFromYAMLString("[]");
  561. EXPECT_EQ(nullptr, FS.get());
  562. FS = getFromYAMLString("'string'");
  563. EXPECT_EQ(nullptr, FS.get());
  564. EXPECT_EQ(3, NumDiagnostics);
  565. }
  566. TEST_F(VFSFromYAMLTest, MappedFiles) {
  567. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  568. Lower->addRegularFile("//root/foo/bar/a");
  569. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  570. getFromYAMLString("{ 'roots': [\n"
  571. "{\n"
  572. " 'type': 'directory',\n"
  573. " 'name': '//root/',\n"
  574. " 'contents': [ {\n"
  575. " 'type': 'file',\n"
  576. " 'name': 'file1',\n"
  577. " 'external-contents': '//root/foo/bar/a'\n"
  578. " },\n"
  579. " {\n"
  580. " 'type': 'file',\n"
  581. " 'name': 'file2',\n"
  582. " 'external-contents': '//root/foo/b'\n"
  583. " }\n"
  584. " ]\n"
  585. "}\n"
  586. "]\n"
  587. "}",
  588. Lower);
  589. ASSERT_TRUE(FS.get() != nullptr);
  590. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  591. new vfs::OverlayFileSystem(Lower));
  592. O->pushOverlay(FS);
  593. // file
  594. ErrorOr<vfs::Status> S = O->status("//root/file1");
  595. ASSERT_FALSE(S.getError());
  596. EXPECT_EQ("//root/foo/bar/a", S->getName());
  597. ErrorOr<vfs::Status> SLower = O->status("//root/foo/bar/a");
  598. EXPECT_EQ("//root/foo/bar/a", SLower->getName());
  599. EXPECT_TRUE(S->equivalent(*SLower));
  600. // directory
  601. S = O->status("//root/");
  602. ASSERT_FALSE(S.getError());
  603. EXPECT_TRUE(S->isDirectory());
  604. EXPECT_TRUE(S->equivalent(*O->status("//root/"))); // non-volatile UniqueID
  605. // broken mapping
  606. EXPECT_EQ(O->status("//root/file2").getError(),
  607. llvm::errc::no_such_file_or_directory);
  608. EXPECT_EQ(0, NumDiagnostics);
  609. }
  610. TEST_F(VFSFromYAMLTest, CaseInsensitive) {
  611. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  612. Lower->addRegularFile("//root/foo/bar/a");
  613. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  614. getFromYAMLString("{ 'case-sensitive': 'false',\n"
  615. " 'roots': [\n"
  616. "{\n"
  617. " 'type': 'directory',\n"
  618. " 'name': '//root/',\n"
  619. " 'contents': [ {\n"
  620. " 'type': 'file',\n"
  621. " 'name': 'XX',\n"
  622. " 'external-contents': '//root/foo/bar/a'\n"
  623. " }\n"
  624. " ]\n"
  625. "}]}",
  626. Lower);
  627. ASSERT_TRUE(FS.get() != nullptr);
  628. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  629. new vfs::OverlayFileSystem(Lower));
  630. O->pushOverlay(FS);
  631. ErrorOr<vfs::Status> S = O->status("//root/XX");
  632. ASSERT_FALSE(S.getError());
  633. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  634. ASSERT_FALSE(SS.getError());
  635. EXPECT_TRUE(S->equivalent(*SS));
  636. SS = O->status("//root/xX");
  637. EXPECT_TRUE(S->equivalent(*SS));
  638. SS = O->status("//root/Xx");
  639. EXPECT_TRUE(S->equivalent(*SS));
  640. EXPECT_EQ(0, NumDiagnostics);
  641. }
  642. TEST_F(VFSFromYAMLTest, CaseSensitive) {
  643. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  644. Lower->addRegularFile("//root/foo/bar/a");
  645. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  646. getFromYAMLString("{ 'case-sensitive': 'true',\n"
  647. " 'roots': [\n"
  648. "{\n"
  649. " 'type': 'directory',\n"
  650. " 'name': '//root/',\n"
  651. " 'contents': [ {\n"
  652. " 'type': 'file',\n"
  653. " 'name': 'XX',\n"
  654. " 'external-contents': '//root/foo/bar/a'\n"
  655. " }\n"
  656. " ]\n"
  657. "}]}",
  658. Lower);
  659. ASSERT_TRUE(FS.get() != nullptr);
  660. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  661. new vfs::OverlayFileSystem(Lower));
  662. O->pushOverlay(FS);
  663. ErrorOr<vfs::Status> SS = O->status("//root/xx");
  664. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  665. SS = O->status("//root/xX");
  666. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  667. SS = O->status("//root/Xx");
  668. EXPECT_EQ(SS.getError(), llvm::errc::no_such_file_or_directory);
  669. EXPECT_EQ(0, NumDiagnostics);
  670. }
  671. TEST_F(VFSFromYAMLTest, IllegalVFSFile) {
  672. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  673. // invalid YAML at top-level
  674. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString("{]", Lower);
  675. EXPECT_EQ(nullptr, FS.get());
  676. // invalid YAML in roots
  677. FS = getFromYAMLString("{ 'roots':[}", Lower);
  678. // invalid YAML in directory
  679. FS = getFromYAMLString(
  680. "{ 'roots':[ { 'name': 'foo', 'type': 'directory', 'contents': [}",
  681. Lower);
  682. EXPECT_EQ(nullptr, FS.get());
  683. // invalid configuration
  684. FS = getFromYAMLString("{ 'knobular': 'true', 'roots':[] }", Lower);
  685. EXPECT_EQ(nullptr, FS.get());
  686. FS = getFromYAMLString("{ 'case-sensitive': 'maybe', 'roots':[] }", Lower);
  687. EXPECT_EQ(nullptr, FS.get());
  688. // invalid roots
  689. FS = getFromYAMLString("{ 'roots':'' }", Lower);
  690. EXPECT_EQ(nullptr, FS.get());
  691. FS = getFromYAMLString("{ 'roots':{} }", Lower);
  692. EXPECT_EQ(nullptr, FS.get());
  693. // invalid entries
  694. FS = getFromYAMLString(
  695. "{ 'roots':[ { 'type': 'other', 'name': 'me', 'contents': '' }", Lower);
  696. EXPECT_EQ(nullptr, FS.get());
  697. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': [], "
  698. "'external-contents': 'other' }",
  699. Lower);
  700. EXPECT_EQ(nullptr, FS.get());
  701. FS = getFromYAMLString(
  702. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': [] }",
  703. Lower);
  704. EXPECT_EQ(nullptr, FS.get());
  705. FS = getFromYAMLString(
  706. "{ 'roots':[ { 'type': 'file', 'name': 'me', 'external-contents': {} }",
  707. Lower);
  708. EXPECT_EQ(nullptr, FS.get());
  709. FS = getFromYAMLString(
  710. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': {} }",
  711. Lower);
  712. EXPECT_EQ(nullptr, FS.get());
  713. FS = getFromYAMLString(
  714. "{ 'roots':[ { 'type': 'directory', 'name': 'me', 'contents': '' }",
  715. Lower);
  716. EXPECT_EQ(nullptr, FS.get());
  717. FS = getFromYAMLString(
  718. "{ 'roots':[ { 'thingy': 'directory', 'name': 'me', 'contents': [] }",
  719. Lower);
  720. EXPECT_EQ(nullptr, FS.get());
  721. // missing mandatory fields
  722. FS = getFromYAMLString("{ 'roots':[ { 'type': 'file', 'name': 'me' }", Lower);
  723. EXPECT_EQ(nullptr, FS.get());
  724. FS = getFromYAMLString(
  725. "{ 'roots':[ { 'type': 'file', 'external-contents': 'other' }", Lower);
  726. EXPECT_EQ(nullptr, FS.get());
  727. FS = getFromYAMLString("{ 'roots':[ { 'name': 'me', 'contents': [] }", Lower);
  728. EXPECT_EQ(nullptr, FS.get());
  729. // duplicate keys
  730. FS = getFromYAMLString("{ 'roots':[], 'roots':[] }", Lower);
  731. EXPECT_EQ(nullptr, FS.get());
  732. FS = getFromYAMLString(
  733. "{ 'case-sensitive':'true', 'case-sensitive':'true', 'roots':[] }",
  734. Lower);
  735. EXPECT_EQ(nullptr, FS.get());
  736. FS =
  737. getFromYAMLString("{ 'roots':[{'name':'me', 'name':'you', 'type':'file', "
  738. "'external-contents':'blah' } ] }",
  739. Lower);
  740. EXPECT_EQ(nullptr, FS.get());
  741. // missing version
  742. FS = getFromYAMLRawString("{ 'roots':[] }", Lower);
  743. EXPECT_EQ(nullptr, FS.get());
  744. // bad version number
  745. FS = getFromYAMLRawString("{ 'version':'foo', 'roots':[] }", Lower);
  746. EXPECT_EQ(nullptr, FS.get());
  747. FS = getFromYAMLRawString("{ 'version':-1, 'roots':[] }", Lower);
  748. EXPECT_EQ(nullptr, FS.get());
  749. FS = getFromYAMLRawString("{ 'version':100000, 'roots':[] }", Lower);
  750. EXPECT_EQ(nullptr, FS.get());
  751. EXPECT_EQ(24, NumDiagnostics);
  752. }
  753. TEST_F(VFSFromYAMLTest, UseExternalName) {
  754. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  755. Lower->addRegularFile("//root/external/file");
  756. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  757. "{ 'roots': [\n"
  758. " { 'type': 'file', 'name': '//root/A',\n"
  759. " 'external-contents': '//root/external/file'\n"
  760. " },\n"
  761. " { 'type': 'file', 'name': '//root/B',\n"
  762. " 'use-external-name': true,\n"
  763. " 'external-contents': '//root/external/file'\n"
  764. " },\n"
  765. " { 'type': 'file', 'name': '//root/C',\n"
  766. " 'use-external-name': false,\n"
  767. " 'external-contents': '//root/external/file'\n"
  768. " }\n"
  769. "] }", Lower);
  770. ASSERT_TRUE(nullptr != FS.get());
  771. // default true
  772. EXPECT_EQ("//root/external/file", FS->status("//root/A")->getName());
  773. // explicit
  774. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  775. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  776. // global configuration
  777. FS = getFromYAMLString(
  778. "{ 'use-external-names': false,\n"
  779. " 'roots': [\n"
  780. " { 'type': 'file', 'name': '//root/A',\n"
  781. " 'external-contents': '//root/external/file'\n"
  782. " },\n"
  783. " { 'type': 'file', 'name': '//root/B',\n"
  784. " 'use-external-name': true,\n"
  785. " 'external-contents': '//root/external/file'\n"
  786. " },\n"
  787. " { 'type': 'file', 'name': '//root/C',\n"
  788. " 'use-external-name': false,\n"
  789. " 'external-contents': '//root/external/file'\n"
  790. " }\n"
  791. "] }", Lower);
  792. ASSERT_TRUE(nullptr != FS.get());
  793. // default
  794. EXPECT_EQ("//root/A", FS->status("//root/A")->getName());
  795. // explicit
  796. EXPECT_EQ("//root/external/file", FS->status("//root/B")->getName());
  797. EXPECT_EQ("//root/C", FS->status("//root/C")->getName());
  798. }
  799. TEST_F(VFSFromYAMLTest, MultiComponentPath) {
  800. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  801. Lower->addRegularFile("//root/other");
  802. // file in roots
  803. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  804. "{ 'roots': [\n"
  805. " { 'type': 'file', 'name': '//root/path/to/file',\n"
  806. " 'external-contents': '//root/other' }]\n"
  807. "}", Lower);
  808. ASSERT_TRUE(nullptr != FS.get());
  809. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  810. EXPECT_FALSE(FS->status("//root/path/to").getError());
  811. EXPECT_FALSE(FS->status("//root/path").getError());
  812. EXPECT_FALSE(FS->status("//root/").getError());
  813. // at the start
  814. FS = getFromYAMLString(
  815. "{ 'roots': [\n"
  816. " { 'type': 'directory', 'name': '//root/path/to',\n"
  817. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  818. " 'external-contents': '//root/other' }]}]\n"
  819. "}", Lower);
  820. ASSERT_TRUE(nullptr != FS.get());
  821. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  822. EXPECT_FALSE(FS->status("//root/path/to").getError());
  823. EXPECT_FALSE(FS->status("//root/path").getError());
  824. EXPECT_FALSE(FS->status("//root/").getError());
  825. // at the end
  826. FS = getFromYAMLString(
  827. "{ 'roots': [\n"
  828. " { 'type': 'directory', 'name': '//root/',\n"
  829. " 'contents': [ { 'type': 'file', 'name': 'path/to/file',\n"
  830. " 'external-contents': '//root/other' }]}]\n"
  831. "}", Lower);
  832. ASSERT_TRUE(nullptr != FS.get());
  833. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  834. EXPECT_FALSE(FS->status("//root/path/to").getError());
  835. EXPECT_FALSE(FS->status("//root/path").getError());
  836. EXPECT_FALSE(FS->status("//root/").getError());
  837. }
  838. TEST_F(VFSFromYAMLTest, TrailingSlashes) {
  839. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  840. Lower->addRegularFile("//root/other");
  841. // file in roots
  842. IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLString(
  843. "{ 'roots': [\n"
  844. " { 'type': 'directory', 'name': '//root/path/to////',\n"
  845. " 'contents': [ { 'type': 'file', 'name': 'file',\n"
  846. " 'external-contents': '//root/other' }]}]\n"
  847. "}", Lower);
  848. ASSERT_TRUE(nullptr != FS.get());
  849. EXPECT_FALSE(FS->status("//root/path/to/file").getError());
  850. EXPECT_FALSE(FS->status("//root/path/to").getError());
  851. EXPECT_FALSE(FS->status("//root/path").getError());
  852. EXPECT_FALSE(FS->status("//root/").getError());
  853. }
  854. TEST_F(VFSFromYAMLTest, DirectoryIteration) {
  855. IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
  856. Lower->addDirectory("//root/");
  857. Lower->addDirectory("//root/foo");
  858. Lower->addDirectory("//root/foo/bar");
  859. Lower->addRegularFile("//root/foo/bar/a");
  860. Lower->addRegularFile("//root/foo/bar/b");
  861. Lower->addRegularFile("//root/file3");
  862. IntrusiveRefCntPtr<vfs::FileSystem> FS =
  863. getFromYAMLString("{ 'use-external-names': false,\n"
  864. " 'roots': [\n"
  865. "{\n"
  866. " 'type': 'directory',\n"
  867. " 'name': '//root/',\n"
  868. " 'contents': [ {\n"
  869. " 'type': 'file',\n"
  870. " 'name': 'file1',\n"
  871. " 'external-contents': '//root/foo/bar/a'\n"
  872. " },\n"
  873. " {\n"
  874. " 'type': 'file',\n"
  875. " 'name': 'file2',\n"
  876. " 'external-contents': '//root/foo/bar/b'\n"
  877. " }\n"
  878. " ]\n"
  879. "}\n"
  880. "]\n"
  881. "}",
  882. Lower);
  883. ASSERT_TRUE(FS.get() != NULL);
  884. IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
  885. new vfs::OverlayFileSystem(Lower));
  886. O->pushOverlay(FS);
  887. std::error_code EC;
  888. {
  889. const char *Contents[] = {"//root/file1", "//root/file2", "//root/file3",
  890. "//root/foo"};
  891. checkContents(O->dir_begin("//root/", EC), makeStringRefVector(Contents));
  892. }
  893. {
  894. const char *Contents[] = {"//root/foo/bar/a", "//root/foo/bar/b"};
  895. checkContents(O->dir_begin("//root/foo/bar", EC),
  896. makeStringRefVector(Contents));
  897. }
  898. }