1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-29 13:41:00 +01:00

Check tarball cache validity

This commit is contained in:
Eelco Dolstra 2023-02-03 12:42:32 +01:00
parent 4142982140
commit ca26ce994b
5 changed files with 48 additions and 11 deletions

View file

@ -1,3 +1,4 @@
#include "git-utils.hh"
#include "input-accessor.hh" #include "input-accessor.hh"
#include <span> #include <span>
@ -43,6 +44,14 @@ static Repository openRepo(const CanonPath & path)
return Repository(_repo); return Repository(_repo);
} }
git_oid hashToOID(const Hash & hash)
{
git_oid oid;
if (git_oid_fromstr(&oid, hash.gitRev().c_str()))
throw Error("cannot convert '%s' to a Git OID", hash.gitRev());
return oid;
}
struct GitInputAccessor : InputAccessor struct GitInputAccessor : InputAccessor
{ {
Repository repo; Repository repo;
@ -51,9 +60,7 @@ struct GitInputAccessor : InputAccessor
GitInputAccessor(Repository && repo_, const Hash & rev) GitInputAccessor(Repository && repo_, const Hash & rev)
: repo(std::move(repo_)) : repo(std::move(repo_))
{ {
git_oid oid; auto oid = hashToOID(rev);
if (git_oid_fromstr(&oid, rev.gitRev().c_str()))
throw Error("cannot convert '%s' to a Git OID", rev.gitRev());
git_object * obj = nullptr; git_object * obj = nullptr;
if (git_object_lookup(&obj, repo.get(), &oid, GIT_OBJECT_ANY)) { if (git_object_lookup(&obj, repo.get(), &oid, GIT_OBJECT_ANY)) {
@ -389,4 +396,20 @@ ref<InputAccessor> makeTarballCacheAccessor(const Hash & rev)
return make_ref<GitInputAccessor>(openTarballCache(), rev); return make_ref<GitInputAccessor>(openTarballCache(), rev);
} }
bool tarballCacheContains(const Hash & treeHash)
{
auto repo = openTarballCache();
auto oid = hashToOID(treeHash);
git_object * obj = nullptr;
if (auto errCode = git_object_lookup(&obj, repo.get(), &oid, GIT_OBJECT_TREE)) {
if (errCode == GIT_ENOTFOUND) return false;
auto err = git_error_last();
throw Error("getting Git object '%s': %s", treeHash.gitRev(), err->message);
}
return true;
}
} }

View file

@ -0,0 +1,15 @@
#pragma once
#include "input-accessor.hh"
namespace nix {
ref<InputAccessor> makeGitInputAccessor(const CanonPath & path, const Hash & rev);
Hash importTarball(Source & source);
ref<InputAccessor> makeTarballCacheAccessor(const Hash & rev);
bool tarballCacheContains(const Hash & treeHash);
}

View file

@ -8,6 +8,7 @@
#include "util.hh" #include "util.hh"
#include "git.hh" #include "git.hh"
#include "fs-input-accessor.hh" #include "fs-input-accessor.hh"
#include "git-utils.hh"
#include "fetch-settings.hh" #include "fetch-settings.hh"

View file

@ -9,6 +9,7 @@
#include "fetch-settings.hh" #include "fetch-settings.hh"
#include "input-accessor.hh" #include "input-accessor.hh"
#include "tarball.hh" #include "tarball.hh"
#include "git-utils.hh"
#include <optional> #include <optional>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -196,8 +197,10 @@ struct GitArchiveInputScheme : InputScheme
if (auto treeHashS = cache->queryFact(treeHashKey)) { if (auto treeHashS = cache->queryFact(treeHashKey)) {
auto treeHash = Hash::parseAny(*treeHashS, htSHA1); auto treeHash = Hash::parseAny(*treeHashS, htSHA1);
// FIXME: verify that treeHash exists in the tarball cache. if (tarballCacheContains(treeHash))
return {std::move(input), treeHash}; return {std::move(input), treeHash};
else
debug("Git tree with hash '%s' has disappeared from the cache, refetching...", treeHash.gitRev());
} }
/* Stream the tarball into the tarball cache. */ /* Stream the tarball into the tarball cache. */
@ -272,6 +275,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
return getStrAttr(input.attrs, "repo"); return getStrAttr(input.attrs, "repo");
} }
/* .commit.tree.sha, .commit.committer.date */
Hash getRevFromRef(nix::ref<Store> store, const Input & input) const override Hash getRevFromRef(nix::ref<Store> store, const Input & input) const override
{ {
auto host = getHost(input); auto host = getHost(input);

View file

@ -117,12 +117,6 @@ ref<InputAccessor> makePatchingInputAccessor(
ref<InputAccessor> next, ref<InputAccessor> next,
const std::vector<std::string> & patches); const std::vector<std::string> & patches);
ref<InputAccessor> makeGitInputAccessor(const CanonPath & path, const Hash & rev);
Hash importTarball(Source & source);
ref<InputAccessor> makeTarballCacheAccessor(const Hash & rev);
struct SourcePath struct SourcePath
{ {
ref<InputAccessor> accessor; ref<InputAccessor> accessor;