aboutsummaryrefslogtreecommitdiffstats
path: root/include/lldb/Utility/FileCollector.h
blob: a89206747fe1920e4493c335a9ea31f1a801c023 (plain) (blame)
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
//===-- 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 LLDB_UTILITY_FILE_COLLECTOR_H
#define LLDB_UTILITY_FILE_COLLECTOR_H

#include "lldb/Utility/FileSpec.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 lldb_private {

/// Collects files into a directory and generates a mapping that can be used by
/// the VFS.
class FileCollector {
public:
  FileCollector(const FileSpec &root, const FileSpec &overlay);

  void AddFile(const llvm::Twine &file);
  void AddFile(const FileSpec &file) { return AddFile(file.GetPath()); }

  /// Write the yaml mapping (for the VFS) to the given file.
  std::error_code WriteMapping(const FileSpec &mapping_file);

  /// Copy the files into the root directory.
  ///
  /// When stop_on_error 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 stop_on_error = true);

protected:
  void AddFileImpl(llvm::StringRef src_path);

  bool MarkAsSeen(llvm::StringRef path) { return m_seen.insert(path).second; }

  bool GetRealPath(llvm::StringRef src_path,
                   llvm::SmallVectorImpl<char> &result);

  void AddFileToMapping(llvm::StringRef virtual_path,
                        llvm::StringRef real_path) {
    m_vfs_writer.addFileMapping(virtual_path, real_path);
  }

  /// Synchronizes adding files.
  std::mutex m_mutex;

  /// The root directory where files are copied.
  FileSpec m_root;

  /// The root directory where the VFS overlay lives.
  FileSpec m_overlay_root;

  /// Tracks already seen files so they can be skipped.
  llvm::StringSet<> m_seen;

  /// The yaml mapping writer.
  llvm::vfs::YAMLVFSWriter m_vfs_writer;

  /// Caches real_path calls when resolving symlinks.
  llvm::StringMap<std::string> m_symlink_map;
};

} // namespace lldb_private

#endif // LLDB_UTILITY_FILE_COLLECTOR_H