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/ADT/Twine.h" 16#include "llvm/Support/VirtualFileSystem.h" 17 18#include <mutex> 19 20namespace llvm { 21 22/// Collects files into a directory and generates a mapping that can be used by 23/// the VFS. 24class FileCollector { 25public: 26 FileCollector(std::string Root, std::string OverlayRoot); 27 28 void addFile(const Twine &file); 29 30 /// Write the yaml mapping (for the VFS) to the given file. 31 std::error_code writeMapping(StringRef mapping_file); 32 33 /// Copy the files into the root directory. 34 /// 35 /// When StopOnError is true (the default) we abort as soon as one file 36 /// cannot be copied. This is relatively common, for example when a file was 37 /// removed after it was added to the mapping. 38 std::error_code copyFiles(bool StopOnError = true); 39 40 /// Create a VFS that collects all the paths that might be looked at by the 41 /// file system accesses. 42 static IntrusiveRefCntPtr<vfs::FileSystem> 43 createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS, 44 std::shared_ptr<FileCollector> Collector); 45 46private: 47 void addFileImpl(StringRef SrcPath); 48 49 bool markAsSeen(StringRef Path) { 50 if (Path.empty()) 51 return false; 52 return Seen.insert(Path).second; 53 } 54 55 bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result); 56 57 void addFileToMapping(StringRef VirtualPath, StringRef RealPath) { 58 VFSWriter.addFileMapping(VirtualPath, RealPath); 59 } 60 61protected: 62 /// Synchronizes adding files. 63 std::mutex Mutex; 64 65 /// The root directory where files are copied. 66 std::string Root; 67 68 /// The root directory where the VFS overlay lives. 69 std::string OverlayRoot; 70 71 /// Tracks already seen files so they can be skipped. 72 StringSet<> Seen; 73 74 /// The yaml mapping writer. 75 vfs::YAMLVFSWriter VFSWriter; 76 77 /// Caches RealPath calls when resolving symlinks. 78 StringMap<std::string> SymlinkMap; 79}; 80 81} // end namespace llvm 82 83#endif // LLVM_SUPPORT_FILE_COLLECTOR_H 84