blob: d483fc33d6f088a8548a1a51358b4706332e8530 [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"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010022#include "llvm/Support/Chrono.h"
23#include "llvm/Support/ErrorOr.h"
24#include "llvm/Support/FileSystem.h"
Andrew Walbran16937d02019-10-22 13:54:20 +010025#include "llvm/Support/Path.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026#include "llvm/Support/SourceMgr.h"
27#include <cassert>
28#include <cstdint>
29#include <ctime>
30#include <memory>
31#include <stack>
32#include <string>
33#include <system_error>
34#include <utility>
35#include <vector>
36
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020037// ANDROID x86_64 defined the FS macro
38#undef FS
39
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040namespace llvm {
41
42class MemoryBuffer;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020043class MemoryBufferRef;
44class Twine;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046namespace vfs {
47
Andrew Scullcdfcccc2018-10-05 20:58:37 +010048/// The result of a \p status operation.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049class Status {
50 std::string Name;
51 llvm::sys::fs::UniqueID UID;
52 llvm::sys::TimePoint<> MTime;
53 uint32_t User;
54 uint32_t Group;
55 uint64_t Size;
56 llvm::sys::fs::file_type Type = llvm::sys::fs::file_type::status_error;
57 llvm::sys::fs::perms Perms;
58
59public:
Andrew Scull0372a572018-11-16 15:47:06 +000060 // FIXME: remove when files support multiple names
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010061 bool IsVFSMapped = false;
62
63 Status() = default;
64 Status(const llvm::sys::fs::file_status &Status);
Andrew Walbran3d2c1972020-04-07 12:24:26 +010065 Status(const Twine &Name, llvm::sys::fs::UniqueID UID,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066 llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group,
67 uint64_t Size, llvm::sys::fs::file_type Type,
68 llvm::sys::fs::perms Perms);
69
70 /// Get a copy of a Status with a different name.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010071 static Status copyWithNewName(const Status &In, const Twine &NewName);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 static Status copyWithNewName(const llvm::sys::fs::file_status &In,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010073 const Twine &NewName);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074
Andrew Scullcdfcccc2018-10-05 20:58:37 +010075 /// Returns the name that should be used for this file or directory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010076 StringRef getName() const { return Name; }
77
78 /// @name Status interface from llvm::sys::fs
79 /// @{
80 llvm::sys::fs::file_type getType() const { return Type; }
81 llvm::sys::fs::perms getPermissions() const { return Perms; }
82 llvm::sys::TimePoint<> getLastModificationTime() const { return MTime; }
83 llvm::sys::fs::UniqueID getUniqueID() const { return UID; }
84 uint32_t getUser() const { return User; }
85 uint32_t getGroup() const { return Group; }
86 uint64_t getSize() const { return Size; }
87 /// @}
88 /// @name Status queries
89 /// These are static queries in llvm::sys::fs.
90 /// @{
91 bool equivalent(const Status &Other) const;
92 bool isDirectory() const;
93 bool isRegularFile() const;
94 bool isOther() const;
95 bool isSymlink() const;
96 bool isStatusKnown() const;
97 bool exists() const;
98 /// @}
99};
100
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100101/// Represents an open file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100102class File {
103public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100104 /// Destroy the file after closing it (if open).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100105 /// Sub-classes should generally call close() inside their destructors. We
106 /// cannot do that from the base class, since close is virtual.
107 virtual ~File();
108
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100109 /// Get the status of the file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100110 virtual llvm::ErrorOr<Status> status() = 0;
111
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112 /// Get the name of the file
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 virtual llvm::ErrorOr<std::string> getName() {
114 if (auto Status = status())
115 return Status->getName().str();
116 else
117 return Status.getError();
118 }
119
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100120 /// Get the contents of the file as a \p MemoryBuffer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100121 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
122 getBuffer(const Twine &Name, int64_t FileSize = -1,
123 bool RequiresNullTerminator = true, bool IsVolatile = false) = 0;
124
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100125 /// Closes the file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100126 virtual std::error_code close() = 0;
127};
128
Andrew Scull0372a572018-11-16 15:47:06 +0000129/// A member of a directory, yielded by a directory_iterator.
130/// Only information available on most platforms is included.
131class directory_entry {
132 std::string Path;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200133 llvm::sys::fs::file_type Type = llvm::sys::fs::file_type::type_unknown;
Andrew Scull0372a572018-11-16 15:47:06 +0000134
135public:
136 directory_entry() = default;
137 directory_entry(std::string Path, llvm::sys::fs::file_type Type)
138 : Path(std::move(Path)), Type(Type) {}
139
140 llvm::StringRef path() const { return Path; }
141 llvm::sys::fs::file_type type() const { return Type; }
142};
143
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100144namespace detail {
145
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100146/// An interface for virtual file systems to provide an iterator over the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147/// (non-recursive) contents of a directory.
148struct DirIterImpl {
149 virtual ~DirIterImpl();
150
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100151 /// Sets \c CurrentEntry to the next entry in the directory on success,
Andrew Scull0372a572018-11-16 15:47:06 +0000152 /// to directory_entry() at end, or returns a system-defined \c error_code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100153 virtual std::error_code increment() = 0;
154
Andrew Scull0372a572018-11-16 15:47:06 +0000155 directory_entry CurrentEntry;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156};
157
158} // namespace detail
159
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100160/// An input iterator over the entries in a virtual path, similar to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100161/// llvm::sys::fs::directory_iterator.
162class directory_iterator {
163 std::shared_ptr<detail::DirIterImpl> Impl; // Input iterator semantics on copy
164
165public:
166 directory_iterator(std::shared_ptr<detail::DirIterImpl> I)
167 : Impl(std::move(I)) {
168 assert(Impl.get() != nullptr && "requires non-null implementation");
Andrew Scull0372a572018-11-16 15:47:06 +0000169 if (Impl->CurrentEntry.path().empty())
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 Impl.reset(); // Normalize the end iterator to Impl == nullptr.
171 }
172
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100173 /// Construct an 'end' iterator.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100174 directory_iterator() = default;
175
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100176 /// Equivalent to operator++, with an error code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100177 directory_iterator &increment(std::error_code &EC) {
178 assert(Impl && "attempting to increment past end");
179 EC = Impl->increment();
Andrew Scull0372a572018-11-16 15:47:06 +0000180 if (Impl->CurrentEntry.path().empty())
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100181 Impl.reset(); // Normalize the end iterator to Impl == nullptr.
182 return *this;
183 }
184
Andrew Scull0372a572018-11-16 15:47:06 +0000185 const directory_entry &operator*() const { return Impl->CurrentEntry; }
186 const directory_entry *operator->() const { return &Impl->CurrentEntry; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187
188 bool operator==(const directory_iterator &RHS) const {
189 if (Impl && RHS.Impl)
Andrew Scull0372a572018-11-16 15:47:06 +0000190 return Impl->CurrentEntry.path() == RHS.Impl->CurrentEntry.path();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100191 return !Impl && !RHS.Impl;
192 }
193 bool operator!=(const directory_iterator &RHS) const {
194 return !(*this == RHS);
195 }
196};
197
198class FileSystem;
199
Andrew Walbran16937d02019-10-22 13:54:20 +0100200namespace detail {
201
202/// Keeps state for the recursive_directory_iterator.
203struct RecDirIterState {
204 std::stack<directory_iterator, std::vector<directory_iterator>> Stack;
205 bool HasNoPushRequest = false;
206};
207
208} // end namespace detail
209
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100210/// An input iterator over the recursive contents of a virtual path,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100211/// similar to llvm::sys::fs::recursive_directory_iterator.
212class recursive_directory_iterator {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100213 FileSystem *FS;
Andrew Walbran16937d02019-10-22 13:54:20 +0100214 std::shared_ptr<detail::RecDirIterState>
215 State; // Input iterator semantics on copy.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100216
217public:
218 recursive_directory_iterator(FileSystem &FS, const Twine &Path,
219 std::error_code &EC);
220
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100221 /// Construct an 'end' iterator.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100222 recursive_directory_iterator() = default;
223
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100224 /// Equivalent to operator++, with an error code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100225 recursive_directory_iterator &increment(std::error_code &EC);
226
Andrew Walbran16937d02019-10-22 13:54:20 +0100227 const directory_entry &operator*() const { return *State->Stack.top(); }
228 const directory_entry *operator->() const { return &*State->Stack.top(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229
230 bool operator==(const recursive_directory_iterator &Other) const {
231 return State == Other.State; // identity
232 }
233 bool operator!=(const recursive_directory_iterator &RHS) const {
234 return !(*this == RHS);
235 }
236
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100237 /// Gets the current level. Starting path is at level 0.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100238 int level() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100239 assert(!State->Stack.empty() &&
240 "Cannot get level without any iteration state");
241 return State->Stack.size() - 1;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100242 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100243
244 void no_push() { State->HasNoPushRequest = true; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100245};
246
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100247/// The virtual file system interface.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100248class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> {
249public:
250 virtual ~FileSystem();
251
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100252 /// Get the status of the entry at \p Path, if one exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100253 virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
254
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100255 /// Get a \p File object for the file at \p Path, if one exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100256 virtual llvm::ErrorOr<std::unique_ptr<File>>
257 openFileForRead(const Twine &Path) = 0;
258
259 /// This is a convenience method that opens a file, gets its content and then
260 /// closes the file.
261 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
262 getBufferForFile(const Twine &Name, int64_t FileSize = -1,
263 bool RequiresNullTerminator = true, bool IsVolatile = false);
264
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100265 /// Get a directory_iterator for \p Dir.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100266 /// \note The 'end' iterator is directory_iterator().
267 virtual directory_iterator dir_begin(const Twine &Dir,
268 std::error_code &EC) = 0;
269
270 /// Set the working directory. This will affect all following operations on
271 /// this file system and may propagate down for nested file systems.
272 virtual std::error_code setCurrentWorkingDirectory(const Twine &Path) = 0;
273
274 /// Get the working directory of this file system.
275 virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const = 0;
276
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100277 /// Gets real path of \p Path e.g. collapse all . and .. patterns, resolve
278 /// symlinks. For real file system, this uses `llvm::sys::fs::real_path`.
279 /// This returns errc::operation_not_permitted if not implemented by subclass.
280 virtual std::error_code getRealPath(const Twine &Path,
281 SmallVectorImpl<char> &Output) const;
282
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100283 /// Check whether a file exists. Provided for convenience.
284 bool exists(const Twine &Path);
285
Andrew Walbran16937d02019-10-22 13:54:20 +0100286 /// Is the file mounted on a local filesystem?
287 virtual std::error_code isLocal(const Twine &Path, bool &Result);
288
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100289 /// Make \a Path an absolute path.
290 ///
291 /// Makes \a Path absolute using the current directory if it is not already.
292 /// An empty \a Path will result in the current directory.
293 ///
294 /// /absolute/path => /absolute/path
295 /// relative/../path => <current-directory>/relative/../path
296 ///
297 /// \param Path A path that is modified to be an absolute path.
298 /// \returns success if \a path has been made absolute, otherwise a
299 /// platform-specific error_code.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200300 virtual std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100301};
302
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100303/// Gets an \p vfs::FileSystem for the 'real' file system, as seen by
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100304/// the operating system.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100305/// The working directory is linked to the process's working directory.
306/// (This is usually thread-hostile).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100307IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
308
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100309/// Create an \p vfs::FileSystem for the 'real' file system, as seen by
310/// the operating system.
311/// It has its own working directory, independent of (but initially equal to)
312/// that of the process.
313std::unique_ptr<FileSystem> createPhysicalFileSystem();
314
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100315/// A file system that allows overlaying one \p AbstractFileSystem on top
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100316/// of another.
317///
318/// Consists of a stack of >=1 \p FileSystem objects, which are treated as being
319/// one merged file system. When there is a directory that exists in more than
320/// one file system, the \p OverlayFileSystem contains a directory containing
321/// the union of their contents. The attributes (permissions, etc.) of the
322/// top-most (most recently added) directory are used. When there is a file
323/// that exists in more than one file system, the file in the top-most file
324/// system overrides the other(s).
325class OverlayFileSystem : public FileSystem {
326 using FileSystemList = SmallVector<IntrusiveRefCntPtr<FileSystem>, 1>;
327
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100328 /// The stack of file systems, implemented as a list in order of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100329 /// their addition.
330 FileSystemList FSList;
331
332public:
333 OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base);
334
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100335 /// Pushes a file system on top of the stack.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100336 void pushOverlay(IntrusiveRefCntPtr<FileSystem> FS);
337
338 llvm::ErrorOr<Status> status(const Twine &Path) override;
339 llvm::ErrorOr<std::unique_ptr<File>>
340 openFileForRead(const Twine &Path) override;
341 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
342 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
343 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
Andrew Walbran16937d02019-10-22 13:54:20 +0100344 std::error_code isLocal(const Twine &Path, bool &Result) override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100345 std::error_code getRealPath(const Twine &Path,
346 SmallVectorImpl<char> &Output) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100347
348 using iterator = FileSystemList::reverse_iterator;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100349 using const_iterator = FileSystemList::const_reverse_iterator;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100350 using reverse_iterator = FileSystemList::iterator;
351 using const_reverse_iterator = FileSystemList::const_iterator;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100352
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100353 /// Get an iterator pointing to the most recently added file system.
354 iterator overlays_begin() { return FSList.rbegin(); }
355 const_iterator overlays_begin() const { return FSList.rbegin(); }
356
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100357 /// Get an iterator pointing one-past the least recently added file system.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100358 iterator overlays_end() { return FSList.rend(); }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100359 const_iterator overlays_end() const { return FSList.rend(); }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100360
361 /// Get an iterator pointing to the least recently added file system.
362 reverse_iterator overlays_rbegin() { return FSList.begin(); }
363 const_reverse_iterator overlays_rbegin() const { return FSList.begin(); }
364
365 /// Get an iterator pointing one-past the most recently added file system.
366 reverse_iterator overlays_rend() { return FSList.end(); }
367 const_reverse_iterator overlays_rend() const { return FSList.end(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100368};
369
Andrew Scull0372a572018-11-16 15:47:06 +0000370/// By default, this delegates all calls to the underlying file system. This
371/// is useful when derived file systems want to override some calls and still
372/// proxy other calls.
373class ProxyFileSystem : public FileSystem {
374public:
375 explicit ProxyFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
376 : FS(std::move(FS)) {}
377
378 llvm::ErrorOr<Status> status(const Twine &Path) override {
379 return FS->status(Path);
380 }
381 llvm::ErrorOr<std::unique_ptr<File>>
382 openFileForRead(const Twine &Path) override {
383 return FS->openFileForRead(Path);
384 }
385 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
386 return FS->dir_begin(Dir, EC);
387 }
388 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
389 return FS->getCurrentWorkingDirectory();
390 }
391 std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
392 return FS->setCurrentWorkingDirectory(Path);
393 }
394 std::error_code getRealPath(const Twine &Path,
395 SmallVectorImpl<char> &Output) const override {
396 return FS->getRealPath(Path, Output);
397 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100398 std::error_code isLocal(const Twine &Path, bool &Result) override {
399 return FS->isLocal(Path, Result);
400 }
Andrew Scull0372a572018-11-16 15:47:06 +0000401
402protected:
403 FileSystem &getUnderlyingFS() { return *FS; }
404
405private:
406 IntrusiveRefCntPtr<FileSystem> FS;
Andrew Walbran16937d02019-10-22 13:54:20 +0100407
408 virtual void anchor();
Andrew Scull0372a572018-11-16 15:47:06 +0000409};
410
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100411namespace detail {
412
413class InMemoryDirectory;
Andrew Scull0372a572018-11-16 15:47:06 +0000414class InMemoryFile;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100415
416} // namespace detail
417
418/// An in-memory file system.
419class InMemoryFileSystem : public FileSystem {
420 std::unique_ptr<detail::InMemoryDirectory> Root;
421 std::string WorkingDirectory;
422 bool UseNormalizedPaths = true;
423
Andrew Scull0372a572018-11-16 15:47:06 +0000424 /// If HardLinkTarget is non-null, a hardlink is created to the To path which
425 /// must be a file. If it is null then it adds the file as the public addFile.
426 bool addFile(const Twine &Path, time_t ModificationTime,
427 std::unique_ptr<llvm::MemoryBuffer> Buffer,
428 Optional<uint32_t> User, Optional<uint32_t> Group,
429 Optional<llvm::sys::fs::file_type> Type,
430 Optional<llvm::sys::fs::perms> Perms,
431 const detail::InMemoryFile *HardLinkTarget);
432
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100433public:
434 explicit InMemoryFileSystem(bool UseNormalizedPaths = true);
435 ~InMemoryFileSystem() override;
436
437 /// Add a file containing a buffer or a directory to the VFS with a
438 /// path. The VFS owns the buffer. If present, User, Group, Type
439 /// and Perms apply to the newly-created file or directory.
440 /// \return true if the file or directory was successfully added,
441 /// false if the file or directory already exists in the file system with
442 /// different contents.
443 bool addFile(const Twine &Path, time_t ModificationTime,
444 std::unique_ptr<llvm::MemoryBuffer> Buffer,
445 Optional<uint32_t> User = None, Optional<uint32_t> Group = None,
446 Optional<llvm::sys::fs::file_type> Type = None,
447 Optional<llvm::sys::fs::perms> Perms = None);
448
Andrew Scull0372a572018-11-16 15:47:06 +0000449 /// Add a hard link to a file.
450 /// Here hard links are not intended to be fully equivalent to the classical
451 /// filesystem. Both the hard link and the file share the same buffer and
452 /// status (and thus have the same UniqueID). Because of this there is no way
453 /// to distinguish between the link and the file after the link has been
454 /// added.
455 ///
456 /// The To path must be an existing file or a hardlink. The From file must not
457 /// have been added before. The To Path must not be a directory. The From Node
458 /// is added as a hard link which points to the resolved file of To Node.
459 /// \return true if the above condition is satisfied and hardlink was
460 /// successfully created, false otherwise.
461 bool addHardLink(const Twine &From, const Twine &To);
462
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100463 /// Add a buffer to the VFS with a path. The VFS does not own the buffer.
464 /// If present, User, Group, Type and Perms apply to the newly-created file
465 /// or directory.
466 /// \return true if the file or directory was successfully added,
467 /// false if the file or directory already exists in the file system with
468 /// different contents.
469 bool addFileNoOwn(const Twine &Path, time_t ModificationTime,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200470 const llvm::MemoryBufferRef &Buffer,
471 Optional<uint32_t> User = None,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100472 Optional<uint32_t> Group = None,
473 Optional<llvm::sys::fs::file_type> Type = None,
474 Optional<llvm::sys::fs::perms> Perms = None);
475
476 std::string toString() const;
477
478 /// Return true if this file system normalizes . and .. in paths.
479 bool useNormalizedPaths() const { return UseNormalizedPaths; }
480
481 llvm::ErrorOr<Status> status(const Twine &Path) override;
482 llvm::ErrorOr<std::unique_ptr<File>>
483 openFileForRead(const Twine &Path) override;
484 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
485
486 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
487 return WorkingDirectory;
488 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100489 /// Canonicalizes \p Path by combining with the current working
490 /// directory and normalizing the path (e.g. remove dots). If the current
491 /// working directory is not set, this returns errc::operation_not_permitted.
492 ///
493 /// This doesn't resolve symlinks as they are not supported in in-memory file
494 /// system.
495 std::error_code getRealPath(const Twine &Path,
496 SmallVectorImpl<char> &Output) const override;
Andrew Walbran16937d02019-10-22 13:54:20 +0100497 std::error_code isLocal(const Twine &Path, bool &Result) override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100498 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
499};
500
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100501/// Get a globally unique ID for a virtual file or directory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100502llvm::sys::fs::UniqueID getNextVirtualUniqueID();
503
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100504/// Gets a \p FileSystem for a virtual file system described in YAML
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100505/// format.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200506std::unique_ptr<FileSystem>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100507getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer,
508 llvm::SourceMgr::DiagHandlerTy DiagHandler,
Andrew Scull0372a572018-11-16 15:47:06 +0000509 StringRef YAMLFilePath, void *DiagContext = nullptr,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100510 IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
511
512struct YAMLVFSEntry {
Andrew Scull0372a572018-11-16 15:47:06 +0000513 template <typename T1, typename T2>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200514 YAMLVFSEntry(T1 &&VPath, T2 &&RPath, bool IsDirectory = false)
515 : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)),
516 IsDirectory(IsDirectory) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100517 std::string VPath;
518 std::string RPath;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200519 bool IsDirectory = false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100520};
521
Andrew Walbran16937d02019-10-22 13:54:20 +0100522class VFSFromYamlDirIterImpl;
523class RedirectingFileSystemParser;
524
525/// A virtual file system parsed from a YAML file.
526///
527/// Currently, this class allows creating virtual directories and mapping
528/// virtual file paths to existing external files, available in \c ExternalFS.
529///
530/// The basic structure of the parsed file is:
531/// \verbatim
532/// {
533/// 'version': <version number>,
534/// <optional configuration>
535/// 'roots': [
536/// <directory entries>
537/// ]
538/// }
539/// \endverbatim
540///
541/// All configuration options are optional.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200542/// 'case-sensitive': <boolean, default=(true for Posix, false for Windows)>
Andrew Walbran16937d02019-10-22 13:54:20 +0100543/// 'use-external-names': <boolean, default=true>
544/// 'overlay-relative': <boolean, default=false>
545/// 'fallthrough': <boolean, default=true>
546///
547/// Virtual directories are represented as
548/// \verbatim
549/// {
550/// 'type': 'directory',
551/// 'name': <string>,
552/// 'contents': [ <file or directory entries> ]
553/// }
554/// \endverbatim
555///
556/// The default attributes for virtual directories are:
557/// \verbatim
558/// MTime = now() when created
559/// Perms = 0777
560/// User = Group = 0
561/// Size = 0
562/// UniqueID = unspecified unique value
563/// \endverbatim
564///
565/// Re-mapped files are represented as
566/// \verbatim
567/// {
568/// 'type': 'file',
569/// 'name': <string>,
570/// 'use-external-name': <boolean> # Optional
571/// 'external-contents': <path to external file>
572/// }
573/// \endverbatim
574///
575/// and inherit their attributes from the external contents.
576///
577/// In both cases, the 'name' field may contain multiple path components (e.g.
578/// /path/to/file). However, any directory that contains more than one child
579/// must be uniquely represented by a directory entry.
580class RedirectingFileSystem : public vfs::FileSystem {
581public:
582 enum EntryKind { EK_Directory, EK_File };
583
584 /// A single file or directory in the VFS.
585 class Entry {
586 EntryKind Kind;
587 std::string Name;
588
589 public:
590 Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
591 virtual ~Entry() = default;
592
593 StringRef getName() const { return Name; }
594 EntryKind getKind() const { return Kind; }
595 };
596
597 class RedirectingDirectoryEntry : public Entry {
598 std::vector<std::unique_ptr<Entry>> Contents;
599 Status S;
600
601 public:
602 RedirectingDirectoryEntry(StringRef Name,
603 std::vector<std::unique_ptr<Entry>> Contents,
604 Status S)
605 : Entry(EK_Directory, Name), Contents(std::move(Contents)),
606 S(std::move(S)) {}
607 RedirectingDirectoryEntry(StringRef Name, Status S)
608 : Entry(EK_Directory, Name), S(std::move(S)) {}
609
610 Status getStatus() { return S; }
611
612 void addContent(std::unique_ptr<Entry> Content) {
613 Contents.push_back(std::move(Content));
614 }
615
616 Entry *getLastContent() const { return Contents.back().get(); }
617
618 using iterator = decltype(Contents)::iterator;
619
620 iterator contents_begin() { return Contents.begin(); }
621 iterator contents_end() { return Contents.end(); }
622
623 static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
624 };
625
626 class RedirectingFileEntry : public Entry {
627 public:
628 enum NameKind { NK_NotSet, NK_External, NK_Virtual };
629
630 private:
631 std::string ExternalContentsPath;
632 NameKind UseName;
633
634 public:
635 RedirectingFileEntry(StringRef Name, StringRef ExternalContentsPath,
636 NameKind UseName)
637 : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
638 UseName(UseName) {}
639
640 StringRef getExternalContentsPath() const { return ExternalContentsPath; }
641
642 /// whether to use the external path as the name for this file.
643 bool useExternalName(bool GlobalUseExternalName) const {
644 return UseName == NK_NotSet ? GlobalUseExternalName
645 : (UseName == NK_External);
646 }
647
648 NameKind getUseName() const { return UseName; }
649
650 static bool classof(const Entry *E) { return E->getKind() == EK_File; }
651 };
652
653private:
654 friend class VFSFromYamlDirIterImpl;
655 friend class RedirectingFileSystemParser;
656
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200657 bool shouldUseExternalFS() const {
658 return ExternalFSValidWD && IsFallthrough;
659 }
660
661 // In a RedirectingFileSystem, keys can be specified in Posix or Windows
662 // style (or even a mixture of both), so this comparison helper allows
663 // slashes (representing a root) to match backslashes (and vice versa). Note
664 // that, other than the root, path components should not contain slashes or
665 // backslashes.
666 bool pathComponentMatches(llvm::StringRef lhs, llvm::StringRef rhs) const {
667 if ((CaseSensitive ? lhs.equals(rhs) : lhs.equals_lower(rhs)))
668 return true;
669 return (lhs == "/" && rhs == "\\") || (lhs == "\\" && rhs == "/");
670 }
671
Andrew Walbran16937d02019-10-22 13:54:20 +0100672 /// The root(s) of the virtual file system.
673 std::vector<std::unique_ptr<Entry>> Roots;
674
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200675 /// The current working directory of the file system.
676 std::string WorkingDirectory;
677
678 /// Whether the current working directory is valid for the external FS.
679 bool ExternalFSValidWD = false;
680
Andrew Walbran16937d02019-10-22 13:54:20 +0100681 /// The file system to use for external references.
682 IntrusiveRefCntPtr<FileSystem> ExternalFS;
683
684 /// If IsRelativeOverlay is set, this represents the directory
685 /// path that should be prefixed to each 'external-contents' entry
686 /// when reading from YAML files.
687 std::string ExternalContentsPrefixDir;
688
689 /// @name Configuration
690 /// @{
691
692 /// Whether to perform case-sensitive comparisons.
693 ///
694 /// Currently, case-insensitive matching only works correctly with ASCII.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200695 bool CaseSensitive =
696#ifdef _WIN32
697 false;
698#else
699 true;
700#endif
Andrew Walbran16937d02019-10-22 13:54:20 +0100701
702 /// IsRelativeOverlay marks whether a ExternalContentsPrefixDir path must
703 /// be prefixed in every 'external-contents' when reading from YAML files.
704 bool IsRelativeOverlay = false;
705
706 /// Whether to use to use the value of 'external-contents' for the
707 /// names of files. This global value is overridable on a per-file basis.
708 bool UseExternalNames = true;
709
710 /// Whether to attempt a file lookup in external file system after it wasn't
711 /// found in VFS.
712 bool IsFallthrough = true;
713 /// @}
714
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200715 RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS);
Andrew Walbran16937d02019-10-22 13:54:20 +0100716
717 /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly
718 /// recursing into the contents of \p From if it is a directory.
719 ErrorOr<Entry *> lookupPath(llvm::sys::path::const_iterator Start,
720 llvm::sys::path::const_iterator End,
721 Entry *From) const;
722
723 /// Get the status of a given an \c Entry.
724 ErrorOr<Status> status(const Twine &Path, Entry *E);
725
726public:
727 /// Looks up \p Path in \c Roots.
728 ErrorOr<Entry *> lookupPath(const Twine &Path) const;
729
730 /// Parses \p Buffer, which is expected to be in YAML format and
731 /// returns a virtual file system representing its contents.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200732 static std::unique_ptr<RedirectingFileSystem>
Andrew Walbran16937d02019-10-22 13:54:20 +0100733 create(std::unique_ptr<MemoryBuffer> Buffer,
734 SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
735 void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
736
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200737 /// Redirect each of the remapped files from first to second.
738 static std::unique_ptr<RedirectingFileSystem>
739 create(ArrayRef<std::pair<std::string, std::string>> RemappedFiles,
740 bool UseExternalNames, FileSystem &ExternalFS);
741
Andrew Walbran16937d02019-10-22 13:54:20 +0100742 ErrorOr<Status> status(const Twine &Path) override;
743 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
744
745 std::error_code getRealPath(const Twine &Path,
746 SmallVectorImpl<char> &Output) const override;
747
748 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
749
750 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
751
752 std::error_code isLocal(const Twine &Path, bool &Result) override;
753
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200754 std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const override;
755
Andrew Walbran16937d02019-10-22 13:54:20 +0100756 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
757
758 void setExternalContentsPrefixDir(StringRef PrefixDir);
759
760 StringRef getExternalContentsPrefixDir() const;
761
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200762 void setFallthrough(bool Fallthrough);
763
764 std::vector<llvm::StringRef> getRoots() const;
765
766 void dump(raw_ostream &OS) const;
767 void dumpEntry(raw_ostream &OS, Entry *E, int NumSpaces = 0) const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100768#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
769 LLVM_DUMP_METHOD void dump() const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100770#endif
771};
772
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100773/// Collect all pairs of <virtual path, real path> entries from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100774/// \p YAMLFilePath. This is used by the module dependency collector to forward
775/// the entries into the reproducer output VFS YAML file.
776void collectVFSFromYAML(
777 std::unique_ptr<llvm::MemoryBuffer> Buffer,
778 llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
779 SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
780 void *DiagContext = nullptr,
781 IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
782
783class YAMLVFSWriter {
784 std::vector<YAMLVFSEntry> Mappings;
785 Optional<bool> IsCaseSensitive;
786 Optional<bool> IsOverlayRelative;
787 Optional<bool> UseExternalNames;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100788 std::string OverlayDir;
789
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200790 void addEntry(StringRef VirtualPath, StringRef RealPath, bool IsDirectory);
791
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100792public:
793 YAMLVFSWriter() = default;
794
795 void addFileMapping(StringRef VirtualPath, StringRef RealPath);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200796 void addDirectoryMapping(StringRef VirtualPath, StringRef RealPath);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100797
798 void setCaseSensitivity(bool CaseSensitive) {
799 IsCaseSensitive = CaseSensitive;
800 }
801
Andrew Scull0372a572018-11-16 15:47:06 +0000802 void setUseExternalNames(bool UseExtNames) { UseExternalNames = UseExtNames; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100803
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100804 void setOverlayDir(StringRef OverlayDirectory) {
805 IsOverlayRelative = true;
806 OverlayDir.assign(OverlayDirectory.str());
807 }
808
Andrew Walbran16937d02019-10-22 13:54:20 +0100809 const std::vector<YAMLVFSEntry> &getMappings() const { return Mappings; }
810
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100811 void write(llvm::raw_ostream &OS);
812};
813
814} // namespace vfs
Andrew Scull0372a572018-11-16 15:47:06 +0000815} // namespace llvm
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100816
Andrew Scull0372a572018-11-16 15:47:06 +0000817#endif // LLVM_SUPPORT_VIRTUALFILESYSTEM_H