1 //===-- FileCollector.h -----------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_SUPPORT_FILE_COLLECTOR_H 10 #define LLVM_SUPPORT_FILE_COLLECTOR_H 11 12 #include "llvm/ADT/SmallVector.h" 13 #include "llvm/ADT/StringMap.h" 14 #include "llvm/ADT/StringSet.h" 15 #include "llvm/Support/VirtualFileSystem.h" 16 #include <mutex> 17 #include <string> 18 19 namespace llvm { 20 class FileCollectorFileSystem; 21 class Twine; 22 23 /// Captures file system interaction and generates data to be later replayed 24 /// with the RedirectingFileSystem. 25 /// 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 30 /// 31 /// That intent is that later when the mapping is used by RedirectingFileSystem 32 /// it simulates the state of FS that we collected. 33 /// 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" 37 /// state. 38 /// 39 /// In order to preserve the relative topology of files we use their real paths 40 /// as relative paths inside of the Root. 41 class FileCollector { 42 public: 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); 47 48 void addFile(const Twine &file); 49 void addDirectory(const Twine &Dir); 50 51 /// Write the yaml mapping (for the VFS) to the given file. 52 std::error_code writeMapping(StringRef MappingFile); 53 54 /// Copy the files into the root directory. 55 /// 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); 60 61 /// Create a VFS that uses \p Collector to collect files accessed via \p 62 /// BaseFS. 63 static IntrusiveRefCntPtr<vfs::FileSystem> 64 createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS, 65 std::shared_ptr<FileCollector> Collector); 66 67 private: 68 friend FileCollectorFileSystem; 69 70 bool markAsSeen(StringRef Path) { 71 if (Path.empty()) 72 return false; 73 return Seen.insert(Path).second; 74 } 75 76 bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result); 77 78 void addFileToMapping(StringRef VirtualPath, StringRef RealPath) { 79 if (sys::fs::is_directory(VirtualPath)) 80 VFSWriter.addDirectoryMapping(VirtualPath, RealPath); 81 else 82 VFSWriter.addFileMapping(VirtualPath, RealPath); 83 } 84 85 protected: 86 void addFileImpl(StringRef SrcPath); 87 88 llvm::vfs::directory_iterator 89 addDirectoryImpl(const llvm::Twine &Dir, 90 IntrusiveRefCntPtr<vfs::FileSystem> FS, std::error_code &EC); 91 92 /// Synchronizes access to Seen, VFSWriter and SymlinkMap. 93 std::mutex Mutex; 94 95 /// The directory where collected files are copied to in copyFiles(). 96 const std::string Root; 97 98 /// The root directory where the VFS overlay lives. 99 const std::string OverlayRoot; 100 101 /// Tracks already seen files so they can be skipped. 102 StringSet<> Seen; 103 104 /// The yaml mapping writer. 105 vfs::YAMLVFSWriter VFSWriter; 106 107 /// Caches RealPath calls when resolving symlinks. 108 StringMap<std::string> SymlinkMap; 109 }; 110 111 } // end namespace llvm 112 113 #endif // LLVM_SUPPORT_FILE_COLLECTOR_H 114