about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-02-25T15·20+0800
committerEelco Dolstra <edolstra@gmail.com>2019-06-24T19·58+0200
commitdc29e9fb47f9f98a851dc88b2bd3cae4b5c1fe6b (patch)
tree987610c21a2bf0796dc9e2d6b92daca215fd9d6c
parent94f11d0a61bba15ff8a93f2de8d1883783c8c508 (diff)
downloadCached: Return ETag
(cherry picked from commit 529add316c5356a8060c35f987643b7bf5c796dc)
-rw-r--r--src/libexpr/common-eval-args.cc2
-rw-r--r--src/libexpr/parser.y2
-rw-r--r--src/libexpr/primops.cc2
-rw-r--r--src/libstore/download.cc17
-rw-r--r--src/libstore/download.hh12
-rwxr-xr-xsrc/nix-channel/nix-channel.cc6
6 files changed, 29 insertions, 12 deletions
diff --git a/src/libexpr/common-eval-args.cc b/src/libexpr/common-eval-args.cc
index 3e0c78f280..37c74a94b2 100644
--- a/src/libexpr/common-eval-args.cc
+++ b/src/libexpr/common-eval-args.cc
@@ -46,7 +46,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
 Path lookupFileArg(EvalState & state, string s)
 {
     if (isUri(s))
-        return getDownloader()->downloadCached(state.store, s, true);
+        return getDownloader()->downloadCached(state.store, s, true).path;
     else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') {
         Path p = s.substr(1, s.size() - 2);
         return state.findFile(p);
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index 78a5039071..29860fb057 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -677,7 +677,7 @@ std::pair<bool, std::string> EvalState::resolveSearchPathElem(const SearchPathEl
 
     if (isUri(elem.second)) {
         try {
-            res = { true, getDownloader()->downloadCached(store, elem.second, true) };
+            res = { true, getDownloader()->downloadCached(store, elem.second, true).path };
         } catch (DownloadError & e) {
             printError(format("warning: Nix search path entry '%1%' cannot be downloaded, ignoring") % elem.second);
             res = { false, "" };
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 06f577f36f..55a1bde117 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -2083,7 +2083,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
     if (evalSettings.pureEval && !expectedHash)
         throw Error("in pure evaluation mode, '%s' requires a 'sha256' argument", who);
 
-    Path res = getDownloader()->downloadCached(state.store, url, unpack, name, expectedHash);
+    Path res = getDownloader()->downloadCached(state.store, url, unpack, name, expectedHash).path;
 
     if (state.allowedPaths)
         state.allowedPaths->insert(res);
diff --git a/src/libstore/download.cc b/src/libstore/download.cc
index 22382ab1d6..cb77cdc77d 100644
--- a/src/libstore/download.cc
+++ b/src/libstore/download.cc
@@ -790,7 +790,7 @@ void Downloader::download(DownloadRequest && request, Sink & sink)
     }
 }
 
-Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpack, string name, const Hash & expectedHash, string * effectiveUrl, int ttl)
+CachedDownloadResult Downloader::downloadCached(ref<Store> store, const string & url_, bool unpack, string name, const Hash & expectedHash, string * effectiveUrl, int ttl)
 {
     auto url = resolveUri(url_);
 
@@ -802,8 +802,11 @@ Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpa
     Path expectedStorePath;
     if (expectedHash) {
         expectedStorePath = store->makeFixedOutputPath(unpack, expectedHash, name);
-        if (store->isValidPath(expectedStorePath))
-            return store->toRealPath(expectedStorePath);
+        if (store->isValidPath(expectedStorePath)) {
+            CachedDownloadResult result;
+            result.path = store->toRealPath(expectedStorePath);
+            return result;
+        }
     }
 
     Path cacheDir = getCacheDir() + "/nix/tarballs";
@@ -822,6 +825,8 @@ Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpa
 
     bool skip = false;
 
+    CachedDownloadResult result;
+
     if (pathExists(fileLink) && pathExists(dataFile)) {
         storePath = readLink(fileLink);
         store->addTempRoot(storePath);
@@ -833,6 +838,7 @@ Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpa
                     skip = true;
                     if (effectiveUrl)
                         *effectiveUrl = url_;
+                    result.etag = ss[1];
                 } else if (!ss[1].empty()) {
                     debug(format("verifying previous ETag '%1%'") % ss[1]);
                     expectedETag = ss[1];
@@ -850,6 +856,7 @@ Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpa
             auto res = download(request);
             if (effectiveUrl)
                 *effectiveUrl = res.effectiveUrl;
+            result.etag = res.etag;
 
             if (!res.cached) {
                 ValidPathInfo info;
@@ -871,6 +878,7 @@ Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpa
         } catch (DownloadError & e) {
             if (storePath.empty()) throw;
             printError(format("warning: %1%; using cached result") % e.msg());
+            result.etag = expectedETag;
         }
     }
 
@@ -904,7 +912,8 @@ Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpa
             url, expectedHash.to_string(), gotHash.to_string());
     }
 
