FileCollector.h 4.8 KB

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