blob: 7b7c9f865ace9706c61094c5b6070a8eecdecabf [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- VirtualFileSystem.h - Virtual File System Layer ----------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
Andrew Scullcdfcccc2018-10-05 20:58:37 +010010/// Defines the virtual file system interface vfs::FileSystem.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010011//
12//===----------------------------------------------------------------------===//
13
Andrew Scull0372a572018-11-16 15:47:06 +000014#ifndef LLVM_SUPPORT_VIRTUALFILESYSTEM_H
15#define LLVM_SUPPORT_VIRTUALFILESYSTEM_H
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010016
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017#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 Walbran16937d02019-10-22 13:54:20 +010026#include "llvm/Support/Path.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027#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
38namespace llvm {
39
40class MemoryBuffer;
41
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042namespace vfs {
43
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044/// The result of a \p status operation.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045class 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
55public:
Andrew Scull0372a572018-11-16 15:47:06 +000056 // FIXME: remove when files support multiple names
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057 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 Scullcdfcccc2018-10-05 20:58:37 +010071 /// Returns the name that should be used for this file or directory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 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 Scullcdfcccc2018-10-05 20:58:37 +010097/// Represents an open file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098class File {
99public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100100 /// Destroy the file after closing it (if open).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100101 /// 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 Scullcdfcccc2018-10-05 20:58:37 +0100105 /// Get the status of the file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106 virtual llvm::ErrorOr<Status> status() = 0;
107
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100108 /// Get the name of the file
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100109 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 Scullcdfcccc2018-10-05 20:58:37 +0100116 /// Get the contents of the file as a \p MemoryBuffer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117 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 Scullcdfcccc2018-10-05 20:58:37 +0100121 /// Closes the file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100122 virtual std::error_code close() = 0;
123};
124
Andrew Scull0372a572018-11-16 15:47:06 +0000125/// A member of a directory, yielded by a directory_iterator.
126/// Only information available on most platforms is included.
127class directory_entry {
128 std::string Path;
129 llvm::sys::fs::file_type Type;
130
131public:
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 Scull5e1ddfa2018-08-14 10:06:54 +0100140namespace detail {
141
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100142/// An interface for virtual file systems to provide an iterator over the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100143/// (non-recursive) contents of a directory.
144struct DirIterImpl {
145 virtual ~DirIterImpl();
146
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100147 /// Sets \c CurrentEntry to the next entry in the directory on success,
Andrew Scull0372a572018-11-16 15:47:06 +0000148 /// to directory_entry() at end, or returns a system-defined \c error_code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100149 virtual std::error_code increment() = 0;
150
Andrew Scull0372a572018-11-16 15:47:06 +0000151 directory_entry CurrentEntry;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152};
153
154} // namespace detail
155
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100156/// An input iterator over the entries in a virtual path, similar to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100157/// llvm::sys::fs::directory_iterator.
158class directory_iterator {
159 std::shared_ptr<detail::DirIterImpl> Impl; // Input iterator semantics on copy
160
161public:
162 directory_iterator(std::shared_ptr<detail::DirIterImpl> I)
163 : Impl(std::move(I)) {
164 assert(Impl.get() != nullptr && "requires non-null implementation");
Andrew Scull0372a572018-11-16 15:47:06 +0000165 if (Impl->CurrentEntry.path().empty())
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100166 Impl.reset(); // Normalize the end iterator to Impl == nullptr.
167 }
168
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100169 /// Construct an 'end' iterator.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 directory_iterator() = default;
171
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100172 /// Equivalent to operator++, with an error code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100173 directory_iterator &increment(std::error_code &EC) {
174 assert(Impl && "attempting to increment past end");
175 EC = Impl->increment();
Andrew Scull0372a572018-11-16 15:47:06 +0000176 if (Impl->CurrentEntry.path().empty())
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100177 Impl.reset(); // Normalize the end iterator to Impl == nullptr.
178 return *this;
179 }
180
Andrew Scull0372a572018-11-16 15:47:06 +0000181 const directory_entry &operator*() const { return Impl->CurrentEntry; }
182 const directory_entry *operator->() const { return &Impl->CurrentEntry; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100183
184 bool operator==(const directory_iterator &RHS) const {
185 if (Impl && RHS.Impl)
Andrew Scull0372a572018-11-16 15:47:06 +0000186 return Impl->CurrentEntry.path() == RHS.Impl->CurrentEntry.path();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187 return !Impl && !RHS.Impl;
188 }
189 bool operator!=(const directory_iterator &RHS) const {
190 return !(*this == RHS);
191 }
192};
193
194class FileSystem;
195
Andrew Walbran16937d02019-10-22 13:54:20 +0100196namespace detail {
197
198/// Keeps state for the recursive_directory_iterator.
199struct RecDirIterState {
200 std::stack<directory_iterator, std::vector<directory_iterator>> Stack;
201 bool HasNoPushRequest = false;
202};
203
204} // end namespace detail
205
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100206/// An input iterator over the recursive contents of a virtual path,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100207/// similar to llvm::sys::fs::recursive_directory_iterator.
208class recursive_directory_iterator {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100209 FileSystem *FS;
Andrew Walbran16937d02019-10-22 13:54:20 +0100210 std::shared_ptr<detail::RecDirIterState>
211 State; // Input iterator semantics on copy.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100212
213public:
214 recursive_directory_iterator(FileSystem &FS, const Twine &Path,
215 std::error_code &EC);
216
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100217 /// Construct an 'end' iterator.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100218 recursive_directory_iterator() = default;
219
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100220 /// Equivalent to operator++, with an error code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100221 recursive_directory_iterator &increment(std::error_code &EC);
222
Andrew Walbran16937d02019-10-22 13:54:20 +0100223 const directory_entry &operator*() const { return *State->Stack.top(); }
224 const directory_entry *operator->() const { return &*State->Stack.top(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100225
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 Scullcdfcccc2018-10-05 20:58:37 +0100233 /// Gets the current level. Starting path is at level 0.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100234 int level() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100235 assert(!State->Stack.empty() &&
236 "Cannot get level without any iteration state");
237 return State->Stack.size() - 1;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100238 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100239
240 void no_push() { State->HasNoPushRequest = true; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100241};
242
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100243/// The virtual file system interface.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100244class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> {
245public:
246 virtual ~FileSystem();
247
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100248 /// Get the status of the entry at \p Path, if one exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100249 virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
250
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100251 /// Get a \p File object for the file at \p Path, if one exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100252 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 Scullcdfcccc2018-10-05 20:58:37 +0100261 /// Get a directory_iterator for \p Dir.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100262 /// \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 Scullcdfcccc2018-10-05 20:58:37 +0100273 /// 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 Scull5e1ddfa2018-08-14 10:06:54 +0100279 /// Check whether a file exists. Provided for convenience.
280 bool exists(const Twine &Path);
281
Andrew Walbran16937d02019-10-22 13:54:20 +0100282 /// Is the file mounted on a local filesystem?
283 virtual std::error_code isLocal(const Twine &Path, bool &Result);
284
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100285 /// 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 Scullcdfcccc2018-10-05 20:58:37 +0100299/// Gets an \p vfs::FileSystem for the 'real' file system, as seen by
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100300/// the operating system.
301IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
302
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100303/// A file system that allows overlaying one \p AbstractFileSystem on top
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100304/// of another.
305///
306/// Consists of a stack of >=1 \p FileSystem objects, which are treated as being
307/// one merged file system. When there is a directory that exists in more than
308/// one file system, the \p OverlayFileSystem contains a directory containing
309/// the union of their contents. The attributes (permissions, etc.) of the
310/// top-most (most recently added) directory are used. When there is a file
311/// that exists in more than one file system, the file in the top-most file
312/// system overrides the other(s).
313class OverlayFileSystem : public FileSystem {
314 using FileSystemList = SmallVector<IntrusiveRefCntPtr<FileSystem>, 1>;
315
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100316 /// The stack of file systems, implemented as a list in order of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100317 /// their addition.
318 FileSystemList FSList;
319
320public:
321 OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base);
322
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100323 /// Pushes a file system on top of the stack.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100324 void pushOverlay(IntrusiveRefCntPtr<FileSystem> FS);
325
326 llvm::ErrorOr<Status> status(const Twine &Path) override;
327 llvm::ErrorOr<std::unique_ptr<File>>
328 openFileForRead(const Twine &Path) override;
329 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
330 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
331 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
Andrew Walbran16937d02019-10-22 13:54:20 +0100332 std::error_code isLocal(const Twine &Path, bool &Result) override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100333 std::error_code getRealPath(const Twine &Path,
334 SmallVectorImpl<char> &Output) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100335
336 using iterator = FileSystemList::reverse_iterator;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100337 using const_iterator = FileSystemList::const_reverse_iterator;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100338
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100339 /// Get an iterator pointing to the most recently added file system.
340 iterator overlays_begin() { return FSList.rbegin(); }
341 const_iterator overlays_begin() const { return FSList.rbegin(); }
342
343 /// Get an iterator pointing one-past the least recently added file
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100344 /// system.
345 iterator overlays_end() { return FSList.rend(); }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100346 const_iterator overlays_end() const { return FSList.rend(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100347};
348
Andrew Scull0372a572018-11-16 15:47:06 +0000349/// By default, this delegates all calls to the underlying file system. This
350/// is useful when derived file systems want to override some calls and still
351/// proxy other calls.
352class ProxyFileSystem : public FileSystem {
353public:
354 explicit ProxyFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
355 : FS(std::move(FS)) {}
356
357 llvm::ErrorOr<Status> status(const Twine &Path) override {
358 return FS->status(Path);
359 }
360 llvm::ErrorOr<std::unique_ptr<File>>
361 openFileForRead(const Twine &Path) override {
362 return FS->openFileForRead(Path);
363 }
364 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
365 return FS->dir_begin(Dir, EC);
366 }
367 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
368 return FS->getCurrentWorkingDirectory();
369 }
370 std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
371 return FS->setCurrentWorkingDirectory(Path);
372 }
373 std::error_code getRealPath(const Twine &Path,
374 SmallVectorImpl<char> &Output) const override {
375 return FS->getRealPath(Path, Output);
376 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100377 std::error_code isLocal(const Twine &Path, bool &Result) override {
378 return FS->isLocal(Path, Result);
379 }
Andrew Scull0372a572018-11-16 15:47:06 +0000380
381protected:
382 FileSystem &getUnderlyingFS() { return *FS; }
383
384private:
385 IntrusiveRefCntPtr<FileSystem> FS;
Andrew Walbran16937d02019-10-22 13:54:20 +0100386
387 virtual void anchor();
Andrew Scull0372a572018-11-16 15:47:06 +0000388};
389
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100390namespace detail {
391
392class InMemoryDirectory;
Andrew Scull0372a572018-11-16 15:47:06 +0000393class InMemoryFile;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100394
395} // namespace detail
396
397/// An in-memory file system.
398class InMemoryFileSystem : public FileSystem {
399 std::unique_ptr<detail::InMemoryDirectory> Root;
400 std::string WorkingDirectory;
401 bool UseNormalizedPaths = true;
402
Andrew Scull0372a572018-11-16 15:47:06 +0000403 /// If HardLinkTarget is non-null, a hardlink is created to the To path which
404 /// must be a file. If it is null then it adds the file as the public addFile.
405 bool addFile(const Twine &Path, time_t ModificationTime,
406 std::unique_ptr<llvm::MemoryBuffer> Buffer,
407 Optional<uint32_t> User, Optional<uint32_t> Group,
408 Optional<llvm::sys::fs::file_type> Type,
409 Optional<llvm::sys::fs::perms> Perms,
410 const detail::InMemoryFile *HardLinkTarget);
411
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100412public:
413 explicit InMemoryFileSystem(bool UseNormalizedPaths = true);
414 ~InMemoryFileSystem() override;
415
416 /// Add a file containing a buffer or a directory to the VFS with a
417 /// path. The VFS owns the buffer. If present, User, Group, Type
418 /// and Perms apply to the newly-created file or directory.
419 /// \return true if the file or directory was successfully added,
420 /// false if the file or directory already exists in the file system with
421 /// different contents.
422 bool addFile(const Twine &Path, time_t ModificationTime,
423 std::unique_ptr<llvm::MemoryBuffer> Buffer,
424 Optional<uint32_t> User = None, Optional<uint32_t> Group = None,
425 Optional<llvm::sys::fs::file_type> Type = None,
426 Optional<llvm::sys::fs::perms> Perms = None);
427
Andrew Scull0372a572018-11-16 15:47:06 +0000428 /// Add a hard link to a file.
429 /// Here hard links are not intended to be fully equivalent to the classical
430 /// filesystem. Both the hard link and the file share the same buffer and
431 /// status (and thus have the same UniqueID). Because of this there is no way
432 /// to distinguish between the link and the file after the link has been
433 /// added.
434 ///
435 /// The To path must be an existing file or a hardlink. The From file must not
436 /// have been added before. The To Path must not be a directory. The From Node
437 /// is added as a hard link which points to the resolved file of To Node.
438 /// \return true if the above condition is satisfied and hardlink was
439 /// successfully created, false otherwise.
440 bool addHardLink(const Twine &From, const Twine &To);
441
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100442 /// Add a buffer to the VFS with a path. The VFS does not own the buffer.
443 /// If present, User, Group, Type and Perms apply to the newly-created file
444 /// or directory.
445 /// \return true if the file or directory was successfully added,
446 /// false if the file or directory already exists in the file system with
447 /// different contents.
448 bool addFileNoOwn(const Twine &Path, time_t ModificationTime,
Andrew Scull0372a572018-11-16 15:47:06 +0000449 llvm::MemoryBuffer *Buffer, Optional<uint32_t> User = None,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100450 Optional<uint32_t> Group = None,
451 Optional<llvm::sys::fs::file_type> Type = None,
452 Optional<llvm::sys::fs::perms> Perms = None);
453
454 std::string toString() const;
455
456 /// Return true if this file system normalizes . and .. in paths.
457 bool useNormalizedPaths() const { return UseNormalizedPaths; }
458
459 llvm::ErrorOr<Status> status(const Twine &Path) override;
460 llvm::ErrorOr<std::unique_ptr<File>>
461 openFileForRead(const Twine &Path) override;
462 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
463
464 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
465 return WorkingDirectory;
466 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100467 /// Canonicalizes \p Path by combining with the current working
468 /// directory and normalizing the path (e.g. remove dots). If the current
469 /// working directory is not set, this returns errc::operation_not_permitted.
470 ///
471 /// This doesn't resolve symlinks as they are not supported in in-memory file
472 /// system.
473 std::error_code getRealPath(const Twine &Path,
474 SmallVectorImpl<char> &Output) const override;
Andrew Walbran16937d02019-10-22 13:54:20 +0100475 std::error_code isLocal(const Twine &Path, bool &Result) override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100476 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
477};
478
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100479/// Get a globally unique ID for a virtual file or directory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100480llvm::sys::fs::UniqueID getNextVirtualUniqueID();
481
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100482/// Gets a \p FileSystem for a virtual file system described in YAML
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100483/// format.
484IntrusiveRefCntPtr<FileSystem>
485getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer,
486 llvm::SourceMgr::DiagHandlerTy DiagHandler,
Andrew Scull0372a572018-11-16 15:47:06 +0000487 StringRef YAMLFilePath, void *DiagContext = nullptr,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100488 IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
489
490struct YAMLVFSEntry {
Andrew Scull0372a572018-11-16 15:47:06 +0000491 template <typename T1, typename T2>
492 YAMLVFSEntry(T1 &&VPath, T2 &&RPath)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100493 : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)) {}
494 std::string VPath;
495 std::string RPath;
496};
497
Andrew Walbran16937d02019-10-22 13:54:20 +0100498class VFSFromYamlDirIterImpl;
499class RedirectingFileSystemParser;
500
501/// A virtual file system parsed from a YAML file.
502///
503/// Currently, this class allows creating virtual directories and mapping
504/// virtual file paths to existing external files, available in \c ExternalFS.
505///
506/// The basic structure of the parsed file is:
507/// \verbatim
508/// {
509/// 'version': <version number>,
510/// <optional configuration>
511/// 'roots': [
512/// <directory entries>
513/// ]
514/// }
515/// \endverbatim
516///
517/// All configuration options are optional.
518/// 'case-sensitive': <boolean, default=true>
519/// 'use-external-names': <boolean, default=true>
520/// 'overlay-relative': <boolean, default=false>
521/// 'fallthrough': <boolean, default=true>
522///
523/// Virtual directories are represented as
524/// \verbatim
525/// {
526/// 'type': 'directory',
527/// 'name': <string>,
528/// 'contents': [ <file or directory entries> ]
529/// }
530/// \endverbatim
531///
532/// The default attributes for virtual directories are:
533/// \verbatim
534/// MTime = now() when created
535/// Perms = 0777
536/// User = Group = 0
537/// Size = 0
538/// UniqueID = unspecified unique value
539/// \endverbatim
540///
541/// Re-mapped files are represented as
542/// \verbatim
543/// {
544/// 'type': 'file',
545/// 'name': <string>,
546/// 'use-external-name': <boolean> # Optional
547/// 'external-contents': <path to external file>
548/// }
549/// \endverbatim
550///
551/// and inherit their attributes from the external contents.
552///
553/// In both cases, the 'name' field may contain multiple path components (e.g.
554/// /path/to/file). However, any directory that contains more than one child
555/// must be uniquely represented by a directory entry.
556class RedirectingFileSystem : public vfs::FileSystem {
557public:
558 enum EntryKind { EK_Directory, EK_File };
559
560 /// A single file or directory in the VFS.
561 class Entry {
562 EntryKind Kind;
563 std::string Name;
564
565 public:
566 Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
567 virtual ~Entry() = default;
568
569 StringRef getName() const { return Name; }
570 EntryKind getKind() const { return Kind; }
571 };
572
573 class RedirectingDirectoryEntry : public Entry {
574 std::vector<std::unique_ptr<Entry>> Contents;
575 Status S;
576
577 public:
578 RedirectingDirectoryEntry(StringRef Name,
579 std::vector<std::unique_ptr<Entry>> Contents,
580 Status S)
581 : Entry(EK_Directory, Name), Contents(std::move(Contents)),
582 S(std::move(S)) {}
583 RedirectingDirectoryEntry(StringRef Name, Status S)
584 : Entry(EK_Directory, Name), S(std::move(S)) {}
585
586 Status getStatus() { return S; }
587
588 void addContent(std::unique_ptr<Entry> Content) {
589 Contents.push_back(std::move(Content));
590 }
591
592 Entry *getLastContent() const { return Contents.back().get(); }
593
594 using iterator = decltype(Contents)::iterator;
595
596 iterator contents_begin() { return Contents.begin(); }
597 iterator contents_end() { return Contents.end(); }
598
599 static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
600 };
601
602 class RedirectingFileEntry : public Entry {
603 public:
604 enum NameKind { NK_NotSet, NK_External, NK_Virtual };
605
606 private:
607 std::string ExternalContentsPath;
608 NameKind UseName;
609
610 public:
611 RedirectingFileEntry(StringRef Name, StringRef ExternalContentsPath,
612 NameKind UseName)
613 : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
614 UseName(UseName) {}
615
616 StringRef getExternalContentsPath() const { return ExternalContentsPath; }
617
618 /// whether to use the external path as the name for this file.
619 bool useExternalName(bool GlobalUseExternalName) const {
620 return UseName == NK_NotSet ? GlobalUseExternalName
621 : (UseName == NK_External);
622 }
623
624 NameKind getUseName() const { return UseName; }
625
626 static bool classof(const Entry *E) { return E->getKind() == EK_File; }
627 };
628
629private:
630 friend class VFSFromYamlDirIterImpl;
631 friend class RedirectingFileSystemParser;
632
633 /// The root(s) of the virtual file system.
634 std::vector<std::unique_ptr<Entry>> Roots;
635
636 /// The file system to use for external references.
637 IntrusiveRefCntPtr<FileSystem> ExternalFS;
638
639 /// If IsRelativeOverlay is set, this represents the directory
640 /// path that should be prefixed to each 'external-contents' entry
641 /// when reading from YAML files.
642 std::string ExternalContentsPrefixDir;
643
644 /// @name Configuration
645 /// @{
646
647 /// Whether to perform case-sensitive comparisons.
648 ///
649 /// Currently, case-insensitive matching only works correctly with ASCII.
650 bool CaseSensitive = true;
651
652 /// IsRelativeOverlay marks whether a ExternalContentsPrefixDir path must
653 /// be prefixed in every 'external-contents' when reading from YAML files.
654 bool IsRelativeOverlay = false;
655
656 /// Whether to use to use the value of 'external-contents' for the
657 /// names of files. This global value is overridable on a per-file basis.
658 bool UseExternalNames = true;
659
660 /// Whether to attempt a file lookup in external file system after it wasn't
661 /// found in VFS.
662 bool IsFallthrough = true;
663 /// @}
664
665 /// Virtual file paths and external files could be canonicalized without "..",
666 /// "." and "./" in their paths. FIXME: some unittests currently fail on
667 /// win32 when using remove_dots and remove_leading_dotslash on paths.
668 bool UseCanonicalizedPaths =
669#ifdef _WIN32
670 false;
671#else
672 true;
673#endif
674
675 RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS)
676 : ExternalFS(std::move(ExternalFS)) {}
677
678 /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly
679 /// recursing into the contents of \p From if it is a directory.
680 ErrorOr<Entry *> lookupPath(llvm::sys::path::const_iterator Start,
681 llvm::sys::path::const_iterator End,
682 Entry *From) const;
683
684 /// Get the status of a given an \c Entry.
685 ErrorOr<Status> status(const Twine &Path, Entry *E);
686
687public:
688 /// Looks up \p Path in \c Roots.
689 ErrorOr<Entry *> lookupPath(const Twine &Path) const;
690
691 /// Parses \p Buffer, which is expected to be in YAML format and
692 /// returns a virtual file system representing its contents.
693 static RedirectingFileSystem *
694 create(std::unique_ptr<MemoryBuffer> Buffer,
695 SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
696 void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
697
698 ErrorOr<Status> status(const Twine &Path) override;
699 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
700
701 std::error_code getRealPath(const Twine &Path,
702 SmallVectorImpl<char> &Output) const override;
703
704 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
705
706 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
707
708 std::error_code isLocal(const Twine &Path, bool &Result) override;
709
710 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
711
712 void setExternalContentsPrefixDir(StringRef PrefixDir);
713
714 StringRef getExternalContentsPrefixDir() const;
715
716#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
717 LLVM_DUMP_METHOD void dump() const;
718 LLVM_DUMP_METHOD void dumpEntry(Entry *E, int NumSpaces = 0) const;
719#endif
720};
721
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100722/// Collect all pairs of <virtual path, real path> entries from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100723/// \p YAMLFilePath. This is used by the module dependency collector to forward
724/// the entries into the reproducer output VFS YAML file.
725void collectVFSFromYAML(
726 std::unique_ptr<llvm::MemoryBuffer> Buffer,
727 llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
728 SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
729 void *DiagContext = nullptr,
730 IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
731
732class YAMLVFSWriter {
733 std::vector<YAMLVFSEntry> Mappings;
734 Optional<bool> IsCaseSensitive;
735 Optional<bool> IsOverlayRelative;
736 Optional<bool> UseExternalNames;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100737 std::string OverlayDir;
738
739public:
740 YAMLVFSWriter() = default;
741
742 void addFileMapping(StringRef VirtualPath, StringRef RealPath);
743
744 void setCaseSensitivity(bool CaseSensitive) {
745 IsCaseSensitive = CaseSensitive;
746 }
747
Andrew Scull0372a572018-11-16 15:47:06 +0000748 void setUseExternalNames(bool UseExtNames) { UseExternalNames = UseExtNames; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100749
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100750 void setOverlayDir(StringRef OverlayDirectory) {
751 IsOverlayRelative = true;
752 OverlayDir.assign(OverlayDirectory.str());
753 }
754
Andrew Walbran16937d02019-10-22 13:54:20 +0100755 const std::vector<YAMLVFSEntry> &getMappings() const { return Mappings; }
756
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100757 void write(llvm::raw_ostream &OS);
758};
759
760} // namespace vfs
Andrew Scull0372a572018-11-16 15:47:06 +0000761} // namespace llvm
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100762
Andrew Scull0372a572018-11-16 15:47:06 +0000763#endif // LLVM_SUPPORT_VIRTUALFILESYSTEM_H