reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
//===-- FileCollector.h -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_FILE_COLLECTOR_H
#define LLVM_SUPPORT_FILE_COLLECTOR_H

#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/VirtualFileSystem.h"

#include <mutex>

namespace llvm {

/// Collects files into a directory and generates a mapping that can be used by
/// the VFS.
class FileCollector {
public:
  FileCollector(std::string Root, std::string OverlayRoot);

  void addFile(const Twine &file);

  /// Write the yaml mapping (for the VFS) to the given file.
  std::error_code writeMapping(StringRef mapping_file);

  /// Copy the files into the root directory.
  ///
  /// When StopOnError is true (the default) we abort as soon as one file
  /// cannot be copied. This is relatively common, for example when a file was
  /// removed after it was added to the mapping.
  std::error_code copyFiles(bool StopOnError = true);

  /// Create a VFS that collects all the paths that might be looked at by the
  /// file system accesses.
  static IntrusiveRefCntPtr<vfs::FileSystem>
  createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
                     std::shared_ptr<FileCollector> Collector);

private:
  void addFileImpl(StringRef SrcPath);

  bool markAsSeen(StringRef Path) { return Seen.insert(Path).second; }

  bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);

  void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
    VFSWriter.addFileMapping(VirtualPath, RealPath);
  }

protected:
  /// Synchronizes adding files.
  std::mutex Mutex;

  /// The root directory where files are copied.
  std::string Root;

  /// The root directory where the VFS overlay lives.
  std::string OverlayRoot;

  /// Tracks already seen files so they can be skipped.
  StringSet<> Seen;

  /// The yaml mapping writer.
  vfs::YAMLVFSWriter VFSWriter;

  /// Caches RealPath calls when resolving symlinks.
  StringMap<std::string> SymlinkMap;
};

} // end namespace llvm

#endif // LLVM_SUPPORT_FILE_COLLECTOR_H