Skip to content

Commit 070e8ee

Browse files
authored
Merge pull request #14368 from NixOS/keep-tarball-cache-open
Move getTarballCache() into fetchers::Settings
2 parents 46b5d2e + bc6b9ce commit 070e8ee

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

src/libfetchers/git-utils.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,13 +1328,18 @@ std::vector<std::tuple<GitRepoImpl::Submodule, Hash>> GitRepoImpl::getSubmodules
13281328
return result;
13291329
}
13301330

1331-
ref<GitRepo> getTarballCache()
1332-
{
1333-
static auto repoDir = std::filesystem::path(getCacheDir()) / "tarball-cache";
1331+
namespace fetchers {
13341332

1335-
return GitRepo::openRepo(repoDir, true, true);
1333+
ref<GitRepo> Settings::getTarballCache() const
1334+
{
1335+
auto tarballCache(_tarballCache.lock());
1336+
if (!*tarballCache)
1337+
*tarballCache = GitRepo::openRepo(std::filesystem::path(getCacheDir()) / "tarball-cache", true, true);
1338+
return ref<GitRepo>(*tarballCache);
13361339
}
13371340

1341+
} // namespace fetchers
1342+
13381343
GitRepo::WorkdirInfo GitRepo::getCachedWorkdirInfo(const std::filesystem::path & path)
13391344
{
13401345
static Sync<std::map<std::filesystem::path, WorkdirInfo>> _cache;

src/libfetchers/github.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ struct GitArchiveInputScheme : InputScheme
270270
if (auto lastModifiedAttrs = cache->lookup(lastModifiedKey)) {
271271
auto treeHash = getRevAttr(*treeHashAttrs, "treeHash");
272272
auto lastModified = getIntAttr(*lastModifiedAttrs, "lastModified");
273-
if (getTarballCache()->hasObject(treeHash))
273+
if (input.settings->getTarballCache()->hasObject(treeHash))
274274
return {std::move(input), TarballInfo{.treeHash = treeHash, .lastModified = (time_t) lastModified}};
275275
else
276276
debug("Git tree with hash '%s' has disappeared from the cache, refetching...", treeHash.gitRev());
@@ -290,7 +290,7 @@ struct GitArchiveInputScheme : InputScheme
290290
*logger, lvlInfo, actUnknown, fmt("unpacking '%s' into the Git cache", input.to_string()));
291291

292292
TarArchive archive{*source};
293-
auto tarballCache = getTarballCache();
293+
auto tarballCache = input.settings->getTarballCache();
294294
auto parseSink = tarballCache->getFileSystemObjectSink();
295295
auto lastModified = unpackTarfileToSink(archive, *parseSink);
296296
auto tree = parseSink->flush();
@@ -324,7 +324,8 @@ struct GitArchiveInputScheme : InputScheme
324324
#endif
325325
input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified));
326326

327-
auto accessor = getTarballCache()->getAccessor(tarballInfo.treeHash, false, "«" + input.to_string() + "»");
327+
auto accessor =
328+
input.settings->getTarballCache()->getAccessor(tarballInfo.treeHash, false, "«" + input.to_string() + "»");
328329

329330
return {accessor, input};
330331
}

src/libfetchers/include/nix/fetchers/fetch-settings.hh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
#include <sys/types.h>
1313

14+
namespace nix {
15+
16+
struct GitRepo;
17+
18+
}
19+
1420
namespace nix::fetchers {
1521

1622
struct Cache;
@@ -125,8 +131,12 @@ struct Settings : public Config
125131

126132
ref<Cache> getCache() const;
127133

134+
ref<GitRepo> getTarballCache() const;
135+
128136
private:
129137
mutable Sync<std::shared_ptr<Cache>> _cache;
138+
139+
mutable Sync<std::shared_ptr<GitRepo>> _tarballCache;
130140
};
131141

132142
} // namespace nix::fetchers

src/libfetchers/include/nix/fetchers/git-utils.hh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ struct GitRepo
120120
virtual Hash dereferenceSingletonDirectory(const Hash & oid) = 0;
121121
};
122122

123-
ref<GitRepo> getTarballCache();
124-
125123
// A helper to ensure that the `git_*_free` functions get called.
126124
template<auto del>
127125
struct Deleter

src/libfetchers/tarball.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ static DownloadTarballResult downloadTarball_(
136136
.treeHash = treeHash,
137137
.lastModified = (time_t) getIntAttr(infoAttrs, "lastModified"),
138138
.immutableUrl = maybeGetStrAttr(infoAttrs, "immutableUrl"),
139-
.accessor = getTarballCache()->getAccessor(treeHash, false, displayPrefix),
139+
.accessor = settings.getTarballCache()->getAccessor(treeHash, false, displayPrefix),
140140
};
141141
};
142142

143-
if (cached && !getTarballCache()->hasObject(getRevAttr(cached->value, "treeHash")))
143+
if (cached && !settings.getTarballCache()->hasObject(getRevAttr(cached->value, "treeHash")))
144144
cached.reset();
145145

146146
if (cached && !cached->expired)
@@ -179,7 +179,7 @@ static DownloadTarballResult downloadTarball_(
179179
TarArchive{path};
180180
})
181181
: TarArchive{*source};
182-
auto tarballCache = getTarballCache();
182+
auto tarballCache = settings.getTarballCache();
183183
auto parseSink = tarballCache->getFileSystemObjectSink();
184184
auto lastModified = unpackTarfileToSink(archive, *parseSink);
185185
auto tree = parseSink->flush();
@@ -398,7 +398,9 @@ struct TarballInputScheme : CurlInputScheme
398398

399399
input.attrs.insert_or_assign(
400400
"narHash",
401-
getTarballCache()->treeHashToNarHash(*input.settings, result.treeHash).to_string(HashFormat::SRI, true));
401+
input.settings->getTarballCache()
402+
->treeHashToNarHash(*input.settings, result.treeHash)
403+
.to_string(HashFormat::SRI, true));
402404

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

0 commit comments

Comments
 (0)