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