1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 12:06:01 +01:00

Move getTarballCache() into fetchers::Settings

This keeps the tarball cache open across calls.
This commit is contained in:
Eelco Dolstra 2025-10-27 14:33:51 +01:00
parent bef3c37cb2
commit bc6b9cef51
5 changed files with 29 additions and 13 deletions

View file

@ -1304,13 +1304,18 @@ std::vector<std::tuple<GitRepoImpl::Submodule, Hash>> GitRepoImpl::getSubmodules
return result; return result;
} }
ref<GitRepo> getTarballCache() namespace fetchers {
{
static auto repoDir = std::filesystem::path(getCacheDir()) / "tarball-cache";
return GitRepo::openRepo(repoDir, true, true); ref<GitRepo> Settings::getTarballCache() const
{
auto tarballCache(_tarballCache.lock());
if (!*tarballCache)
*tarballCache = GitRepo::openRepo(std::filesystem::path(getCacheDir()) / "tarball-cache", true, true);
return ref<GitRepo>(*tarballCache);
} }
} // namespace fetchers
GitRepo::WorkdirInfo GitRepo::getCachedWorkdirInfo(const std::filesystem::path & path) GitRepo::WorkdirInfo GitRepo::getCachedWorkdirInfo(const std::filesystem::path & path)
{ {
static Sync<std::map<std::filesystem::path, WorkdirInfo>> _cache; static Sync<std::map<std::filesystem::path, WorkdirInfo>> _cache;

View file

@ -270,7 +270,7 @@ struct GitArchiveInputScheme : InputScheme
if (auto lastModifiedAttrs = cache->lookup(lastModifiedKey)) { if (auto lastModifiedAttrs = cache->lookup(lastModifiedKey)) {
auto treeHash = getRevAttr(*treeHashAttrs, "treeHash"); auto treeHash = getRevAttr(*treeHashAttrs, "treeHash");
auto lastModified = getIntAttr(*lastModifiedAttrs, "lastModified"); auto lastModified = getIntAttr(*lastModifiedAttrs, "lastModified");
if (getTarballCache()->hasObject(treeHash)) if (input.settings->getTarballCache()->hasObject(treeHash))
return {std::move(input), TarballInfo{.treeHash = treeHash, .lastModified = (time_t) lastModified}}; return {std::move(input), TarballInfo{.treeHash = treeHash, .lastModified = (time_t) lastModified}};
else else
debug("Git tree with hash '%s' has disappeared from the cache, refetching...", treeHash.gitRev()); debug("Git tree with hash '%s' has disappeared from the cache, refetching...", treeHash.gitRev());
@ -290,7 +290,7 @@ struct GitArchiveInputScheme : InputScheme
*logger, lvlInfo, actUnknown, fmt("unpacking '%s' into the Git cache", input.to_string())); *logger, lvlInfo, actUnknown, fmt("unpacking '%s' into the Git cache", input.to_string()));
TarArchive archive{*source}; TarArchive archive{*source};
auto tarballCache = getTarballCache(); auto tarballCache = input.settings->getTarballCache();
auto parseSink = tarballCache->getFileSystemObjectSink(); auto parseSink = tarballCache->getFileSystemObjectSink();
auto lastModified = unpackTarfileToSink(archive, *parseSink); auto lastModified = unpackTarfileToSink(archive, *parseSink);
auto tree = parseSink->flush(); auto tree = parseSink->flush();
@ -324,7 +324,8 @@ struct GitArchiveInputScheme : InputScheme
#endif #endif
input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified)); input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified));
auto accessor = getTarballCache()->getAccessor(tarballInfo.treeHash, false, "«" + input.to_string() + "»"); auto accessor =
input.settings->getTarballCache()->getAccessor(tarballInfo.treeHash, false, "«" + input.to_string() + "»");
return {accessor, input}; return {accessor, input};
} }

View file

@ -11,6 +11,12 @@
#include <sys/types.h> #include <sys/types.h>
namespace nix {
struct GitRepo;
}
namespace nix::fetchers { namespace nix::fetchers {
struct Cache; struct Cache;
@ -125,8 +131,12 @@ struct Settings : public Config
ref<Cache> getCache() const; ref<Cache> getCache() const;
ref<GitRepo> getTarballCache() const;
private: private:
mutable Sync<std::shared_ptr<Cache>> _cache; mutable Sync<std::shared_ptr<Cache>> _cache;
mutable Sync<std::shared_ptr<GitRepo>> _tarballCache;
}; };
} // namespace nix::fetchers } // namespace nix::fetchers

View file

@ -120,8 +120,6 @@ struct GitRepo
virtual Hash dereferenceSingletonDirectory(const Hash & oid) = 0; virtual Hash dereferenceSingletonDirectory(const Hash & oid) = 0;
}; };
ref<GitRepo> getTarballCache();
// A helper to ensure that the `git_*_free` functions get called. // A helper to ensure that the `git_*_free` functions get called.
template<auto del> template<auto del>
struct Deleter struct Deleter

View file

@ -136,11 +136,11 @@ static DownloadTarballResult downloadTarball_(
.treeHash = treeHash, .treeHash = treeHash,
.lastModified = (time_t) getIntAttr(infoAttrs, "lastModified"), .lastModified = (time_t) getIntAttr(infoAttrs, "lastModified"),
.immutableUrl = maybeGetStrAttr(infoAttrs, "immutableUrl"), .immutableUrl = maybeGetStrAttr(infoAttrs, "immutableUrl"),
.accessor = getTarballCache()->getAccessor(treeHash, false, displayPrefix), .accessor = settings.getTarballCache()->getAccessor(treeHash, false, displayPrefix),
}; };
}; };
if (cached && !getTarballCache()->hasObject(getRevAttr(cached->value, "treeHash"))) if (cached && !settings.getTarballCache()->hasObject(getRevAttr(cached->value, "treeHash")))
cached.reset(); cached.reset();
if (cached && !cached->expired) if (cached && !cached->expired)
@ -179,7 +179,7 @@ static DownloadTarballResult downloadTarball_(
TarArchive{path}; TarArchive{path};
}) })
: TarArchive{*source}; : TarArchive{*source};
auto tarballCache = getTarballCache(); auto tarballCache = settings.getTarballCache();
auto parseSink = tarballCache->getFileSystemObjectSink(); auto parseSink = tarballCache->getFileSystemObjectSink();
auto lastModified = unpackTarfileToSink(archive, *parseSink); auto lastModified = unpackTarfileToSink(archive, *parseSink);
auto tree = parseSink->flush(); auto tree = parseSink->flush();
@ -398,7 +398,9 @@ struct TarballInputScheme : CurlInputScheme
input.attrs.insert_or_assign( input.attrs.insert_or_assign(
"narHash", "narHash",
getTarballCache()->treeHashToNarHash(*input.settings, result.treeHash).to_string(HashFormat::SRI, true)); input.settings->getTarballCache()
->treeHashToNarHash(*input.settings, result.treeHash)
.to_string(HashFormat::SRI, true));
return {result.accessor, input}; return {result.accessor, input};
} }