2023-11-14 15:01:38 +02:00
|
|
|
#include "mounted-input-accessor.hh"
|
2023-10-27 19:39:00 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-11-14 15:01:38 +02:00
|
|
|
struct MountedInputAccessor : InputAccessor
|
2023-10-27 19:39:00 +03:00
|
|
|
{
|
|
|
|
std::map<CanonPath, ref<InputAccessor>> mounts;
|
|
|
|
|
2023-11-14 15:01:38 +02:00
|
|
|
MountedInputAccessor(std::map<CanonPath, ref<InputAccessor>> _mounts)
|
2023-10-27 19:39:00 +03:00
|
|
|
: mounts(std::move(_mounts))
|
|
|
|
{
|
|
|
|
// Currently we require a root filesystem. This could be relaxed.
|
|
|
|
assert(mounts.contains(CanonPath::root));
|
|
|
|
|
2023-11-14 14:57:24 +02:00
|
|
|
// FIXME: return dummy parent directories automatically?
|
2023-10-27 19:39:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string readFile(const CanonPath & path) override
|
|
|
|
{
|
|
|
|
auto [accessor, subpath] = resolve(path);
|
|
|
|
return accessor->readFile(subpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pathExists(const CanonPath & path) override
|
|
|
|
{
|
|
|
|
auto [accessor, subpath] = resolve(path);
|
|
|
|
return accessor->pathExists(subpath);
|
|
|
|
}
|
|
|
|
|
2023-11-02 17:16:37 +02:00
|
|
|
std::optional<Stat> maybeLstat(const CanonPath & path) override
|
2023-10-27 19:39:00 +03:00
|
|
|
{
|
|
|
|
auto [accessor, subpath] = resolve(path);
|
2023-11-02 17:16:37 +02:00
|
|
|
return accessor->maybeLstat(subpath);
|
2023-10-27 19:39:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 accessor->showPath(subpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<ref<InputAccessor>, CanonPath> resolve(CanonPath path)
|
|
|
|
{
|
|
|
|
// Find the nearest parent of `path` that is a mount point.
|
2023-11-14 15:47:17 +02:00
|
|
|
std::vector<std::string> subpath;
|
2023-10-27 19:39:00 +03:00
|
|
|
while (true) {
|
|
|
|
auto i = mounts.find(path);
|
|
|
|
if (i != mounts.end()) {
|
2023-11-14 15:47:17 +02:00
|
|
|
std::reverse(subpath.begin(), subpath.end());
|
|
|
|
return {i->second, CanonPath(subpath)};
|
2023-10-27 19:39:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(!path.isRoot());
|
2023-11-14 15:47:17 +02:00
|
|
|
subpath.push_back(std::string(*path.baseName()));
|
2023-10-27 19:39:00 +03:00
|
|
|
path.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-14 15:01:38 +02:00
|
|
|
ref<InputAccessor> makeMountedInputAccessor(std::map<CanonPath, ref<InputAccessor>> mounts)
|
2023-10-27 19:39:00 +03:00
|
|
|
{
|
2023-11-14 15:01:38 +02:00
|
|
|
return make_ref<MountedInputAccessor>(std::move(mounts));
|
2023-10-27 19:39:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|