diff --git a/lix/libfetchers/cache.cc b/lix/libfetchers/cache.cc index 3f9f4fb65..b225928bf 100644 --- a/lix/libfetchers/cache.cc +++ b/lix/libfetchers/cache.cc @@ -1,10 +1,12 @@ #include "lix/libfetchers/cache.hh" #include "lix/libstore/sqlite.hh" +#include "lix/libutil/async.hh" #include "lix/libutil/sync.hh" #include "lix/libstore/store-api.hh" #include "lix/libutil/users.hh" #include +#include namespace nix::fetchers { @@ -72,23 +74,25 @@ struct CacheImpl : Cache (time(0)).exec(); } - std::optional> lookup( + kj::Promise>>> lookup( ref store, const Attrs & inAttrs) override - { - if (auto res = lookupExpired(store, inAttrs)) { + try { + if (auto res = TRY_AWAIT(lookupExpired(store, inAttrs))) { if (!res->expired) - return std::make_pair(std::move(res->infoAttrs), std::move(res->storePath)); + co_return std::make_pair(std::move(res->infoAttrs), std::move(res->storePath)); debug("ignoring expired cache entry '%s'", attrsToJSON(inAttrs).dump()); } - return {}; + co_return std::nullopt; + } catch (...) { + co_return result::current_exception(); } - std::optional lookupExpired( + kj::Promise>> lookupExpired( ref store, const Attrs & inAttrs) override - { + try { auto state(_state.lock()); auto inAttrsJSON = attrsToJSON(inAttrs).dump(); @@ -96,7 +100,7 @@ struct CacheImpl : Cache auto stmt(state->lookup.use()(inAttrsJSON)); if (!stmt.next()) { debug("did not find cache entry for '%s'", inAttrsJSON); - return {}; + co_return std::nullopt; } auto infoJSON = stmt.getStr(0); @@ -108,17 +112,19 @@ struct CacheImpl : Cache if (!store->isValidPath(storePath)) { // FIXME: we could try to substitute 'storePath'. debug("ignoring disappeared cache entry '%s'", inAttrsJSON); - return {}; + co_return std::nullopt; } debug("using cache entry '%s' -> '%s', '%s'", inAttrsJSON, infoJSON, store->printStorePath(storePath)); - return Result { + co_return LookupResult { .expired = !locked && (settings.tarballTtl.get() == 0 || timestamp + settings.tarballTtl < time(0)), .infoAttrs = jsonToAttrs(nlohmann::json::parse(infoJSON)), .storePath = std::move(storePath) }; + } catch (...) { + co_return result::current_exception(); } }; diff --git a/lix/libfetchers/cache.hh b/lix/libfetchers/cache.hh index 91af2d80a..5379b5fd4 100644 --- a/lix/libfetchers/cache.hh +++ b/lix/libfetchers/cache.hh @@ -17,18 +17,18 @@ struct Cache const StorePath & storePath, bool locked) = 0; - virtual std::optional> lookup( + virtual kj::Promise>>> lookup( ref store, const Attrs & inAttrs) = 0; - struct Result + struct LookupResult { bool expired = false; Attrs infoAttrs; StorePath storePath; }; - virtual std::optional lookupExpired( + virtual kj::Promise>> lookupExpired( ref store, const Attrs & inAttrs) = 0; }; diff --git a/lix/libfetchers/git.cc b/lix/libfetchers/git.cc index 7240fba07..ec9285f67 100644 --- a/lix/libfetchers/git.cc +++ b/lix/libfetchers/git.cc @@ -491,7 +491,7 @@ struct GitInputScheme : InputScheme }; if (input.getRev()) { - if (auto res = getCache()->lookup(store, getLockedAttrs())) + if (auto res = TRY_AWAIT(getCache()->lookup(store, getLockedAttrs()))) co_return makeResult(res->first, std::move(res->second)); } @@ -547,7 +547,7 @@ struct GitInputScheme : InputScheme } } - if (auto res = getCache()->lookup(store, unlockedAttrs)) { + if (auto res = TRY_AWAIT(getCache()->lookup(store, unlockedAttrs))) { auto rev2 = Hash::parseAny(getStrAttr(res->first, "rev"), HashType::SHA1); if (!input.getRev() || input.getRev() == rev2) { input.attrs.insert_or_assign("rev", rev2.gitRev()); @@ -686,7 +686,7 @@ struct GitInputScheme : InputScheme /* Now that we know the ref, check again whether we have it in the store. */ - if (auto res = getCache()->lookup(store, getLockedAttrs())) + if (auto res = TRY_AWAIT(getCache()->lookup(store, getLockedAttrs()))) co_return makeResult(res->first, std::move(res->second)); Path tmpDir = createTempDir(); diff --git a/lix/libfetchers/mercurial.cc b/lix/libfetchers/mercurial.cc index 9e20c29a7..623cf9d36 100644 --- a/lix/libfetchers/mercurial.cc +++ b/lix/libfetchers/mercurial.cc @@ -1,6 +1,7 @@ #include "lix/libfetchers/fetchers.hh" #include "lix/libfetchers/cache.hh" #include "lix/libfetchers/builtin-fetchers.hh" +#include "lix/libutil/async.hh" #include "lix/libutil/processes.hh" #include "lix/libstore/store-api.hh" #include "lix/libstore/temporary-dir.hh" @@ -243,7 +244,7 @@ struct MercurialInputScheme : InputScheme }; if (input.getRev()) { - if (auto res = getCache()->lookup(store, getLockedAttrs())) + if (auto res = TRY_AWAIT(getCache()->lookup(store, getLockedAttrs()))) co_return makeResult(res->first, std::move(res->second)); } @@ -256,7 +257,7 @@ struct MercurialInputScheme : InputScheme {"ref", *input.getRef()}, }); - if (auto res = getCache()->lookup(store, unlockedAttrs)) { + if (auto res = TRY_AWAIT(getCache()->lookup(store, unlockedAttrs))) { auto rev2 = Hash::parseAny(getStrAttr(res->first, "rev"), HashType::SHA1); if (!input.getRev() || input.getRev() == rev2) { input.attrs.insert_or_assign("rev", rev2.gitRev()); @@ -302,7 +303,7 @@ struct MercurialInputScheme : InputScheme auto revCount = std::stoull(tokens[1]); input.attrs.insert_or_assign("ref", tokens[2]); - if (auto res = getCache()->lookup(store, getLockedAttrs())) + if (auto res = TRY_AWAIT(getCache()->lookup(store, getLockedAttrs()))) co_return makeResult(res->first, std::move(res->second)); Path tmpDir = createTempDir(); diff --git a/lix/libfetchers/tarball.cc b/lix/libfetchers/tarball.cc index 1d05a4642..201552b77 100644 --- a/lix/libfetchers/tarball.cc +++ b/lix/libfetchers/tarball.cc @@ -28,7 +28,7 @@ try { {"name", name}, }); - auto cached = getCache()->lookupExpired(store, inAttrs); + auto cached = TRY_AWAIT(getCache()->lookupExpired(store, inAttrs)); auto useCached = [&]() -> DownloadFileResult { @@ -135,7 +135,7 @@ try { {"name", name}, }); - auto cached = getCache()->lookupExpired(store, inAttrs); + auto cached = TRY_AWAIT(getCache()->lookupExpired(store, inAttrs)); if (cached && !cached->expired) co_return DownloadTarballResult{