Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- VirtualFileSystem.h - Virtual File System Layer ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 11 | /// Defines the virtual file system interface vfs::FileSystem. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 15 | #ifndef LLVM_SUPPORT_VIRTUALFILESYSTEM_H |
| 16 | #define LLVM_SUPPORT_VIRTUALFILESYSTEM_H |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 18 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 19 | #include "llvm/ADT/None.h" |
| 20 | #include "llvm/ADT/Optional.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
| 23 | #include "llvm/ADT/Twine.h" |
| 24 | #include "llvm/Support/Chrono.h" |
| 25 | #include "llvm/Support/ErrorOr.h" |
| 26 | #include "llvm/Support/FileSystem.h" |
| 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); |
| 61 | Status(StringRef Name, llvm::sys::fs::UniqueID UID, |
| 62 | llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group, |
| 63 | uint64_t Size, llvm::sys::fs::file_type Type, |
| 64 | llvm::sys::fs::perms Perms); |
| 65 | |
| 66 | /// Get a copy of a Status with a different name. |
| 67 | static Status copyWithNewName(const Status &In, StringRef NewName); |
| 68 | static Status copyWithNewName(const llvm::sys::fs::file_status &In, |
| 69 | StringRef NewName); |
| 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 196 | /// An input iterator over the recursive contents of a virtual path, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 197 | /// similar to llvm::sys::fs::recursive_directory_iterator. |
| 198 | class recursive_directory_iterator { |
| 199 | using IterState = |
| 200 | std::stack<directory_iterator, std::vector<directory_iterator>>; |
| 201 | |
| 202 | FileSystem *FS; |
| 203 | std::shared_ptr<IterState> State; // Input iterator semantics on copy. |
| 204 | |
| 205 | public: |
| 206 | recursive_directory_iterator(FileSystem &FS, const Twine &Path, |
| 207 | std::error_code &EC); |
| 208 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 209 | /// Construct an 'end' iterator. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 210 | recursive_directory_iterator() = default; |
| 211 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 212 | /// Equivalent to operator++, with an error code. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 213 | recursive_directory_iterator &increment(std::error_code &EC); |
| 214 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 215 | const directory_entry &operator*() const { return *State->top(); } |
| 216 | const directory_entry *operator->() const { return &*State->top(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 217 | |
| 218 | bool operator==(const recursive_directory_iterator &Other) const { |
| 219 | return State == Other.State; // identity |
| 220 | } |
| 221 | bool operator!=(const recursive_directory_iterator &RHS) const { |
| 222 | return !(*this == RHS); |
| 223 | } |
| 224 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 225 | /// Gets the current level. Starting path is at level 0. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 226 | int level() const { |
| 227 | assert(!State->empty() && "Cannot get level without any iteration state"); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 228 | return State->size() - 1; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 229 | } |
| 230 | }; |
| 231 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 232 | /// The virtual file system interface. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 233 | class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> { |
| 234 | public: |
| 235 | virtual ~FileSystem(); |
| 236 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 237 | /// Get the status of the entry at \p Path, if one exists. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 238 | virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0; |
| 239 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 240 | /// 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] | 241 | virtual llvm::ErrorOr<std::unique_ptr<File>> |
| 242 | openFileForRead(const Twine &Path) = 0; |
| 243 | |
| 244 | /// This is a convenience method that opens a file, gets its content and then |
| 245 | /// closes the file. |
| 246 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 247 | getBufferForFile(const Twine &Name, int64_t FileSize = -1, |
| 248 | bool RequiresNullTerminator = true, bool IsVolatile = false); |
| 249 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 250 | /// Get a directory_iterator for \p Dir. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 251 | /// \note The 'end' iterator is directory_iterator(). |
| 252 | virtual directory_iterator dir_begin(const Twine &Dir, |
| 253 | std::error_code &EC) = 0; |
| 254 | |
| 255 | /// Set the working directory. This will affect all following operations on |
| 256 | /// this file system and may propagate down for nested file systems. |
| 257 | virtual std::error_code setCurrentWorkingDirectory(const Twine &Path) = 0; |
| 258 | |
| 259 | /// Get the working directory of this file system. |
| 260 | virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const = 0; |
| 261 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 262 | /// Gets real path of \p Path e.g. collapse all . and .. patterns, resolve |
| 263 | /// symlinks. For real file system, this uses `llvm::sys::fs::real_path`. |
| 264 | /// This returns errc::operation_not_permitted if not implemented by subclass. |
| 265 | virtual std::error_code getRealPath(const Twine &Path, |
| 266 | SmallVectorImpl<char> &Output) const; |
| 267 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 268 | /// Check whether a file exists. Provided for convenience. |
| 269 | bool exists(const Twine &Path); |
| 270 | |
| 271 | /// Make \a Path an absolute path. |
| 272 | /// |
| 273 | /// Makes \a Path absolute using the current directory if it is not already. |
| 274 | /// An empty \a Path will result in the current directory. |
| 275 | /// |
| 276 | /// /absolute/path => /absolute/path |
| 277 | /// relative/../path => <current-directory>/relative/../path |
| 278 | /// |
| 279 | /// \param Path A path that is modified to be an absolute path. |
| 280 | /// \returns success if \a path has been made absolute, otherwise a |
| 281 | /// platform-specific error_code. |
| 282 | std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const; |
| 283 | }; |
| 284 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 285 | /// 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] | 286 | /// the operating system. |
| 287 | IntrusiveRefCntPtr<FileSystem> getRealFileSystem(); |
| 288 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 289 | /// A file system that allows overlaying one \p AbstractFileSystem on top |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 290 | /// of another. |
| 291 | /// |
| 292 | /// Consists of a stack of >=1 \p FileSystem objects, which are treated as being |
| 293 | /// one merged file system. When there is a directory that exists in more than |
| 294 | /// one file system, the \p OverlayFileSystem contains a directory containing |
| 295 | /// the union of their contents. The attributes (permissions, etc.) of the |
| 296 | /// top-most (most recently added) directory are used. When there is a file |
| 297 | /// that exists in more than one file system, the file in the top-most file |
| 298 | /// system overrides the other(s). |
| 299 | class OverlayFileSystem : public FileSystem { |
| 300 | using FileSystemList = SmallVector<IntrusiveRefCntPtr<FileSystem>, 1>; |
| 301 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 302 | /// The stack of file systems, implemented as a list in order of |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 303 | /// their addition. |
| 304 | FileSystemList FSList; |
| 305 | |
| 306 | public: |
| 307 | OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base); |
| 308 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 309 | /// Pushes a file system on top of the stack. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 310 | void pushOverlay(IntrusiveRefCntPtr<FileSystem> FS); |
| 311 | |
| 312 | llvm::ErrorOr<Status> status(const Twine &Path) override; |
| 313 | llvm::ErrorOr<std::unique_ptr<File>> |
| 314 | openFileForRead(const Twine &Path) override; |
| 315 | directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; |
| 316 | llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override; |
| 317 | std::error_code setCurrentWorkingDirectory(const Twine &Path) override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 318 | std::error_code getRealPath(const Twine &Path, |
| 319 | SmallVectorImpl<char> &Output) const override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 320 | |
| 321 | using iterator = FileSystemList::reverse_iterator; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 322 | using const_iterator = FileSystemList::const_reverse_iterator; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 323 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 324 | /// Get an iterator pointing to the most recently added file system. |
| 325 | iterator overlays_begin() { return FSList.rbegin(); } |
| 326 | const_iterator overlays_begin() const { return FSList.rbegin(); } |
| 327 | |
| 328 | /// Get an iterator pointing one-past the least recently added file |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 329 | /// system. |
| 330 | iterator overlays_end() { return FSList.rend(); } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 331 | const_iterator overlays_end() const { return FSList.rend(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 332 | }; |
| 333 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 334 | /// By default, this delegates all calls to the underlying file system. This |
| 335 | /// is useful when derived file systems want to override some calls and still |
| 336 | /// proxy other calls. |
| 337 | class ProxyFileSystem : public FileSystem { |
| 338 | public: |
| 339 | explicit ProxyFileSystem(IntrusiveRefCntPtr<FileSystem> FS) |
| 340 | : FS(std::move(FS)) {} |
| 341 | |
| 342 | llvm::ErrorOr<Status> status(const Twine &Path) override { |
| 343 | return FS->status(Path); |
| 344 | } |
| 345 | llvm::ErrorOr<std::unique_ptr<File>> |
| 346 | openFileForRead(const Twine &Path) override { |
| 347 | return FS->openFileForRead(Path); |
| 348 | } |
| 349 | directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override { |
| 350 | return FS->dir_begin(Dir, EC); |
| 351 | } |
| 352 | llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override { |
| 353 | return FS->getCurrentWorkingDirectory(); |
| 354 | } |
| 355 | std::error_code setCurrentWorkingDirectory(const Twine &Path) override { |
| 356 | return FS->setCurrentWorkingDirectory(Path); |
| 357 | } |
| 358 | std::error_code getRealPath(const Twine &Path, |
| 359 | SmallVectorImpl<char> &Output) const override { |
| 360 | return FS->getRealPath(Path, Output); |
| 361 | } |
| 362 | |
| 363 | protected: |
| 364 | FileSystem &getUnderlyingFS() { return *FS; } |
| 365 | |
| 366 | private: |
| 367 | IntrusiveRefCntPtr<FileSystem> FS; |
| 368 | }; |
| 369 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 370 | namespace detail { |
| 371 | |
| 372 | class InMemoryDirectory; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 373 | class InMemoryFile; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 374 | |
| 375 | } // namespace detail |
| 376 | |
| 377 | /// An in-memory file system. |
| 378 | class InMemoryFileSystem : public FileSystem { |
| 379 | std::unique_ptr<detail::InMemoryDirectory> Root; |
| 380 | std::string WorkingDirectory; |
| 381 | bool UseNormalizedPaths = true; |
| 382 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 383 | /// If HardLinkTarget is non-null, a hardlink is created to the To path which |
| 384 | /// must be a file. If it is null then it adds the file as the public addFile. |
| 385 | bool addFile(const Twine &Path, time_t ModificationTime, |
| 386 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 387 | Optional<uint32_t> User, Optional<uint32_t> Group, |
| 388 | Optional<llvm::sys::fs::file_type> Type, |
| 389 | Optional<llvm::sys::fs::perms> Perms, |
| 390 | const detail::InMemoryFile *HardLinkTarget); |
| 391 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 392 | public: |
| 393 | explicit InMemoryFileSystem(bool UseNormalizedPaths = true); |
| 394 | ~InMemoryFileSystem() override; |
| 395 | |
| 396 | /// Add a file containing a buffer or a directory to the VFS with a |
| 397 | /// path. The VFS owns the buffer. If present, User, Group, Type |
| 398 | /// and Perms apply to the newly-created file or directory. |
| 399 | /// \return true if the file or directory was successfully added, |
| 400 | /// false if the file or directory already exists in the file system with |
| 401 | /// different contents. |
| 402 | bool addFile(const Twine &Path, time_t ModificationTime, |
| 403 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 404 | Optional<uint32_t> User = None, Optional<uint32_t> Group = None, |
| 405 | Optional<llvm::sys::fs::file_type> Type = None, |
| 406 | Optional<llvm::sys::fs::perms> Perms = None); |
| 407 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 408 | /// Add a hard link to a file. |
| 409 | /// Here hard links are not intended to be fully equivalent to the classical |
| 410 | /// filesystem. Both the hard link and the file share the same buffer and |
| 411 | /// status (and thus have the same UniqueID). Because of this there is no way |
| 412 | /// to distinguish between the link and the file after the link has been |
| 413 | /// added. |
| 414 | /// |
| 415 | /// The To path must be an existing file or a hardlink. The From file must not |
| 416 | /// have been added before. The To Path must not be a directory. The From Node |
| 417 | /// is added as a hard link which points to the resolved file of To Node. |
| 418 | /// \return true if the above condition is satisfied and hardlink was |
| 419 | /// successfully created, false otherwise. |
| 420 | bool addHardLink(const Twine &From, const Twine &To); |
| 421 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 422 | /// Add a buffer to the VFS with a path. The VFS does not own the buffer. |
| 423 | /// If present, User, Group, Type and Perms apply to the newly-created file |
| 424 | /// or directory. |
| 425 | /// \return true if the file or directory was successfully added, |
| 426 | /// false if the file or directory already exists in the file system with |
| 427 | /// different contents. |
| 428 | bool addFileNoOwn(const Twine &Path, time_t ModificationTime, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 429 | llvm::MemoryBuffer *Buffer, Optional<uint32_t> User = None, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 430 | Optional<uint32_t> Group = None, |
| 431 | Optional<llvm::sys::fs::file_type> Type = None, |
| 432 | Optional<llvm::sys::fs::perms> Perms = None); |
| 433 | |
| 434 | std::string toString() const; |
| 435 | |
| 436 | /// Return true if this file system normalizes . and .. in paths. |
| 437 | bool useNormalizedPaths() const { return UseNormalizedPaths; } |
| 438 | |
| 439 | llvm::ErrorOr<Status> status(const Twine &Path) override; |
| 440 | llvm::ErrorOr<std::unique_ptr<File>> |
| 441 | openFileForRead(const Twine &Path) override; |
| 442 | directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; |
| 443 | |
| 444 | llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override { |
| 445 | return WorkingDirectory; |
| 446 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 447 | /// Canonicalizes \p Path by combining with the current working |
| 448 | /// directory and normalizing the path (e.g. remove dots). If the current |
| 449 | /// working directory is not set, this returns errc::operation_not_permitted. |
| 450 | /// |
| 451 | /// This doesn't resolve symlinks as they are not supported in in-memory file |
| 452 | /// system. |
| 453 | std::error_code getRealPath(const Twine &Path, |
| 454 | SmallVectorImpl<char> &Output) const override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 455 | |
| 456 | std::error_code setCurrentWorkingDirectory(const Twine &Path) override; |
| 457 | }; |
| 458 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 459 | /// Get a globally unique ID for a virtual file or directory. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 460 | llvm::sys::fs::UniqueID getNextVirtualUniqueID(); |
| 461 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 462 | /// Gets a \p FileSystem for a virtual file system described in YAML |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 463 | /// format. |
| 464 | IntrusiveRefCntPtr<FileSystem> |
| 465 | getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 466 | llvm::SourceMgr::DiagHandlerTy DiagHandler, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 467 | StringRef YAMLFilePath, void *DiagContext = nullptr, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 468 | IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem()); |
| 469 | |
| 470 | struct YAMLVFSEntry { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 471 | template <typename T1, typename T2> |
| 472 | YAMLVFSEntry(T1 &&VPath, T2 &&RPath) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 473 | : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)) {} |
| 474 | std::string VPath; |
| 475 | std::string RPath; |
| 476 | }; |
| 477 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 478 | /// Collect all pairs of <virtual path, real path> entries from the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 479 | /// \p YAMLFilePath. This is used by the module dependency collector to forward |
| 480 | /// the entries into the reproducer output VFS YAML file. |
| 481 | void collectVFSFromYAML( |
| 482 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 483 | llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath, |
| 484 | SmallVectorImpl<YAMLVFSEntry> &CollectedEntries, |
| 485 | void *DiagContext = nullptr, |
| 486 | IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem()); |
| 487 | |
| 488 | class YAMLVFSWriter { |
| 489 | std::vector<YAMLVFSEntry> Mappings; |
| 490 | Optional<bool> IsCaseSensitive; |
| 491 | Optional<bool> IsOverlayRelative; |
| 492 | Optional<bool> UseExternalNames; |
| 493 | Optional<bool> IgnoreNonExistentContents; |
| 494 | std::string OverlayDir; |
| 495 | |
| 496 | public: |
| 497 | YAMLVFSWriter() = default; |
| 498 | |
| 499 | void addFileMapping(StringRef VirtualPath, StringRef RealPath); |
| 500 | |
| 501 | void setCaseSensitivity(bool CaseSensitive) { |
| 502 | IsCaseSensitive = CaseSensitive; |
| 503 | } |
| 504 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 505 | void setUseExternalNames(bool UseExtNames) { UseExternalNames = UseExtNames; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 506 | |
| 507 | void setIgnoreNonExistentContents(bool IgnoreContents) { |
| 508 | IgnoreNonExistentContents = IgnoreContents; |
| 509 | } |
| 510 | |
| 511 | void setOverlayDir(StringRef OverlayDirectory) { |
| 512 | IsOverlayRelative = true; |
| 513 | OverlayDir.assign(OverlayDirectory.str()); |
| 514 | } |
| 515 | |
| 516 | void write(llvm::raw_ostream &OS); |
| 517 | }; |
| 518 | |
| 519 | } // namespace vfs |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 520 | } // namespace llvm |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 521 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 522 | #endif // LLVM_SUPPORT_VIRTUALFILESYSTEM_H |