1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-02 13:31:48 +02:00

downloadCached: Return ETag

This allows fetchFlake() to return the Git revision of a GitHub
archive.
This commit is contained in:
Eelco Dolstra 2019-02-25 23:20:50 +08:00
parent 6e9182fbc2
commit 529add316c
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
7 changed files with 64 additions and 26 deletions

View file

@ -771,7 +771,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_);
@ -783,8 +783,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";
@ -803,6 +806,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);
@ -814,6 +819,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];
@ -831,6 +837,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;
@ -852,6 +859,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;
}
}
@ -885,7 +893,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;
}

View file

@ -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 };
};