From e947c895ecebc987c7364eaec063fd6383bd4e88 Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Tue, 28 Oct 2025 03:02:02 +0300 Subject: [PATCH] binary-cache-store: UpsertFile accept `Source &` instead of `std::istream` --- src/libstore/binary-cache-store.cc | 11 +++++------ src/libstore/http-binary-cache-store.cc | 8 ++------ .../include/nix/store/binary-cache-store.hh | 7 ++----- .../include/nix/store/http-binary-cache-store.hh | 7 ++----- src/libstore/local-binary-cache-store.cc | 7 +------ src/libstore/s3-binary-cache-store.cc | 14 ++++---------- 6 files changed, 16 insertions(+), 38 deletions(-) diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index 9bb81add7..0133a45e7 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -79,7 +79,8 @@ std::optional BinaryCacheStore::getNixCacheInfo() void BinaryCacheStore::upsertFile( const std::string & path, std::string && data, const std::string & mimeType, uint64_t sizeHint) { - upsertFile(path, std::make_shared(std::move(data)), mimeType, sizeHint); + StringSource source{data}; + upsertFile(path, source, mimeType, sizeHint); } void BinaryCacheStore::getFile(const std::string & path, Callback> callback) noexcept @@ -271,12 +272,10 @@ ref BinaryCacheStore::addToStoreCommon( /* Atomically write the NAR file. */ if (repair || !fileExists(narInfo->url)) { + AutoCloseFD fd = toDescriptor(open(fnTemp.c_str(), O_RDONLY)); + FdSource source{fd.get()}; stats.narWrite++; - upsertFile( - narInfo->url, - std::make_shared(fnTemp, std::ios_base::in | std::ios_base::binary), - "application/x-nix-nar", - narInfo->fileSize); + upsertFile(narInfo->url, source, "application/x-nix-nar", narInfo->fileSize); } else stats.narWriteAverted++; diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 4b8276572..987ceef97 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -135,15 +135,11 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path) } void HttpBinaryCacheStore::upsertFile( - const std::string & path, - std::shared_ptr> istream, - const std::string & mimeType, - uint64_t sizeHint) + const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) { auto req = makeRequest(path); req.method = HttpMethod::PUT; - auto data = StreamToSourceAdapter(istream).drain(); - + auto data = source.drain(); auto compressionMethod = getCompressionMethod(path); if (compressionMethod) { diff --git a/src/libstore/include/nix/store/binary-cache-store.hh b/src/libstore/include/nix/store/binary-cache-store.hh index 0bed09aec..e5c88a8d4 100644 --- a/src/libstore/include/nix/store/binary-cache-store.hh +++ b/src/libstore/include/nix/store/binary-cache-store.hh @@ -100,11 +100,8 @@ public: virtual bool fileExists(const std::string & path) = 0; - virtual void upsertFile( - const std::string & path, - std::shared_ptr> istream, - const std::string & mimeType, - uint64_t sizeHint) = 0; + virtual void + upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) = 0; void upsertFile( const std::string & path, diff --git a/src/libstore/include/nix/store/http-binary-cache-store.hh b/src/libstore/include/nix/store/http-binary-cache-store.hh index ecad09975..723034d4d 100644 --- a/src/libstore/include/nix/store/http-binary-cache-store.hh +++ b/src/libstore/include/nix/store/http-binary-cache-store.hh @@ -80,11 +80,8 @@ protected: bool fileExists(const std::string & path) override; - void upsertFile( - const std::string & path, - std::shared_ptr> istream, - const std::string & mimeType, - uint64_t sizeHint) override; + void + upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override; FileTransferRequest makeRequest(std::string_view path); diff --git a/src/libstore/local-binary-cache-store.cc b/src/libstore/local-binary-cache-store.cc index c1811bf17..8f07286f4 100644 --- a/src/libstore/local-binary-cache-store.cc +++ b/src/libstore/local-binary-cache-store.cc @@ -53,17 +53,12 @@ protected: bool fileExists(const std::string & path) override; - void upsertFile( - const std::string & path, - std::shared_ptr> istream, - const std::string & mimeType, - uint64_t sizeHint) override + void upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override { auto path2 = config->binaryCacheDir + "/" + path; static std::atomic counter{0}; Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter); AutoDelete del(tmp, false); - StreamToSourceAdapter source(istream); writeFile(tmp, source); std::filesystem::rename(tmp, path2); del.cancel(); diff --git a/src/libstore/s3-binary-cache-store.cc b/src/libstore/s3-binary-cache-store.cc index 8617c9fe9..387efbeb4 100644 --- a/src/libstore/s3-binary-cache-store.cc +++ b/src/libstore/s3-binary-cache-store.cc @@ -20,11 +20,8 @@ public: { } - void upsertFile( - const std::string & path, - std::shared_ptr> istream, - const std::string & mimeType, - uint64_t sizeHint) override; + void + upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override; private: ref s3Config; @@ -70,12 +67,9 @@ private: }; void S3BinaryCacheStore::upsertFile( - const std::string & path, - std::shared_ptr> istream, - const std::string & mimeType, - uint64_t sizeHint) + const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) { - HttpBinaryCacheStore::upsertFile(path, istream, mimeType, sizeHint); + HttpBinaryCacheStore::upsertFile(path, source, mimeType, sizeHint); } std::string S3BinaryCacheStore::createMultipartUpload(