1 //===-- FileCollector.h -----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_SUPPORT_FILE_COLLECTOR_H
10 #define LLVM_SUPPORT_FILE_COLLECTOR_H
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/Support/VirtualFileSystem.h"
20 class FileCollectorFileSystem;
23 /// Captures file system interaction and generates data to be later replayed
24 /// with the RedirectingFileSystem.
26 /// For any file that gets accessed we eventually create:
27 /// - a copy of the file inside Root
28 /// - a record in RedirectingFileSystem mapping that maps:
29 /// current real path -> path to the copy in Root
31 /// That intent is that later when the mapping is used by RedirectingFileSystem
32 /// it simulates the state of FS that we collected.
34 /// We generate file copies and mapping lazily - see writeMapping and copyFiles.
35 /// We don't try to capture the state of the file at the exact time when it's
36 /// accessed. Files might get changed, deleted ... we record only the "final"
39 /// In order to preserve the relative topology of files we use their real paths
40 /// as relative paths inside of the Root.
43 /// \p Root is the directory where collected files are will be stored.
44 /// \p OverlayRoot is VFS mapping root.
45 /// \p Root directory gets created in copyFiles unless it already exists.
46 FileCollector(std::string Root, std::string OverlayRoot);
48 void addFile(const Twine &file);
49 void addDirectory(const Twine &Dir);
51 /// Write the yaml mapping (for the VFS) to the given file.
52 std::error_code writeMapping(StringRef MappingFile);
54 /// Copy the files into the root directory.
56 /// When StopOnError is true (the default) we abort as soon as one file
57 /// cannot be copied. This is relatively common, for example when a file was
58 /// removed after it was added to the mapping.
59 std::error_code copyFiles(bool StopOnError = true);
61 /// Create a VFS that uses \p Collector to collect files accessed via \p
63 static IntrusiveRefCntPtr<vfs::FileSystem>
64 createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
65 std::shared_ptr<FileCollector> Collector);
68 friend FileCollectorFileSystem;
70 bool markAsSeen(StringRef Path) {
73 return Seen.insert(Path).second;
76 bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);
78 void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
79 if (sys::fs::is_directory(VirtualPath))
80 VFSWriter.addDirectoryMapping(VirtualPath, RealPath);
82 VFSWriter.addFileMapping(VirtualPath, RealPath);
86 void addFileImpl(StringRef SrcPath);
88 llvm::vfs::directory_iterator
89 addDirectoryImpl(const llvm::Twine &Dir,
90 IntrusiveRefCntPtr<vfs::FileSystem> FS, std::error_code &EC);
92 /// Synchronizes access to Seen, VFSWriter and SymlinkMap.
95 /// The directory where collected files are copied to in copyFiles().
96 const std::string Root;
98 /// The root directory where the VFS overlay lives.
99 const std::string OverlayRoot;
101 /// Tracks already seen files so they can be skipped.
104 /// The yaml mapping writer.
105 vfs::YAMLVFSWriter VFSWriter;
107 /// Caches RealPath calls when resolving symlinks.
108 StringMap<std::string> SymlinkMap;
111 } // end namespace llvm
113 #endif // LLVM_SUPPORT_FILE_COLLECTOR_H