mirror of
https://github.com/NixOS/nix
synced 2025-07-06 00:51:47 +02:00
For example, instead of doing #include "nix/store-config.hh" #include "nix/derived-path.hh" Now do #include "nix/store/config.hh" #include "nix/store/derived-path.hh" This was originally planned in the issue, and also recent requested by Eelco. Most of the change is purely mechanical. There is just one small additional issue. See how, in the example above, we took this opportunity to also turn `<comp>-config.hh` into `<comp>/config.hh`. Well, there was already a `nix/util/config.{cc,hh}`. Even though there is not a public configuration header for libutil (which also would be called `nix/util/config.{cc,hh}`) that's still confusing, To avoid any such confusion, we renamed that to `nix/util/configuration.{cc,hh}`. Finally, note that the libflake headers already did this, so we didn't need to do anything to them. We wouldn't want to mistakenly get `nix/flake/flake/flake.hh`! Progress on #7876
79 lines
2.3 KiB
C++
79 lines
2.3 KiB
C++
#include "nix/util/source-accessor.hh"
|
|
|
|
namespace nix {
|
|
|
|
struct MountedSourceAccessor : SourceAccessor
|
|
{
|
|
std::map<CanonPath, ref<SourceAccessor>> mounts;
|
|
|
|
MountedSourceAccessor(std::map<CanonPath, ref<SourceAccessor>> _mounts)
|
|
: mounts(std::move(_mounts))
|
|
{
|
|
displayPrefix.clear();
|
|
|
|
// Currently we require a root filesystem. This could be relaxed.
|
|
assert(mounts.contains(CanonPath::root));
|
|
|
|
// FIXME: return dummy parent directories automatically?
|
|
}
|
|
|
|
std::string readFile(const CanonPath & path) override
|
|
{
|
|
auto [accessor, subpath] = resolve(path);
|
|
return accessor->readFile(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) {
|
|
auto i = mounts.find(path);
|
|
if (i != mounts.end()) {
|
|
std::reverse(subpath.begin(), subpath.end());
|
|
return {i->second, 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);
|
|
}
|
|
};
|
|
|
|
ref<SourceAccessor> makeMountedSourceAccessor(std::map<CanonPath, ref<SourceAccessor>> mounts)
|
|
{
|
|
return make_ref<MountedSourceAccessor>(std::move(mounts));
|
|
}
|
|
|
|
}
|