FileCollector.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef LLVM_SUPPORT_FILECOLLECTOR_H
  9. #define LLVM_SUPPORT_FILECOLLECTOR_H
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/StringMap.h"
  12. #include "llvm/ADT/StringSet.h"
  13. #include "llvm/Support/VirtualFileSystem.h"
  14. #include <mutex>
  15. #include <string>
  16. namespace llvm {
  17. class FileCollectorFileSystem;
  18. class Twine;
  19. class FileCollectorBase {
  20. public:
  21. FileCollectorBase();
  22. virtual ~FileCollectorBase();
  23. void addFile(const Twine &file);
  24. void addDirectory(const Twine &Dir);
  25. protected:
  26. bool markAsSeen(StringRef Path) {
  27. if (Path.empty())
  28. return false;
  29. return Seen.insert(Path).second;
  30. }
  31. virtual void addFileImpl(StringRef SrcPath) = 0;
  32. virtual llvm::vfs::directory_iterator
  33. addDirectoryImpl(const llvm::Twine &Dir,
  34. IntrusiveRefCntPtr<vfs::FileSystem> FS,
  35. std::error_code &EC) = 0;
  36. /// Synchronizes access to internal data structures.
  37. std::mutex Mutex;
  38. /// Tracks already seen files so they can be skipped.
  39. StringSet<> Seen;
  40. };
  41. /// Captures file system interaction and generates data to be later replayed
  42. /// with the RedirectingFileSystem.
  43. ///
  44. /// For any file that gets accessed we eventually create:
  45. /// - a copy of the file inside Root
  46. /// - a record in RedirectingFileSystem mapping that maps:
  47. /// current real path -> path to the copy in Root
  48. ///
  49. /// That intent is that later when the mapping is used by RedirectingFileSystem
  50. /// it simulates the state of FS that we collected.
  51. ///
  52. /// We generate file copies and mapping lazily - see writeMapping and copyFiles.
  53. /// We don't try to capture the state of the file at the exact time when it's
  54. /// accessed. Files might get changed, deleted ... we record only the "final"
  55. /// state.
  56. ///
  57. /// In order to preserve the relative topology of files we use their real paths
  58. /// as relative paths inside of the Root.
  59. class FileCollector : public FileCollectorBase {
  60. public:
  61. /// Helper utility that encapsulates the logic for canonicalizing a virtual
  62. /// path and a path to copy from.
  63. class PathCanonicalizer {
  64. public:
  65. struct PathStorage {
  66. SmallString<256> CopyFrom;
  67. SmallString<256> VirtualPath;
  68. };
  69. /// Canonicalize a pair of virtual and real paths.
  70. PathStorage canonicalize(StringRef SrcPath);
  71. private:
  72. /// Replace with a (mostly) real path, or don't modify. Resolves symlinks
  73. /// in the directory, using \a CachedDirs to avoid redundant lookups, but
  74. /// leaves the filename as a possible symlink.
  75. void updateWithRealPath(SmallVectorImpl<char> &Path);
  76. StringMap<std::string> CachedDirs;
  77. };
  78. /// \p Root is the directory where collected files are will be stored.
  79. /// \p OverlayRoot is VFS mapping root.
  80. /// \p Root directory gets created in copyFiles unless it already exists.
  81. FileCollector(std::string Root, std::string OverlayRoot);
  82. /// Write the yaml mapping (for the VFS) to the given file.
  83. std::error_code writeMapping(StringRef MappingFile);
  84. /// Copy the files into the root directory.
  85. ///
  86. /// When StopOnError is true (the default) we abort as soon as one file
  87. /// cannot be copied. This is relatively common, for example when a file was
  88. /// removed after it was added to the mapping.
  89. std::error_code copyFiles(bool StopOnError = true);
  90. /// Create a VFS that uses \p Collector to collect files accessed via \p
  91. /// BaseFS.
  92. static IntrusiveRefCntPtr<vfs::FileSystem>
  93. createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
  94. std::shared_ptr<FileCollector> Collector);
  95. private:
  96. friend FileCollectorFileSystem;
  97. void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
  98. if (sys::fs::is_directory(VirtualPath))
  99. VFSWriter.addDirectoryMapping(VirtualPath, RealPath);
  100. else
  101. VFSWriter.addFileMapping(VirtualPath, RealPath);
  102. }
  103. protected:
  104. void addFileImpl(StringRef SrcPath) override;
  105. llvm::vfs::directory_iterator
  106. addDirectoryImpl(const llvm::Twine &Dir,
  107. IntrusiveRefCntPtr<vfs::FileSystem> FS,
  108. std::error_code &EC) override;
  109. /// The directory where collected files are copied to in copyFiles().
  110. const std::string Root;
  111. /// The root directory where the VFS overlay lives.
  112. const std::string OverlayRoot;
  113. /// The yaml mapping writer.
  114. vfs::YAMLVFSWriter VFSWriter;
  115. /// Helper utility for canonicalizing paths.
  116. PathCanonicalizer Canonicalizer;
  117. };
  118. } // end namespace llvm
  119. #endif // LLVM_SUPPORT_FILECOLLECTOR_H