aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libstore/fetchers/cache.cc24
-rw-r--r--src/libstore/fetchers/cache.hh11
-rw-r--r--src/libstore/fetchers/fetchers.hh8
-rw-r--r--src/libstore/fetchers/github.cc2
-rw-r--r--src/libstore/fetchers/registry.cc2
-rw-r--r--src/libstore/fetchers/tarball.cc94
6 files changed, 99 insertions, 42 deletions
diff --git a/src/libstore/fetchers/cache.cc b/src/libstore/fetchers/cache.cc
index 4c88b64c5..14a84744a 100644
--- a/src/libstore/fetchers/cache.cc
+++ b/src/libstore/fetchers/cache.cc
@@ -66,6 +66,19 @@ struct CacheImpl : Cache
ref<Store> store,
const Attrs & inAttrs) override
{
+ if (auto res = lookupExpired(store, inAttrs)) {
+ if (!res->expired)
+ return std::make_pair(std::move(res->infoAttrs), std::move(res->storePath));
+ debug("ignoring expired cache entry '%s'",
+ attrsToJson(inAttrs).dump());
+ }
+ return {};
+ }
+
+ std::optional<Result> lookupExpired(
+ ref<Store> store,
+ const Attrs & inAttrs) override
+ {
auto state(_state.lock());
auto inAttrsJson = attrsToJson(inAttrs).dump();
@@ -81,11 +94,6 @@ struct CacheImpl : Cache
auto immutable = stmt.getInt(2) != 0;
auto timestamp = stmt.getInt(3);
- if (!immutable && (settings.tarballTtl.get() == 0 || timestamp + settings.tarballTtl < time(0))) {
- debug("ignoring expired cache entry '%s'", inAttrsJson);
- return {};
- }
-
store->addTempRoot(storePath);
if (!store->isValidPath(storePath)) {
// FIXME: we could try to substitute 'storePath'.
@@ -96,7 +104,11 @@ struct CacheImpl : Cache
debug("using cache entry '%s' -> '%s', '%s'",
inAttrsJson, infoJson, store->printStorePath(storePath));
- return {{jsonToAttrs(nlohmann::json::parse(infoJson)), std::move(storePath)}};
+ return Result {
+ .expired = !immutable && (settings.tarballTtl.get() == 0 || timestamp + settings.tarballTtl < time(0)),
+ .infoAttrs = jsonToAttrs(nlohmann::json::parse(infoJson)),
+ .storePath = std::move(storePath)
+ };
}
};
diff --git a/src/libstore/fetchers/cache.hh b/src/libstore/fetchers/cache.hh
index ba2d30629..a25b05985 100644
--- a/src/libstore/fetchers/cache.hh
+++ b/src/libstore/fetchers/cache.hh
@@ -17,6 +17,17 @@ struct Cache
virtual std::optional<std::pair<Attrs, StorePath>> lookup(
ref<Store> store,
const Attrs & inAttrs) = 0;
+
+ struct Result
+ {
+ bool expired = false;
+ Attrs infoAttrs;
+ StorePath storePath;
+ };
+
+ virtual std::optional<Result> lookupExpired(
+ ref<Store> store,
+ const Attrs & inAttrs) = 0;
};
ref<Cache> getCache();
diff --git a/src/libstore/fetchers/fetchers.hh b/src/libstore/fetchers/fetchers.hh
index 0a028cf7a..9c931dfa1 100644
--- a/src/libstore/fetchers/fetchers.hh
+++ b/src/libstore/fetchers/fetchers.hh
@@ -93,7 +93,13 @@ std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs);
void registerInputScheme(std::unique_ptr<InputScheme> && fetcher);
-StorePath downloadFile(
+struct DownloadFileResult
+{
+ StorePath storePath;
+ std::string etag;
+};
+
+DownloadFileResult downloadFile(
ref<Store> store,
const std::string & url,
const std::string & name,
diff --git a/src/libstore/fetchers/github.cc b/src/libstore/fetchers/github.cc
index 1041b98a5..0fef456df 100644
--- a/src/libstore/fetchers/github.cc
+++ b/src/libstore/fetchers/github.cc
@@ -81,7 +81,7 @@ struct GitHubInput : Input
auto json = nlohmann::json::parse(
readFile(
store->toRealPath(
- downloadFile(store, url, "source", false))));
+ downloadFile(store, url, "source", false).storePath)));
rev = Hash(json["sha"], htSHA1);
debug("HEAD revision for '%s' is %s", url, rev->gitRev());
}
diff --git a/src/libstore/fetchers/registry.cc b/src/libstore/fetchers/registry.cc
index 0aac18375..638e6e50a 100644
--- a/src/libstore/fetchers/registry.cc
+++ b/src/libstore/fetchers/registry.cc
@@ -130,7 +130,7 @@ static std::shared_ptr<Registry> getGlobalRegistry(ref<Store> store)
if (!hasPrefix(path, "/"))
// FIXME: register as GC root.
// FIXME: if download fails, use previous version if available.
- path = store->toRealPath(downloadFile(store, path, "flake-registry.json", false));
+ path = store->toRealPath(downloadFile(store, path, "flake-registry.json", false).storePath);
return Registry::read(path, Registry::Global);
}();
diff --git a/src/libstore/fetchers/tarball.cc b/src/libstore/fetchers/tarball.cc
index bbc96d70b..53971ec2b 100644
--- a/src/libstore/fetchers/tarball.cc
+++ b/src/libstore/fetchers/tarball.cc
@@ -9,7 +9,7 @@
namespace nix::fetchers {
-StorePath downloadFile(
+DownloadFileResult downloadFile(
ref<Store> store,
const std::string & url,
const std::string & name,
@@ -23,37 +23,54 @@ StorePath downloadFile(
{"name", name},
});
- if (auto res = getCache()->lookup(store, inAttrs))
- return std::move(res->second);
+ auto cached = getCache()->lookupExpired(store, inAttrs);
- // FIXME: use ETag.
+ if (cached && !cached->expired)
+ return {
+ .storePath = std::move(cached->storePath),
+ .etag = getStrAttr(cached->infoAttrs, "etag")
+ };
DownloadRequest request(url);
+ if (cached)
+ request.expectedETag = getStrAttr(cached->infoAttrs, "etag");
auto res = getDownloader()->download(request);
// FIXME: write to temporary file.
- StringSink sink;
- dumpString(*res.data, sink);
- auto hash = hashString(htSHA256, *res.data);
- ValidPathInfo info(store->makeFixedOutputPath(false, hash, name));
- info.narHash = hashString(htSHA256, *sink.s);
- info.narSize = sink.s->size();
- info.ca = makeFixedOutputCA(false, hash);
- store->addToStore(info, sink.s, NoRepair, NoCheckSigs);
-
Attrs infoAttrs({
{"etag", res.etag},
});
+ std::optional<StorePath> storePath;
+
+ if (res.cached) {
+ assert(cached);
+ assert(request.expectedETag == res.etag);
+ storePath = std::move(cached->storePath);
+ } else {
+ StringSink sink;
+ dumpString(*res.data, sink);
+ auto hash = hashString(htSHA256, *res.data);
+ ValidPathInfo info(store->makeFixedOutputPath(false, hash, name));
+ info.narHash = hashString(htSHA256, *sink.s);
+ info.narSize = sink.s->size();
+ info.ca = makeFixedOutputCA(false, hash);
+ store->addToStore(info, sink.s, NoRepair, NoCheckSigs);
+ storePath = std::move(info.path);
+ }
+
getCache()->add(
store,
inAttrs,
infoAttrs,
- info.path.clone(),
+ *storePath,
immutable);
- return std::move(info.path);
+ return {
+ .storePath = std::move(*storePath),
+ .etag = res.etag,
+ };
}
Tree downloadTarball(
@@ -68,41 +85,52 @@ Tree downloadTarball(
{"name", name},
});
- if (auto res = getCache()->lookup(store, inAttrs))
+ auto cached = getCache()->lookupExpired(store, inAttrs);
+
+ if (cached && !cached->expired)
return Tree {
- .actualPath = store->toRealPath(res->second),
- .storePath = std::move(res->second),
+ .actualPath = store->toRealPath(cached->storePath),
+ .storePath = std::move(cached->storePath),
.info = TreeInfo {
- .lastModified = getIntAttr(res->first, "lastModified"),
+ .lastModified = getIntAttr(cached->infoAttrs, "lastModified"),
},
};
- auto tarball = downloadFile(store, url, name, immutable);
-
- Path tmpDir = createTempDir();
- AutoDelete autoDelete(tmpDir, true);
- unpackTarfile(store->toRealPath(tarball), tmpDir);
- auto members = readDirectory(tmpDir);
- if (members.size() != 1)
- throw nix::Error("tarball '%s' contains an unexpected number of top-level files", url);
- auto topDir = tmpDir + "/" + members.begin()->name;
- auto lastModified = lstat(topDir).st_mtime;
- auto unpackedStorePath = store->addToStore(name, topDir, true, htSHA256, defaultPathFilter, NoRepair);
+ auto res = downloadFile(store, url, name, immutable);
+
+ std::optional<StorePath> unpackedStorePath;
+ time_t lastModified;
+
+ if (cached && res.etag != "" && getStrAttr(cached->infoAttrs, "etag") == res.etag) {
+ unpackedStorePath = std::move(cached->storePath);
+ lastModified = getIntAttr(cached->infoAttrs, "lastModified");
+ } else {
+ Path tmpDir = createTempDir();
+ AutoDelete autoDelete(tmpDir, true);
+ unpackTarfile(store->toRealPath(res.storePath), tmpDir);
+ auto members = readDirectory(tmpDir);
+ if (members.size() != 1)
+ throw nix::Error("tarball '%s' contains an unexpected number of top-level files", url);
+ auto topDir = tmpDir + "/" + members.begin()->name;
+ lastModified = lstat(topDir).st_mtime;
+ unpackedStorePath = store->addToStore(name, topDir, true, htSHA256, defaultPathFilter, NoRepair);
+ }
Attrs infoAttrs({
{"lastModified", lastModified},
+ {"etag", res.etag},
});
getCache()->add(
store,
inAttrs,
infoAttrs,
- unpackedStorePath,
+ *unpackedStorePath,
immutable);
return Tree {
- .actualPath = store->toRealPath(unpackedStorePath),
- .storePath = std::move(unpackedStorePath),
+ .actualPath = store->toRealPath(*unpackedStorePath),
+ .storePath = std::move(*unpackedStorePath),
.info = TreeInfo {
.lastModified = lastModified,
},