Skip to content

Commit

Permalink
Add parameter to make downloadFile return disappeared store path
Browse files Browse the repository at this point in the history
  • Loading branch information
yshui committed Feb 11, 2024
1 parent 2eb3d90 commit f19ee2f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/libfetchers/tarball.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ DownloadFileResult downloadFile(
const std::string & url,
const std::string & name,
bool locked,
const Headers & headers)
const Headers & headers,
bool onlyIfChanged)
{
// FIXME: check store

Expand All @@ -32,25 +33,27 @@ DownloadFileResult downloadFile(
auto useCached = [&]() -> DownloadFileResult
{
return {
.storePathValid = cached->storePathValid,
.storePath = std::move(cached->storePath),
.etag = getStrAttr(cached->infoAttrs, "etag"),
.effectiveUrl = getStrAttr(cached->infoAttrs, "url"),
.immutableUrl = maybeGetStrAttr(cached->infoAttrs, "immutableUrl"),
};
};

if (cached && !cached->expired && cached->storePathValid)
bool storePathUseable = cached && (cached->storePathValid || onlyIfChanged);
if (storePathUseable && !cached->expired)
return useCached();

FileTransferRequest request(url);
request.headers = headers;
if (cached && cached->storePathValid)
if (storePathUseable)
request.expectedETag = getStrAttr(cached->infoAttrs, "etag");
FileTransferResult res;
try {
res = getFileTransfer()->download(request);
} catch (FileTransferError & e) {
if (cached && cached->storePathValid) {
if (storePathUseable) {
warn("%s; using cached version", e.msg());
return useCached();
} else
Expand All @@ -69,7 +72,7 @@ DownloadFileResult downloadFile(
std::optional<StorePath> storePath;

if (res.cached) {
assert(cached && cached->storePathValid);
assert(storePathUseable);
storePath = std::move(cached->storePath);
} else {
StringSink sink;
Expand Down Expand Up @@ -111,6 +114,7 @@ DownloadFileResult downloadFile(
locked);

return {
.storePathValid = true,
.storePath = std::move(*storePath),
.etag = res.etag,
.effectiveUrl = res.effectiveUri,
Expand Down Expand Up @@ -143,6 +147,7 @@ DownloadTarballResult downloadTarball(
};

auto res = downloadFile(store, url, name, locked, headers);
assert(res.storePathValid);

std::optional<StorePath> unpackedStorePath;
time_t lastModified;
Expand Down Expand Up @@ -280,6 +285,7 @@ struct FileInputScheme : CurlInputScheme
std::pair<StorePath, Input> fetch(ref<Store> store, const Input & input) override
{
auto file = downloadFile(store, getStrAttr(input.attrs, "url"), input.getName(), false);
assert(file.storePathValid);
return {std::move(file.storePath), input};
}
};
Expand Down
4 changes: 3 additions & 1 deletion src/libfetchers/tarball.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace nix::fetchers {

struct DownloadFileResult
{
bool storePathValid;
StorePath storePath;
std::string etag;
std::string effectiveUrl;
Expand All @@ -24,7 +25,8 @@ DownloadFileResult downloadFile(
const std::string & url,
const std::string & name,
bool locked,
const Headers & headers = {});
const Headers & headers = {},
bool onlyIfChanged = false);

struct DownloadTarballResult
{
Expand Down

0 comments on commit f19ee2f

Please sign in to comment.