1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 03:56:01 +01:00

MountedSourceAccessor: Move into a separate header, add mount method

This commit is contained in:
Eelco Dolstra 2025-09-22 22:28:12 +02:00 committed by John Ericson
parent d1958e1b2c
commit 339338e166
7 changed files with 51 additions and 13 deletions

View file

@ -17,6 +17,7 @@
#include "nix/expr/print.hh" #include "nix/expr/print.hh"
#include "nix/fetchers/filtering-source-accessor.hh" #include "nix/fetchers/filtering-source-accessor.hh"
#include "nix/util/memory-source-accessor.hh" #include "nix/util/memory-source-accessor.hh"
#include "nix/util/mounted-source-accessor.hh"
#include "nix/expr/gc-small-vector.hh" #include "nix/expr/gc-small-vector.hh"
#include "nix/util/url.hh" #include "nix/util/url.hh"
#include "nix/fetchers/fetch-to-store.hh" #include "nix/fetchers/fetch-to-store.hh"

View file

@ -15,6 +15,7 @@
#include "nix/fetchers/fetch-settings.hh" #include "nix/fetchers/fetch-settings.hh"
#include "nix/util/json-utils.hh" #include "nix/util/json-utils.hh"
#include "nix/util/archive.hh" #include "nix/util/archive.hh"
#include "nix/util/mounted-source-accessor.hh"
#include <regex> #include <regex>
#include <string.h> #include <string.h>

View file

@ -47,6 +47,7 @@ headers = files(
'logging.hh', 'logging.hh',
'lru-cache.hh', 'lru-cache.hh',
'memory-source-accessor.hh', 'memory-source-accessor.hh',
'mounted-source-accessor.hh',
'muxable-pipe.hh', 'muxable-pipe.hh',
'os-string.hh', 'os-string.hh',
'pool.hh', 'pool.hh',

View file

@ -0,0 +1,20 @@
#pragma once
#include "source-accessor.hh"
namespace nix {
struct MountedSourceAccessor : SourceAccessor
{
virtual void mount(CanonPath mountPoint, ref<SourceAccessor> accessor) = 0;
/**
* Return the accessor mounted on `mountPoint`, or `nullptr` if
* there is no such mount point.
*/
virtual std::shared_ptr<SourceAccessor> getMount(CanonPath mountPoint) = 0;
};
ref<MountedSourceAccessor> makeMountedSourceAccessor(std::map<CanonPath, ref<SourceAccessor>> mounts);
} // namespace nix

View file

@ -214,8 +214,6 @@ ref<SourceAccessor> getFSSourceAccessor();
*/ */
ref<SourceAccessor> makeFSSourceAccessor(std::filesystem::path root); 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 * Construct an accessor that presents a "union" view of a vector of
* underlying accessors. Earlier accessors take precedence over later. * underlying accessors. Earlier accessors take precedence over later.

View file

@ -1,18 +1,22 @@
#include "nix/util/source-accessor.hh" #include "nix/util/mounted-source-accessor.hh"
#include <boost/unordered/concurrent_flat_map.hpp>
namespace nix { namespace nix {
struct MountedSourceAccessor : SourceAccessor struct MountedSourceAccessorImpl : MountedSourceAccessor
{ {
std::map<CanonPath, ref<SourceAccessor>> mounts; boost::concurrent_flat_map<CanonPath, ref<SourceAccessor>> mounts;
MountedSourceAccessor(std::map<CanonPath, ref<SourceAccessor>> _mounts) MountedSourceAccessorImpl(std::map<CanonPath, ref<SourceAccessor>> _mounts)
: mounts(std::move(_mounts))
{ {
displayPrefix.clear(); displayPrefix.clear();
// Currently we require a root filesystem. This could be relaxed. // Currently we require a root filesystem. This could be relaxed.
assert(mounts.contains(CanonPath::root)); assert(_mounts.contains(CanonPath::root));
for (auto & [path, accessor] : _mounts)
mount(path, accessor);
// FIXME: return dummy parent directories automatically? // FIXME: return dummy parent directories automatically?
} }
@ -52,10 +56,9 @@ struct MountedSourceAccessor : SourceAccessor
// Find the nearest parent of `path` that is a mount point. // Find the nearest parent of `path` that is a mount point.
std::vector<std::string> subpath; std::vector<std::string> subpath;
while (true) { while (true) {
auto i = mounts.find(path); if (auto mount = getMount(path)) {
if (i != mounts.end()) {
std::reverse(subpath.begin(), subpath.end()); std::reverse(subpath.begin(), subpath.end());
return {i->second, CanonPath(subpath)}; return {ref(mount), CanonPath(subpath)};
} }
assert(!path.isRoot()); assert(!path.isRoot());
@ -69,11 +72,24 @@ struct MountedSourceAccessor : SourceAccessor
auto [accessor, subpath] = resolve(path); auto [accessor, subpath] = resolve(path);
return accessor->getPhysicalPath(subpath); 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;
}
}; };
ref<SourceAccessor> makeMountedSourceAccessor(std::map<CanonPath, ref<SourceAccessor>> mounts) ref<MountedSourceAccessor> makeMountedSourceAccessor(std::map<CanonPath, ref<SourceAccessor>> mounts)
{ {
return make_ref<MountedSourceAccessor>(std::move(mounts)); return make_ref<MountedSourceAccessorImpl>(std::move(mounts));
} }
} // namespace nix } // namespace nix

View file

@ -7,6 +7,7 @@
#include "nix/util/strings.hh" #include "nix/util/strings.hh"
#include "nix/util/executable-path.hh" #include "nix/util/executable-path.hh"
#include "nix/util/environment-variables.hh" #include "nix/util/environment-variables.hh"
#include "nix/util/mounted-source-accessor.hh"
using namespace nix; using namespace nix;