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

libutil: Get rid of restartableSourceFromFactory

Instead we can just seek back in the file - duh. Also this makes use
of the anonymous temp file facility, since that is much safer (no need
window where the we don't have an open file descriptor for it).
This commit is contained in:
Sergei Zimmerman 2025-12-01 04:21:27 +03:00
parent 4ad272015e
commit 4b3536e092
No known key found for this signature in database
4 changed files with 43 additions and 73 deletions

View file

@ -79,8 +79,8 @@ std::optional<std::string> BinaryCacheStore::getNixCacheInfo()
void BinaryCacheStore::upsertFile(
const std::string & path, std::string && data, const std::string & mimeType, uint64_t sizeHint)
{
auto source = restartableSourceFromFactory([data = std::move(data)]() { return make_unique<StringSource>(data); });
upsertFile(path, *source, mimeType, sizeHint);
StringSource source{data};
upsertFile(path, source, mimeType, sizeHint);
}
void BinaryCacheStore::getFile(const std::string & path, Callback<std::optional<std::string>> callback) noexcept
@ -140,9 +140,7 @@ void BinaryCacheStore::writeNarInfo(ref<NarInfo> narInfo)
ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
Source & narSource, RepairFlag repair, CheckSigsFlag checkSigs, std::function<ValidPathInfo(HashResult)> mkInfo)
{
auto [fdTemp, fnTemp] = createTempFile();
AutoDelete autoDelete(fnTemp);
auto fdTemp = createAnonymousTempFile();
auto now1 = std::chrono::steady_clock::now();
@ -272,19 +270,10 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
/* Atomically write the NAR file. */
if (repair || !fileExists(narInfo->url)) {
auto source = restartableSourceFromFactory([fnTemp]() {
struct AutoCloseFDSource : AutoCloseFD, FdSource
{
AutoCloseFDSource(AutoCloseFD fd)
: AutoCloseFD(std::move(fd))
, FdSource(get())
{
}
};
return std::make_unique<AutoCloseFDSource>(toDescriptor(open(fnTemp.c_str(), O_RDONLY)));
});
FdSource source{fdTemp.get()};
source.restart(); /* Seek back to the start of the file. */
stats.narWrite++;
upsertFile(narInfo->url, *source, "application/x-nix-nar", narInfo->fileSize);
upsertFile(narInfo->url, source, "application/x-nix-nar", narInfo->fileSize);
} else
stats.narWriteAverted++;