From db7c3a88b2ca83185b2c86ee60f02d027db63533 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Mon, 28 Jul 2025 17:15:50 +0200 Subject: [PATCH] libutil: move SizedSource into its only user we should not encourage this kind of framing. in the future we will have to do this on async streams, which we *absolutely* should not encourage. Change-Id: Ib89e144afb564284db64fc7367cba7fffc18fdaf --- lix/legacy/nix-store.cc | 28 ++++++++++++++++++++++++++++ lix/libutil/serialise.hh | 35 ----------------------------------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/lix/legacy/nix-store.cc b/lix/legacy/nix-store.cc index 58bf45843..b6766957a 100644 --- a/lix/legacy/nix-store.cc +++ b/lix/legacy/nix-store.cc @@ -1074,6 +1074,34 @@ opServe(std::shared_ptr store, AsyncIoRoot & aio, Strings opFlags, String if (info.narSize == 0) throw Error("narInfo is too old and missing the narSize field"); + struct SizedSource : Source + { + Source & orig; + size_t remain; + SizedSource(Source & orig, size_t size) : orig(orig), remain(size) {} + size_t read(char * data, size_t len) override + { + if (this->remain <= 0) { + throw EndOfFile("sized: unexpected end-of-file"); + } + len = std::min(len, this->remain); + size_t n = this->orig.read(data, len); + this->remain -= n; + return n; + } + + size_t drainAll() + { + std::vector buf(8192); + size_t sum = 0; + while (this->remain > 0) { + size_t n = read(buf.data(), buf.size()); + sum += n; + } + return sum; + } + }; + SizedSource sizedSource(in, info.narSize); AsyncSourceInputStream stream{sizedSource}; diff --git a/lix/libutil/serialise.hh b/lix/libutil/serialise.hh index 58d9f436f..2ea65f3a7 100644 --- a/lix/libutil/serialise.hh +++ b/lix/libutil/serialise.hh @@ -226,41 +226,6 @@ struct TeeSource : Source } }; -/** - * A reader that consumes the original Source until 'size'. - */ -struct SizedSource : Source -{ - Source & orig; - size_t remain; - SizedSource(Source & orig, size_t size) - : orig(orig), remain(size) { } - size_t read(char * data, size_t len) override - { - if (this->remain <= 0) { - throw EndOfFile("sized: unexpected end-of-file"); - } - len = std::min(len, this->remain); - size_t n = this->orig.read(data, len); - this->remain -= n; - return n; - } - - /** - * Consume the original source until no remain data is left to consume. - */ - size_t drainAll() - { - std::vector buf(8192); - size_t sum = 0; - while (this->remain > 0) { - size_t n = read(buf.data(), buf.size()); - sum += n; - } - return sum; - } -}; - /** * A sink that that just counts the number of bytes given to it */