VirtualFileSystem.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- VirtualFileSystem.h - Virtual File System Layer ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// Defines the virtual file system interface vfs::FileSystem.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_VIRTUALFILESYSTEM_H
  19. #define LLVM_SUPPORT_VIRTUALFILESYSTEM_H
  20. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  21. #include "llvm/ADT/None.h"
  22. #include "llvm/ADT/Optional.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/ADT/StringMap.h"
  26. #include "llvm/ADT/STLFunctionalExtras.h"
  27. #include "llvm/Support/Chrono.h"
  28. #include "llvm/Support/ErrorOr.h"
  29. #include "llvm/Support/FileSystem.h"
  30. #include "llvm/Support/Path.h"
  31. #include "llvm/Support/SourceMgr.h"
  32. #include <cassert>
  33. #include <cstdint>
  34. #include <ctime>
  35. #include <memory>
  36. #include <stack>
  37. #include <string>
  38. #include <system_error>
  39. #include <utility>
  40. #include <vector>
  41. namespace llvm {
  42. class MemoryBuffer;
  43. class MemoryBufferRef;
  44. class Twine;
  45. namespace vfs {
  46. /// The result of a \p status operation.
  47. class Status {
  48. std::string Name;
  49. llvm::sys::fs::UniqueID UID;
  50. llvm::sys::TimePoint<> MTime;
  51. uint32_t User;
  52. uint32_t Group;
  53. uint64_t Size;
  54. llvm::sys::fs::file_type Type = llvm::sys::fs::file_type::status_error;
  55. llvm::sys::fs::perms Perms;
  56. public:
  57. // FIXME: remove when files support multiple names
  58. bool IsVFSMapped = false;
  59. Status() = default;
  60. Status(const llvm::sys::fs::file_status &Status);
  61. Status(const Twine &Name, llvm::sys::fs::UniqueID UID,
  62. llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group,
  63. uint64_t Size, llvm::sys::fs::file_type Type,
  64. llvm::sys::fs::perms Perms);
  65. /// Get a copy of a Status with a different size.
  66. static Status copyWithNewSize(const Status &In, uint64_t NewSize);
  67. /// Get a copy of a Status with a different name.
  68. static Status copyWithNewName(const Status &In, const Twine &NewName);
  69. static Status copyWithNewName(const llvm::sys::fs::file_status &In,
  70. const Twine &NewName);
  71. /// Returns the name that should be used for this file or directory.
  72. StringRef getName() const { return Name; }
  73. /// @name Status interface from llvm::sys::fs
  74. /// @{
  75. llvm::sys::fs::file_type getType() const { return Type; }
  76. llvm::sys::fs::perms getPermissions() const { return Perms; }
  77. llvm::sys::TimePoint<> getLastModificationTime() const { return MTime; }
  78. llvm::sys::fs::UniqueID getUniqueID() const { return UID; }
  79. uint32_t getUser() const { return User; }
  80. uint32_t getGroup() const { return Group; }
  81. uint64_t getSize() const { return Size; }
  82. /// @}
  83. /// @name Status queries
  84. /// These are static queries in llvm::sys::fs.
  85. /// @{
  86. bool equivalent(const Status &Other) const;
  87. bool isDirectory() const;
  88. bool isRegularFile() const;
  89. bool isOther() const;
  90. bool isSymlink() const;
  91. bool isStatusKnown() const;
  92. bool exists() const;
  93. /// @}
  94. };
  95. /// Represents an open file.
  96. class File {
  97. public:
  98. /// Destroy the file after closing it (if open).
  99. /// Sub-classes should generally call close() inside their destructors. We
  100. /// cannot do that from the base class, since close is virtual.
  101. virtual ~File();
  102. /// Get the status of the file.
  103. virtual llvm::ErrorOr<Status> status() = 0;
  104. /// Get the name of the file
  105. virtual llvm::ErrorOr<std::string> getName() {
  106. if (auto Status = status())
  107. return Status->getName().str();
  108. else
  109. return Status.getError();
  110. }
  111. /// Get the contents of the file as a \p MemoryBuffer.
  112. virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  113. getBuffer(const Twine &Name, int64_t FileSize = -1,
  114. bool RequiresNullTerminator = true, bool IsVolatile = false) = 0;
  115. /// Closes the file.
  116. virtual std::error_code close() = 0;
  117. // Get the same file with a different path.
  118. static ErrorOr<std::unique_ptr<File>>
  119. getWithPath(ErrorOr<std::unique_ptr<File>> Result, const Twine &P);
  120. protected:
  121. // Set the file's underlying path.
  122. virtual void setPath(const Twine &Path) {}
  123. };
  124. /// A member of a directory, yielded by a directory_iterator.
  125. /// Only information available on most platforms is included.
  126. class directory_entry {
  127. std::string Path;
  128. llvm::sys::fs::file_type Type = llvm::sys::fs::file_type::type_unknown;
  129. public:
  130. directory_entry() = default;
  131. directory_entry(std::string Path, llvm::sys::fs::file_type Type)
  132. : Path(std::move(Path)), Type(Type) {}
  133. llvm::StringRef path() const { return Path; }
  134. llvm::sys::fs::file_type type() const { return Type; }
  135. };
  136. namespace detail {
  137. /// An interface for virtual file systems to provide an iterator over the
  138. /// (non-recursive) contents of a directory.
  139. struct DirIterImpl {
  140. virtual ~DirIterImpl();
  141. /// Sets \c CurrentEntry to the next entry in the directory on success,
  142. /// to directory_entry() at end, or returns a system-defined \c error_code.
  143. virtual std::error_code increment() = 0;
  144. directory_entry CurrentEntry;
  145. };
  146. } // namespace detail
  147. /// An input iterator over the entries in a virtual path, similar to
  148. /// llvm::sys::fs::directory_iterator.
  149. class directory_iterator {
  150. std::shared_ptr<detail::DirIterImpl> Impl; // Input iterator semantics on copy
  151. public:
  152. directory_iterator(std::shared_ptr<detail::DirIterImpl> I)
  153. : Impl(std::move(I)) {
  154. assert(Impl.get() != nullptr && "requires non-null implementation");
  155. if (Impl->CurrentEntry.path().empty())
  156. Impl.reset(); // Normalize the end iterator to Impl == nullptr.
  157. }
  158. /// Construct an 'end' iterator.
  159. directory_iterator() = default;
  160. /// Equivalent to operator++, with an error code.
  161. directory_iterator &increment(std::error_code &EC) {
  162. assert(Impl && "attempting to increment past end");
  163. EC = Impl->increment();
  164. if (Impl->CurrentEntry.path().empty())
  165. Impl.reset(); // Normalize the end iterator to Impl == nullptr.
  166. return *this;
  167. }
  168. const directory_entry &operator*() const { return Impl->CurrentEntry; }
  169. const directory_entry *operator->() const { return &Impl->CurrentEntry; }
  170. bool operator==(const directory_iterator &RHS) const {
  171. if (Impl && RHS.Impl)
  172. return Impl->CurrentEntry.path() == RHS.Impl->CurrentEntry.path();
  173. return !Impl && !RHS.Impl;
  174. }
  175. bool operator!=(const directory_iterator &RHS) const {
  176. return !(*this == RHS);
  177. }
  178. };
  179. class FileSystem;
  180. namespace detail {
  181. /// Keeps state for the recursive_directory_iterator.
  182. struct RecDirIterState {
  183. std::stack<directory_iterator, std::vector<directory_iterator>> Stack;
  184. bool HasNoPushRequest = false;
  185. };
  186. } // end namespace detail
  187. /// An input iterator over the recursive contents of a virtual path,
  188. /// similar to llvm::sys::fs::recursive_directory_iterator.
  189. class recursive_directory_iterator {
  190. FileSystem *FS;
  191. std::shared_ptr<detail::RecDirIterState>
  192. State; // Input iterator semantics on copy.
  193. public:
  194. recursive_directory_iterator(FileSystem &FS, const Twine &Path,
  195. std::error_code &EC);
  196. /// Construct an 'end' iterator.
  197. recursive_directory_iterator() = default;
  198. /// Equivalent to operator++, with an error code.
  199. recursive_directory_iterator &increment(std::error_code &EC);
  200. const directory_entry &operator*() const { return *State->Stack.top(); }
  201. const directory_entry *operator->() const { return &*State->Stack.top(); }
  202. bool operator==(const recursive_directory_iterator &Other) const {
  203. return State == Other.State; // identity
  204. }
  205. bool operator!=(const recursive_directory_iterator &RHS) const {
  206. return !(*this == RHS);
  207. }
  208. /// Gets the current level. Starting path is at level 0.
  209. int level() const {
  210. assert(!State->Stack.empty() &&
  211. "Cannot get level without any iteration state");
  212. return State->Stack.size() - 1;
  213. }
  214. void no_push() { State->HasNoPushRequest = true; }
  215. };
  216. /// The virtual file system interface.
  217. class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> {
  218. public:
  219. virtual ~FileSystem();
  220. /// Get the status of the entry at \p Path, if one exists.
  221. virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
  222. /// Get a \p File object for the file at \p Path, if one exists.
  223. virtual llvm::ErrorOr<std::unique_ptr<File>>
  224. openFileForRead(const Twine &Path) = 0;
  225. /// This is a convenience method that opens a file, gets its content and then
  226. /// closes the file.
  227. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  228. getBufferForFile(const Twine &Name, int64_t FileSize = -1,
  229. bool RequiresNullTerminator = true, bool IsVolatile = false);
  230. /// Get a directory_iterator for \p Dir.
  231. /// \note The 'end' iterator is directory_iterator().
  232. virtual directory_iterator dir_begin(const Twine &Dir,
  233. std::error_code &EC) = 0;
  234. /// Set the working directory. This will affect all following operations on
  235. /// this file system and may propagate down for nested file systems.
  236. virtual std::error_code setCurrentWorkingDirectory(const Twine &Path) = 0;
  237. /// Get the working directory of this file system.
  238. virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const = 0;
  239. /// Gets real path of \p Path e.g. collapse all . and .. patterns, resolve
  240. /// symlinks. For real file system, this uses `llvm::sys::fs::real_path`.
  241. /// This returns errc::operation_not_permitted if not implemented by subclass.
  242. virtual std::error_code getRealPath(const Twine &Path,
  243. SmallVectorImpl<char> &Output) const;
  244. /// Check whether a file exists. Provided for convenience.
  245. bool exists(const Twine &Path);
  246. /// Is the file mounted on a local filesystem?
  247. virtual std::error_code isLocal(const Twine &Path, bool &Result);
  248. /// Make \a Path an absolute path.
  249. ///
  250. /// Makes \a Path absolute using the current directory if it is not already.
  251. /// An empty \a Path will result in the current directory.
  252. ///
  253. /// /absolute/path => /absolute/path
  254. /// relative/../path => <current-directory>/relative/../path
  255. ///
  256. /// \param Path A path that is modified to be an absolute path.
  257. /// \returns success if \a path has been made absolute, otherwise a
  258. /// platform-specific error_code.
  259. virtual std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const;
  260. };
  261. /// Gets an \p vfs::FileSystem for the 'real' file system, as seen by
  262. /// the operating system.
  263. /// The working directory is linked to the process's working directory.
  264. /// (This is usually thread-hostile).
  265. IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
  266. /// Create an \p vfs::FileSystem for the 'real' file system, as seen by
  267. /// the operating system.
  268. /// It has its own working directory, independent of (but initially equal to)
  269. /// that of the process.
  270. std::unique_ptr<FileSystem> createPhysicalFileSystem();
  271. /// A file system that allows overlaying one \p AbstractFileSystem on top
  272. /// of another.
  273. ///
  274. /// Consists of a stack of >=1 \p FileSystem objects, which are treated as being
  275. /// one merged file system. When there is a directory that exists in more than
  276. /// one file system, the \p OverlayFileSystem contains a directory containing
  277. /// the union of their contents. The attributes (permissions, etc.) of the
  278. /// top-most (most recently added) directory are used. When there is a file
  279. /// that exists in more than one file system, the file in the top-most file
  280. /// system overrides the other(s).
  281. class OverlayFileSystem : public FileSystem {
  282. using FileSystemList = SmallVector<IntrusiveRefCntPtr<FileSystem>, 1>;
  283. /// The stack of file systems, implemented as a list in order of
  284. /// their addition.
  285. FileSystemList FSList;
  286. public:
  287. OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base);
  288. /// Pushes a file system on top of the stack.
  289. void pushOverlay(IntrusiveRefCntPtr<FileSystem> FS);
  290. llvm::ErrorOr<Status> status(const Twine &Path) override;
  291. llvm::ErrorOr<std::unique_ptr<File>>
  292. openFileForRead(const Twine &Path) override;
  293. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
  294. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
  295. std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
  296. std::error_code isLocal(const Twine &Path, bool &Result) override;
  297. std::error_code getRealPath(const Twine &Path,
  298. SmallVectorImpl<char> &Output) const override;
  299. using iterator = FileSystemList::reverse_iterator;
  300. using const_iterator = FileSystemList::const_reverse_iterator;
  301. using reverse_iterator = FileSystemList::iterator;
  302. using const_reverse_iterator = FileSystemList::const_iterator;
  303. /// Get an iterator pointing to the most recently added file system.
  304. iterator overlays_begin() { return FSList.rbegin(); }
  305. const_iterator overlays_begin() const { return FSList.rbegin(); }
  306. /// Get an iterator pointing one-past the least recently added file system.
  307. iterator overlays_end() { return FSList.rend(); }
  308. const_iterator overlays_end() const { return FSList.rend(); }
  309. /// Get an iterator pointing to the least recently added file system.
  310. reverse_iterator overlays_rbegin() { return FSList.begin(); }
  311. const_reverse_iterator overlays_rbegin() const { return FSList.begin(); }
  312. /// Get an iterator pointing one-past the most recently added file system.
  313. reverse_iterator overlays_rend() { return FSList.end(); }
  314. const_reverse_iterator overlays_rend() const { return FSList.end(); }
  315. };
  316. class CaseInsensitiveFileSystem : public FileSystem {
  317. IntrusiveRefCntPtr<FileSystem> Base;
  318. /// Try to find Path by means of case-insensitive lookup. Stores the result in
  319. /// FoundPath on success, or returns an error code otherwise.
  320. std::error_code findCaseInsensitivePath(StringRef Path,
  321. SmallVectorImpl<char> &FoundPath);
  322. /// Attempt to exclude the possibility that File exists in Dir based on
  323. /// previous information.
  324. bool exclude(StringRef Dir, StringRef File);
  325. /// Map from directory to map from lowercase to real-case filename.
  326. llvm::StringMap<llvm::StringMap<std::string>> Maps;
  327. public:
  328. CaseInsensitiveFileSystem(IntrusiveRefCntPtr<FileSystem> Base) : Base(Base) {}
  329. llvm::ErrorOr<Status> status(const Twine &Path) override;
  330. llvm::ErrorOr<std::unique_ptr<File>>
  331. openFileForRead(const Twine &Path) override;
  332. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
  333. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
  334. return Base->getCurrentWorkingDirectory();
  335. }
  336. std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
  337. Maps.clear();
  338. return Base->setCurrentWorkingDirectory(Path);
  339. }
  340. };
  341. /// By default, this delegates all calls to the underlying file system. This
  342. /// is useful when derived file systems want to override some calls and still
  343. /// proxy other calls.
  344. class ProxyFileSystem : public FileSystem {
  345. public:
  346. explicit ProxyFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
  347. : FS(std::move(FS)) {}
  348. llvm::ErrorOr<Status> status(const Twine &Path) override {
  349. return FS->status(Path);
  350. }
  351. llvm::ErrorOr<std::unique_ptr<File>>
  352. openFileForRead(const Twine &Path) override {
  353. return FS->openFileForRead(Path);
  354. }
  355. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
  356. return FS->dir_begin(Dir, EC);
  357. }
  358. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
  359. return FS->getCurrentWorkingDirectory();
  360. }
  361. std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
  362. return FS->setCurrentWorkingDirectory(Path);
  363. }
  364. std::error_code getRealPath(const Twine &Path,
  365. SmallVectorImpl<char> &Output) const override {
  366. return FS->getRealPath(Path, Output);
  367. }
  368. std::error_code isLocal(const Twine &Path, bool &Result) override {
  369. return FS->isLocal(Path, Result);
  370. }
  371. protected:
  372. FileSystem &getUnderlyingFS() { return *FS; }
  373. private:
  374. IntrusiveRefCntPtr<FileSystem> FS;
  375. virtual void anchor();
  376. };
  377. namespace detail {
  378. class InMemoryDirectory;
  379. class InMemoryFile;
  380. class InMemoryNode;
  381. struct NewInMemoryNodeInfo {
  382. llvm::sys::fs::UniqueID DirUID;
  383. StringRef Path;
  384. StringRef Name;
  385. time_t ModificationTime;
  386. std::unique_ptr<llvm::MemoryBuffer> Buffer;
  387. uint32_t User;
  388. uint32_t Group;
  389. llvm::sys::fs::file_type Type;
  390. llvm::sys::fs::perms Perms;
  391. Status makeStatus() const;
  392. };
  393. } // namespace detail
  394. /// An in-memory file system.
  395. class InMemoryFileSystem : public FileSystem {
  396. std::unique_ptr<detail::InMemoryDirectory> Root;
  397. std::string WorkingDirectory;
  398. bool UseNormalizedPaths = true;
  399. using MakeNodeFn = llvm::function_ref<std::unique_ptr<detail::InMemoryNode>(
  400. detail::NewInMemoryNodeInfo)>;
  401. /// Create node with \p MakeNode and add it into this filesystem at \p Path.
  402. bool addFile(const Twine &Path, time_t ModificationTime,
  403. std::unique_ptr<llvm::MemoryBuffer> Buffer,
  404. Optional<uint32_t> User, Optional<uint32_t> Group,
  405. Optional<llvm::sys::fs::file_type> Type,
  406. Optional<llvm::sys::fs::perms> Perms, MakeNodeFn MakeNode);
  407. public:
  408. explicit InMemoryFileSystem(bool UseNormalizedPaths = true);
  409. ~InMemoryFileSystem() override;
  410. /// Add a file containing a buffer or a directory to the VFS with a
  411. /// path. The VFS owns the buffer. If present, User, Group, Type
  412. /// and Perms apply to the newly-created file or directory.
  413. /// \return true if the file or directory was successfully added,
  414. /// false if the file or directory already exists in the file system with
  415. /// different contents.
  416. bool addFile(const Twine &Path, time_t ModificationTime,
  417. std::unique_ptr<llvm::MemoryBuffer> Buffer,
  418. Optional<uint32_t> User = None, Optional<uint32_t> Group = None,
  419. Optional<llvm::sys::fs::file_type> Type = None,
  420. Optional<llvm::sys::fs::perms> Perms = None);
  421. /// Add a hard link to a file.
  422. /// Here hard links are not intended to be fully equivalent to the classical
  423. /// filesystem. Both the hard link and the file share the same buffer and
  424. /// status (and thus have the same UniqueID). Because of this there is no way
  425. /// to distinguish between the link and the file after the link has been
  426. /// added.
  427. ///
  428. /// The To path must be an existing file or a hardlink. The From file must not
  429. /// have been added before. The To Path must not be a directory. The From Node
  430. /// is added as a hard link which points to the resolved file of To Node.
  431. /// \return true if the above condition is satisfied and hardlink was
  432. /// successfully created, false otherwise.
  433. bool addHardLink(const Twine &From, const Twine &To);
  434. /// Add a buffer to the VFS with a path. The VFS does not own the buffer.
  435. /// If present, User, Group, Type and Perms apply to the newly-created file
  436. /// or directory.
  437. /// \return true if the file or directory was successfully added,
  438. /// false if the file or directory already exists in the file system with
  439. /// different contents.
  440. bool addFileNoOwn(const Twine &Path, time_t ModificationTime,
  441. const llvm::MemoryBufferRef &Buffer,
  442. Optional<uint32_t> User = None,
  443. Optional<uint32_t> Group = None,
  444. Optional<llvm::sys::fs::file_type> Type = None,
  445. Optional<llvm::sys::fs::perms> Perms = None);
  446. std::string toString() const;
  447. /// Return true if this file system normalizes . and .. in paths.
  448. bool useNormalizedPaths() const { return UseNormalizedPaths; }
  449. llvm::ErrorOr<Status> status(const Twine &Path) override;
  450. llvm::ErrorOr<std::unique_ptr<File>>
  451. openFileForRead(const Twine &Path) override;
  452. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
  453. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
  454. return WorkingDirectory;
  455. }
  456. /// Canonicalizes \p Path by combining with the current working
  457. /// directory and normalizing the path (e.g. remove dots). If the current
  458. /// working directory is not set, this returns errc::operation_not_permitted.
  459. ///
  460. /// This doesn't resolve symlinks as they are not supported in in-memory file
  461. /// system.
  462. std::error_code getRealPath(const Twine &Path,
  463. SmallVectorImpl<char> &Output) const override;
  464. std::error_code isLocal(const Twine &Path, bool &Result) override;
  465. std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
  466. };
  467. /// Get a globally unique ID for a virtual file or directory.
  468. llvm::sys::fs::UniqueID getNextVirtualUniqueID();
  469. /// Gets a \p FileSystem for a virtual file system described in YAML
  470. /// format.
  471. std::unique_ptr<FileSystem>
  472. getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer,
  473. llvm::SourceMgr::DiagHandlerTy DiagHandler,
  474. StringRef YAMLFilePath, void *DiagContext = nullptr,
  475. IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
  476. struct YAMLVFSEntry {
  477. template <typename T1, typename T2>
  478. YAMLVFSEntry(T1 &&VPath, T2 &&RPath, bool IsDirectory = false)
  479. : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)),
  480. IsDirectory(IsDirectory) {}
  481. std::string VPath;
  482. std::string RPath;
  483. bool IsDirectory = false;
  484. };
  485. class RedirectingFSDirIterImpl;
  486. class RedirectingFileSystemParser;
  487. /// A virtual file system parsed from a YAML file.
  488. ///
  489. /// Currently, this class allows creating virtual files and directories. Virtual
  490. /// files map to existing external files in \c ExternalFS, and virtual
  491. /// directories may either map to existing directories in \c ExternalFS or list
  492. /// their contents in the form of other virtual directories and/or files.
  493. ///
  494. /// The basic structure of the parsed file is:
  495. /// \verbatim
  496. /// {
  497. /// 'version': <version number>,
  498. /// <optional configuration>
  499. /// 'roots': [
  500. /// <directory entries>
  501. /// ]
  502. /// }
  503. /// \endverbatim
  504. ///
  505. /// The roots may be absolute or relative. If relative they will be made
  506. /// absolute against the current working directory.
  507. ///
  508. /// All configuration options are optional.
  509. /// 'case-sensitive': <boolean, default=(true for Posix, false for Windows)>
  510. /// 'use-external-names': <boolean, default=true>
  511. /// 'overlay-relative': <boolean, default=false>
  512. /// 'fallthrough': <boolean, default=true>
  513. ///
  514. /// Virtual directories that list their contents are represented as
  515. /// \verbatim
  516. /// {
  517. /// 'type': 'directory',
  518. /// 'name': <string>,
  519. /// 'contents': [ <file or directory entries> ]
  520. /// }
  521. /// \endverbatim
  522. ///
  523. /// The default attributes for such virtual directories are:
  524. /// \verbatim
  525. /// MTime = now() when created
  526. /// Perms = 0777
  527. /// User = Group = 0
  528. /// Size = 0
  529. /// UniqueID = unspecified unique value
  530. /// \endverbatim
  531. ///
  532. /// When a path prefix matches such a directory, the next component in the path
  533. /// is matched against the entries in the 'contents' array.
  534. ///
  535. /// Re-mapped directories, on the other hand, are represented as
  536. /// /// \verbatim
  537. /// {
  538. /// 'type': 'directory-remap',
  539. /// 'name': <string>,
  540. /// 'use-external-name': <boolean>, # Optional
  541. /// 'external-contents': <path to external directory>
  542. /// }
  543. /// \endverbatim
  544. ///
  545. /// and inherit their attributes from the external directory. When a path
  546. /// prefix matches such an entry, the unmatched components are appended to the
  547. /// 'external-contents' path, and the resulting path is looked up in the
  548. /// external file system instead.
  549. ///
  550. /// Re-mapped files are represented as
  551. /// \verbatim
  552. /// {
  553. /// 'type': 'file',
  554. /// 'name': <string>,
  555. /// 'use-external-name': <boolean>, # Optional
  556. /// 'external-contents': <path to external file>
  557. /// }
  558. /// \endverbatim
  559. ///
  560. /// Their attributes and file contents are determined by looking up the file at
  561. /// their 'external-contents' path in the external file system.
  562. ///
  563. /// For 'file', 'directory' and 'directory-remap' entries the 'name' field may
  564. /// contain multiple path components (e.g. /path/to/file). However, any
  565. /// directory in such a path that contains more than one child must be uniquely
  566. /// represented by a 'directory' entry.
  567. ///
  568. /// When the 'use-external-name' field is set, calls to \a vfs::File::status()
  569. /// give the external (remapped) filesystem name instead of the name the file
  570. /// was accessed by. This is an intentional leak through the \a
  571. /// RedirectingFileSystem abstraction layer. It enables clients to discover
  572. /// (and use) the external file location when communicating with users or tools
  573. /// that don't use the same VFS overlay.
  574. ///
  575. /// FIXME: 'use-external-name' causes behaviour that's inconsistent with how
  576. /// "real" filesystems behave. Maybe there should be a separate channel for
  577. /// this information.
  578. class RedirectingFileSystem : public vfs::FileSystem {
  579. public:
  580. enum EntryKind { EK_Directory, EK_DirectoryRemap, EK_File };
  581. enum NameKind { NK_NotSet, NK_External, NK_Virtual };
  582. /// A single file or directory in the VFS.
  583. class Entry {
  584. EntryKind Kind;
  585. std::string Name;
  586. public:
  587. Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
  588. virtual ~Entry() = default;
  589. StringRef getName() const { return Name; }
  590. EntryKind getKind() const { return Kind; }
  591. };
  592. /// A directory in the vfs with explicitly specified contents.
  593. class DirectoryEntry : public Entry {
  594. std::vector<std::unique_ptr<Entry>> Contents;
  595. Status S;
  596. public:
  597. /// Constructs a directory entry with explicitly specified contents.
  598. DirectoryEntry(StringRef Name, std::vector<std::unique_ptr<Entry>> Contents,
  599. Status S)
  600. : Entry(EK_Directory, Name), Contents(std::move(Contents)),
  601. S(std::move(S)) {}
  602. /// Constructs an empty directory entry.
  603. DirectoryEntry(StringRef Name, Status S)
  604. : Entry(EK_Directory, Name), S(std::move(S)) {}
  605. Status getStatus() { return S; }
  606. void addContent(std::unique_ptr<Entry> Content) {
  607. Contents.push_back(std::move(Content));
  608. }
  609. Entry *getLastContent() const { return Contents.back().get(); }
  610. using iterator = decltype(Contents)::iterator;
  611. iterator contents_begin() { return Contents.begin(); }
  612. iterator contents_end() { return Contents.end(); }
  613. static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
  614. };
  615. /// A file or directory in the vfs that is mapped to a file or directory in
  616. /// the external filesystem.
  617. class RemapEntry : public Entry {
  618. std::string ExternalContentsPath;
  619. NameKind UseName;
  620. protected:
  621. RemapEntry(EntryKind K, StringRef Name, StringRef ExternalContentsPath,
  622. NameKind UseName)
  623. : Entry(K, Name), ExternalContentsPath(ExternalContentsPath),
  624. UseName(UseName) {}
  625. public:
  626. StringRef getExternalContentsPath() const { return ExternalContentsPath; }
  627. /// Whether to use the external path as the name for this file or directory.
  628. bool useExternalName(bool GlobalUseExternalName) const {
  629. return UseName == NK_NotSet ? GlobalUseExternalName
  630. : (UseName == NK_External);
  631. }
  632. NameKind getUseName() const { return UseName; }
  633. static bool classof(const Entry *E) {
  634. switch (E->getKind()) {
  635. case EK_DirectoryRemap:
  636. LLVM_FALLTHROUGH;
  637. case EK_File:
  638. return true;
  639. case EK_Directory:
  640. return false;
  641. }
  642. llvm_unreachable("invalid entry kind");
  643. }
  644. };
  645. /// A directory in the vfs that maps to a directory in the external file
  646. /// system.
  647. class DirectoryRemapEntry : public RemapEntry {
  648. public:
  649. DirectoryRemapEntry(StringRef Name, StringRef ExternalContentsPath,
  650. NameKind UseName)
  651. : RemapEntry(EK_DirectoryRemap, Name, ExternalContentsPath, UseName) {}
  652. static bool classof(const Entry *E) {
  653. return E->getKind() == EK_DirectoryRemap;
  654. }
  655. };
  656. /// A file in the vfs that maps to a file in the external file system.
  657. class FileEntry : public RemapEntry {
  658. public:
  659. FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName)
  660. : RemapEntry(EK_File, Name, ExternalContentsPath, UseName) {}
  661. static bool classof(const Entry *E) { return E->getKind() == EK_File; }
  662. };
  663. /// Represents the result of a path lookup into the RedirectingFileSystem.
  664. struct LookupResult {
  665. /// The entry the looked-up path corresponds to.
  666. Entry *E;
  667. private:
  668. /// When the found Entry is a DirectoryRemapEntry, stores the path in the
  669. /// external file system that the looked-up path in the virtual file system
  670. // corresponds to.
  671. Optional<std::string> ExternalRedirect;
  672. public:
  673. LookupResult(Entry *E, sys::path::const_iterator Start,
  674. sys::path::const_iterator End);
  675. /// If the found Entry maps the the input path to a path in the external
  676. /// file system (i.e. it is a FileEntry or DirectoryRemapEntry), returns
  677. /// that path.
  678. Optional<StringRef> getExternalRedirect() const {
  679. if (isa<DirectoryRemapEntry>(E))
  680. return StringRef(*ExternalRedirect);
  681. if (auto *FE = dyn_cast<FileEntry>(E))
  682. return FE->getExternalContentsPath();
  683. return None;
  684. }
  685. };
  686. private:
  687. friend class RedirectingFSDirIterImpl;
  688. friend class RedirectingFileSystemParser;
  689. bool shouldUseExternalFS() const { return IsFallthrough; }
  690. /// Canonicalize path by removing ".", "..", "./", components. This is
  691. /// a VFS request, do not bother about symlinks in the path components
  692. /// but canonicalize in order to perform the correct entry search.
  693. std::error_code makeCanonical(SmallVectorImpl<char> &Path) const;
  694. /// Whether to fall back to the external file system when an operation fails
  695. /// with the given error code on a path associated with the provided Entry.
  696. bool shouldFallBackToExternalFS(std::error_code EC, Entry *E = nullptr) const;
  697. /// Get the File status, or error, from the underlying external file system.
  698. /// This returns the status with the originally requested name, while looking
  699. /// up the entry using the canonical path.
  700. ErrorOr<Status> getExternalStatus(const Twine &CanonicalPath,
  701. const Twine &OriginalPath) const;
  702. // In a RedirectingFileSystem, keys can be specified in Posix or Windows
  703. // style (or even a mixture of both), so this comparison helper allows
  704. // slashes (representing a root) to match backslashes (and vice versa). Note
  705. // that, other than the root, path components should not contain slashes or
  706. // backslashes.
  707. bool pathComponentMatches(llvm::StringRef lhs, llvm::StringRef rhs) const {
  708. if ((CaseSensitive ? lhs.equals(rhs) : lhs.equals_insensitive(rhs)))
  709. return true;
  710. return (lhs == "/" && rhs == "\\") || (lhs == "\\" && rhs == "/");
  711. }
  712. /// The root(s) of the virtual file system.
  713. std::vector<std::unique_ptr<Entry>> Roots;
  714. /// The current working directory of the file system.
  715. std::string WorkingDirectory;
  716. /// The file system to use for external references.
  717. IntrusiveRefCntPtr<FileSystem> ExternalFS;
  718. /// If IsRelativeOverlay is set, this represents the directory
  719. /// path that should be prefixed to each 'external-contents' entry
  720. /// when reading from YAML files.
  721. std::string ExternalContentsPrefixDir;
  722. /// @name Configuration
  723. /// @{
  724. /// Whether to perform case-sensitive comparisons.
  725. ///
  726. /// Currently, case-insensitive matching only works correctly with ASCII.
  727. bool CaseSensitive = is_style_posix(sys::path::Style::native);
  728. /// IsRelativeOverlay marks whether a ExternalContentsPrefixDir path must
  729. /// be prefixed in every 'external-contents' when reading from YAML files.
  730. bool IsRelativeOverlay = false;
  731. /// Whether to use to use the value of 'external-contents' for the
  732. /// names of files. This global value is overridable on a per-file basis.
  733. bool UseExternalNames = true;
  734. /// Whether to attempt a file lookup in external file system after it wasn't
  735. /// found in VFS.
  736. bool IsFallthrough = true;
  737. /// @}
  738. RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS);
  739. /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly recursing
  740. /// into the contents of \p From if it is a directory. Returns a LookupResult
  741. /// giving the matched entry and, if that entry is a FileEntry or
  742. /// DirectoryRemapEntry, the path it redirects to in the external file system.
  743. ErrorOr<LookupResult> lookupPathImpl(llvm::sys::path::const_iterator Start,
  744. llvm::sys::path::const_iterator End,
  745. Entry *From) const;
  746. /// Get the status for a path with the provided \c LookupResult.
  747. ErrorOr<Status> status(const Twine &CanonicalPath, const Twine &OriginalPath,
  748. const LookupResult &Result);
  749. public:
  750. /// Looks up \p Path in \c Roots and returns a LookupResult giving the
  751. /// matched entry and, if the entry was a FileEntry or DirectoryRemapEntry,
  752. /// the path it redirects to in the external file system.
  753. ErrorOr<LookupResult> lookupPath(StringRef Path) const;
  754. /// Parses \p Buffer, which is expected to be in YAML format and
  755. /// returns a virtual file system representing its contents.
  756. static std::unique_ptr<RedirectingFileSystem>
  757. create(std::unique_ptr<MemoryBuffer> Buffer,
  758. SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
  759. void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
  760. /// Redirect each of the remapped files from first to second.
  761. static std::unique_ptr<RedirectingFileSystem>
  762. create(ArrayRef<std::pair<std::string, std::string>> RemappedFiles,
  763. bool UseExternalNames, FileSystem &ExternalFS);
  764. ErrorOr<Status> status(const Twine &Path) override;
  765. ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
  766. std::error_code getRealPath(const Twine &Path,
  767. SmallVectorImpl<char> &Output) const override;
  768. llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
  769. std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
  770. std::error_code isLocal(const Twine &Path, bool &Result) override;
  771. std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const override;
  772. directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
  773. void setExternalContentsPrefixDir(StringRef PrefixDir);
  774. StringRef getExternalContentsPrefixDir() const;
  775. void setFallthrough(bool Fallthrough);
  776. std::vector<llvm::StringRef> getRoots() const;
  777. void dump(raw_ostream &OS) const;
  778. void dumpEntry(raw_ostream &OS, Entry *E, int NumSpaces = 0) const;
  779. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  780. LLVM_DUMP_METHOD void dump() const;
  781. #endif
  782. };
  783. /// Collect all pairs of <virtual path, real path> entries from the
  784. /// \p YAMLFilePath. This is used by the module dependency collector to forward
  785. /// the entries into the reproducer output VFS YAML file.
  786. void collectVFSFromYAML(
  787. std::unique_ptr<llvm::MemoryBuffer> Buffer,
  788. llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
  789. SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
  790. void *DiagContext = nullptr,
  791. IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
  792. class YAMLVFSWriter {
  793. std::vector<YAMLVFSEntry> Mappings;
  794. Optional<bool> IsCaseSensitive;
  795. Optional<bool> IsOverlayRelative;
  796. Optional<bool> UseExternalNames;
  797. std::string OverlayDir;
  798. void addEntry(StringRef VirtualPath, StringRef RealPath, bool IsDirectory);
  799. public:
  800. YAMLVFSWriter() = default;
  801. void addFileMapping(StringRef VirtualPath, StringRef RealPath);
  802. void addDirectoryMapping(StringRef VirtualPath, StringRef RealPath);
  803. void setCaseSensitivity(bool CaseSensitive) {
  804. IsCaseSensitive = CaseSensitive;
  805. }
  806. void setUseExternalNames(bool UseExtNames) { UseExternalNames = UseExtNames; }
  807. void setOverlayDir(StringRef OverlayDirectory) {
  808. IsOverlayRelative = true;
  809. OverlayDir.assign(OverlayDirectory.str());
  810. }
  811. const std::vector<YAMLVFSEntry> &getMappings() const { return Mappings; }
  812. void write(llvm::raw_ostream &OS);
  813. };
  814. } // namespace vfs
  815. } // namespace llvm
  816. #endif // LLVM_SUPPORT_VIRTUALFILESYSTEM_H
  817. #ifdef __GNUC__
  818. #pragma GCC diagnostic pop
  819. #endif