Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions src/libfetchers/git-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1632,32 +1632,6 @@ std::vector<std::tuple<GitRepoImpl::Submodule, Hash>> GitRepoImpl::getSubmodules
return result;
}

namespace fetchers {

ref<GitRepo> Settings::getTarballCache() const
{
/* v1: Had either only loose objects or thin packfiles referring to loose objects
* v2: Must have only packfiles with no loose objects. Should get repacked periodically
* for optimal packfiles.
*/
static auto repoDir = std::filesystem::path(getCacheDir()) / "tarball-cache-v2";
auto tarballCache(_tarballCache.lock());
if (!*tarballCache)
*tarballCache = GitRepo::openRepo(
repoDir,
{
.create = true,
.bare = true,
.packfilesOnly = true,
/* Tarball unpacking is not expected to benefit from deltas much,
compared to how much CPU times it takes to find. */
.dontFindDeltas = true,
});
return ref<GitRepo>(*tarballCache);
}

} // namespace fetchers

static Sync<std::map<std::filesystem::path, GitRepo::WorkdirInfo>> workdirInfoCache_;

GitRepo::WorkdirInfo GitRepo::getCachedWorkdirInfo(const std::filesystem::path & path)
Expand Down
7 changes: 4 additions & 3 deletions src/libfetchers/github.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "nix/fetchers/tarball.hh"
#include "nix/util/tarfile.hh"
#include "nix/fetchers/git-utils.hh"
#include "nix/fetchers/tarball-cache.hh"

#include <optional>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -287,7 +288,7 @@ struct GitArchiveInputScheme : InputScheme
if (auto lastModifiedAttrs = cache->lookup(lastModifiedKey)) {
auto treeHash = getRevAttr(*treeHashAttrs, "treeHash");
auto lastModified = getIntAttr(*lastModifiedAttrs, "lastModified");
if (settings.getTarballCache()->hasObject(treeHash))
if (settings.getTarballCache()->hasTree(treeHash))
return {
{std::move(input), TarballInfo{.treeHash = treeHash, .lastModified = (time_t) lastModified}}};
else
Expand All @@ -308,7 +309,7 @@ struct GitArchiveInputScheme : InputScheme
});

auto act = std::make_unique<Activity>(
*logger, lvlInfo, actUnknown, fmt("unpacking '%s' into the Git cache", input.to_string()));
*logger, lvlInfo, actUnknown, fmt("unpacking '%s' into the tarball cache", input.to_string()));

TarArchive archive{*source};
auto tarballCache = settings.getTarballCache();
Expand Down Expand Up @@ -351,7 +352,7 @@ struct GitArchiveInputScheme : InputScheme
input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified));

auto accessor =
settings.getTarballCache()->getAccessor(tarballInfo.treeHash, {}, "«" + input.to_string(true) + "»");
settings.getTarballCache()->getAccessor(tarballInfo.treeHash, "«" + input.to_string(true) + "»");

if (!settings.trustTarballsFromGitForges)
// FIXME: computing the NAR hash here is wasteful if
Expand Down
6 changes: 3 additions & 3 deletions src/libfetchers/include/nix/fetchers/fetch-settings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace nix {

struct GitRepo;
struct SrcToStore;

} // namespace nix

namespace nix::fetchers {

struct Cache;
struct TarballCache;

struct Settings : public Config
{
Expand Down Expand Up @@ -155,7 +155,7 @@ struct Settings : public Config

ref<Cache> getCache() const;

ref<GitRepo> getTarballCache() const;
ref<TarballCache> getTarballCache() const;

/**
* In-memory cache for calls to fetchToStore(); maps source paths to their store
Expand All @@ -171,7 +171,7 @@ private:

mutable Sync<std::shared_ptr<Cache>> _cache;

mutable Sync<std::shared_ptr<GitRepo>> _tarballCache;
mutable Sync<std::shared_ptr<TarballCache>> _tarballCache;
};

} // namespace nix::fetchers
Expand Down
1 change: 1 addition & 0 deletions src/libfetchers/include/nix/fetchers/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ headers = files(
'input-cache.hh',
'provenance.hh',
'registry.hh',
'tarball-cache.hh',
'tarball.hh',
)
59 changes: 59 additions & 0 deletions src/libfetchers/include/nix/fetchers/tarball-cache.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once
///@file

#include "nix/fetchers/git-utils.hh"
#include "nix/util/hash.hh"
#include "nix/util/ref.hh"
#include "nix/util/source-accessor.hh"

#include <filesystem>

namespace nix::fetchers {

struct Settings;

/**
* A content-addressed cache of unpacked tarballs, stored in a SQLite
* database. Files and directories are identified by their Git (SHA-1)
* blob and tree hashes.
*/
struct TarballCache
{
virtual ~TarballCache() = default;

/**
* Open (and create if necessary) the tarball cache stored in the
* SQLite database `dbPath`.
*/
static ref<TarballCache> open(const std::filesystem::path & dbPath);

/**
* Whether the cache contains the tree `treeHash`. If so, then
* everything reachable from that tree is present as well.
*/
virtual bool hasTree(const Hash & treeHash) = 0;

virtual ref<SourceAccessor> getAccessor(const Hash & treeHash, std::string displayPrefix) = 0;

/**
* Return a sink that imports a file system tree into the cache.
* Calling `flush()` on the sink returns the hash of the root
* tree.
*/
virtual ref<GitFileSystemObjectSink> getFileSystemObjectSink() = 0;

/**
* Given a Git tree hash, compute the hash of its NAR
* serialisation. This is memoised on-disk.
*/
virtual Hash treeHashToNarHash(const Settings & settings, const Hash & treeHash) = 0;

/**
* If the specified tree is a directory with a single entry that
* is a directory, return the hash of that entry. Otherwise return
* the passed hash unchanged.
*/
virtual Hash dereferenceSingletonDirectory(const Hash & treeHash) = 0;
};

} // namespace nix::fetchers
4 changes: 4 additions & 0 deletions src/libfetchers/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ deps_public += nlohmann_json
libgit2 = dependency('libgit2', version : '>= 1.9')
deps_private += libgit2

zstd = dependency('libzstd', version : '>= 1.4.0')
deps_private += zstd

subdir('nix-meson-build-support/common')

sources = files(
Expand All @@ -51,6 +54,7 @@ sources = files(
'path.cc',
'provenance.cc',
'registry.cc',
'tarball-cache.cc',
'tarball.cc',
)

Expand Down
2 changes: 2 additions & 0 deletions src/libfetchers/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
nix-store,
nlohmann_json,
libgit2,
zstd,

# Configuration Options

Expand Down Expand Up @@ -35,6 +36,7 @@ mkMesonLibrary (finalAttrs: {

buildInputs = [
libgit2
zstd
];

propagatedBuildInputs = [
Expand Down
Loading
Loading