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