Skip to content

Commit

Permalink
Eliminate old downloadTarball()
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Oct 13, 2023
1 parent e350f84 commit fa8618f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 92 deletions.
5 changes: 2 additions & 3 deletions src/libcmd/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
SourcePath lookupFileArg(EvalState & state, std::string_view s)
{
if (EvalSettings::isPseudoUrl(s)) {
auto storePath = fetchers::downloadTarball(
state.store, EvalSettings::resolvePseudoUrl(s), "source", false).storePath;
auto accessor = makeStorePathAccessor(state.store, storePath);
auto accessor = fetchers::downloadTarball(
EvalSettings::resolvePseudoUrl(s)).accessor;
state.registerAccessor(accessor);
return accessor->root();
}
Expand Down
7 changes: 3 additions & 4 deletions src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -796,12 +796,11 @@ std::optional<SourcePath> EvalState::resolveSearchPathPath(const SearchPath::Pat

if (EvalSettings::isPseudoUrl(value)) {
try {
auto storePath = fetchers::downloadTarball(
store, EvalSettings::resolvePseudoUrl(value), "source", false).storePath;
auto accessor = makeStorePathAccessor(store, storePath);
auto accessor = fetchers::downloadTarball(
EvalSettings::resolvePseudoUrl(value)).accessor;
registerAccessor(accessor);
res.emplace(accessor->root());
} catch (FileTransferError & e) {
} catch (Error & e) {
logWarning({
.msg = hintfmt("Nix search path entry '%1%' cannot be downloaded, ignoring", value)
});
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/primops/fetchTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v
// https://github.com/NixOS/nix/issues/4313
auto storePath =
unpack
? fetchers::downloadTarball(state.store, *url, name, (bool) expectedHash).storePath
? fetchers::downloadTarball(*url).accessor->fetchToStore(state.store, CanonPath::root, name)
: fetchers::downloadFile(state.store, *url, name, (bool) expectedHash).storePath;

if (expectedHash) {
Expand Down
88 changes: 8 additions & 80 deletions src/libfetchers/tarball.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,81 +111,9 @@ DownloadFileResult downloadFile(
};
}

DownloadTarballResult downloadTarball(
ref<Store> store,
const std::string & url,
const std::string & name,
bool locked,
const Headers & headers)
{
Attrs inAttrs({
{"type", "tarball"},
{"url", url},
{"name", name},
});

auto cached = getCache()->lookupExpired(store, inAttrs);

if (cached && !cached->expired)
return {
.storePath = std::move(cached->storePath),
.lastModified = (time_t) getIntAttr(cached->infoAttrs, "lastModified"),
.immutableUrl = maybeGetStrAttr(cached->infoAttrs, "immutableUrl"),
};

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

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, FileIngestionMethod::Recursive, htSHA256, defaultPathFilter, NoRepair);
}

Attrs infoAttrs({
{"lastModified", uint64_t(lastModified)},
{"etag", res.etag},
});

if (res.immutableUrl)
infoAttrs.emplace("immutableUrl", *res.immutableUrl);

getCache()->add(
store,
inAttrs,
infoAttrs,
*unpackedStorePath,
locked);

return {
.storePath = std::move(*unpackedStorePath),
.lastModified = lastModified,
.immutableUrl = res.immutableUrl,
};
}

struct DownloadTarballResult2
{
Hash treeHash;
time_t lastModified;
std::optional<std::string> immutableUrl;
};

/* Download and import a tarball into the Git cache. The result is
the Git tree hash of the root directory. */
DownloadTarballResult2 downloadTarball2(
ref<Store> store,
DownloadTarballResult downloadTarball(
const std::string & url,
const Headers & headers)
{
Expand All @@ -198,10 +126,12 @@ DownloadTarballResult2 downloadTarball2(

auto attrsToResult = [&](const Attrs & infoAttrs)
{
return DownloadTarballResult2 {
.treeHash = getRevAttr(infoAttrs, "treeHash"),
auto treeHash = getRevAttr(infoAttrs, "treeHash");
return DownloadTarballResult {
.treeHash = treeHash,
.lastModified = (time_t) getIntAttr(infoAttrs, "lastModified"),
.immutableUrl = maybeGetStrAttr(infoAttrs, "immutableUrl"),
.accessor = getTarballCache()->getAccessor(treeHash),
};
};

Expand Down Expand Up @@ -389,11 +319,9 @@ struct TarballInputScheme : CurlInputScheme
{
auto input(_input);

auto result = downloadTarball2(store, getStrAttr(input.attrs, "url"), {});

auto accessor = getTarballCache()->getAccessor(result.treeHash);
auto result = downloadTarball(getStrAttr(input.attrs, "url"), {});

accessor->setPathDisplay("«" + input.to_string() + "»");
result.accessor->setPathDisplay("«" + input.to_string() + "»");

if (result.immutableUrl) {
auto immutableInput = Input::fromURL(*result.immutableUrl);
Expand All @@ -410,7 +338,7 @@ struct TarballInputScheme : CurlInputScheme
input.attrs.insert_or_assign("narHash",
getTarballCache()->treeHashToNarHash(result.treeHash).to_string(SRI, true));

return {accessor, input};
return {result.accessor, input};
}
};

Expand Down
9 changes: 5 additions & 4 deletions src/libfetchers/tarball.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#include "types.hh"
#include "path.hh"
#include "hash.hh"

#include <optional>

namespace nix {
class Store;
struct InputAccessor;
}

namespace nix::fetchers {
Expand All @@ -28,16 +30,15 @@ DownloadFileResult downloadFile(

struct DownloadTarballResult
{
StorePath storePath;
Hash treeHash;
time_t lastModified;
std::optional<std::string> immutableUrl;
ref<InputAccessor> accessor;
};

DownloadTarballResult downloadTarball(
ref<Store> store,
const std::string & url,
const std::string & name,
bool locked,
const Headers & headers = {});


}

0 comments on commit fa8618f

Please sign in to comment.