mirror of
https://github.com/NixOS/nix
synced 2025-06-29 06:21:14 +02:00
Add a UnionSourceAccessor
This commit is contained in:
parent
c3d8799f9f
commit
5b7c240ebd
3 changed files with 89 additions and 0 deletions
|
@ -167,6 +167,7 @@ sources = files(
|
|||
'tarfile.cc',
|
||||
'terminal.cc',
|
||||
'thread-pool.cc',
|
||||
'union-source-accessor.cc',
|
||||
'unix-domain-socket.cc',
|
||||
'url.cc',
|
||||
'users.cc',
|
||||
|
|
|
@ -216,4 +216,10 @@ ref<SourceAccessor> makeFSSourceAccessor(std::filesystem::path root);
|
|||
|
||||
ref<SourceAccessor> makeMountedSourceAccessor(std::map<CanonPath, ref<SourceAccessor>> mounts);
|
||||
|
||||
/**
|
||||
* Construct an accessor that presents a "union" view of a vector of
|
||||
* underlying accessors. Earlier accessors take precedence over later.
|
||||
*/
|
||||
ref<SourceAccessor> makeUnionSourceAccessor(std::vector<ref<SourceAccessor>> && accessors);
|
||||
|
||||
}
|
||||
|
|
82
src/libutil/union-source-accessor.cc
Normal file
82
src/libutil/union-source-accessor.cc
Normal file
|
@ -0,0 +1,82 @@
|
|||
#include "source-accessor.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
struct UnionSourceAccessor : SourceAccessor
|
||||
{
|
||||
std::vector<ref<SourceAccessor>> accessors;
|
||||
|
||||
UnionSourceAccessor(std::vector<ref<SourceAccessor>> _accessors)
|
||||
: accessors(std::move(_accessors))
|
||||
{
|
||||
displayPrefix.clear();
|
||||
}
|
||||
|
||||
std::string readFile(const CanonPath & path) override
|
||||
{
|
||||
for (auto & accessor : accessors) {
|
||||
auto st = accessor->maybeLstat(path);
|
||||
if (st && st->type == Type::tRegular)
|
||||
return accessor->readFile(path);
|
||||
}
|
||||
throw FileNotFound("path '%s' does not exist", showPath(path));
|
||||
}
|
||||
|
||||
std::optional<Stat> maybeLstat(const CanonPath & path) override
|
||||
{
|
||||
for (auto & accessor : accessors) {
|
||||
auto st = accessor->maybeLstat(path);
|
||||
if (st)
|
||||
return st;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
DirEntries readDirectory(const CanonPath & path) override
|
||||
{
|
||||
DirEntries result;
|
||||
for (auto & accessor : accessors) {
|
||||
auto st = accessor->maybeLstat(path);
|
||||
if (!st || st->type != Type::tDirectory)
|
||||
continue;
|
||||
for (auto & entry : accessor->readDirectory(path))
|
||||
// Don't override entries from previous accessors.
|
||||
result.insert(entry);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string readLink(const CanonPath & path) override
|
||||
{
|
||||
for (auto & accessor : accessors) {
|
||||
auto st = accessor->maybeLstat(path);
|
||||
if (st && st->type == Type::tSymlink)
|
||||
return accessor->readLink(path);
|
||||
}
|
||||
throw FileNotFound("path '%s' does not exist", showPath(path));
|
||||
}
|
||||
|
||||
std::string showPath(const CanonPath & path) override
|
||||
{
|
||||
for (auto & accessor : accessors)
|
||||
return accessor->showPath(path);
|
||||
return SourceAccessor::showPath(path);
|
||||
}
|
||||
|
||||
std::optional<std::filesystem::path> getPhysicalPath(const CanonPath & path) override
|
||||
{
|
||||
for (auto & accessor : accessors) {
|
||||
auto p = accessor->getPhysicalPath(path);
|
||||
if (p)
|
||||
return p;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
};
|
||||
|
||||
ref<SourceAccessor> makeUnionSourceAccessor(std::vector<ref<SourceAccessor>> && accessors)
|
||||
{
|
||||
return make_ref<UnionSourceAccessor>(std::move(accessors));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue