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