File: PrefixMappingFileSystem.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (108 lines) | stat: -rw-r--r-- 3,970 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===- PrefixMappingFileSystem.cpp - --------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/PrefixMappingFileSystem.h"
#include "llvm/CAS/CASReference.h"
#include "llvm/Support/PrefixMapper.h"
#include "llvm/Support/VirtualFileSystem.h"

using namespace llvm;
using namespace llvm::vfs;

namespace {

class PrefixMappingFile : public vfs::File {
  std::unique_ptr<vfs::File> Underlying;

public:
  PrefixMappingFile(std::unique_ptr<vfs::File> F) : Underlying(std::move(F)) {}

  llvm::ErrorOr<Status> status() final {
    auto S = Underlying->status();
    if (S)
      S->ExposesExternalVFSPath = true;
    return S;
  }
  llvm::ErrorOr<std::string> getName() final { return Underlying->getName(); }
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
            bool IsVolatile) final {
    return Underlying->getBuffer(Name, FileSize, RequiresNullTerminator,
                                 IsVolatile);
  }
  llvm::ErrorOr<std::optional<cas::ObjectRef>> getObjectRefForContent() final {
    return Underlying->getObjectRefForContent();
  }
  std::error_code close() final { return Underlying->close(); }
};

class PrefixMappingFileSystem final : public ProxyFileSystem {
  mutable PrefixMapper Mapper;

public:
  PrefixMappingFileSystem(PrefixMapper Mapper,
                          IntrusiveRefCntPtr<FileSystem> UnderlyingFS)
      : ProxyFileSystem(std::move(UnderlyingFS)), Mapper(std::move(Mapper)) {}

#define PREFIX_MAP_PATH(Old, New)                                              \
  SmallString<256> New;                                                        \
  Old.toVector(New);                                                           \
  bool DidMap = Mapper.mapInPlace(New);                                        \
  (void)DidMap; // Prevent unused variable warnings.

  llvm::ErrorOr<Status> status(const Twine &Path) override {
    PREFIX_MAP_PATH(Path, MappedPath)
    auto S = ProxyFileSystem::status(MappedPath);
    // If we remapped the path, indicate the external path.
    if (DidMap && S)
      S->ExposesExternalVFSPath = true;
    return S;
  }

  llvm::ErrorOr<std::unique_ptr<File>>
  openFileForRead(const Twine &Path) override {
    PREFIX_MAP_PATH(Path, MappedPath)
    auto F = ProxyFileSystem::openFileForRead(MappedPath);
    // If we remapped the path, indicate the external path.
    if (DidMap && F)
      F = std::make_unique<PrefixMappingFile>(std::move(F.get()));
    return F;
  }

  directory_iterator dir_begin(const Twine &Path,
                               std::error_code &EC) override {
    SmallString<256> MappedPath;
    Path.toVector(MappedPath);
    Mapper.mapInPlace(MappedPath);
    return ProxyFileSystem::dir_begin(MappedPath, EC);
  }

  std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
    PREFIX_MAP_PATH(Path, MappedPath)
    return ProxyFileSystem::setCurrentWorkingDirectory(MappedPath);
  }

  std::error_code getRealPath(const Twine &Path,
                              SmallVectorImpl<char> &Output) override {
    PREFIX_MAP_PATH(Path, MappedPath)
    return ProxyFileSystem::getRealPath(MappedPath, Output);
  }

  std::error_code isLocal(const Twine &Path, bool &Result) override {
    PREFIX_MAP_PATH(Path, MappedPath)
    return ProxyFileSystem::isLocal(MappedPath, Result);
  }
};

} // namespace

std::unique_ptr<FileSystem> vfs::createPrefixMappingFileSystem(
    PrefixMapper Mapper, IntrusiveRefCntPtr<FileSystem> UnderlyingFS) {
  return std::make_unique<PrefixMappingFileSystem>(std::move(Mapper),
                                                   std::move(UnderlyingFS));
}