From 3bf0dcaef32140d641de51e690faaab43c1e9cbd Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sat, 1 Mar 2025 23:55:42 +0100 Subject: [PATCH] libstore: split fetchToStore only a single caller uses flat fetching at all. it still makes sense to not inline that single caller for now, mostly due to activity reporting Change-Id: I1a6420868443c3a684deafc7a4f567d4d4b1bd53 --- lix/libcmd/installable-value.cc | 2 +- lix/libexpr/eval.cc | 3 +-- lix/libexpr/primops.cc | 16 ++++++------- lix/libfetchers/fetch-to-store.cc | 39 ++++++++++++++++--------------- lix/libfetchers/fetch-to-store.hh | 8 +++++-- 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/lix/libcmd/installable-value.cc b/lix/libcmd/installable-value.cc index 115a9af3a..601be415c 100644 --- a/lix/libcmd/installable-value.cc +++ b/lix/libcmd/installable-value.cc @@ -48,7 +48,7 @@ std::optional InstallableValue::trySinglePathToDerivedPaths { if (v.type() == nPath) { auto storePath = state.aio.blockOn( - fetchToStore(*evaluator->store, state.ctx.paths.checkSourcePath(v.path())) + fetchToStoreRecursive(*evaluator->store, state.ctx.paths.checkSourcePath(v.path())) ); return {{ .path = DerivedPath::Opaque { diff --git a/lix/libexpr/eval.cc b/lix/libexpr/eval.cc index 211c1cb7e..92006dee7 100644 --- a/lix/libexpr/eval.cc +++ b/lix/libexpr/eval.cc @@ -2383,11 +2383,10 @@ try { auto dstPath = i != srcToStore.end() ? i->second : ({ - auto dstPath = TRY_AWAIT(fetchToStore( + auto dstPath = TRY_AWAIT(fetchToStoreRecursive( *store, checkSourcePath(path), path.baseName(), - FileIngestionMethod::Recursive, nullptr, repair )); diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index 13f2a1b6b..003703231 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -1564,14 +1564,14 @@ static void addPath( }); if (!expectedHash || !state.ctx.store->isValidPath(*expectedStorePath)) { - auto dstPath = state.aio.blockOn(fetchToStore( - *state.ctx.store, - state.ctx.paths.checkSourcePath(CanonPath(realPath)), - name, - method, - &filter, - state.ctx.repair - )); + auto checkedPath = state.ctx.paths.checkSourcePath(CanonPath(realPath)); + auto dstPath = state.aio.blockOn( + method == FileIngestionMethod::Flat + ? fetchToStoreFlat(*state.ctx.store, checkedPath, name, state.ctx.repair) + : fetchToStoreRecursive( + *state.ctx.store, checkedPath, name, &filter, state.ctx.repair + ) + ); if (expectedHash && expectedStorePath != dstPath) state.ctx.errors.make( "store path mismatch in (possibly filtered) path added from '%s'", diff --git a/lix/libfetchers/fetch-to-store.cc b/lix/libfetchers/fetch-to-store.cc index f6cc21a7c..835bd77e0 100644 --- a/lix/libfetchers/fetch-to-store.cc +++ b/lix/libfetchers/fetch-to-store.cc @@ -4,11 +4,26 @@ namespace nix { -kj::Promise> fetchToStore( +kj::Promise> fetchToStoreFlat( + Store & store, + const CheckedSourcePath & path, + std::string_view name, + RepairFlag repair) +try { + Activity act(*logger, lvlChatty, actUnknown, fmt("copying '%s' to the store", path)); + auto physicalPath = path.canonical().abs(); + + co_return settings.readOnlyMode + ? store.computeStorePathForPathFlat(name, physicalPath) + : TRY_AWAIT(store.addToStoreFlat(name, physicalPath, HashType::SHA256, repair)); +} catch (...) { + co_return result::current_exception(); +} + +kj::Promise> fetchToStoreRecursive( Store & store, const CheckedSourcePath & path, std::string_view name, - FileIngestionMethod method, PathFilter * filter, RepairFlag repair) try { @@ -17,23 +32,9 @@ try { auto filter2 = filter ? *filter : defaultPathFilter; auto physicalPath = path.canonical().abs(); - if (settings.readOnlyMode) { - switch (method) { - case FileIngestionMethod::Recursive: - co_return store.computeStorePathForPathRecursive(name, physicalPath, filter2); - case FileIngestionMethod::Flat: - co_return store.computeStorePathForPathFlat(name, physicalPath); - } - } else { - switch (method) { - case FileIngestionMethod::Recursive: - co_return TRY_AWAIT( - store.addToStoreRecursive(name, physicalPath, HashType::SHA256, filter2, repair) - ); - case FileIngestionMethod::Flat: - co_return TRY_AWAIT(store.addToStoreFlat(name, physicalPath, HashType::SHA256, repair)); - } - } + co_return settings.readOnlyMode + ? store.computeStorePathForPathRecursive(name, physicalPath, filter2) + : TRY_AWAIT(store.addToStoreRecursive(name, physicalPath, HashType::SHA256, filter2, repair)); } catch (...) { co_return result::current_exception(); } diff --git a/lix/libfetchers/fetch-to-store.hh b/lix/libfetchers/fetch-to-store.hh index 680189316..848a49ef8 100644 --- a/lix/libfetchers/fetch-to-store.hh +++ b/lix/libfetchers/fetch-to-store.hh @@ -11,11 +11,15 @@ namespace nix { /** * Copy the `path` to the Nix store. */ -kj::Promise> fetchToStore( +kj::Promise> fetchToStoreFlat( + Store & store, + const CheckedSourcePath & path, + std::string_view name = "source", + RepairFlag repair = NoRepair); +kj::Promise> fetchToStoreRecursive( Store & store, const CheckedSourcePath & path, std::string_view name = "source", - FileIngestionMethod method = FileIngestionMethod::Recursive, PathFilter * filter = nullptr, RepairFlag repair = NoRepair);