LLVM  11.0.0git
FileCollector.h
Go to the documentation of this file.
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"
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.
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.
65  std::shared_ptr<FileCollector> Collector);
66 
67 private:
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 
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.
103 
104  /// The yaml mapping writer.
106 
107  /// Caches RealPath calls when resolving symlinks.
109 };
110 
111 } // end namespace llvm
112 
113 #endif // LLVM_SUPPORT_FILE_COLLECTOR_H
*ViewGraph Emit a dot run run gv on the postscript file
Definition: GraphWriter.h:372
const std::string OverlayRoot
The root directory where the VFS overlay lives.
Definition: FileCollector.h:99
This class represents lattice values for constants.
Definition: AllocatorList.h:23
StringMap< std::string > SymlinkMap
Caches RealPath calls when resolving symlinks.
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1078
void addDirectoryMapping(StringRef VirtualPath, StringRef RealPath)
std::error_code copyFiles(bool StopOnError=true)
Copy the files into the root directory.
Captures file system interaction and generates data to be later replayed with the RedirectingFileSyst...
Definition: FileCollector.h:41
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:156
void addFileImpl(StringRef SrcPath)
llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, IntrusiveRefCntPtr< vfs::FileSystem > FS, std::error_code &EC)
StringSet Seen
Tracks already seen files so they can be skipped.
FileCollector(std::string Root, std::string OverlayRoot)
Root is the directory where collected files are will be stored.
void addFileMapping(StringRef VirtualPath, StringRef RealPath)
vfs::YAMLVFSWriter VFSWriter
The yaml mapping writer.
void addFile(const Twine &file)
std::error_code writeMapping(StringRef MappingFile)
Write the yaml mapping (for the VFS) to the given file.
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:33
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
static IntrusiveRefCntPtr< vfs::FileSystem > createCollectorVFS(IntrusiveRefCntPtr< vfs::FileSystem > BaseFS, std::shared_ptr< FileCollector > Collector)
Create a VFS that uses Collector to collect files accessed via BaseFS.
Defines the virtual file system interface vfs::FileSystem.
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:22
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Register Usage Information Collector
std::mutex Mutex
Synchronizes access to Seen, VFSWriter and SymlinkMap.
Definition: FileCollector.h:93
const std::string Root
The directory where collected files are copied to in copyFiles().
Definition: FileCollector.h:96
void addDirectory(const Twine &Dir)