VirtualFileSystem.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. //===- VirtualFileSystem.h - Virtual File System Layer ----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file
  10. /// Defines the virtual file system interface vfs::FileSystem.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_VIRTUALFILESYSTEM_H
  14. #define LLVM_SUPPORT_VIRTUALFILESYSTEM_H
  15. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  16. #include "llvm/ADT/None.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/ADT/Twine.h"
  21. #include "llvm/Support/Chrono.h"
  22. #include "llvm/Support/ErrorOr.h"
  23. #include "llvm/Support/FileSystem.h"
  24. #include "llvm/Support/Path.h"
  25. #include "llvm/Support/SourceMgr.h"
  26. #include <cassert>
  27. #include <cstdint>
  28. #include <ctime>
  29. #include <memory>
  30. #include <stack>
  31. #include <string>
  32. #include <system_error>
  33. #include <utility>
  34. #include <vector>
  35. namespace llvm {
  36. class MemoryBuffer;
  37. namespace vfs {
  38. /// The result of a \p status operation.
  39. class Status {
  40. std::string Name;
  41. llvm::sys::fs::UniqueID UID;
  42. llvm::sys::TimePoint<> MTime;
  43. uint32_t User;
  44. uint32_t Group;
  45. uint64_t Size;
  46. llvm::sys::fs::file_type Type = llvm::sys::fs::file_type::status_error;
  47. llvm::sys::fs::perms Perms;
  48. public:
  49. // FIXME: remove when files support multiple names
  50. bool IsVFSMapped = false;
  51. Status() = default;
  52. Status(const llvm::sys::fs::file_status &Status);
  53. Status(const Twine &Name, llvm::sys::fs::UniqueID UID,
  54. llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group,
  55. uint64_t Size, llvm::sys::fs::file_type Type,
  56. llvm::sys::fs::perms Perms);
  57. /// Get a copy of a Status with a different name.
  58. static Status copyWithNewName(const Status &In, const Twine &NewName);
  59. static Status copyWithNewName(const llvm::sys::fs::file_status &In,
  60. const Twine &NewName);
  61. /// Returns the name that should be used for this file or directory.
  62. StringRef getName() const { return Name; }
  63. /// @name Status interface from llvm::sys::fs
  64. /// @{
  65. llvm::sys::fs::file_type getType() const { return Type; }
  66. llvm::sys::fs::perms getPermissions() const { return Perms; }
  67. llvm::sys::TimePoint<> getLastModificationTime() const { return MTime; }
  68. llvm::sys::fs::UniqueID getUniqueID() const { return UID; }
  69. uint32_t getUser() const { return User; }
  70. uint32_t getGroup() const { return Group; }
  71. uint64_t getSize() const { return Size; }
  72. /// @}
  73. /// @name Status queries
  74. /// These are static queries in llvm::sys::fs.
  75. /// @{
  76. bool equivalent(const Status &Other) const;
  77. bool isDirectory() const;
  78. bool isRegularFile() const;
  79. bool isOther() const;
  80. bool isSymlink() const;
  81. bool isStatusKnown() const;
  82. bool exists() const;
  83. /// @}
  84. };
  85. /// Represents an open file.
  86. class File {
  87. public:
  88. /// Destroy the file after closing it (if open).
  89. /// Sub-classes should generally call close() inside their destructors. We
  90. /// cannot do that from the base class, since close is virtual.
  91. virtual ~File();
  92. /// Get the status of the file.
  93. virtual llvm::ErrorOr<Status> status() = 0;
  94. /// Get the name of the file
  95. virtual llvm::ErrorOr<std::string> getName() {
  96. if (auto Status = status())
  97. return Status->getName().str();
  98. else
  99. return Status.getError();
  100. }
  101. /// Get the contents of the file as a \p MemoryBuffer.
  102. virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  103. getBuffer(const Twine &Name, int64_t FileSize = -1,
  104. bool RequiresNullTerminator = true, bool IsVolatile = false) = 0;
  105. /// Closes the file.
  106. virtual std::error_code close() = 0;
  107. };
  108. /// A member of a directory, yielded by a directory_iterator.
  109. /// Only information available on most platforms is included.
  110. class directory_entry {
  111. std::string Path;
  112. llvm::sys::fs::file_type Type;
  113. public:
  114. directory_entry() = default;
  115. directory_entry(std::string Path, llvm::sys::fs::file_type Type)
  116. : Path(std::move(Path)), Type(Type) {}
  117. llvm::StringRef path() const { return Path; }
  118. llvm::sys::fs::file_type type() const { return Type; }
  119. };
  120. namespace detail {
  121. /// An interface for virtual file systems to provide an iterator over the
  122. /// (non-recursive) contents of a directory.
  123. struct DirIterImpl {
  124. virtual ~DirIterImpl();
  125. /// Sets \c CurrentEntry to the next entry in the directory on success,
  126. /// to directory_entry() at end, or returns a system-defined \c error_code.
  127. virtual std::error_code increment() = 0;
  128. directory_entry CurrentEntry;
  129. };
  130. } // namespace detail
  131. /// An input iterator over the entries in a virtual path, similar to
  132. /// llvm::sys::fs::directory_iterator.
  133. class directory_iterator {
  134. std::shared_ptr<detail::DirIterImpl> Impl; // Input iterator semantics on copy
  135. public:
  136. directory_iterator(std::shared_ptr<detail::DirIterImpl> I)
  137. : Impl(std::move(I)) {
  138. assert(Impl.get() != nullptr && "requires non-null implementation");
  139. if (Impl->CurrentEntry.path().empty())
  140. Impl.reset(); // Normalize the end iterator to Impl == nullptr.
  141. }
  142. /// Construct an 'end' iterator.
  143. directory_iterator() = default;
  144. /// Equivalent to operator++, with an error code.
  145. directory_iterator &increment(std::error_code &EC) {
  146. assert(Impl && "attempting to increment past end");
  147. EC = Impl->increment();
  148. if (Impl->CurrentEntry.path().empty())
  149. Impl.reset(); // Normalize the end iterator to Impl == nullptr.
  150. return *this;
  151. }
  152. const directory_entry &operator*() const { return Impl->CurrentEntry; }
  153. const directory_entry *operator->() const { return &Impl->CurrentEntry; }
  154. bool operator==(const directory_iterator &RHS) const {
  155. if (Impl && RHS.Impl)
  156. return Impl->CurrentEntry.path() == RHS.Impl->CurrentEntry.path();
  157. return !Impl && !RHS.Impl;
  158. }
  159. bool operator!=(const directory_iterator &RHS) const {
  160. return !(*this == RHS);
  161. }
  162. };
  163. class FileSystem;
  164. namespace detail {
  165. /// Keeps state for the recursive_directory_iterator.
  166. struct RecDirIterState {
  167. std::stack<directory_iterator, std::vector<directory_iterator>> Stack;
  168. bool HasNoPushRequest = false;
  169. };
  170. } // end namespace detail
  171. /// An input iterator over the recursive contents of a virtual path,
  172. /// similar to llvm::sys::fs::recursive_directory_iterator.
  173. class recursive_directory_iterator {
  174. FileSystem *FS;
  175. std::shared_ptr<detail::RecDirIterState>
  176. State; // Input iterator semantics on copy.
  177. public:
  178. recursive_directory_iterator(FileSystem &FS, const Twine &Path,
  179. std::error_code &EC);
  180. /// Construct an 'end' iterator.
  181. recursive_directory_iterator() = default;
  182. /// Equivalent to operator++, with an error code.
  183. recursive_directory_iterator &increment(std::error_code &EC);
  184. const directory_entry &operator*() const { return *State->Stack.top(); }
  185. const directory_entry *operator->() const { return &*State->Stack.top(); }
  186. bool operator==(const recursive_directory_iterator &Other) const {
  187. return State == Other.State; // identity
  188. }
  189. bool operator!=(const recursive_directory_iterator &RHS) const {
  190. return !(*this == RHS);
  191. }
  192. /// Gets the current level. Starting path is at level 0.
  193. int level() const {
  194. assert(!State->Stack.empty() &&
  195. "Cannot get level without any iteration state");
  196. return State->Stack.size() - 1;
  197. }
  198. void no_push() { State->HasNoPushRequest = true; }
  199. };
  200. /// The virtual file system interface.
  201. class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> {
  202. public:
  203. virtual ~FileSystem();
  204. /// Get the status of the entry at \p Path, if one exists.
  205. virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
  206. /// Get a \p File object for the file at \p Path, if one exists.
  207. virtual llvm::ErrorOr<std::unique_ptr<File>>
  208. openFileForRead(const Twine &Path) = 0;
  209. /// This is a convenience method that opens a file, gets its content and then
  210. /// closes the file.
  211. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  212. getBufferForFile(const Twine &Name, int64_t FileSize = -1,
  213. bool RequiresNullTerminator = true, bool IsVolatile = false);
  214. /// Get a directory_iterator for \p Dir.
  215. /// \note The 'end' iterator is directory_iterator().
  216. virtual directory_iterator dir_begin(const Twine &Dir,
  217. std::error_code &EC) = 0;
  218. /// Set the working directory. This will affect all following operations on
  219. /// this file system and may propagate down for nested file systems.
  220. virtual std::error_code setCurrentWorkingDirectory(const Twine &Path) = 0;
  221. /// Get the working directory of this file system.
  222. virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const = 0;
  223. /// Gets real path of \p Path e.g. collapse all . and .. patterns, resolve
  224. /// symlinks. For real file system, this uses `llvm::sys::fs::real_path`.
  225. /// This returns errc::operation_not_permitted if not implemented by subclass.
  226. virtual std::error_code getRealPath(const Twine &Path,
  227. SmallVectorImpl<char> &Output) const;
  228. /// Check whether a file exists. Provided for convenience.
  229. bool exists(const Twine &Path);
  230. /// Is the file mounted on a local filesystem?
  231. virtual std::error_code isLocal(const Twine &Path, bool &Result);
  232. /// Make \a Path an absolute path.
  233. ///
  234. /// Makes \a Path absolute using the current directory if it is not already.
  235. /// An empty \a Path will result in the current directory.
  236. ///
  237. /// /absolute/path => /absolute/path
  238. /// relative/../path => <current-directory>/relative/../path
  239. ///
  240. /// \param Path A path that is modified to be an absolute path.
  241. /// \returns success if \a path has been made absolute, otherwise a
  242. /// platform-specific error_code.
  243. std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const;
  244. };
  245. /// Gets an \p vfs::FileSystem for the 'real' file system, as seen by
  246. /// the operating system.
  247. /// The working directory is linked to the process's working directory.
  248. /// (This is usually thread-hostile).
  249. IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
  250. /// Create an \p vfs::FileSystem for the 'real' file system, as seen by
  251. /// the operating system.
  252. /// It has its own working directory, independent of (but initially equal to)
  253. /// that of the process.
  254. std::unique_ptr<FileSystem> createPhysicalFileSystem();
  255. /// A file system that allows overlaying one \p AbstractFileSystem on top
  256. /// of another.
  257. ///
  258. /// Consists of a stack of >=1 \p FileSystem objects, which are treated as being
  259. /// one merged file system. When there is a directory that exists in more than
  260. /// one file system, the \p OverlayFileSystem contains a directory containing
  261. /// the union of their contents. The attributes (permissions, etc.) of the
  262. /// top-most (most recently added) directory are used. When there is a file
  263. /// that exists in more than one file system, the file in the top-most file
  264. /// system overrides the other(s).
  265. class OverlayFileSystem : public FileSystem {
  266. using FileSystemList = SmallVector<IntrusiveRefCntPtr<FileSystem>, 1>;
  267. /// The stack of file systems, implemented as a list in order of
  268. /// their addition.
  269. FileSystemList FSList;
  270. public:
  271. OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base);
  272. /// Pushes a file system on top of the stack.
  273. void pushOverlay(IntrusiveRefCntPtr<FileSystem> FS);
  274. llvm::ErrorOr<Status> status(const Twine &Path) override;
  275. llvm::ErrorOr<std::unique_ptr<File>>
  276. openFileForRead(const Twine &Path) override;
  277. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
  278. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
  279. std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
  280. std::error_code isLocal(const Twine &Path, bool &Result) override;
  281. std::error_code getRealPath(const Twine &Path,
  282. SmallVectorImpl<char> &Output) const override;
  283. using iterator = FileSystemList::reverse_iterator;
  284. using const_iterator = FileSystemList::const_reverse_iterator;
  285. using reverse_iterator = FileSystemList::iterator;
  286. using const_reverse_iterator = FileSystemList::const_iterator;
  287. /// Get an iterator pointing to the most recently added file system.
  288. iterator overlays_begin() { return FSList.rbegin(); }
  289. const_iterator overlays_begin() const { return FSList.rbegin(); }
  290. /// Get an iterator pointing one-past the least recently added file system.
  291. iterator overlays_end() { return FSList.rend(); }
  292. const_iterator overlays_end() const { return FSList.rend(); }
  293. /// Get an iterator pointing to the least recently added file system.
  294. reverse_iterator overlays_rbegin() { return FSList.begin(); }
  295. const_reverse_iterator overlays_rbegin() const { return FSList.begin(); }
  296. /// Get an iterator pointing one-past the most recently added file system.
  297. reverse_iterator overlays_rend() { return FSList.end(); }
  298. const_reverse_iterator overlays_rend() const { return FSList.end(); }
  299. };
  300. /// By default, this delegates all calls to the underlying file system. This
  301. /// is useful when derived file systems want to override some calls and still
  302. /// proxy other calls.
  303. class ProxyFileSystem : public FileSystem {
  304. public:
  305. explicit ProxyFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
  306. : FS(std::move(FS)) {}
  307. llvm::ErrorOr<Status> status(const Twine &Path) override {
  308. return FS->status(Path);
  309. }
  310. llvm::ErrorOr<std::unique_ptr<File>>
  311. openFileForRead(const Twine &Path) override {
  312. return FS->openFileForRead(Path);
  313. }
  314. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
  315. return FS->dir_begin(Dir, EC);
  316. }
  317. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
  318. return FS->getCurrentWorkingDirectory();
  319. }
  320. std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
  321. return FS->setCurrentWorkingDirectory(Path);
  322. }
  323. std::error_code getRealPath(const Twine &Path,
  324. SmallVectorImpl<char> &Output) const override {
  325. return FS->getRealPath(Path, Output);
  326. }
  327. std::error_code isLocal(const Twine &Path, bool &Result) override {
  328. return FS->isLocal(Path, Result);
  329. }
  330. protected:
  331. FileSystem &getUnderlyingFS() { return *FS; }
  332. private:
  333. IntrusiveRefCntPtr<FileSystem> FS;
  334. virtual void anchor();
  335. };
  336. namespace detail {
  337. class InMemoryDirectory;
  338. class InMemoryFile;
  339. } // namespace detail
  340. /// An in-memory file system.
  341. class InMemoryFileSystem : public FileSystem {
  342. std::unique_ptr<detail::InMemoryDirectory> Root;
  343. std::string WorkingDirectory;
  344. bool UseNormalizedPaths = true;
  345. /// If HardLinkTarget is non-null, a hardlink is created to the To path which
  346. /// must be a file. If it is null then it adds the file as the public addFile.
  347. bool addFile(const Twine &Path, time_t ModificationTime,
  348. std::unique_ptr<llvm::MemoryBuffer> Buffer,
  349. Optional<uint32_t> User, Optional<uint32_t> Group,
  350. Optional<llvm::sys::fs::file_type> Type,
  351. Optional<llvm::sys::fs::perms> Perms,
  352. const detail::InMemoryFile *HardLinkTarget);
  353. public:
  354. explicit InMemoryFileSystem(bool UseNormalizedPaths = true);
  355. ~InMemoryFileSystem() override;
  356. /// Add a file containing a buffer or a directory to the VFS with a
  357. /// path. The VFS owns the buffer. If present, User, Group, Type
  358. /// and Perms apply to the newly-created file or directory.
  359. /// \return true if the file or directory was successfully added,
  360. /// false if the file or directory already exists in the file system with
  361. /// different contents.
  362. bool addFile(const Twine &Path, time_t ModificationTime,
  363. std::unique_ptr<llvm::MemoryBuffer> Buffer,
  364. Optional<uint32_t> User = None, Optional<uint32_t> Group = None,
  365. Optional<llvm::sys::fs::file_type> Type = None,
  366. Optional<llvm::sys::fs::perms> Perms = None);
  367. /// Add a hard link to a file.
  368. /// Here hard links are not intended to be fully equivalent to the classical
  369. /// filesystem. Both the hard link and the file share the same buffer and
  370. /// status (and thus have the same UniqueID). Because of this there is no way
  371. /// to distinguish between the link and the file after the link has been
  372. /// added.
  373. ///
  374. /// The To path must be an existing file or a hardlink. The From file must not
  375. /// have been added before. The To Path must not be a directory. The From Node
  376. /// is added as a hard link which points to the resolved file of To Node.
  377. /// \return true if the above condition is satisfied and hardlink was
  378. /// successfully created, false otherwise.
  379. bool addHardLink(const Twine &From, const Twine &To);
  380. /// Add a buffer to the VFS with a path. The VFS does not own the buffer.
  381. /// If present, User, Group, Type and Perms apply to the newly-created file
  382. /// or directory.
  383. /// \return true if the file or directory was successfully added,
  384. /// false if the file or directory already exists in the file system with
  385. /// different contents.
  386. bool addFileNoOwn(const Twine &Path, time_t ModificationTime,
  387. llvm::MemoryBuffer *Buffer, Optional<uint32_t> User = None,
  388. Optional<uint32_t> Group = None,
  389. Optional<llvm::sys::fs::file_type> Type = None,
  390. Optional<llvm::sys::fs::perms> Perms = None);
  391. std::string toString() const;
  392. /// Return true if this file system normalizes . and .. in paths.
  393. bool useNormalizedPaths() const { return UseNormalizedPaths; }
  394. llvm::ErrorOr<Status> status(const Twine &Path) override;
  395. llvm::ErrorOr<std::unique_ptr<File>>
  396. openFileForRead(const Twine &Path) override;
  397. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
  398. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
  399. return WorkingDirectory;
  400. }
  401. /// Canonicalizes \p Path by combining with the current working
  402. /// directory and normalizing the path (e.g. remove dots). If the current
  403. /// working directory is not set, this returns errc::operation_not_permitted.
  404. ///
  405. /// This doesn't resolve symlinks as they are not supported in in-memory file
  406. /// system.
  407. std::error_code getRealPath(const Twine &Path,
  408. SmallVectorImpl<char> &Output) const override;
  409. std::error_code isLocal(const Twine &Path, bool &Result) override;
  410. std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
  411. };
  412. /// Get a globally unique ID for a virtual file or directory.
  413. llvm::sys::fs::UniqueID getNextVirtualUniqueID();
  414. /// Gets a \p FileSystem for a virtual file system described in YAML
  415. /// format.
  416. IntrusiveRefCntPtr<FileSystem>
  417. getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer,
  418. llvm::SourceMgr::DiagHandlerTy DiagHandler,
  419. StringRef YAMLFilePath, void *DiagContext = nullptr,
  420. IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
  421. struct YAMLVFSEntry {
  422. template <typename T1, typename T2>
  423. YAMLVFSEntry(T1 &&VPath, T2 &&RPath)
  424. : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)) {}
  425. std::string VPath;
  426. std::string RPath;
  427. };
  428. class VFSFromYamlDirIterImpl;
  429. class RedirectingFileSystemParser;
  430. /// A virtual file system parsed from a YAML file.
  431. ///
  432. /// Currently, this class allows creating virtual directories and mapping
  433. /// virtual file paths to existing external files, available in \c ExternalFS.
  434. ///
  435. /// The basic structure of the parsed file is:
  436. /// \verbatim
  437. /// {
  438. /// 'version': <version number>,
  439. /// <optional configuration>
  440. /// 'roots': [
  441. /// <directory entries>
  442. /// ]
  443. /// }
  444. /// \endverbatim
  445. ///
  446. /// All configuration options are optional.
  447. /// 'case-sensitive': <boolean, default=true>
  448. /// 'use-external-names': <boolean, default=true>
  449. /// 'overlay-relative': <boolean, default=false>
  450. /// 'fallthrough': <boolean, default=true>
  451. ///
  452. /// Virtual directories are represented as
  453. /// \verbatim
  454. /// {
  455. /// 'type': 'directory',
  456. /// 'name': <string>,
  457. /// 'contents': [ <file or directory entries> ]
  458. /// }
  459. /// \endverbatim
  460. ///
  461. /// The default attributes for virtual directories are:
  462. /// \verbatim
  463. /// MTime = now() when created
  464. /// Perms = 0777
  465. /// User = Group = 0
  466. /// Size = 0
  467. /// UniqueID = unspecified unique value
  468. /// \endverbatim
  469. ///
  470. /// Re-mapped files are represented as
  471. /// \verbatim
  472. /// {
  473. /// 'type': 'file',
  474. /// 'name': <string>,
  475. /// 'use-external-name': <boolean> # Optional
  476. /// 'external-contents': <path to external file>
  477. /// }
  478. /// \endverbatim
  479. ///
  480. /// and inherit their attributes from the external contents.
  481. ///
  482. /// In both cases, the 'name' field may contain multiple path components (e.g.
  483. /// /path/to/file). However, any directory that contains more than one child
  484. /// must be uniquely represented by a directory entry.
  485. class RedirectingFileSystem : public vfs::FileSystem {
  486. public:
  487. enum EntryKind { EK_Directory, EK_File };
  488. /// A single file or directory in the VFS.
  489. class Entry {
  490. EntryKind Kind;
  491. std::string Name;
  492. public:
  493. Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
  494. virtual ~Entry() = default;
  495. StringRef getName() const { return Name; }
  496. EntryKind getKind() const { return Kind; }
  497. };
  498. class RedirectingDirectoryEntry : public Entry {
  499. std::vector<std::unique_ptr<Entry>> Contents;
  500. Status S;
  501. public:
  502. RedirectingDirectoryEntry(StringRef Name,
  503. std::vector<std::unique_ptr<Entry>> Contents,
  504. Status S)
  505. : Entry(EK_Directory, Name), Contents(std::move(Contents)),
  506. S(std::move(S)) {}
  507. RedirectingDirectoryEntry(StringRef Name, Status S)
  508. : Entry(EK_Directory, Name), S(std::move(S)) {}
  509. Status getStatus() { return S; }
  510. void addContent(std::unique_ptr<Entry> Content) {
  511. Contents.push_back(std::move(Content));
  512. }
  513. Entry *getLastContent() const { return Contents.back().get(); }
  514. using iterator = decltype(Contents)::iterator;
  515. iterator contents_begin() { return Contents.begin(); }
  516. iterator contents_end() { return Contents.end(); }
  517. static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
  518. };
  519. class RedirectingFileEntry : public Entry {
  520. public:
  521. enum NameKind { NK_NotSet, NK_External, NK_Virtual };
  522. private:
  523. std::string ExternalContentsPath;
  524. NameKind UseName;
  525. public:
  526. RedirectingFileEntry(StringRef Name, StringRef ExternalContentsPath,
  527. NameKind UseName)
  528. : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
  529. UseName(UseName) {}
  530. StringRef getExternalContentsPath() const { return ExternalContentsPath; }
  531. /// whether to use the external path as the name for this file.
  532. bool useExternalName(bool GlobalUseExternalName) const {
  533. return UseName == NK_NotSet ? GlobalUseExternalName
  534. : (UseName == NK_External);
  535. }
  536. NameKind getUseName() const { return UseName; }
  537. static bool classof(const Entry *E) { return E->getKind() == EK_File; }
  538. };
  539. private:
  540. friend class VFSFromYamlDirIterImpl;
  541. friend class RedirectingFileSystemParser;
  542. /// The root(s) of the virtual file system.
  543. std::vector<std::unique_ptr<Entry>> Roots;
  544. /// The file system to use for external references.
  545. IntrusiveRefCntPtr<FileSystem> ExternalFS;
  546. /// If IsRelativeOverlay is set, this represents the directory
  547. /// path that should be prefixed to each 'external-contents' entry
  548. /// when reading from YAML files.
  549. std::string ExternalContentsPrefixDir;
  550. /// @name Configuration
  551. /// @{
  552. /// Whether to perform case-sensitive comparisons.
  553. ///
  554. /// Currently, case-insensitive matching only works correctly with ASCII.
  555. bool CaseSensitive = true;
  556. /// IsRelativeOverlay marks whether a ExternalContentsPrefixDir path must
  557. /// be prefixed in every 'external-contents' when reading from YAML files.
  558. bool IsRelativeOverlay = false;
  559. /// Whether to use to use the value of 'external-contents' for the
  560. /// names of files. This global value is overridable on a per-file basis.
  561. bool UseExternalNames = true;
  562. /// Whether to attempt a file lookup in external file system after it wasn't
  563. /// found in VFS.
  564. bool IsFallthrough = true;
  565. /// @}
  566. /// Virtual file paths and external files could be canonicalized without "..",
  567. /// "." and "./" in their paths. FIXME: some unittests currently fail on
  568. /// win32 when using remove_dots and remove_leading_dotslash on paths.
  569. bool UseCanonicalizedPaths =
  570. #ifdef _WIN32
  571. false;
  572. #else
  573. true;
  574. #endif
  575. RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS)
  576. : ExternalFS(std::move(ExternalFS)) {}
  577. /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly
  578. /// recursing into the contents of \p From if it is a directory.
  579. ErrorOr<Entry *> lookupPath(llvm::sys::path::const_iterator Start,
  580. llvm::sys::path::const_iterator End,
  581. Entry *From) const;
  582. /// Get the status of a given an \c Entry.
  583. ErrorOr<Status> status(const Twine &Path, Entry *E);
  584. public:
  585. /// Looks up \p Path in \c Roots.
  586. ErrorOr<Entry *> lookupPath(const Twine &Path) const;
  587. /// Parses \p Buffer, which is expected to be in YAML format and
  588. /// returns a virtual file system representing its contents.
  589. static RedirectingFileSystem *
  590. create(std::unique_ptr<MemoryBuffer> Buffer,
  591. SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
  592. void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
  593. ErrorOr<Status> status(const Twine &Path) override;
  594. ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
  595. std::error_code getRealPath(const Twine &Path,
  596. SmallVectorImpl<char> &Output) const override;
  597. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
  598. std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
  599. std::error_code isLocal(const Twine &Path, bool &Result) override;
  600. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
  601. void setExternalContentsPrefixDir(StringRef PrefixDir);
  602. StringRef getExternalContentsPrefixDir() const;
  603. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  604. LLVM_DUMP_METHOD void dump() const;
  605. LLVM_DUMP_METHOD void dumpEntry(Entry *E, int NumSpaces = 0) const;
  606. #endif
  607. };
  608. /// Collect all pairs of <virtual path, real path> entries from the
  609. /// \p YAMLFilePath. This is used by the module dependency collector to forward
  610. /// the entries into the reproducer output VFS YAML file.
  611. void collectVFSFromYAML(
  612. std::unique_ptr<llvm::MemoryBuffer> Buffer,
  613. llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
  614. SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
  615. void *DiagContext = nullptr,
  616. IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
  617. class YAMLVFSWriter {
  618. std::vector<YAMLVFSEntry> Mappings;
  619. Optional<bool> IsCaseSensitive;
  620. Optional<bool> IsOverlayRelative;
  621. Optional<bool> UseExternalNames;
  622. std::string OverlayDir;
  623. public:
  624. YAMLVFSWriter() = default;
  625. void addFileMapping(StringRef VirtualPath, StringRef RealPath);
  626. void setCaseSensitivity(bool CaseSensitive) {
  627. IsCaseSensitive = CaseSensitive;
  628. }
  629. void setUseExternalNames(bool UseExtNames) { UseExternalNames = UseExtNames; }
  630. void setOverlayDir(StringRef OverlayDirectory) {
  631. IsOverlayRelative = true;
  632. OverlayDir.assign(OverlayDirectory.str());
  633. }
  634. const std::vector<YAMLVFSEntry> &getMappings() const { return Mappings; }
  635. void write(llvm::raw_ostream &OS);
  636. };
  637. } // namespace vfs
  638. } // namespace llvm
  639. #endif // LLVM_SUPPORT_VIRTUALFILESYSTEM_H