Line data Source code
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
11 : /// Defines the virtual file system interface vfs::FileSystem.
12 : //
13 : //===----------------------------------------------------------------------===//
14 :
15 : #ifndef LLVM_SUPPORT_VIRTUALFILESYSTEM_H
16 : #define LLVM_SUPPORT_VIRTUALFILESYSTEM_H
17 :
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 :
42 : namespace vfs {
43 :
44 : /// The result of a \p status operation.
45 2331515 : 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:
56 : // FIXME: remove when files support multiple names
57 : bool IsVFSMapped = false;
58 :
59 2629 : 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 :
71 : /// Returns the name that should be used for this file or directory.
72 : StringRef getName() const { return Name; }
73 :
74 : /// @name Status interface from llvm::sys::fs
75 : /// @{
76 0 : llvm::sys::fs::file_type getType() const { return Type; }
77 0 : llvm::sys::fs::perms getPermissions() const { return Perms; }
78 0 : llvm::sys::TimePoint<> getLastModificationTime() const { return MTime; }
79 0 : llvm::sys::fs::UniqueID getUniqueID() const { return UID; }
80 0 : uint32_t getUser() const { return User; }
81 0 : uint32_t getGroup() const { return Group; }
82 0 : 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 :
97 : /// Represents an open file.
98 852443 : class File {
99 : public:
100 : /// Destroy the file after closing it (if open).
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 :
105 : /// Get the status of the file.
106 : virtual llvm::ErrorOr<Status> status() = 0;
107 :
108 : /// Get the name of the file
109 5511 : virtual llvm::ErrorOr<std::string> getName() {
110 5511 : if (auto Status = status())
111 5511 : return Status->getName().str();
112 : else
113 0 : return Status.getError();
114 : }
115 :
116 : /// Get the contents of the file as a \p MemoryBuffer.
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 :
121 : /// Closes the file.
122 : virtual std::error_code close() = 0;
123 : };
124 :
125 : /// A member of a directory, yielded by a directory_iterator.
126 : /// Only information available on most platforms is included.
127 1067407 : 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 93162 : : Path(std::move(Path)), Type(Type) {}
135 :
136 : llvm::StringRef path() const { return Path; }
137 0 : llvm::sys::fs::file_type type() const { return Type; }
138 : };
139 :
140 : namespace detail {
141 :
142 : /// An interface for virtual file systems to provide an iterator over the
143 : /// (non-recursive) contents of a directory.
144 6266588 : struct DirIterImpl {
145 : virtual ~DirIterImpl();
146 :
147 : /// Sets \c CurrentEntry to the next entry in the directory on success,
148 : /// to directory_entry() at end, or returns a system-defined \c error_code.
149 : virtual std::error_code increment() = 0;
150 :
151 : directory_entry CurrentEntry;
152 : };
153 :
154 : } // namespace detail
155 :
156 : /// An input iterator over the entries in a virtual path, similar to
157 : /// llvm::sys::fs::directory_iterator.
158 4320355 : class directory_iterator {
159 : std::shared_ptr<detail::DirIterImpl> Impl; // Input iterator semantics on copy
160 :
161 : public:
162 6266152 : directory_iterator(std::shared_ptr<detail::DirIterImpl> I)
163 : : Impl(std::move(I)) {
164 : assert(Impl.get() != nullptr && "requires non-null implementation");
165 6266152 : if (Impl->CurrentEntry.path().empty())
166 : Impl.reset(); // Normalize the end iterator to Impl == nullptr.
167 6266433 : }
168 :
169 : /// Construct an 'end' iterator.
170 : directory_iterator() = default;
171 :
172 : /// Equivalent to operator++, with an error code.
173 53801 : directory_iterator &increment(std::error_code &EC) {
174 : assert(Impl && "attempting to increment past end");
175 53801 : EC = Impl->increment();
176 53800 : if (Impl->CurrentEntry.path().empty())
177 : Impl.reset(); // Normalize the end iterator to Impl == nullptr.
178 53801 : return *this;
179 : }
180 :
181 : const directory_entry &operator*() const { return Impl->CurrentEntry; }
182 : const directory_entry *operator->() const { return &Impl->CurrentEntry; }
183 :
184 3986696 : bool operator==(const directory_iterator &RHS) const {
185 3986696 : if (Impl && RHS.Impl)
186 : return Impl->CurrentEntry.path() == RHS.Impl->CurrentEntry.path();
187 3986696 : return !Impl && !RHS.Impl;
188 : }
189 : bool operator!=(const directory_iterator &RHS) const {
190 2023475 : return !(*this == RHS);
191 : }
192 : };
193 :
194 : class FileSystem;
195 :
196 : /// An input iterator over the recursive contents of a virtual path,
197 : /// similar to llvm::sys::fs::recursive_directory_iterator.
198 3 : 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 :
209 : /// Construct an 'end' iterator.
210 : recursive_directory_iterator() = default;
211 :
212 : /// Equivalent to operator++, with an error code.
213 : recursive_directory_iterator &increment(std::error_code &EC);
214 :
215 : const directory_entry &operator*() const { return *State->top(); }
216 : const directory_entry *operator->() const { return &*State->top(); }
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 :
225 : /// Gets the current level. Starting path is at level 0.
226 : int level() const {
227 : assert(!State->empty() && "Cannot get level without any iteration state");
228 4 : return State->size() - 1;
229 : }
230 : };
231 :
232 : /// The virtual file system interface.
233 101811 : class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> {
234 : public:
235 : virtual ~FileSystem();
236 :
237 : /// Get the status of the entry at \p Path, if one exists.
238 : virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
239 :
240 : /// Get a \p File object for the file at \p Path, if one exists.
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 :
250 : /// Get a directory_iterator for \p Dir.
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 :
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 :
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 :
285 : /// Gets an \p vfs::FileSystem for the 'real' file system, as seen by
286 : /// the operating system.
287 : IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
288 :
289 : /// A file system that allows overlaying one \p AbstractFileSystem on top
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 :
302 : /// The stack of file systems, implemented as a list in order of
303 : /// their addition.
304 : FileSystemList FSList;
305 :
306 : public:
307 : OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base);
308 :
309 : /// Pushes a file system on top of the stack.
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;
318 : std::error_code getRealPath(const Twine &Path,
319 : SmallVectorImpl<char> &Output) const override;
320 :
321 : using iterator = FileSystemList::reverse_iterator;
322 : using const_iterator = FileSystemList::const_reverse_iterator;
323 :
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
329 : /// system.
330 980504 : iterator overlays_end() { return FSList.rend(); }
331 : const_iterator overlays_end() const { return FSList.rend(); }
332 : };
333 :
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 81 : : FS(std::move(FS)) {}
341 :
342 0 : llvm::ErrorOr<Status> status(const Twine &Path) override {
343 0 : return FS->status(Path);
344 : }
345 : llvm::ErrorOr<std::unique_ptr<File>>
346 568 : openFileForRead(const Twine &Path) override {
347 568 : return FS->openFileForRead(Path);
348 : }
349 4095 : directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
350 4095 : return FS->dir_begin(Dir, EC);
351 : }
352 78 : llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
353 78 : return FS->getCurrentWorkingDirectory();
354 : }
355 36 : std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
356 36 : return FS->setCurrentWorkingDirectory(Path);
357 : }
358 70 : std::error_code getRealPath(const Twine &Path,
359 : SmallVectorImpl<char> &Output) const override {
360 70 : return FS->getRealPath(Path, Output);
361 : }
362 :
363 : protected:
364 1508 : FileSystem &getUnderlyingFS() { return *FS; }
365 :
366 : private:
367 : IntrusiveRefCntPtr<FileSystem> FS;
368 : };
369 :
370 : namespace detail {
371 :
372 : class InMemoryDirectory;
373 : class InMemoryFile;
374 :
375 : } // namespace detail
376 :
377 : /// An in-memory file system.
378 153482 : class InMemoryFileSystem : public FileSystem {
379 : std::unique_ptr<detail::InMemoryDirectory> Root;
380 : std::string WorkingDirectory;
381 : bool UseNormalizedPaths = true;
382 :
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 :
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 :
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 :
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,
429 : llvm::MemoryBuffer *Buffer, Optional<uint32_t> User = None,
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 0 : 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 229665 : llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
445 229652 : return WorkingDirectory;
446 : }
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;
455 :
456 : std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
457 : };
458 :
459 : /// Get a globally unique ID for a virtual file or directory.
460 : llvm::sys::fs::UniqueID getNextVirtualUniqueID();
461 :
462 : /// Gets a \p FileSystem for a virtual file system described in YAML
463 : /// format.
464 : IntrusiveRefCntPtr<FileSystem>
465 : getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer,
466 : llvm::SourceMgr::DiagHandlerTy DiagHandler,
467 : StringRef YAMLFilePath, void *DiagContext = nullptr,
468 : IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
469 :
470 1129 : struct YAMLVFSEntry {
471 : template <typename T1, typename T2>
472 321 : YAMLVFSEntry(T1 &&VPath, T2 &&RPath)
473 321 : : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)) {}
474 321 : std::string VPath;
475 321 : std::string RPath;
476 0 : };
477 0 :
478 : /// Collect all pairs of <virtual path, real path> entries from the
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 :
505 : void setUseExternalNames(bool UseExtNames) { UseExternalNames = UseExtNames; }
506 :
507 : void setIgnoreNonExistentContents(bool IgnoreContents) {
508 : IgnoreNonExistentContents = IgnoreContents;
509 : }
510 :
511 9 : void setOverlayDir(StringRef OverlayDirectory) {
512 : IsOverlayRelative = true;
513 9 : OverlayDir.assign(OverlayDirectory.str());
514 9 : }
515 :
516 : void write(llvm::raw_ostream &OS);
517 : };
518 :
519 : } // namespace vfs
520 : } // namespace llvm
521 :
522 : #endif // LLVM_SUPPORT_VIRTUALFILESYSTEM_H
|