From ebe3d2d3704aca001ff818295af014581dcf348f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 21 Aug 2018 15:22:04 +0200 Subject: [PATCH] Improve 'coroutine has finished' error message --- src/libstore/store-api.cc | 2 ++ src/libutil/serialise.cc | 15 +++++++++------ src/libutil/serialise.hh | 6 +++++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 7a4a5f5eb..1f42097fc 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -609,6 +609,8 @@ void copyStorePath(ref srcStore, ref dstStore, act.progress(total, info->narSize); }); srcStore->narFromPath({storePath}, wrapperSink); + }, [&]() { + throw EndOfFile("NAR for '%s' fetched from '%s' is incomplete", storePath, srcStore->getUri()); }); dstStore->addToStore(*info, *source, repair, checkSigs); diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc index b2c49d911..17448f70e 100644 --- a/src/libutil/serialise.cc +++ b/src/libutil/serialise.cc @@ -161,16 +161,20 @@ size_t StringSource::read(unsigned char * data, size_t len) #error Coroutines are broken in this version of Boost! #endif -std::unique_ptr sinkToSource(std::function fun) +std::unique_ptr sinkToSource( + std::function fun, + std::function eof) { struct SinkToSource : Source { typedef boost::coroutines2::coroutine coro_t; + std::function eof; coro_t::pull_type coro; - SinkToSource(std::function fun) - : coro([&](coro_t::push_type & yield) { + SinkToSource(std::function fun, std::function eof) + : eof(eof) + , coro([&](coro_t::push_type & yield) { LambdaSink sink([&](const unsigned char * data, size_t len) { if (len) yield(std::string((const char *) data, len)); }); @@ -184,8 +188,7 @@ std::unique_ptr sinkToSource(std::function fun) size_t read(unsigned char * data, size_t len) override { - if (!coro) - throw EndOfFile("coroutine has finished"); + if (!coro) { eof(); abort(); } if (pos == cur.size()) { if (!cur.empty()) coro(); @@ -201,7 +204,7 @@ std::unique_ptr sinkToSource(std::function fun) } }; - return std::make_unique(fun); + return std::make_unique(fun, eof); } diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh index 14b62fdb6..4b6ad5da5 100644 --- a/src/libutil/serialise.hh +++ b/src/libutil/serialise.hh @@ -214,7 +214,11 @@ struct LambdaSource : Source /* Convert a function that feeds data into a Sink into a Source. The Source executes the function as a coroutine. */ -std::unique_ptr sinkToSource(std::function fun); +std::unique_ptr sinkToSource( + std::function fun, + std::function eof = []() { + throw EndOfFile("coroutine has finished"); + }); void writePadding(size_t len, Sink & sink);