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 */