From 821ad98beb1a915ea7a456c274bcfca9e059ba91 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Fri, 5 Apr 2024 03:51:45 +0200 Subject: Revert "libutil: drop Fs{Source,Sink}::good" This reverts commit 1340807e30dba4b3972c31f02861bbaeaeb60e61. Change-Id: I34d2a80eb3c3e9d79cb02b92cd1189da32d18cb6 --- src/libutil/serialise.cc | 23 ++++++++++++++++++++--- src/libutil/serialise.hh | 11 +++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) (limited to 'src/libutil') diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc index 6450a9651..692144b75 100644 --- a/src/libutil/serialise.cc +++ b/src/libutil/serialise.cc @@ -52,7 +52,18 @@ FdSink::~FdSink() void FdSink::writeUnbuffered(std::string_view data) { written += data.size(); - writeFull(fd, data); + try { + writeFull(fd, data); + } catch (SysError & e) { + _good = false; + throw; + } +} + + +bool FdSink::good() +{ + return _good; } @@ -117,13 +128,19 @@ size_t FdSource::readUnbuffered(char * data, size_t len) checkInterrupt(); n = ::read(fd, data, len); } while (n == -1 && errno == EINTR); - if (n == -1) { throw SysError("reading from file"); } - if (n == 0) { throw EndOfFile(std::string(*endOfFileError)); } + if (n == -1) { _good = false; throw SysError("reading from file"); } + if (n == 0) { _good = false; throw EndOfFile(std::string(*endOfFileError)); } read += n; return n; } +bool FdSource::good() +{ + return _good; +} + + size_t StringSource::read(char * data, size_t len) { if (pos == s.size()) throw EndOfFile("end of string reached"); diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh index e6290a652..d1c791823 100644 --- a/src/libutil/serialise.hh +++ b/src/libutil/serialise.hh @@ -18,6 +18,7 @@ struct Sink { virtual ~Sink() { } virtual void operator () (std::string_view data) = 0; + virtual bool good() { return true; } }; /** @@ -79,6 +80,8 @@ struct Source */ virtual size_t read(char * data, size_t len) = 0; + virtual bool good() { return true; } + void drainInto(Sink & sink); std::string drain(); @@ -133,6 +136,11 @@ struct FdSink : BufferedSink ~FdSink(); void writeUnbuffered(std::string_view data) override; + + bool good() override; + +private: + bool _good = true; }; @@ -157,8 +165,11 @@ struct FdSource : BufferedSource return *this; } + bool good() override; protected: size_t readUnbuffered(char * data, size_t len) override; +private: + bool _good = true; }; -- cgit v1.2.3