From 1c127e6a82dbc128602aa4451dfa8f4c2fe4a751 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 19 Mar 2020 11:42:50 +0100 Subject: [PATCH] downloadFile(): Use expired file if the download fails --- src/libstore/fetchers/tarball.cc | 18 ++++++++++++++++-- tests/flakes.sh | 7 +++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/libstore/fetchers/tarball.cc b/src/libstore/fetchers/tarball.cc index 54b59babc..62d43ca36 100644 --- a/src/libstore/fetchers/tarball.cc +++ b/src/libstore/fetchers/tarball.cc @@ -25,17 +25,31 @@ DownloadFileResult downloadFile( auto cached = getCache()->lookupExpired(store, inAttrs); - if (cached && !cached->expired) + auto useCached = [&]() -> DownloadFileResult + { return { .storePath = std::move(cached->storePath), .etag = getStrAttr(cached->infoAttrs, "etag"), .effectiveUrl = getStrAttr(cached->infoAttrs, "url") }; + }; + + if (cached && !cached->expired) + return useCached(); DownloadRequest request(url); if (cached) request.expectedETag = getStrAttr(cached->infoAttrs, "etag"); - auto res = getDownloader()->download(request); + DownloadResult res; + try { + res = getDownloader()->download(request); + } catch (DownloadError & e) { + if (cached) { + warn("%s; using cached version", e.msg()); + return useCached(); + } else + throw; + } // FIXME: write to temporary file. diff --git a/tests/flakes.sh b/tests/flakes.sh index fc1c23fe9..52f5fabc0 100644 --- a/tests/flakes.sh +++ b/tests/flakes.sh @@ -268,10 +268,9 @@ nix build -o $TEST_ROOT/result $flake3Dir#sth 2>&1 | grep 'unsupported edition' # Test whether registry caching works. nix flake list --flake-registry file://$registry | grep -q flake3 -# FIXME -#mv $registry $registry.tmp -#nix flake list --flake-registry file://$registry --refresh | grep -q flake3 -#mv $registry.tmp $registry +mv $registry $registry.tmp +nix flake list --flake-registry file://$registry --refresh | grep -q flake3 +mv $registry.tmp $registry # Test whether flakes are registered as GC roots for offline use. # FIXME: use tarballs rather than git.