VirtualFileSystem.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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/OwningPtr.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/StringExtras.h"
  16. #include "llvm/Support/Atomic.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/Support/Path.h"
  19. #include "llvm/Support/YAMLParser.h"
  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. OwningPtr<MemoryBuffer> &Result,
  61. int64_t FileSize,
  62. bool RequiresNullTerminator) {
  63. llvm::OwningPtr<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. /// \brief Wrapper around a raw file descriptor.
  73. class RealFile : public File {
  74. int FD;
  75. Status S;
  76. friend class RealFileSystem;
  77. RealFile(int FD) : FD(FD) {
  78. assert(FD >= 0 && "Invalid or inactive file descriptor");
  79. }
  80. public:
  81. ~RealFile();
  82. ErrorOr<Status> status() LLVM_OVERRIDE;
  83. error_code getBuffer(const Twine &Name, OwningPtr<MemoryBuffer> &Result,
  84. int64_t FileSize = -1,
  85. bool RequiresNullTerminator = true) LLVM_OVERRIDE;
  86. error_code close() LLVM_OVERRIDE;
  87. void setName(StringRef Name) LLVM_OVERRIDE;
  88. };
  89. RealFile::~RealFile() { close(); }
  90. ErrorOr<Status> RealFile::status() {
  91. assert(FD != -1 && "cannot stat closed file");
  92. if (!S.isStatusKnown()) {
  93. file_status RealStatus;
  94. if (error_code EC = sys::fs::status(FD, RealStatus))
  95. return EC;
  96. Status NewS(RealStatus);
  97. NewS.setName(S.getName());
  98. S = llvm_move(NewS);
  99. }
  100. return S;
  101. }
  102. error_code RealFile::getBuffer(const Twine &Name,
  103. OwningPtr<MemoryBuffer> &Result,
  104. int64_t FileSize, bool RequiresNullTerminator) {
  105. assert(FD != -1 && "cannot get buffer for closed file");
  106. return MemoryBuffer::getOpenFile(FD, Name.str().c_str(), Result, FileSize,
  107. RequiresNullTerminator);
  108. }
  109. // FIXME: This is terrible, we need this for ::close.
  110. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  111. #include <unistd.h>
  112. #include <sys/uio.h>
  113. #else
  114. #include <io.h>
  115. #ifndef S_ISFIFO
  116. #define S_ISFIFO(x) (0)
  117. #endif
  118. #endif
  119. error_code RealFile::close() {
  120. if (::close(FD))
  121. return error_code(errno, system_category());
  122. FD = -1;
  123. return error_code::success();
  124. }
  125. void RealFile::setName(StringRef Name) {
  126. S.setName(Name);
  127. }
  128. /// \brief The file system according to your operating system.
  129. class RealFileSystem : public FileSystem {
  130. public:
  131. ErrorOr<Status> status(const Twine &Path) LLVM_OVERRIDE;
  132. error_code openFileForRead(const Twine &Path,
  133. OwningPtr<File> &Result) LLVM_OVERRIDE;
  134. };
  135. ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
  136. sys::fs::file_status RealStatus;
  137. if (error_code EC = sys::fs::status(Path, RealStatus))
  138. return EC;
  139. Status Result(RealStatus);
  140. Result.setName(Path.str());
  141. return Result;
  142. }
  143. error_code RealFileSystem::openFileForRead(const Twine &Name,
  144. OwningPtr<File> &Result) {
  145. int FD;
  146. if (error_code EC = sys::fs::openFileForRead(Name, FD))
  147. return EC;
  148. Result.reset(new RealFile(FD));
  149. Result->setName(Name.str());
  150. return error_code::success();
  151. }
  152. IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
  153. static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
  154. return FS;
  155. }
  156. //===-----------------------------------------------------------------------===/
  157. // OverlayFileSystem implementation
  158. //===-----------------------------------------------------------------------===/
  159. OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
  160. pushOverlay(BaseFS);
  161. }
  162. void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
  163. FSList.push_back(FS);
  164. }
  165. ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
  166. // FIXME: handle symlinks that cross file systems
  167. for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
  168. ErrorOr<Status> Status = (*I)->status(Path);
  169. if (Status || Status.getError() != errc::no_such_file_or_directory)
  170. return Status;
  171. }
  172. return error_code(errc::no_such_file_or_directory, system_category());
  173. }
  174. error_code OverlayFileSystem::openFileForRead(const llvm::Twine &Path,
  175. OwningPtr<File> &Result) {
  176. // FIXME: handle symlinks that cross file systems
  177. for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
  178. error_code EC = (*I)->openFileForRead(Path, Result);
  179. if (!EC || EC != errc::no_such_file_or_directory)
  180. return EC;
  181. }
  182. return error_code(errc::no_such_file_or_directory, system_category());
  183. }
  184. //===-----------------------------------------------------------------------===/
  185. // VFSFromYAML implementation
  186. //===-----------------------------------------------------------------------===/
  187. // Allow DenseMap<StringRef, ...>. This is useful below because we know all the
  188. // strings are literals and will outlive the map, and there is no reason to
  189. // store them.
  190. namespace llvm {
  191. template<>
  192. struct DenseMapInfo<StringRef> {
  193. // This assumes that "" will never be a valid key.
  194. static inline StringRef getEmptyKey() { return StringRef(""); }
  195. static inline StringRef getTombstoneKey() { return StringRef(); }
  196. static unsigned getHashValue(StringRef Val) { return HashString(Val); }
  197. static bool isEqual(StringRef LHS, StringRef RHS) { return LHS == RHS; }
  198. };
  199. }
  200. namespace {
  201. enum EntryKind {
  202. EK_Directory,
  203. EK_File
  204. };
  205. /// \brief A single file or directory in the VFS.
  206. class Entry {
  207. EntryKind Kind;
  208. std::string Name;
  209. public:
  210. virtual ~Entry();
  211. Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
  212. StringRef getName() const { return Name; }
  213. EntryKind getKind() const { return Kind; }
  214. };
  215. class DirectoryEntry : public Entry {
  216. std::vector<Entry *> Contents;
  217. Status S;
  218. public:
  219. virtual ~DirectoryEntry();
  220. #if LLVM_HAS_RVALUE_REFERENCES
  221. DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S)
  222. : Entry(EK_Directory, Name), Contents(std::move(Contents)),
  223. S(std::move(S)) {}
  224. #endif
  225. DirectoryEntry(StringRef Name, ArrayRef<Entry *> Contents, const Status &S)
  226. : Entry(EK_Directory, Name), Contents(Contents), S(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) LLVM_OVERRIDE;
  343. error_code openFileForRead(const Twine &Path,
  344. OwningPtr<File> &Result) LLVM_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)
  517. StringRef Trimmed(Name);
  518. while (Trimmed.size() > 1 && sys::path::is_separator(Trimmed.back()))
  519. Trimmed = Trimmed.slice(0, Trimmed.size()-1);
  520. // Get the last component
  521. StringRef LastComponent = sys::path::filename(Trimmed);
  522. Entry *Result = 0;
  523. switch (Kind) {
  524. case EK_File:
  525. Result = new FileEntry(LastComponent, llvm_move(ExternalContentsPath),
  526. UseExternalName);
  527. break;
  528. case EK_Directory:
  529. Result = new DirectoryEntry(LastComponent, llvm_move(EntryArrayContents),
  530. Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
  531. 0, file_type::directory_file, sys::fs::all_all));
  532. break;
  533. }
  534. StringRef Parent = sys::path::parent_path(Trimmed);
  535. if (Parent.empty())
  536. return Result;
  537. // if 'name' contains multiple components, create implicit directory entries
  538. for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
  539. E = sys::path::rend(Parent);
  540. I != E; ++I) {
  541. Result = new DirectoryEntry(*I, Result,
  542. Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
  543. 0, file_type::directory_file, sys::fs::all_all));
  544. }
  545. return Result;
  546. }
  547. public:
  548. VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {}
  549. // false on error
  550. bool parse(yaml::Node *Root, VFSFromYAML *FS) {
  551. yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
  552. if (!Top) {
  553. error(Root, "expected mapping node");
  554. return false;
  555. }
  556. KeyStatusPair Fields[] = {
  557. KeyStatusPair("version", true),
  558. KeyStatusPair("case-sensitive", false),
  559. KeyStatusPair("use-external-names", false),
  560. KeyStatusPair("roots", true),
  561. };
  562. DenseMap<StringRef, KeyStatus> Keys(
  563. &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));
  564. // Parse configuration and 'roots'
  565. for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
  566. ++I) {
  567. SmallString<10> KeyBuffer;
  568. StringRef Key;
  569. if (!parseScalarString(I->getKey(), Key, KeyBuffer))
  570. return false;
  571. if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
  572. return false;
  573. if (Key == "roots") {
  574. yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
  575. if (!Roots) {
  576. error(I->getValue(), "expected array");
  577. return false;
  578. }
  579. for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
  580. I != E; ++I) {
  581. if (Entry *E = parseEntry(&*I))
  582. FS->Roots.push_back(E);
  583. else
  584. return false;
  585. }
  586. } else if (Key == "version") {
  587. StringRef VersionString;
  588. SmallString<4> Storage;
  589. if (!parseScalarString(I->getValue(), VersionString, Storage))
  590. return false;
  591. int Version;
  592. if (VersionString.getAsInteger<int>(10, Version)) {
  593. error(I->getValue(), "expected integer");
  594. return false;
  595. }
  596. if (Version < 0) {
  597. error(I->getValue(), "invalid version number");
  598. return false;
  599. }
  600. if (Version != 0) {
  601. error(I->getValue(), "version mismatch, expected 0");
  602. return false;
  603. }
  604. } else if (Key == "case-sensitive") {
  605. if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
  606. return false;
  607. } else if (Key == "use-external-names") {
  608. if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
  609. return false;
  610. } else {
  611. llvm_unreachable("key missing from Keys");
  612. }
  613. }
  614. if (Stream.failed())
  615. return false;
  616. if (!checkMissingKeys(Top, Keys))
  617. return false;
  618. return true;
  619. }
  620. };
  621. } // end of anonymous namespace
  622. Entry::~Entry() {}
  623. DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); }
  624. VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); }
  625. VFSFromYAML *VFSFromYAML::create(MemoryBuffer *Buffer,
  626. SourceMgr::DiagHandlerTy DiagHandler,
  627. void *DiagContext,
  628. IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  629. SourceMgr SM;
  630. yaml::Stream Stream(Buffer, SM);
  631. SM.setDiagHandler(DiagHandler, DiagContext);
  632. yaml::document_iterator DI = Stream.begin();
  633. yaml::Node *Root = DI->getRoot();
  634. if (DI == Stream.end() || !Root) {
  635. SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
  636. return NULL;
  637. }
  638. VFSFromYAMLParser P(Stream);
  639. OwningPtr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
  640. if (!P.parse(Root, FS.get()))
  641. return NULL;
  642. return FS.take();
  643. }
  644. ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) {
  645. SmallVector<char, 256> Storage;
  646. StringRef Path = Path_.toNullTerminatedStringRef(Storage);
  647. if (Path.empty())
  648. return error_code(errc::invalid_argument, system_category());
  649. sys::path::const_iterator Start = sys::path::begin(Path);
  650. sys::path::const_iterator End = sys::path::end(Path);
  651. for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end();
  652. I != E; ++I) {
  653. ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
  654. if (Result || Result.getError() != errc::no_such_file_or_directory)
  655. return Result;
  656. }
  657. return error_code(errc::no_such_file_or_directory, system_category());
  658. }
  659. ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start,
  660. sys::path::const_iterator End,
  661. Entry *From) {
  662. // FIXME: handle . and ..
  663. if (CaseSensitive ? !Start->equals(From->getName())
  664. : !Start->equals_lower(From->getName()))
  665. // failure to match
  666. return error_code(errc::no_such_file_or_directory, system_category());
  667. ++Start;
  668. if (Start == End) {
  669. // Match!
  670. return From;
  671. }
  672. DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From);
  673. if (!DE)
  674. return error_code(errc::not_a_directory, system_category());
  675. for (DirectoryEntry::iterator I = DE->contents_begin(),
  676. E = DE->contents_end();
  677. I != E; ++I) {
  678. ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
  679. if (Result || Result.getError() != errc::no_such_file_or_directory)
  680. return Result;
  681. }
  682. return error_code(errc::no_such_file_or_directory, system_category());
  683. }
  684. ErrorOr<Status> VFSFromYAML::status(const Twine &Path) {
  685. ErrorOr<Entry *> Result = lookupPath(Path);
  686. if (!Result)
  687. return Result.getError();
  688. std::string PathStr(Path.str());
  689. if (FileEntry *F = dyn_cast<FileEntry>(*Result)) {
  690. ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
  691. assert(!S || S->getName() == F->getExternalContentsPath());
  692. if (S && !F->useExternalName(UseExternalNames))
  693. S->setName(PathStr);
  694. return S;
  695. } else { // directory
  696. DirectoryEntry *DE = cast<DirectoryEntry>(*Result);
  697. Status S = DE->getStatus();
  698. S.setName(PathStr);
  699. return S;
  700. }
  701. }
  702. error_code VFSFromYAML::openFileForRead(const Twine &Path,
  703. OwningPtr<vfs::File> &Result) {
  704. ErrorOr<Entry *> E = lookupPath(Path);
  705. if (!E)
  706. return E.getError();
  707. FileEntry *F = dyn_cast<FileEntry>(*E);
  708. if (!F) // FIXME: errc::not_a_file?
  709. return error_code(errc::invalid_argument, system_category());
  710. if (error_code EC = ExternalFS->openFileForRead(F->getExternalContentsPath(),
  711. Result))
  712. return EC;
  713. if (!F->useExternalName(UseExternalNames))
  714. Result->setName(Path.str());
  715. return error_code::success();
  716. }
  717. IntrusiveRefCntPtr<FileSystem>
  718. vfs::getVFSFromYAML(MemoryBuffer *Buffer, SourceMgr::DiagHandlerTy DiagHandler,
  719. void *DiagContext,
  720. IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  721. return VFSFromYAML::create(Buffer, DiagHandler, DiagContext, ExternalFS);
  722. }
  723. UniqueID vfs::getNextVirtualUniqueID() {
  724. static volatile sys::cas_flag UID = 0;
  725. sys::cas_flag ID = llvm::sys::AtomicIncrement(&UID);
  726. // The following assumes that uint64_t max will never collide with a real
  727. // dev_t value from the OS.
  728. return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
  729. }