-    return store->toRealPath(storePath);
+    result.path = store->toRealPath(storePath);
+    return result;
 }
 
 
diff --git a/src/libstore/download.hh b/src/libstore/download.hh
index f0228f7d05..8acfe4e1a3 100644
--- a/src/libstore/download.hh
+++ b/src/libstore/download.hh
@@ -41,6 +41,12 @@ struct DownloadResult
     uint64_t bodySize = 0;
 };
 
+struct CachedDownloadResult
+{
+    Path path;
+    std::optional<std::string> etag;
+};
+
 class Store;
 
 struct Downloader
@@ -64,8 +70,10 @@ struct Downloader
        and is more recent than ‘tarball-ttl’ seconds. Otherwise,
        use the recorded ETag to verify if the server has a more
        recent version, and if so, download it to the Nix store. */
-    Path downloadCached(ref<Store> store, const string & uri, bool unpack, string name = "",
-        const Hash & expectedHash = Hash(), string * effectiveUri = nullptr, int ttl = settings.tarballTtl);
+    CachedDownloadResult downloadCached(
+        ref<Store> store, const string & uri, bool unpack, string name = "",
+        const Hash & expectedHash = Hash(), string * effectiveUri = nullptr,
+        int ttl = settings.tarballTtl);
 
     enum Error { NotFound, Forbidden, Misc, Transient, Interrupted };
 };
diff --git a/src/nix-channel/nix-channel.cc b/src/nix-channel/nix-channel.cc
index 8b66cc7e31..7b23088a2c 100755
--- a/src/nix-channel/nix-channel.cc
+++ b/src/nix-channel/nix-channel.cc
@@ -88,7 +88,7 @@ static void update(const StringSet & channelNames)
         // definition from a consistent location if the redirect changes mid-download.
         std::string effectiveUrl;
         auto dl = getDownloader();
-        auto filename = dl->downloadCached(store, url, false, "", Hash(), &effectiveUrl, 0);
+        auto filename = dl->downloadCached(store, url, false, "", Hash(), &effectiveUrl, 0).path;
         url = chomp(std::move(effectiveUrl));
 
         // If the URL contains a version number, append it to the name
@@ -123,10 +123,10 @@ static void update(const StringSet & channelNames)
             // Download the channel tarball.
             auto fullURL = url + "/nixexprs.tar.xz";
             try {
-                filename = dl->downloadCached(store, fullURL, false);
+                filename = dl->downloadCached(store, fullURL, false).path;
             } catch (DownloadError & e) {
                 fullURL = url + "/nixexprs.tar.bz2";
-                filename = dl->downloadCached(store, fullURL, false);
+                filename = dl->downloadCached(store, fullURL, false).path;
             }
             chomp(filename);
         }