forked from NixOS/nix
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmounted-source-accessor.cc
More file actions
115 lines (93 loc) · 3.34 KB
/
Copy pathmounted-source-accessor.cc
File metadata and controls
115 lines (93 loc) · 3.34 KB
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
109
110
111
112
113
114
115
#include "nix/util/mounted-source-accessor.hh"
#include <boost/unordered/concurrent_flat_map.hpp>
namespace nix {
struct MountedSourceAccessorImpl : MountedSourceAccessor
{
boost::concurrent_flat_map<CanonPath, ref<SourceAccessor>> mounts;
MountedSourceAccessorImpl(std::map<CanonPath, ref<SourceAccessor>> _mounts)
{
displayPrefix.clear();
// Currently we require a root filesystem. This could be relaxed.
assert(_mounts.contains(CanonPath::root));
for (auto & [path, accessor] : _mounts)
mount(path, accessor);
// FIXME: return dummy parent directories automatically?
}
std::string readFile(const CanonPath & path) override
{
auto [accessor, subpath] = resolve(path);
return accessor->readFile(subpath);
}
Stat lstat(const CanonPath & path) override
{
auto [accessor, subpath] = resolve(path);
return accessor->lstat(subpath);
}
std::optional<Stat> maybeLstat(const CanonPath & path) override
{
auto [accessor, subpath] = resolve(path);
return accessor->maybeLstat(subpath);
}
DirEntries readDirectory(const CanonPath & path) override
{
auto [accessor, subpath] = resolve(path);
return accessor->readDirectory(subpath);
}
std::string readLink(const CanonPath & path) override
{
auto [accessor, subpath] = resolve(path);
return accessor->readLink(subpath);
}
std::string showPath(const CanonPath & path) override
{
auto [accessor, subpath] = resolve(path);
return displayPrefix + accessor->showPath(subpath) + displaySuffix;
}
std::pair<ref<SourceAccessor>, CanonPath> resolve(CanonPath path)
{
// Find the nearest parent of `path` that is a mount point.
std::vector<std::string> subpath;
while (true) {
if (auto mount = getMount(path)) {
std::reverse(subpath.begin(), subpath.end());
return {ref(mount), CanonPath(subpath)};
}
assert(!path.isRoot());
subpath.push_back(std::string(*path.baseName()));
path.pop();
}
}
std::optional<std::filesystem::path> getPhysicalPath(const CanonPath & path) override
{
auto [accessor, subpath] = resolve(path);
return accessor->getPhysicalPath(subpath);
}
void mount(CanonPath mountPoint, ref<SourceAccessor> accessor) override
{
mounts.emplace(std::move(mountPoint), std::move(accessor));
}
std::shared_ptr<SourceAccessor> getMount(CanonPath mountPoint) override
{
if (auto res = getConcurrent(mounts, mountPoint))
return *res;
else
return nullptr;
}
std::pair<CanonPath, std::optional<std::string>> getFingerprint(const CanonPath & path) override
{
if (fingerprint)
return {path, fingerprint};
auto [accessor, subpath] = resolve(path);
return accessor->getFingerprint(subpath);
}
void invalidateCache(const CanonPath & path) override
{
auto [accessor, subpath] = resolve(path);
accessor->invalidateCache(subpath);
}
};
ref<MountedSourceAccessor> makeMountedSourceAccessor(std::map<CanonPath, ref<SourceAccessor>> mounts)
{
return make_ref<MountedSourceAccessorImpl>(std::move(mounts));
}
} // namespace nix