From ed2d1080beaf2c3c64626f2ef0595c09f9684da8 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Fri, 7 Feb 2025 01:01:41 +0100 Subject: [PATCH] libfetchers: asyncify fetchToStore Change-Id: I0153cefbe974137ab3d4fd9218c8a77137976f8c --- lix/libcmd/installable-value.cc | 4 +++- lix/libexpr/eval.cc | 15 +++++++++++---- lix/libexpr/primops.cc | 4 ++-- lix/libfetchers/fetch-to-store.cc | 8 +++++--- lix/libfetchers/fetch-to-store.hh | 2 +- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lix/libcmd/installable-value.cc b/lix/libcmd/installable-value.cc index 42d87936b..115a9af3a 100644 --- a/lix/libcmd/installable-value.cc +++ b/lix/libcmd/installable-value.cc @@ -47,7 +47,9 @@ std::optional InstallableValue::trySinglePathToDerivedPaths ) { if (v.type() == nPath) { - auto storePath = fetchToStore(*evaluator->store, state.ctx.paths.checkSourcePath(v.path())); + auto storePath = state.aio.blockOn( + fetchToStore(*evaluator->store, state.ctx.paths.checkSourcePath(v.path())) + ); return {{ .path = DerivedPath::Opaque { .path = std::move(storePath), diff --git a/lix/libexpr/eval.cc b/lix/libexpr/eval.cc index 6cd44c38e..1fd065000 100644 --- a/lix/libexpr/eval.cc +++ b/lix/libexpr/eval.cc @@ -2366,13 +2366,20 @@ try { auto dstPath = i != srcToStore.end() ? i->second - : [&]() { - auto dstPath = fetchToStore(*store, checkSourcePath(path), path.baseName(), FileIngestionMethod::Recursive, nullptr, repair); + : ({ + auto dstPath = TRY_AWAIT(fetchToStore( + *store, + checkSourcePath(path), + path.baseName(), + FileIngestionMethod::Recursive, + nullptr, + repair + )); allowPath(dstPath); srcToStore.insert_or_assign(path, dstPath); printMsg(lvlChatty, "copied source '%1%' -> '%2%'", path, store->printStorePath(dstPath)); - return dstPath; - }(); + std::move(dstPath); + }); context.insert(NixStringContextElem::Opaque { .path = dstPath diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index 1d597a9c8..961687185 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -1557,14 +1557,14 @@ static void addPath( }); if (!expectedHash || !state.ctx.store->isValidPath(*expectedStorePath)) { - auto dstPath = fetchToStore( + auto dstPath = state.aio.blockOn(fetchToStore( *state.ctx.store, state.ctx.paths.checkSourcePath(CanonPath(path)), name, method, &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 a276a48fd..09ae5ff7c 100644 --- a/lix/libfetchers/fetch-to-store.cc +++ b/lix/libfetchers/fetch-to-store.cc @@ -4,22 +4,24 @@ namespace nix { -StorePath fetchToStore( +kj::Promise> fetchToStore( Store & store, const CheckedSourcePath & path, std::string_view name, FileIngestionMethod method, PathFilter * filter, RepairFlag repair) -{ +try { Activity act(*logger, lvlChatty, actUnknown, fmt("copying '%s' to the store", path)); auto filter2 = filter ? *filter : defaultPathFilter; - return + co_return settings.readOnlyMode ? store.computeStorePathForPath(name, path.canonical().abs(), method, HashType::SHA256, filter2).first : store.addToStore(name, path.canonical().abs(), method, 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 6d4d072aa..680189316 100644 --- a/lix/libfetchers/fetch-to-store.hh +++ b/lix/libfetchers/fetch-to-store.hh @@ -11,7 +11,7 @@ namespace nix { /** * Copy the `path` to the Nix store. */ -StorePath fetchToStore( +kj::Promise> fetchToStore( Store & store, const CheckedSourcePath & path, std::string_view name = "source",