xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Support/VirtualFileSystem.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
14 #ifndef LLVM_SUPPORT_VIRTUALFILESYSTEM_H
15 #define LLVM_SUPPORT_VIRTUALFILESYSTEM_H
16 
17 #include "llvm/ADT/IntrusiveRefCntPtr.h"
18 #include "llvm/ADT/STLFunctionalExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Chrono.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Errc.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/ErrorOr.h"
26 #include "llvm/Support/ExtensibleRTTI.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include <cassert>
31 #include <cstdint>
32 #include <ctime>
33 #include <memory>
34 #include <optional>
35 #include <string>
36 #include <system_error>
37 #include <utility>
38 #include <vector>
39 
40 namespace llvm {
41 
42 class MemoryBuffer;
43 class MemoryBufferRef;
44 class Twine;
45 
46 namespace vfs {
47 
48 /// The result of a \p status operation.
49 class Status {
50   std::string Name;
51   llvm::sys::fs::UniqueID UID;
52   llvm::sys::TimePoint<> MTime;
53   uint32_t User;
54   uint32_t Group;
55   uint64_t Size;
56   llvm::sys::fs::file_type Type = llvm::sys::fs::file_type::status_error;
57   llvm::sys::fs::perms Perms;
58 
59 public:
60   /// Whether this entity has an external path different from the virtual path,
61   /// and the external path is exposed by leaking it through the abstraction.
62   /// For example, a RedirectingFileSystem will set this for paths where
63   /// UseExternalName is true.
64   ///
65   /// FIXME: Currently the external path is exposed by replacing the virtual
66   /// path in this Status object. Instead, we should leave the path in the
67   /// Status intact (matching the requested virtual path) - see
68   /// FileManager::getFileRef for how we plan to fix this.
69   bool ExposesExternalVFSPath = false;
70 
71   Status() = default;
72   LLVM_ABI Status(const llvm::sys::fs::file_status &Status);
73   LLVM_ABI Status(const Twine &Name, llvm::sys::fs::UniqueID UID,
74                   llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group,
75                   uint64_t Size, llvm::sys::fs::file_type Type,
76                   llvm::sys::fs::perms Perms);
77 
78   /// Get a copy of a Status with a different size.
79   LLVM_ABI static Status copyWithNewSize(const Status &In, uint64_t NewSize);
80   /// Get a copy of a Status with a different name.
81   LLVM_ABI static Status copyWithNewName(const Status &In,
82                                          const Twine &NewName);
83   LLVM_ABI static Status copyWithNewName(const llvm::sys::fs::file_status &In,
84                                          const Twine &NewName);
85 
86   /// Returns the name that should be used for this file or directory.
getName()87   StringRef getName() const { return Name; }
88 
89   /// @name Status interface from llvm::sys::fs
90   /// @{
getType()91   llvm::sys::fs::file_type getType() const { return Type; }
getPermissions()92   llvm::sys::fs::perms getPermissions() const { return Perms; }
getLastModificationTime()93   llvm::sys::TimePoint<> getLastModificationTime() const { return MTime; }
getUniqueID()94   llvm::sys::fs::UniqueID getUniqueID() const { return UID; }
getUser()95   uint32_t getUser() const { return User; }
getGroup()96   uint32_t getGroup() const { return Group; }
getSize()97   uint64_t getSize() const { return Size; }
98   /// @}
99   /// @name Status queries
100   /// These are static queries in llvm::sys::fs.
101   /// @{
102   LLVM_ABI bool equivalent(const Status &Other) const;
103   LLVM_ABI bool isDirectory() const;
104   LLVM_ABI bool isRegularFile() const;
105   LLVM_ABI bool isOther() const;
106   LLVM_ABI bool isSymlink() const;
107   LLVM_ABI bool isStatusKnown() const;
108   LLVM_ABI bool exists() const;
109   /// @}
110 };
111 
112 /// Represents an open file.
113 class LLVM_ABI File {
114 public:
115   /// Destroy the file after closing it (if open).
116   /// Sub-classes should generally call close() inside their destructors.  We
117   /// cannot do that from the base class, since close is virtual.
118   virtual ~File();
119 
120   /// Get the status of the file.
121   virtual llvm::ErrorOr<Status> status() = 0;
122 
123   /// Get the name of the file
getName()124   virtual llvm::ErrorOr<std::string> getName() {
125     if (auto Status = status())
126       return Status->getName().str();
127     else
128       return Status.getError();
129   }
130 
131   /// Get the contents of the file as a \p MemoryBuffer.
132   virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
133   getBuffer(const Twine &Name, int64_t FileSize = -1,
134             bool RequiresNullTerminator = true, bool IsVolatile = false) = 0;
135 
136   /// Closes the file.
137   virtual std::error_code close() = 0;
138 
139   // Get the same file with a different path.
140   static ErrorOr<std::unique_ptr<File>>
141   getWithPath(ErrorOr<std::unique_ptr<File>> Result, const Twine &P);
142 
143 protected:
144   // Set the file's underlying path.
setPath(const Twine & Path)145   virtual void setPath(const Twine &Path) {}
146 };
147 
148 /// A member of a directory, yielded by a directory_iterator.
149 /// Only information available on most platforms is included.
150 class directory_entry {
151   std::string Path;
152   llvm::sys::fs::file_type Type = llvm::sys::fs::file_type::type_unknown;
153 
154 public:
155   directory_entry() = default;
directory_entry(std::string Path,llvm::sys::fs::file_type Type)156   directory_entry(std::string Path, llvm::sys::fs::file_type Type)
157       : Path(std::move(Path)), Type(Type) {}
158 
path()159   llvm::StringRef path() const { return Path; }
type()160   llvm::sys::fs::file_type type() const { return Type; }
161 };
162 
163 namespace detail {
164 
165 /// An interface for virtual file systems to provide an iterator over the
166 /// (non-recursive) contents of a directory.
167 struct LLVM_ABI DirIterImpl {
168   virtual ~DirIterImpl();
169 
170   /// Sets \c CurrentEntry to the next entry in the directory on success,
171   /// to directory_entry() at end,  or returns a system-defined \c error_code.
172   virtual std::error_code increment() = 0;
173 
174   directory_entry CurrentEntry;
175 };
176 
177 } // namespace detail
178 
179 /// An input iterator over the entries in a virtual path, similar to
180 /// llvm::sys::fs::directory_iterator.
181 class directory_iterator {
182   std::shared_ptr<detail::DirIterImpl> Impl; // Input iterator semantics on copy
183 
184 public:
directory_iterator(std::shared_ptr<detail::DirIterImpl> I)185   directory_iterator(std::shared_ptr<detail::DirIterImpl> I)
186       : Impl(std::move(I)) {
187     assert(Impl.get() != nullptr && "requires non-null implementation");
188     if (Impl->CurrentEntry.path().empty())
189       Impl.reset(); // Normalize the end iterator to Impl == nullptr.
190   }
191 
192   /// Construct an 'end' iterator.
193   directory_iterator() = default;
194 
195   /// Equivalent to operator++, with an error code.
increment(std::error_code & EC)196   directory_iterator &increment(std::error_code &EC) {
197     assert(Impl && "attempting to increment past end");
198     EC = Impl->increment();
199     if (Impl->CurrentEntry.path().empty())
200       Impl.reset(); // Normalize the end iterator to Impl == nullptr.
201     return *this;
202   }
203 
204   const directory_entry &operator*() const { return Impl->CurrentEntry; }
205   const directory_entry *operator->() const { return &Impl->CurrentEntry; }
206 
207   bool operator==(const directory_iterator &RHS) const {
208     if (Impl && RHS.Impl)
209       return Impl->CurrentEntry.path() == RHS.Impl->CurrentEntry.path();
210     return !Impl && !RHS.Impl;
211   }
212   bool operator!=(const directory_iterator &RHS) const {
213     return !(*this == RHS);
214   }
215 };
216 
217 class FileSystem;
218 
219 namespace detail {
220 
221 /// Keeps state for the recursive_directory_iterator.
222 struct RecDirIterState {
223   std::vector<directory_iterator> Stack;
224   bool HasNoPushRequest = false;
225 };
226 
227 } // end namespace detail
228 
229 /// An input iterator over the recursive contents of a virtual path,
230 /// similar to llvm::sys::fs::recursive_directory_iterator.
231 class recursive_directory_iterator {
232   FileSystem *FS;
233   std::shared_ptr<detail::RecDirIterState>
234       State; // Input iterator semantics on copy.
235 
236 public:
237   LLVM_ABI recursive_directory_iterator(FileSystem &FS, const Twine &Path,
238                                         std::error_code &EC);
239 
240   /// Construct an 'end' iterator.
241   recursive_directory_iterator() = default;
242 
243   /// Equivalent to operator++, with an error code.
244   LLVM_ABI recursive_directory_iterator &increment(std::error_code &EC);
245 
246   const directory_entry &operator*() const { return *State->Stack.back(); }
247   const directory_entry *operator->() const { return &*State->Stack.back(); }
248 
249   bool operator==(const recursive_directory_iterator &Other) const {
250     return State == Other.State; // identity
251   }
252   bool operator!=(const recursive_directory_iterator &RHS) const {
253     return !(*this == RHS);
254   }
255 
256   /// Gets the current level. Starting path is at level 0.
level()257   int level() const {
258     assert(!State->Stack.empty() &&
259            "Cannot get level without any iteration state");
260     return State->Stack.size() - 1;
261   }
262 
no_push()263   void no_push() { State->HasNoPushRequest = true; }
264 };
265 
266 /// The virtual file system interface.
267 class LLVM_ABI FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem>,
268                             public RTTIExtends<FileSystem, RTTIRoot> {
269 public:
270   static const char ID;
271   virtual ~FileSystem();
272 
273   /// Get the status of the entry at \p Path, if one exists.
274   virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
275 
276   /// Get a \p File object for the text file at \p Path, if one exists.
277   virtual llvm::ErrorOr<std::unique_ptr<File>>
278   openFileForRead(const Twine &Path) = 0;
279 
280   /// Get a \p File object for the binary file at \p Path, if one exists.
281   /// Some non-ascii based file systems perform encoding conversions
282   /// when reading as a text file, and this function should be used if
283   /// a file's bytes should be read as-is. On most filesystems, this
284   /// is the same behaviour as openFileForRead.
285   virtual llvm::ErrorOr<std::unique_ptr<File>>
openFileForReadBinary(const Twine & Path)286   openFileForReadBinary(const Twine &Path) {
287     return openFileForRead(Path);
288   }
289 
290   /// This is a convenience method that opens a file, gets its content and then
291   /// closes the file.
292   /// The IsText parameter is used to distinguish whether the file should be
293   /// opened as a binary or text file.
294   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
295   getBufferForFile(const Twine &Name, int64_t FileSize = -1,
296                    bool RequiresNullTerminator = true, bool IsVolatile = false,
297                    bool IsText = true);
298 
299   /// Get a directory_iterator for \p Dir.
300   /// \note The 'end' iterator is directory_iterator().
301   virtual directory_iterator dir_begin(const Twine &Dir,
302                                        std::error_code &EC) = 0;
303 
304   /// Set the working directory. This will affect all following operations on
305   /// this file system and may propagate down for nested file systems.
306   virtual std::error_code setCurrentWorkingDirectory(const Twine &Path) = 0;
307 
308   /// Get the working directory of this file system.
309   virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const = 0;
310 
311   /// Gets real path of \p Path e.g. collapse all . and .. patterns, resolve
312   /// symlinks. For real file system, this uses `llvm::sys::fs::real_path`.
313   /// This returns errc::operation_not_permitted if not implemented by subclass.
314   virtual std::error_code getRealPath(const Twine &Path,
315                                       SmallVectorImpl<char> &Output);
316 
317   /// Check whether \p Path exists. By default this uses \c status(), but
318   /// filesystems may provide a more efficient implementation if available.
319   virtual bool exists(const Twine &Path);
320 
321   /// Is the file mounted on a local filesystem?
322   virtual std::error_code isLocal(const Twine &Path, bool &Result);
323 
324   /// Make \a Path an absolute path.
325   ///
326   /// Makes \a Path absolute using the current directory if it is not already.
327   /// An empty \a Path will result in the current directory.
328   ///
329   /// /absolute/path   => /absolute/path
330   /// relative/../path => <current-directory>/relative/../path
331   ///
332   /// \param Path A path that is modified to be an absolute path.
333   /// \returns success if \a path has been made absolute, otherwise a
334   ///          platform-specific error_code.
335   virtual std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const;
336 
337   /// \returns true if \p A and \p B represent the same file, or an error or
338   /// false if they do not.
339   llvm::ErrorOr<bool> equivalent(const Twine &A, const Twine &B);
340 
341   enum class PrintType { Summary, Contents, RecursiveContents };
342   void print(raw_ostream &OS, PrintType Type = PrintType::Contents,
343              unsigned IndentLevel = 0) const {
344     printImpl(OS, Type, IndentLevel);
345   }
346 
347   using VisitCallbackTy = llvm::function_ref<void(FileSystem &)>;
visitChildFileSystems(VisitCallbackTy Callback)348   virtual void visitChildFileSystems(VisitCallbackTy Callback) {}
visit(VisitCallbackTy Callback)349   void visit(VisitCallbackTy Callback) {
350     Callback(*this);
351     visitChildFileSystems(Callback);
352   }
353 
354 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
355   LLVM_DUMP_METHOD void dump() const;
356 #endif
357 
358 protected:
printImpl(raw_ostream & OS,PrintType Type,unsigned IndentLevel)359   virtual void printImpl(raw_ostream &OS, PrintType Type,
360                          unsigned IndentLevel) const {
361     printIndent(OS, IndentLevel);
362     OS << "FileSystem\n";
363   }
364 
printIndent(raw_ostream & OS,unsigned IndentLevel)365   void printIndent(raw_ostream &OS, unsigned IndentLevel) const {
366     for (unsigned i = 0; i < IndentLevel; ++i)
367       OS << "  ";
368   }
369 };
370 
371 /// Gets an \p vfs::FileSystem for the 'real' file system, as seen by
372 /// the operating system.
373 /// The working directory is linked to the process's working directory.
374 /// (This is usually thread-hostile).
375 LLVM_ABI IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
376 
377 /// Create an \p vfs::FileSystem for the 'real' file system, as seen by
378 /// the operating system.
379 /// It has its own working directory, independent of (but initially equal to)
380 /// that of the process.
381 LLVM_ABI std::unique_ptr<FileSystem> createPhysicalFileSystem();
382 
383 /// A file system that allows overlaying one \p AbstractFileSystem on top
384 /// of another.
385 ///
386 /// Consists of a stack of >=1 \p FileSystem objects, which are treated as being
387 /// one merged file system. When there is a directory that exists in more than
388 /// one file system, the \p OverlayFileSystem contains a directory containing
389 /// the union of their contents.  The attributes (permissions, etc.) of the
390 /// top-most (most recently added) directory are used.  When there is a file
391 /// that exists in more than one file system, the file in the top-most file
392 /// system overrides the other(s).
393 class LLVM_ABI OverlayFileSystem
394     : public RTTIExtends<OverlayFileSystem, FileSystem> {
395   using FileSystemList = SmallVector<IntrusiveRefCntPtr<FileSystem>, 1>;
396 
397   /// The stack of file systems, implemented as a list in order of
398   /// their addition.
399   FileSystemList FSList;
400 
401 public:
402   static const char ID;
403   OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base);
404 
405   /// Pushes a file system on top of the stack.
406   void pushOverlay(IntrusiveRefCntPtr<FileSystem> FS);
407 
408   llvm::ErrorOr<Status> status(const Twine &Path) override;
409   bool exists(const Twine &Path) override;
410   llvm::ErrorOr<std::unique_ptr<File>>
411   openFileForRead(const Twine &Path) override;
412   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
413   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
414   std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
415   std::error_code isLocal(const Twine &Path, bool &Result) override;
416   std::error_code getRealPath(const Twine &Path,
417                               SmallVectorImpl<char> &Output) override;
418 
419   using iterator = FileSystemList::reverse_iterator;
420   using const_iterator = FileSystemList::const_reverse_iterator;
421   using reverse_iterator = FileSystemList::iterator;
422   using const_reverse_iterator = FileSystemList::const_iterator;
423   using range = iterator_range<iterator>;
424   using const_range = iterator_range<const_iterator>;
425 
426   /// Get an iterator pointing to the most recently added file system.
overlays_begin()427   iterator overlays_begin() { return FSList.rbegin(); }
overlays_begin()428   const_iterator overlays_begin() const { return FSList.rbegin(); }
429 
430   /// Get an iterator pointing one-past the least recently added file system.
overlays_end()431   iterator overlays_end() { return FSList.rend(); }
overlays_end()432   const_iterator overlays_end() const { return FSList.rend(); }
433 
434   /// Get an iterator pointing to the least recently added file system.
overlays_rbegin()435   reverse_iterator overlays_rbegin() { return FSList.begin(); }
overlays_rbegin()436   const_reverse_iterator overlays_rbegin() const { return FSList.begin(); }
437 
438   /// Get an iterator pointing one-past the most recently added file system.
overlays_rend()439   reverse_iterator overlays_rend() { return FSList.end(); }
overlays_rend()440   const_reverse_iterator overlays_rend() const { return FSList.end(); }
441 
overlays_range()442   range overlays_range() { return llvm::reverse(FSList); }
overlays_range()443   const_range overlays_range() const { return llvm::reverse(FSList); }
444 
445 protected:
446   void printImpl(raw_ostream &OS, PrintType Type,
447                  unsigned IndentLevel) const override;
448   void visitChildFileSystems(VisitCallbackTy Callback) override;
449 };
450 
451 /// By default, this delegates all calls to the underlying file system. This
452 /// is useful when derived file systems want to override some calls and still
453 /// proxy other calls.
454 class LLVM_ABI ProxyFileSystem
455     : public RTTIExtends<ProxyFileSystem, FileSystem> {
456 public:
457   static const char ID;
ProxyFileSystem(IntrusiveRefCntPtr<FileSystem> FS)458   explicit ProxyFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
459       : FS(std::move(FS)) {}
460 
status(const Twine & Path)461   llvm::ErrorOr<Status> status(const Twine &Path) override {
462     return FS->status(Path);
463   }
exists(const Twine & Path)464   bool exists(const Twine &Path) override { return FS->exists(Path); }
465   llvm::ErrorOr<std::unique_ptr<File>>
openFileForRead(const Twine & Path)466   openFileForRead(const Twine &Path) override {
467     return FS->openFileForRead(Path);
468   }
dir_begin(const Twine & Dir,std::error_code & EC)469   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
470     return FS->dir_begin(Dir, EC);
471   }
getCurrentWorkingDirectory()472   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
473     return FS->getCurrentWorkingDirectory();
474   }
setCurrentWorkingDirectory(const Twine & Path)475   std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
476     return FS->setCurrentWorkingDirectory(Path);
477   }
getRealPath(const Twine & Path,SmallVectorImpl<char> & Output)478   std::error_code getRealPath(const Twine &Path,
479                               SmallVectorImpl<char> &Output) override {
480     return FS->getRealPath(Path, Output);
481   }
isLocal(const Twine & Path,bool & Result)482   std::error_code isLocal(const Twine &Path, bool &Result) override {
483     return FS->isLocal(Path, Result);
484   }
485 
486 protected:
getUnderlyingFS()487   FileSystem &getUnderlyingFS() const { return *FS; }
visitChildFileSystems(VisitCallbackTy Callback)488   void visitChildFileSystems(VisitCallbackTy Callback) override {
489     if (FS) {
490       Callback(*FS);
491       FS->visitChildFileSystems(Callback);
492     }
493   }
494 
495 private:
496   IntrusiveRefCntPtr<FileSystem> FS;
497 
498   virtual void anchor() override;
499 };
500 
501 namespace detail {
502 
503 class InMemoryDirectory;
504 class InMemoryNode;
505 
506 struct NewInMemoryNodeInfo {
507   llvm::sys::fs::UniqueID DirUID;
508   StringRef Path;
509   StringRef Name;
510   time_t ModificationTime;
511   std::unique_ptr<llvm::MemoryBuffer> Buffer;
512   uint32_t User;
513   uint32_t Group;
514   llvm::sys::fs::file_type Type;
515   llvm::sys::fs::perms Perms;
516 
517   LLVM_ABI Status makeStatus() const;
518 };
519 
520 class NamedNodeOrError {
521   ErrorOr<std::pair<llvm::SmallString<128>, const detail::InMemoryNode *>>
522       Value;
523 
524 public:
NamedNodeOrError(llvm::SmallString<128> Name,const detail::InMemoryNode * Node)525   NamedNodeOrError(llvm::SmallString<128> Name,
526                    const detail::InMemoryNode *Node)
527       : Value(std::make_pair(Name, Node)) {}
NamedNodeOrError(std::error_code EC)528   NamedNodeOrError(std::error_code EC) : Value(EC) {}
NamedNodeOrError(llvm::errc EC)529   NamedNodeOrError(llvm::errc EC) : Value(EC) {}
530 
getName()531   StringRef getName() const { return (*Value).first; }
532   explicit operator bool() const { return static_cast<bool>(Value); }
error_code()533   operator std::error_code() const { return Value.getError(); }
getError()534   std::error_code getError() const { return Value.getError(); }
535   const detail::InMemoryNode *operator*() const { return (*Value).second; }
536 };
537 
538 } // namespace detail
539 
540 /// An in-memory file system.
541 class LLVM_ABI InMemoryFileSystem
542     : public RTTIExtends<InMemoryFileSystem, FileSystem> {
543   std::unique_ptr<detail::InMemoryDirectory> Root;
544   std::string WorkingDirectory;
545   bool UseNormalizedPaths = true;
546 
547 public:
548   static const char ID;
549 
550 private:
551   using MakeNodeFn = llvm::function_ref<std::unique_ptr<detail::InMemoryNode>(
552       detail::NewInMemoryNodeInfo)>;
553 
554   /// Create node with \p MakeNode and add it into this filesystem at \p Path.
555   bool addFile(const Twine &Path, time_t ModificationTime,
556                std::unique_ptr<llvm::MemoryBuffer> Buffer,
557                std::optional<uint32_t> User, std::optional<uint32_t> Group,
558                std::optional<llvm::sys::fs::file_type> Type,
559                std::optional<llvm::sys::fs::perms> Perms, MakeNodeFn MakeNode);
560 
561   /// Looks up the in-memory node for the path \p P.
562   /// If \p FollowFinalSymlink is true, the returned node is guaranteed to
563   /// not be a symlink and its path may differ from \p P.
564   detail::NamedNodeOrError lookupNode(const Twine &P, bool FollowFinalSymlink,
565                                       size_t SymlinkDepth = 0) const;
566 
567   class DirIterator;
568 
569 public:
570   explicit InMemoryFileSystem(bool UseNormalizedPaths = true);
571   ~InMemoryFileSystem() override;
572 
573   /// Add a file containing a buffer or a directory to the VFS with a
574   /// path. The VFS owns the buffer.  If present, User, Group, Type
575   /// and Perms apply to the newly-created file or directory.
576   /// \return true if the file or directory was successfully added,
577   /// false if the file or directory already exists in the file system with
578   /// different contents.
579   bool addFile(const Twine &Path, time_t ModificationTime,
580                std::unique_ptr<llvm::MemoryBuffer> Buffer,
581                std::optional<uint32_t> User = std::nullopt,
582                std::optional<uint32_t> Group = std::nullopt,
583                std::optional<llvm::sys::fs::file_type> Type = std::nullopt,
584                std::optional<llvm::sys::fs::perms> Perms = std::nullopt);
585 
586   /// Add a hard link to a file.
587   ///
588   /// Here hard links are not intended to be fully equivalent to the classical
589   /// filesystem. Both the hard link and the file share the same buffer and
590   /// status (and thus have the same UniqueID). Because of this there is no way
591   /// to distinguish between the link and the file after the link has been
592   /// added.
593   ///
594   /// The \p Target path must be an existing file or a hardlink. The
595   /// \p NewLink file must not have been added before. The \p Target
596   /// path must not be a directory. The \p NewLink node is added as a hard
597   /// link which points to the resolved file of \p Target node.
598   /// \return true if the above condition is satisfied and hardlink was
599   /// successfully created, false otherwise.
600   bool addHardLink(const Twine &NewLink, const Twine &Target);
601 
602   /// Arbitrary max depth to search through symlinks. We can get into problems
603   /// if a link links to a link that links back to the link, for example.
604   static constexpr size_t MaxSymlinkDepth = 16;
605 
606   /// Add a symbolic link. Unlike a HardLink, because \p Target doesn't need
607   /// to refer to a file (or refer to anything, as it happens). Also, an
608   /// in-memory directory for \p Target isn't automatically created.
609   bool
610   addSymbolicLink(const Twine &NewLink, const Twine &Target,
611                   time_t ModificationTime,
612                   std::optional<uint32_t> User = std::nullopt,
613                   std::optional<uint32_t> Group = std::nullopt,
614                   std::optional<llvm::sys::fs::perms> Perms = std::nullopt);
615 
616   /// Add a buffer to the VFS with a path. The VFS does not own the buffer.
617   /// If present, User, Group, Type and Perms apply to the newly-created file
618   /// or directory.
619   /// \return true if the file or directory was successfully added,
620   /// false if the file or directory already exists in the file system with
621   /// different contents.
622   bool addFileNoOwn(const Twine &Path, time_t ModificationTime,
623                     const llvm::MemoryBufferRef &Buffer,
624                     std::optional<uint32_t> User = std::nullopt,
625                     std::optional<uint32_t> Group = std::nullopt,
626                     std::optional<llvm::sys::fs::file_type> Type = std::nullopt,
627                     std::optional<llvm::sys::fs::perms> Perms = std::nullopt);
628 
629   std::string toString() const;
630 
631   /// Return true if this file system normalizes . and .. in paths.
useNormalizedPaths()632   bool useNormalizedPaths() const { return UseNormalizedPaths; }
633 
634   llvm::ErrorOr<Status> status(const Twine &Path) override;
635   llvm::ErrorOr<std::unique_ptr<File>>
636   openFileForRead(const Twine &Path) override;
637   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
638 
getCurrentWorkingDirectory()639   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
640     return WorkingDirectory;
641   }
642   /// Canonicalizes \p Path by combining with the current working
643   /// directory and normalizing the path (e.g. remove dots). If the current
644   /// working directory is not set, this returns errc::operation_not_permitted.
645   ///
646   /// This doesn't resolve symlinks as they are not supported in in-memory file
647   /// system.
648   std::error_code getRealPath(const Twine &Path,
649                               SmallVectorImpl<char> &Output) override;
650   std::error_code isLocal(const Twine &Path, bool &Result) override;
651   std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
652 
653 protected:
654   void printImpl(raw_ostream &OS, PrintType Type,
655                  unsigned IndentLevel) const override;
656 };
657 
658 /// Get a globally unique ID for a virtual file or directory.
659 LLVM_ABI llvm::sys::fs::UniqueID getNextVirtualUniqueID();
660 
661 /// Gets a \p FileSystem for a virtual file system described in YAML
662 /// format.
663 LLVM_ABI std::unique_ptr<FileSystem>
664 getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer,
665                llvm::SourceMgr::DiagHandlerTy DiagHandler,
666                StringRef YAMLFilePath, void *DiagContext = nullptr,
667                IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
668 
669 struct YAMLVFSEntry {
670   template <typename T1, typename T2>
671   YAMLVFSEntry(T1 &&VPath, T2 &&RPath, bool IsDirectory = false)
VPathYAMLVFSEntry672       : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)),
673         IsDirectory(IsDirectory) {}
674   std::string VPath;
675   std::string RPath;
676   bool IsDirectory = false;
677 };
678 
679 class RedirectingFSDirIterImpl;
680 class RedirectingFileSystemParser;
681 
682 /// A virtual file system parsed from a YAML file.
683 ///
684 /// Currently, this class allows creating virtual files and directories. Virtual
685 /// files map to existing external files in \c ExternalFS, and virtual
686 /// directories may either map to existing directories in \c ExternalFS or list
687 /// their contents in the form of other virtual directories and/or files.
688 ///
689 /// The basic structure of the parsed file is:
690 /// \verbatim
691 /// {
692 ///   'version': <version number>,
693 ///   <optional configuration>
694 ///   'roots': [
695 ///              <directory entries>
696 ///            ]
697 /// }
698 /// \endverbatim
699 /// The roots may be absolute or relative. If relative they will be made
700 /// absolute against either current working directory or the directory where
701 /// the Overlay YAML file is located, depending on the 'root-relative'
702 /// configuration.
703 ///
704 /// All configuration options are optional.
705 ///   'case-sensitive': <boolean, default=(true for Posix, false for Windows)>
706 ///   'use-external-names': <boolean, default=true>
707 ///   'root-relative': <string, one of 'cwd' or 'overlay-dir', default='cwd'>
708 ///   'overlay-relative': <boolean, default=false>
709 ///   'fallthrough': <boolean, default=true, deprecated - use 'redirecting-with'
710 ///                   instead>
711 ///   'redirecting-with': <string, one of 'fallthrough', 'fallback', or
712 ///                        'redirect-only', default='fallthrough'>
713 ///
714 /// To clarify, 'root-relative' option will prepend the current working
715 /// directory, or the overlay directory to the 'roots->name' field only if
716 /// 'roots->name' is a relative path. On the other hand, when 'overlay-relative'
717 /// is set to 'true', external paths will always be prepended with the overlay
718 /// directory, even if external paths are not relative paths. The
719 /// 'root-relative' option has no interaction with the 'overlay-relative'
720 /// option.
721 ///
722 /// Virtual directories that list their contents are represented as
723 /// \verbatim
724 /// {
725 ///   'type': 'directory',
726 ///   'name': <string>,
727 ///   'contents': [ <file or directory entries> ]
728 /// }
729 /// \endverbatim
730 /// The default attributes for such virtual directories are:
731 /// \verbatim
732 /// MTime = now() when created
733 /// Perms = 0777
734 /// User = Group = 0
735 /// Size = 0
736 /// UniqueID = unspecified unique value
737 /// \endverbatim
738 /// When a path prefix matches such a directory, the next component in the path
739 /// is matched against the entries in the 'contents' array.
740 ///
741 /// Re-mapped directories, on the other hand, are represented as
742 /// /// \verbatim
743 /// {
744 ///   'type': 'directory-remap',
745 ///   'name': <string>,
746 ///   'use-external-name': <boolean>, # Optional
747 ///   'external-contents': <path to external directory>
748 /// }
749 /// \endverbatim
750 /// and inherit their attributes from the external directory. When a path
751 /// prefix matches such an entry, the unmatched components are appended to the
752 /// 'external-contents' path, and the resulting path is looked up in the
753 /// external file system instead.
754 ///
755 /// Re-mapped files are represented as
756 /// \verbatim
757 /// {
758 ///   'type': 'file',
759 ///   'name': <string>,
760 ///   'use-external-name': <boolean>, # Optional
761 ///   'external-contents': <path to external file>
762 /// }
763 /// \endverbatim
764 /// Their attributes and file contents are determined by looking up the file at
765 /// their 'external-contents' path in the external file system.
766 ///
767 /// For 'file', 'directory' and 'directory-remap' entries the 'name' field may
768 /// contain multiple path components (e.g. /path/to/file). However, any
769 /// directory in such a path that contains more than one child must be uniquely
770 /// represented by a 'directory' entry.
771 ///
772 /// When the 'use-external-name' field is set, calls to \a vfs::File::status()
773 /// give the external (remapped) filesystem name instead of the name the file
774 /// was accessed by. This is an intentional leak through the \a
775 /// RedirectingFileSystem abstraction layer. It enables clients to discover
776 /// (and use) the external file location when communicating with users or tools
777 /// that don't use the same VFS overlay.
778 ///
779 /// FIXME: 'use-external-name' causes behaviour that's inconsistent with how
780 /// "real" filesystems behave. Maybe there should be a separate channel for
781 /// this information.
782 class LLVM_ABI RedirectingFileSystem
783     : public RTTIExtends<RedirectingFileSystem, vfs::FileSystem> {
784 public:
785   static const char ID;
786   enum EntryKind { EK_Directory, EK_DirectoryRemap, EK_File };
787   enum NameKind { NK_NotSet, NK_External, NK_Virtual };
788 
789   /// The type of redirection to perform.
790   enum class RedirectKind {
791     /// Lookup the redirected path first (ie. the one specified in
792     /// 'external-contents') and if that fails "fallthrough" to a lookup of the
793     /// originally provided path.
794     Fallthrough,
795     /// Lookup the provided path first and if that fails, "fallback" to a
796     /// lookup of the redirected path.
797     Fallback,
798     /// Only lookup the redirected path, do not lookup the originally provided
799     /// path.
800     RedirectOnly
801   };
802 
803   /// The type of relative path used by Roots.
804   enum class RootRelativeKind {
805     /// The roots are relative to the current working directory.
806     CWD,
807     /// The roots are relative to the directory where the Overlay YAML file
808     // locates.
809     OverlayDir
810   };
811 
812   /// A single file or directory in the VFS.
813   class Entry {
814     EntryKind Kind;
815     std::string Name;
816 
817   public:
Entry(EntryKind K,StringRef Name)818     Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
819     virtual ~Entry() = default;
820 
getName()821     StringRef getName() const { return Name; }
getKind()822     EntryKind getKind() const { return Kind; }
823   };
824 
825   /// A directory in the vfs with explicitly specified contents.
826   class DirectoryEntry : public Entry {
827     std::vector<std::unique_ptr<Entry>> Contents;
828     Status S;
829 
830   public:
831     /// Constructs a directory entry with explicitly specified contents.
DirectoryEntry(StringRef Name,std::vector<std::unique_ptr<Entry>> Contents,Status S)832     DirectoryEntry(StringRef Name, std::vector<std::unique_ptr<Entry>> Contents,
833                    Status S)
834         : Entry(EK_Directory, Name), Contents(std::move(Contents)),
835           S(std::move(S)) {}
836 
837     /// Constructs an empty directory entry.
DirectoryEntry(StringRef Name,Status S)838     DirectoryEntry(StringRef Name, Status S)
839         : Entry(EK_Directory, Name), S(std::move(S)) {}
840 
getStatus()841     Status getStatus() { return S; }
842 
addContent(std::unique_ptr<Entry> Content)843     void addContent(std::unique_ptr<Entry> Content) {
844       Contents.push_back(std::move(Content));
845     }
846 
getLastContent()847     Entry *getLastContent() const { return Contents.back().get(); }
848 
849     using iterator = decltype(Contents)::iterator;
850 
contents_begin()851     iterator contents_begin() { return Contents.begin(); }
contents_end()852     iterator contents_end() { return Contents.end(); }
853 
classof(const Entry * E)854     static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
855   };
856 
857   /// A file or directory in the vfs that is mapped to a file or directory in
858   /// the external filesystem.
859   class RemapEntry : public Entry {
860     std::string ExternalContentsPath;
861     NameKind UseName;
862 
863   protected:
RemapEntry(EntryKind K,StringRef Name,StringRef ExternalContentsPath,NameKind UseName)864     RemapEntry(EntryKind K, StringRef Name, StringRef ExternalContentsPath,
865                NameKind UseName)
866         : Entry(K, Name), ExternalContentsPath(ExternalContentsPath),
867           UseName(UseName) {}
868 
869   public:
getExternalContentsPath()870     StringRef getExternalContentsPath() const { return ExternalContentsPath; }
871 
872     /// Whether to use the external path as the name for this file or directory.
useExternalName(bool GlobalUseExternalName)873     bool useExternalName(bool GlobalUseExternalName) const {
874       return UseName == NK_NotSet ? GlobalUseExternalName
875                                   : (UseName == NK_External);
876     }
877 
getUseName()878     NameKind getUseName() const { return UseName; }
879 
classof(const Entry * E)880     static bool classof(const Entry *E) {
881       switch (E->getKind()) {
882       case EK_DirectoryRemap:
883         [[fallthrough]];
884       case EK_File:
885         return true;
886       case EK_Directory:
887         return false;
888       }
889       llvm_unreachable("invalid entry kind");
890     }
891   };
892 
893   /// A directory in the vfs that maps to a directory in the external file
894   /// system.
895   class DirectoryRemapEntry : public RemapEntry {
896   public:
DirectoryRemapEntry(StringRef Name,StringRef ExternalContentsPath,NameKind UseName)897     DirectoryRemapEntry(StringRef Name, StringRef ExternalContentsPath,
898                         NameKind UseName)
899         : RemapEntry(EK_DirectoryRemap, Name, ExternalContentsPath, UseName) {}
900 
classof(const Entry * E)901     static bool classof(const Entry *E) {
902       return E->getKind() == EK_DirectoryRemap;
903     }
904   };
905 
906   /// A file in the vfs that maps to a file in the external file system.
907   class FileEntry : public RemapEntry {
908   public:
FileEntry(StringRef Name,StringRef ExternalContentsPath,NameKind UseName)909     FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName)
910         : RemapEntry(EK_File, Name, ExternalContentsPath, UseName) {}
911 
classof(const Entry * E)912     static bool classof(const Entry *E) { return E->getKind() == EK_File; }
913   };
914 
915   /// Represents the result of a path lookup into the RedirectingFileSystem.
916   struct LookupResult {
917     /// Chain of parent directory entries for \c E.
918     llvm::SmallVector<Entry *, 32> Parents;
919 
920     /// The entry the looked-up path corresponds to.
921     Entry *E;
922 
923   private:
924     /// When the found Entry is a DirectoryRemapEntry, stores the path in the
925     /// external file system that the looked-up path in the virtual file system
926     //  corresponds to.
927     std::optional<std::string> ExternalRedirect;
928 
929   public:
930     LLVM_ABI LookupResult(Entry *E, sys::path::const_iterator Start,
931                           sys::path::const_iterator End);
932 
933     /// If the found Entry maps the input path to a path in the external
934     /// file system (i.e. it is a FileEntry or DirectoryRemapEntry), returns
935     /// that path.
getExternalRedirectLookupResult936     std::optional<StringRef> getExternalRedirect() const {
937       if (isa<DirectoryRemapEntry>(E))
938         return StringRef(*ExternalRedirect);
939       if (auto *FE = dyn_cast<FileEntry>(E))
940         return FE->getExternalContentsPath();
941       return std::nullopt;
942     }
943 
944     /// Get the (canonical) path of the found entry. This uses the as-written
945     /// path components from the VFS specification.
946     LLVM_ABI void getPath(llvm::SmallVectorImpl<char> &Path) const;
947   };
948 
949 private:
950   friend class RedirectingFSDirIterImpl;
951   friend class RedirectingFileSystemParser;
952 
953   /// Canonicalize path by removing ".", "..", "./", components. This is
954   /// a VFS request, do not bother about symlinks in the path components
955   /// but canonicalize in order to perform the correct entry search.
956   std::error_code makeCanonicalForLookup(SmallVectorImpl<char> &Path) const;
957 
958   /// Get the File status, or error, from the underlying external file system.
959   /// This returns the status with the originally requested name, while looking
960   /// up the entry using a potentially different path.
961   ErrorOr<Status> getExternalStatus(const Twine &LookupPath,
962                                     const Twine &OriginalPath) const;
963 
964   /// Make \a Path an absolute path.
965   ///
966   /// Makes \a Path absolute using the \a WorkingDir if it is not already.
967   ///
968   /// /absolute/path   => /absolute/path
969   /// relative/../path => <WorkingDir>/relative/../path
970   ///
971   /// \param WorkingDir  A path that will be used as the base Dir if \a Path
972   ///                    is not already absolute.
973   /// \param Path A path that is modified to be an absolute path.
974   /// \returns success if \a path has been made absolute, otherwise a
975   ///          platform-specific error_code.
976   std::error_code makeAbsolute(StringRef WorkingDir,
977                                SmallVectorImpl<char> &Path) const;
978 
979   // In a RedirectingFileSystem, keys can be specified in Posix or Windows
980   // style (or even a mixture of both), so this comparison helper allows
981   // slashes (representing a root) to match backslashes (and vice versa).  Note
982   // that, other than the root, path components should not contain slashes or
983   // backslashes.
pathComponentMatches(llvm::StringRef lhs,llvm::StringRef rhs)984   bool pathComponentMatches(llvm::StringRef lhs, llvm::StringRef rhs) const {
985     if ((CaseSensitive ? lhs == rhs : lhs.equals_insensitive(rhs)))
986       return true;
987     return (lhs == "/" && rhs == "\\") || (lhs == "\\" && rhs == "/");
988   }
989 
990   /// The root(s) of the virtual file system.
991   std::vector<std::unique_ptr<Entry>> Roots;
992 
993   /// The current working directory of the file system.
994   std::string WorkingDirectory;
995 
996   /// The file system to use for external references.
997   IntrusiveRefCntPtr<FileSystem> ExternalFS;
998 
999   /// This represents the directory path that the YAML file is located.
1000   /// This will be prefixed to each 'external-contents' if IsRelativeOverlay
1001   /// is set. This will also be prefixed to each 'roots->name' if RootRelative
1002   /// is set to RootRelativeKind::OverlayDir and the path is relative.
1003   std::string OverlayFileDir;
1004 
1005   /// @name Configuration
1006   /// @{
1007 
1008   /// Whether to perform case-sensitive comparisons.
1009   ///
1010   /// Currently, case-insensitive matching only works correctly with ASCII.
1011   bool CaseSensitive = is_style_posix(sys::path::Style::native);
1012 
1013   /// IsRelativeOverlay marks whether a OverlayFileDir path must
1014   /// be prefixed in every 'external-contents' when reading from YAML files.
1015   bool IsRelativeOverlay = false;
1016 
1017   /// Whether to use to use the value of 'external-contents' for the
1018   /// names of files.  This global value is overridable on a per-file basis.
1019   bool UseExternalNames = true;
1020 
1021   /// True if this FS has redirected a lookup. This does not include
1022   /// fallthrough.
1023   mutable bool HasBeenUsed = false;
1024 
1025   /// Used to enable or disable updating `HasBeenUsed`.
1026   bool UsageTrackingActive = false;
1027 
1028   /// Determines the lookups to perform, as well as their order. See
1029   /// \c RedirectKind for details.
1030   RedirectKind Redirection = RedirectKind::Fallthrough;
1031 
1032   /// Determine the prefix directory if the roots are relative paths. See
1033   /// \c RootRelativeKind for details.
1034   RootRelativeKind RootRelative = RootRelativeKind::CWD;
1035   /// @}
1036 
1037   RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS);
1038 
1039   // Explicitly non-copyable.
1040   RedirectingFileSystem(RedirectingFileSystem const &) = delete;
1041   RedirectingFileSystem &operator=(RedirectingFileSystem const &) = delete;
1042 
1043   /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly recursing
1044   /// into the contents of \p From if it is a directory. Returns a LookupResult
1045   /// giving the matched entry and, if that entry is a FileEntry or
1046   /// DirectoryRemapEntry, the path it redirects to in the external file system.
1047   ErrorOr<LookupResult>
1048   lookupPathImpl(llvm::sys::path::const_iterator Start,
1049                  llvm::sys::path::const_iterator End, Entry *From,
1050                  llvm::SmallVectorImpl<Entry *> &Entries) const;
1051 
1052   /// Get the status for a path with the provided \c LookupResult.
1053   ErrorOr<Status> status(const Twine &LookupPath, const Twine &OriginalPath,
1054                          const LookupResult &Result);
1055 
1056 public:
1057   /// Looks up \p Path in \c Roots and returns a LookupResult giving the
1058   /// matched entry and, if the entry was a FileEntry or DirectoryRemapEntry,
1059   /// the path it redirects to in the external file system.
1060   ErrorOr<LookupResult> lookupPath(StringRef Path) const;
1061 
1062   /// Parses \p Buffer, which is expected to be in YAML format and
1063   /// returns a virtual file system representing its contents.
1064   static std::unique_ptr<RedirectingFileSystem>
1065   create(std::unique_ptr<MemoryBuffer> Buffer,
1066          SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
1067          void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
1068 
1069   /// Redirect each of the remapped files from first to second.
1070   static std::unique_ptr<RedirectingFileSystem>
1071   create(ArrayRef<std::pair<std::string, std::string>> RemappedFiles,
1072          bool UseExternalNames, FileSystem &ExternalFS);
1073 
1074   ErrorOr<Status> status(const Twine &Path) override;
1075   bool exists(const Twine &Path) override;
1076   ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
1077 
1078   std::error_code getRealPath(const Twine &Path,
1079                               SmallVectorImpl<char> &Output) override;
1080 
1081   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
1082 
1083   std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
1084 
1085   std::error_code isLocal(const Twine &Path, bool &Result) override;
1086 
1087   std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const override;
1088 
1089   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
1090 
1091   void setOverlayFileDir(StringRef PrefixDir);
1092 
1093   StringRef getOverlayFileDir() const;
1094 
1095   /// Sets the redirection kind to \c Fallthrough if true or \c RedirectOnly
1096   /// otherwise. Will removed in the future, use \c setRedirection instead.
1097   void setFallthrough(bool Fallthrough);
1098 
1099   void setRedirection(RedirectingFileSystem::RedirectKind Kind);
1100 
1101   std::vector<llvm::StringRef> getRoots() const;
1102 
hasBeenUsed()1103   bool hasBeenUsed() const { return HasBeenUsed; };
clearHasBeenUsed()1104   void clearHasBeenUsed() { HasBeenUsed = false; }
1105 
setUsageTrackingActive(bool Active)1106   void setUsageTrackingActive(bool Active) { UsageTrackingActive = Active; }
1107 
1108   void printEntry(raw_ostream &OS, Entry *E, unsigned IndentLevel = 0) const;
1109 
1110 protected:
1111   void printImpl(raw_ostream &OS, PrintType Type,
1112                  unsigned IndentLevel) const override;
1113   void visitChildFileSystems(VisitCallbackTy Callback) override;
1114 };
1115 
1116 /// Collect all pairs of <virtual path, real path> entries from the
1117 /// \p YAMLFilePath. This is used by the module dependency collector to forward
1118 /// the entries into the reproducer output VFS YAML file.
1119 LLVM_ABI void collectVFSFromYAML(
1120     std::unique_ptr<llvm::MemoryBuffer> Buffer,
1121     llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
1122     SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
1123     void *DiagContext = nullptr,
1124     IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
1125 
1126 class YAMLVFSWriter {
1127   std::vector<YAMLVFSEntry> Mappings;
1128   std::optional<bool> IsCaseSensitive;
1129   std::optional<bool> IsOverlayRelative;
1130   std::optional<bool> UseExternalNames;
1131   std::string OverlayDir;
1132 
1133   void addEntry(StringRef VirtualPath, StringRef RealPath, bool IsDirectory);
1134 
1135 public:
1136   YAMLVFSWriter() = default;
1137 
1138   LLVM_ABI void addFileMapping(StringRef VirtualPath, StringRef RealPath);
1139   LLVM_ABI void addDirectoryMapping(StringRef VirtualPath, StringRef RealPath);
1140 
setCaseSensitivity(bool CaseSensitive)1141   void setCaseSensitivity(bool CaseSensitive) {
1142     IsCaseSensitive = CaseSensitive;
1143   }
1144 
setUseExternalNames(bool UseExtNames)1145   void setUseExternalNames(bool UseExtNames) { UseExternalNames = UseExtNames; }
1146 
setOverlayDir(StringRef OverlayDirectory)1147   void setOverlayDir(StringRef OverlayDirectory) {
1148     IsOverlayRelative = true;
1149     OverlayDir.assign(OverlayDirectory.str());
1150   }
1151 
getMappings()1152   const std::vector<YAMLVFSEntry> &getMappings() const { return Mappings; }
1153 
1154   LLVM_ABI void write(llvm::raw_ostream &OS);
1155 };
1156 
1157 /// File system that tracks the number of calls to the underlying file system.
1158 /// This is particularly useful when wrapped around \c RealFileSystem to add
1159 /// lightweight tracking of expensive syscalls.
1160 class LLVM_ABI TracingFileSystem
1161     : public llvm::RTTIExtends<TracingFileSystem, ProxyFileSystem> {
1162 public:
1163   static const char ID;
1164 
1165   std::size_t NumStatusCalls = 0;
1166   std::size_t NumOpenFileForReadCalls = 0;
1167   std::size_t NumDirBeginCalls = 0;
1168   std::size_t NumGetRealPathCalls = 0;
1169   std::size_t NumExistsCalls = 0;
1170   std::size_t NumIsLocalCalls = 0;
1171 
TracingFileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)1172   TracingFileSystem(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
1173       : RTTIExtends(std::move(FS)) {}
1174 
status(const Twine & Path)1175   ErrorOr<Status> status(const Twine &Path) override {
1176     ++NumStatusCalls;
1177     return ProxyFileSystem::status(Path);
1178   }
1179 
openFileForRead(const Twine & Path)1180   ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override {
1181     ++NumOpenFileForReadCalls;
1182     return ProxyFileSystem::openFileForRead(Path);
1183   }
1184 
dir_begin(const Twine & Dir,std::error_code & EC)1185   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
1186     ++NumDirBeginCalls;
1187     return ProxyFileSystem::dir_begin(Dir, EC);
1188   }
1189 
getRealPath(const Twine & Path,SmallVectorImpl<char> & Output)1190   std::error_code getRealPath(const Twine &Path,
1191                               SmallVectorImpl<char> &Output) override {
1192     ++NumGetRealPathCalls;
1193     return ProxyFileSystem::getRealPath(Path, Output);
1194   }
1195 
exists(const Twine & Path)1196   bool exists(const Twine &Path) override {
1197     ++NumExistsCalls;
1198     return ProxyFileSystem::exists(Path);
1199   }
1200 
isLocal(const Twine & Path,bool & Result)1201   std::error_code isLocal(const Twine &Path, bool &Result) override {
1202     ++NumIsLocalCalls;
1203     return ProxyFileSystem::isLocal(Path, Result);
1204   }
1205 
1206 protected:
1207   void printImpl(raw_ostream &OS, PrintType Type,
1208                  unsigned IndentLevel) const override;
1209 };
1210 
1211 } // namespace vfs
1212 } // namespace llvm
1213 
1214 #endif // LLVM_SUPPORT_VIRTUALFILESYSTEM_H
1215