VirtualFileSystem.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. //===- VirtualFileSystem.cpp - Virtual File System Layer --------*- C++ -*-===//
  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. // This file implements the VirtualFileSystem interface.
  10. //===----------------------------------------------------------------------===//
  11. #include "clang/Basic/VirtualFileSystem.h"
  12. #include "llvm/ADT/DenseMap.h"
  13. #include "llvm/ADT/STLExtras.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Support/MemoryBuffer.h"
  16. #include "llvm/Support/Path.h"
  17. #include "llvm/Support/YAMLParser.h"
  18. #include <atomic>
  19. #include <memory>
  20. using namespace clang;
  21. using namespace clang::vfs;
  22. using namespace llvm;
  23. using llvm::sys::fs::file_status;
  24. using llvm::sys::fs::file_type;
  25. using llvm::sys::fs::perms;
  26. using llvm::sys::fs::UniqueID;
  27. Status::Status(const file_status &Status)
  28. : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
  29. User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
  30. Type(Status.type()), Perms(Status.permissions()) {}
  31. Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID,
  32. sys::TimeValue MTime, uint32_t User, uint32_t Group,
  33. uint64_t Size, file_type Type, perms Perms)
  34. : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
  35. Type(Type), Perms(Perms) {}
  36. bool Status::equivalent(const Status &Other) const {
  37. return getUniqueID() == Other.getUniqueID();
  38. }
  39. bool Status::isDirectory() const {
  40. return Type == file_type::directory_file;
  41. }
  42. bool Status::isRegularFile() const {
  43. return Type == file_type::regular_file;
  44. }
  45. bool Status::isOther() const {
  46. return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
  47. }
  48. bool Status::isSymlink() const {
  49. return Type == file_type::symlink_file;
  50. }
  51. bool Status::isStatusKnown() const {
  52. return Type != file_type::status_error;
  53. }
  54. bool Status::exists() const {
  55. return isStatusKnown() && Type != file_type::file_not_found;
  56. }
  57. File::~File() {}
  58. FileSystem::~FileSystem() {}
  59. error_code FileSystem::getBufferForFile(const llvm::Twine &Name,
  60. std::unique_ptr<MemoryBuffer> &Result,
  61. int64_t FileSize,
  62. bool RequiresNullTerminator) {
  63. std::unique_ptr<File> F;
  64. if (error_code EC = openFileForRead(Name, F))
  65. return EC;
  66. error_code EC = F->getBuffer(Name, Result, FileSize, RequiresNullTerminator);
  67. return EC;
  68. }
  69. //===-----------------------------------------------------------------------===/
  70. // RealFileSystem implementation
  71. //===-----------------------------------------------------------------------===/
  72. namespace {
  73. /// \brief Wrapper around a raw file descriptor.
  74. class RealFile : public File {
  75. int FD;
  76. Status S;
  77. friend class RealFileSystem;
  78. RealFile(int FD) : FD(FD) {
  79. assert(FD >= 0 && "Invalid or inactive file descriptor");
  80. }
  81. public:
  82. ~RealFile();
  83. ErrorOr<Status> status() override;
  84. error_code getBuffer(const Twine &Name, std::unique_ptr<MemoryBuffer> &Result,
  85. int64_t FileSize = -1,
  86. bool RequiresNullTerminator = true) override;
  87. error_code close() override;
  88. void setName(StringRef Name) override;
  89. };
  90. } // end anonymous namespace
  91. RealFile::~RealFile() { close(); }
  92. ErrorOr<Status> RealFile::status() {
  93. assert(FD != -1 && "cannot stat closed file");
  94. if (!S.isStatusKnown()) {
  95. file_status RealStatus;
  96. if (error_code EC = sys::fs::status(FD, RealStatus))
  97. return EC;
  98. Status NewS(RealStatus);
  99. NewS.setName(S.getName());
  100. S = std::move(NewS);
  101. }
  102. return S;
  103. }
  104. error_code RealFile::getBuffer(const Twine &Name,
  105. std::unique_ptr<MemoryBuffer> &Result,
  106. int64_t FileSize, bool RequiresNullTerminator) {
  107. assert(FD != -1 && "cannot get buffer for closed file");
  108. return MemoryBuffer::getOpenFile(FD, Name.str().c_str(), Result, FileSize,
  109. RequiresNullTerminator);
  110. }
  111. // FIXME: This is terrible, we need this for ::close.
  112. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  113. #include <unistd.h>
  114. #include <sys/uio.h>
  115. #else
  116. #include <io.h>
  117. #ifndef S_ISFIFO
  118. #define S_ISFIFO(x) (0)
  119. #endif
  120. #endif
  121. error_code RealFile::close() {
  122. if (::close(FD))
  123. return error_code(errno, system_category());
  124. FD = -1;
  125. return error_code::success();
  126. }
  127. void RealFile::setName(StringRef Name) {
  128. S.setName(Name);
  129. }
  130. namespace {
  131. /// \brief The file system according to your operating system.
  132. class RealFileSystem : public FileSystem {
  133. public:
  134. ErrorOr<Status> status(const Twine &Path) override;
  135. error_code openFileForRead(const Twine &Path,
  136. std::unique_ptr<File> &Result) override;
  137. };
  138. } // end anonymous namespace
  139. ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
  140. sys::fs::file_status RealStatus;
  141. if (error_code EC = sys::fs::status(Path, RealStatus))
  142. return EC;
  143. Status Result(RealStatus);
  144. Result.setName(Path.str());
  145. return Result;
  146. }
  147. error_code RealFileSystem::openFileForRead(const Twine &Name,
  148. std::unique_ptr<File> &Result) {
  149. int FD;
  150. if (error_code EC = sys::fs::openFileForRead(Name, FD))
  151. return EC;
  152. Result.reset(new RealFile(FD));
  153. Result->setName(Name.str());
  154. return error_code::success();
  155. }
  156. IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
  157. static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
  158. return FS;
  159. }
  160. //===-----------------------------------------------------------------------===/
  161. // OverlayFileSystem implementation
  162. //===-----------------------------------------------------------------------===/
  163. OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
  164. pushOverlay(BaseFS);
  165. }
  166. void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
  167. FSList.push_back(FS);
  168. }
  169. ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
  170. // FIXME: handle symlinks that cross file systems
  171. for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
  172. ErrorOr<Status> Status = (*I)->status(Path);
  173. if (Status || Status.getError() != errc::no_such_file_or_directory)
  174. return Status;
  175. }
  176. return error_code(errc::no_such_file_or_directory, system_category());
  177. }
  178. error_code OverlayFileSystem::openFileForRead(const llvm::Twine &Path,
  179. std::unique_ptr<File> &Result) {
  180. // FIXME: handle symlinks that cross file systems
  181. for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
  182. error_code EC = (*I)->openFileForRead(Path, Result);
  183. if (!EC || EC != errc::no_such_file_or_directory)
  184. return EC;
  185. }
  186. return error_code(errc::no_such_file_or_directory, system_category());
  187. }
  188. //===-----------------------------------------------------------------------===/
  189. // VFSFromYAML implementation
  190. //===-----------------------------------------------------------------------===/
  191. // Allow DenseMap<StringRef, ...>. This is useful below because we know all the
  192. // strings are literals and will outlive the map, and there is no reason to
  193. // store them.
  194. namespace llvm {
  195. template<>
  196. struct DenseMapInfo<StringRef> {
  197. // This assumes that "" will never be a valid key.
  198. static inline StringRef getEmptyKey() { return StringRef(""); }
  199. static inline StringRef getTombstoneKey() { return StringRef(); }
  200. static unsigned getHashValue(StringRef Val) { return HashString(Val); }
  201. static bool isEqual(StringRef LHS, StringRef RHS) { return LHS == RHS; }
  202. };
  203. }
  204. namespace {
  205. enum EntryKind {
  206. EK_Directory,
  207. EK_File
  208. };
  209. /// \brief A single file or directory in the VFS.
  210. class Entry {
  211. EntryKind Kind;
  212. std::string Name;
  213. public:
  214. virtual ~Entry();
  215. Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
  216. StringRef getName() const { return Name; }
  217. EntryKind getKind() const { return Kind; }
  218. };
  219. class DirectoryEntry : public Entry {
  220. std::vector<Entry *> Contents;
  221. Status S;
  222. public:
  223. virtual ~DirectoryEntry();
  224. DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S)
  225. : Entry(EK_Directory, Name), Contents(std::move(Contents)),
  226. S(std::move(S)) {}
  227. Status getStatus() { return S; }
  228. typedef std::vector<Entry *>::iterator iterator;
  229. iterator contents_begin() { return Contents.begin(); }
  230. iterator contents_end() { return Contents.end(); }
  231. static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
  232. };
  233. class FileEntry : public Entry {
  234. public:
  235. enum NameKind {
  236. NK_NotSet,
  237. NK_External,
  238. NK_Virtual
  239. };
  240. private:
  241. std::string ExternalContentsPath;
  242. NameKind UseName;
  243. public:
  244. FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName)
  245. : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
  246. UseName(UseName) {}
  247. StringRef getExternalContentsPath() const { return ExternalContentsPath; }
  248. /// \brief whether to use the external path as the name for this file.
  249. bool useExternalName(bool GlobalUseExternalName) const {
  250. return UseName == NK_NotSet ? GlobalUseExternalName
  251. : (UseName == NK_External);
  252. }
  253. static bool classof(const Entry *E) { return E->getKind() == EK_File; }
  254. };
  255. /// \brief A virtual file system parsed from a YAML file.
  256. ///
  257. /// Currently, this class allows creating virtual directories and mapping
  258. /// virtual file paths to existing external files, available in \c ExternalFS.
  259. ///
  260. /// The basic structure of the parsed file is:
  261. /// \verbatim
  262. /// {
  263. /// 'version': <version number>,
  264. /// <optional configuration>
  265. /// 'roots': [
  266. /// <directory entries>
  267. /// ]
  268. /// }
  269. /// \endverbatim
  270. ///
  271. /// All configuration options are optional.
  272. /// 'case-sensitive': <boolean, default=true>
  273. /// 'use-external-names': <boolean, default=true>
  274. ///
  275. /// Virtual directories are represented as
  276. /// \verbatim
  277. /// {
  278. /// 'type': 'directory',
  279. /// 'name': <string>,
  280. /// 'contents': [ <file or directory entries> ]
  281. /// }
  282. /// \endverbatim
  283. ///
  284. /// The default attributes for virtual directories are:
  285. /// \verbatim
  286. /// MTime = now() when created
  287. /// Perms = 0777
  288. /// User = Group = 0
  289. /// Size = 0
  290. /// UniqueID = unspecified unique value
  291. /// \endverbatim
  292. ///
  293. /// Re-mapped files are represented as
  294. /// \verbatim
  295. /// {
  296. /// 'type': 'file',
  297. /// 'name': <string>,
  298. /// 'use-external-name': <boolean> # Optional
  299. /// 'external-contents': <path to external file>)
  300. /// }
  301. /// \endverbatim
  302. ///
  303. /// and inherit their attributes from the external contents.
  304. ///
  305. /// In both cases, the 'name' field may contain multiple path components (e.g.
  306. /// /path/to/file). However, any directory that contains more than one child
  307. /// must be uniquely represented by a directory entry.
  308. class VFSFromYAML : public vfs::FileSystem {
  309. std::vector<Entry *> Roots; ///< The root(s) of the virtual file system.
  310. /// \brief The file system to use for external references.
  311. IntrusiveRefCntPtr<FileSystem> ExternalFS;
  312. /// @name Configuration
  313. /// @{
  314. /// \brief Whether to perform case-sensitive comparisons.
  315. ///
  316. /// Currently, case-insensitive matching only works correctly with ASCII.
  317. bool CaseSensitive;
  318. /// \brief Whether to use to use the value of 'external-contents' for the
  319. /// names of files. This global value is overridable on a per-file basis.
  320. bool UseExternalNames;
  321. /// @}
  322. friend class VFSFromYAMLParser;
  323. private:
  324. VFSFromYAML(IntrusiveRefCntPtr<FileSystem> ExternalFS)
  325. : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {}
  326. /// \brief Looks up \p Path in \c Roots.
  327. ErrorOr<Entry *> lookupPath(const Twine &Path);
  328. /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly
  329. /// recursing into the contents of \p From if it is a directory.
  330. ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
  331. sys::path::const_iterator End, Entry *From);
  332. public:
  333. ~VFSFromYAML();
  334. /// \brief Parses \p Buffer, which is expected to be in YAML format and
  335. /// returns a virtual file system representing its contents.
  336. ///
  337. /// Takes ownership of \p Buffer.
  338. static VFSFromYAML *create(MemoryBuffer *Buffer,
  339. SourceMgr::DiagHandlerTy DiagHandler,
  340. void *DiagContext,
  341. IntrusiveRefCntPtr<FileSystem> ExternalFS);
  342. ErrorOr<Status> status(const Twine &Path) override;
  343. error_code openFileForRead(const Twine &Path,
  344. std::unique_ptr<File> &Result) override;
  345. };
  346. /// \brief A helper class to hold the common YAML parsing state.
  347. class VFSFromYAMLParser {
  348. yaml::Stream &Stream;
  349. void error(yaml::Node *N, const Twine &Msg) {
  350. Stream.printError(N, Msg);
  351. }
  352. // false on error
  353. bool parseScalarString(yaml::Node *N, StringRef &Result,
  354. SmallVectorImpl<char> &Storage) {
  355. yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
  356. if (!S) {
  357. error(N, "expected string");
  358. return false;
  359. }
  360. Result = S->getValue(Storage);
  361. return true;
  362. }
  363. // false on error
  364. bool parseScalarBool(yaml::Node *N, bool &Result) {
  365. SmallString<5> Storage;
  366. StringRef Value;
  367. if (!parseScalarString(N, Value, Storage))
  368. return false;
  369. if (Value.equals_lower("true") || Value.equals_lower("on") ||
  370. Value.equals_lower("yes") || Value == "1") {
  371. Result = true;
  372. return true;
  373. } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
  374. Value.equals_lower("no") || Value == "0") {
  375. Result = false;
  376. return true;
  377. }
  378. error(N, "expected boolean value");
  379. return false;
  380. }
  381. struct KeyStatus {
  382. KeyStatus(bool Required=false) : Required(Required), Seen(false) {}
  383. bool Required;
  384. bool Seen;
  385. };
  386. typedef std::pair<StringRef, KeyStatus> KeyStatusPair;
  387. // false on error
  388. bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
  389. DenseMap<StringRef, KeyStatus> &Keys) {
  390. if (!Keys.count(Key)) {
  391. error(KeyNode, "unknown key");
  392. return false;
  393. }
  394. KeyStatus &S = Keys[Key];
  395. if (S.Seen) {
  396. error(KeyNode, Twine("duplicate key '") + Key + "'");
  397. return false;
  398. }
  399. S.Seen = true;
  400. return true;
  401. }
  402. // false on error
  403. bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
  404. for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(),
  405. E = Keys.end();
  406. I != E; ++I) {
  407. if (I->second.Required && !I->second.Seen) {
  408. error(Obj, Twine("missing key '") + I->first + "'");
  409. return false;
  410. }
  411. }
  412. return true;
  413. }
  414. Entry *parseEntry(yaml::Node *N) {
  415. yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N);
  416. if (!M) {
  417. error(N, "expected mapping node for file or directory entry");
  418. return NULL;
  419. }
  420. KeyStatusPair Fields[] = {
  421. KeyStatusPair("name", true),
  422. KeyStatusPair("type", true),
  423. KeyStatusPair("contents", false),
  424. KeyStatusPair("external-contents", false),
  425. KeyStatusPair("use-external-name", false),
  426. };
  427. DenseMap<StringRef, KeyStatus> Keys(
  428. &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));
  429. bool HasContents = false; // external or otherwise
  430. std::vector<Entry *> EntryArrayContents;
  431. std::string ExternalContentsPath;
  432. std::string Name;
  433. FileEntry::NameKind UseExternalName = FileEntry::NK_NotSet;
  434. EntryKind Kind;
  435. for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E;
  436. ++I) {
  437. StringRef Key;
  438. // Reuse the buffer for key and value, since we don't look at key after
  439. // parsing value.
  440. SmallString<256> Buffer;
  441. if (!parseScalarString(I->getKey(), Key, Buffer))
  442. return NULL;
  443. if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
  444. return NULL;
  445. StringRef Value;
  446. if (Key == "name") {
  447. if (!parseScalarString(I->getValue(), Value, Buffer))
  448. return NULL;
  449. Name = Value;
  450. } else if (Key == "type") {
  451. if (!parseScalarString(I->getValue(), Value, Buffer))
  452. return NULL;
  453. if (Value == "file")
  454. Kind = EK_File;
  455. else if (Value == "directory")
  456. Kind = EK_Directory;
  457. else {
  458. error(I->getValue(), "unknown value for 'type'");
  459. return NULL;
  460. }
  461. } else if (Key == "contents") {
  462. if (HasContents) {
  463. error(I->getKey(),
  464. "entry already has 'contents' or 'external-contents'");
  465. return NULL;
  466. }
  467. HasContents = true;
  468. yaml::SequenceNode *Contents =
  469. dyn_cast<yaml::SequenceNode>(I->getValue());
  470. if (!Contents) {
  471. // FIXME: this is only for directories, what about files?
  472. error(I->getValue(), "expected array");
  473. return NULL;
  474. }
  475. for (yaml::SequenceNode::iterator I = Contents->begin(),
  476. E = Contents->end();
  477. I != E; ++I) {
  478. if (Entry *E = parseEntry(&*I))
  479. EntryArrayContents.push_back(E);
  480. else
  481. return NULL;
  482. }
  483. } else if (Key == "external-contents") {
  484. if (HasContents) {
  485. error(I->getKey(),
  486. "entry already has 'contents' or 'external-contents'");
  487. return NULL;
  488. }
  489. HasContents = true;
  490. if (!parseScalarString(I->getValue(), Value, Buffer))
  491. return NULL;
  492. ExternalContentsPath = Value;
  493. } else if (Key == "use-external-name") {
  494. bool Val;
  495. if (!parseScalarBool(I->getValue(), Val))
  496. return NULL;
  497. UseExternalName = Val ? FileEntry::NK_External : FileEntry::NK_Virtual;
  498. } else {
  499. llvm_unreachable("key missing from Keys");
  500. }
  501. }
  502. if (Stream.failed())
  503. return NULL;
  504. // check for missing keys
  505. if (!HasContents) {
  506. error(N, "missing key 'contents' or 'external-contents'");
  507. return NULL;
  508. }
  509. if (!checkMissingKeys(N, Keys))
  510. return NULL;
  511. // check invalid configuration
  512. if (Kind == EK_Directory && UseExternalName != FileEntry::NK_NotSet) {
  513. error(N, "'use-external-name' is not supported for directories");
  514. return NULL;
  515. }
  516. // Remove trailing slash(es), being careful not to remove the root path
  517. StringRef Trimmed(Name);
  518. size_t RootPathLen = sys::path::root_path(Trimmed).size();
  519. while (Trimmed.size() > RootPathLen &&
  520. sys::path::is_separator(Trimmed.back()))
  521. Trimmed = Trimmed.slice(0, Trimmed.size()-1);
  522. // Get the last component
  523. StringRef LastComponent = sys::path::filename(Trimmed);
  524. Entry *Result = 0;
  525. switch (Kind) {
  526. case EK_File:
  527. Result = new FileEntry(LastComponent, std::move(ExternalContentsPath),
  528. UseExternalName);
  529. break;
  530. case EK_Directory:
  531. Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents),
  532. Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
  533. 0, file_type::directory_file, sys::fs::all_all));
  534. break;
  535. }
  536. StringRef Parent = sys::path::parent_path(Trimmed);
  537. if (Parent.empty())
  538. return Result;
  539. // if 'name' contains multiple components, create implicit directory entries
  540. for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
  541. E = sys::path::rend(Parent);
  542. I != E; ++I) {
  543. Result = new DirectoryEntry(*I, llvm::makeArrayRef(Result),
  544. Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
  545. 0, file_type::directory_file, sys::fs::all_all));
  546. }
  547. return Result;
  548. }
  549. public:
  550. VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {}
  551. // false on error
  552. bool parse(yaml::Node *Root, VFSFromYAML *FS) {
  553. yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
  554. if (!Top) {
  555. error(Root, "expected mapping node");
  556. return false;
  557. }
  558. KeyStatusPair Fields[] = {
  559. KeyStatusPair("version", true),
  560. KeyStatusPair("case-sensitive", false),
  561. KeyStatusPair("use-external-names", false),
  562. KeyStatusPair("roots", true),
  563. };
  564. DenseMap<StringRef, KeyStatus> Keys(
  565. &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));
  566. // Parse configuration and 'roots'
  567. for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
  568. ++I) {
  569. SmallString<10> KeyBuffer;
  570. StringRef Key;
  571. if (!parseScalarString(I->getKey(), Key, KeyBuffer))
  572. return false;
  573. if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
  574. return false;
  575. if (Key == "roots") {
  576. yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
  577. if (!Roots) {
  578. error(I->getValue(), "expected array");
  579. return false;
  580. }
  581. for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
  582. I != E; ++I) {
  583. if (Entry *E = parseEntry(&*I))
  584. FS->Roots.push_back(E);
  585. else
  586. return false;
  587. }
  588. } else if (Key == "version") {
  589. StringRef VersionString;
  590. SmallString<4> Storage;
  591. if (!parseScalarString(I->getValue(), VersionString, Storage))
  592. return false;
  593. int Version;
  594. if (VersionString.getAsInteger<int>(10, Version)) {
  595. error(I->getValue(), "expected integer");
  596. return false;
  597. }
  598. if (Version < 0) {
  599. error(I->getValue(), "invalid version number");
  600. return false;
  601. }
  602. if (Version != 0) {
  603. error(I->getValue(), "version mismatch, expected 0");
  604. return false;
  605. }
  606. } else if (Key == "case-sensitive") {
  607. if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
  608. return false;
  609. } else if (Key == "use-external-names") {
  610. if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
  611. return false;
  612. } else {
  613. llvm_unreachable("key missing from Keys");
  614. }
  615. }
  616. if (Stream.failed())
  617. return false;
  618. if (!checkMissingKeys(Top, Keys))
  619. return false;
  620. return true;
  621. }
  622. };
  623. } // end of anonymous namespace
  624. Entry::~Entry() {}
  625. DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); }
  626. VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); }
  627. VFSFromYAML *VFSFromYAML::create(MemoryBuffer *Buffer,
  628. SourceMgr::DiagHandlerTy DiagHandler,
  629. void *DiagContext,
  630. IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  631. SourceMgr SM;
  632. yaml::Stream Stream(Buffer, SM);
  633. SM.setDiagHandler(DiagHandler, DiagContext);
  634. yaml::document_iterator DI = Stream.begin();
  635. yaml::Node *Root = DI->getRoot();
  636. if (DI == Stream.end() || !Root) {
  637. SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
  638. return NULL;
  639. }
  640. VFSFromYAMLParser P(Stream);
  641. std::unique_ptr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
  642. if (!P.parse(Root, FS.get()))
  643. return NULL;
  644. return FS.release();
  645. }
  646. ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) {
  647. SmallString<256> Path;
  648. Path_.toVector(Path);
  649. // Handle relative paths
  650. if (error_code EC = sys::fs::make_absolute(Path))
  651. return EC;
  652. if (Path.empty())
  653. return error_code(errc::invalid_argument, system_category());
  654. sys::path::const_iterator Start = sys::path::begin(Path);
  655. sys::path::const_iterator End = sys::path::end(Path);
  656. for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end();
  657. I != E; ++I) {
  658. ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
  659. if (Result || Result.getError() != errc::no_such_file_or_directory)
  660. return Result;
  661. }
  662. return error_code(errc::no_such_file_or_directory, system_category());
  663. }
  664. ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start,
  665. sys::path::const_iterator End,
  666. Entry *From) {
  667. if (Start->equals("."))
  668. ++Start;
  669. // FIXME: handle ..
  670. if (CaseSensitive ? !Start->equals(From->getName())
  671. : !Start->equals_lower(From->getName()))
  672. // failure to match
  673. return error_code(errc::no_such_file_or_directory, system_category());
  674. ++Start;
  675. if (Start == End) {
  676. // Match!
  677. return From;
  678. }
  679. DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From);
  680. if (!DE)
  681. return error_code(errc::not_a_directory, system_category());
  682. for (DirectoryEntry::iterator I = DE->contents_begin(),
  683. E = DE->contents_end();
  684. I != E; ++I) {
  685. ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
  686. if (Result || Result.getError() != errc::no_such_file_or_directory)
  687. return Result;
  688. }
  689. return error_code(errc::no_such_file_or_directory, system_category());
  690. }
  691. ErrorOr<Status> VFSFromYAML::status(const Twine &Path) {
  692. ErrorOr<Entry *> Result = lookupPath(Path);
  693. if (!Result)
  694. return Result.getError();
  695. std::string PathStr(Path.str());
  696. if (FileEntry *F = dyn_cast<FileEntry>(*Result)) {
  697. ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
  698. assert(!S || S->getName() == F->getExternalContentsPath());
  699. if (S && !F->useExternalName(UseExternalNames))
  700. S->setName(PathStr);
  701. return S;
  702. } else { // directory
  703. DirectoryEntry *DE = cast<DirectoryEntry>(*Result);
  704. Status S = DE->getStatus();
  705. S.setName(PathStr);
  706. return S;
  707. }
  708. }
  709. error_code VFSFromYAML::openFileForRead(const Twine &Path,
  710. std::unique_ptr<vfs::File> &Result) {
  711. ErrorOr<Entry *> E = lookupPath(Path);
  712. if (!E)
  713. return E.getError();
  714. FileEntry *F = dyn_cast<FileEntry>(*E);
  715. if (!F) // FIXME: errc::not_a_file?
  716. return error_code(errc::invalid_argument, system_category());
  717. if (error_code EC = ExternalFS->openFileForRead(F->getExternalContentsPath(),
  718. Result))
  719. return EC;
  720. if (!F->useExternalName(UseExternalNames))
  721. Result->setName(Path.str());
  722. return error_code::success();
  723. }
  724. IntrusiveRefCntPtr<FileSystem>
  725. vfs::getVFSFromYAML(MemoryBuffer *Buffer, SourceMgr::DiagHandlerTy DiagHandler,
  726. void *DiagContext,
  727. IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  728. return VFSFromYAML::create(Buffer, DiagHandler, DiagContext, ExternalFS);
  729. }
  730. UniqueID vfs::getNextVirtualUniqueID() {
  731. static std::atomic<unsigned> UID;
  732. unsigned ID = ++UID;
  733. // The following assumes that uint64_t max will never collide with a real
  734. // dev_t value from the OS.
  735. return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
  736. }