libutil: convert drainFD to source

Change-Id: I54eb2e59a4009014e324797f16b80b962759c7d3
This commit is contained in:
eldritch horrors 2024-03-23 19:37:09 +01:00
parent 6f30245e0a
commit 98d9dcb221
3 changed files with 11 additions and 8 deletions

View file

@ -377,7 +377,8 @@ std::unique_ptr<Source> sinkToSource(
struct SerializingTransform;
using WireFormatGenerator = Generator<std::span<const char>, SerializingTransform>;
inline void drainGenerator(WireFormatGenerator g, std::derived_from<Sink> auto & into)
template<typename T>
void drainGenerator(Generator<std::span<const char>, T> g, std::derived_from<Sink> auto & into)
{
while (g) {
auto bit = g();
@ -459,7 +460,8 @@ struct SerializingTransform
WireFormatGenerator operator()(const Error & s);
};
inline Sink & operator<<(Sink & sink, WireFormatGenerator && g)
template<typename Transform>
inline Sink & operator<<(Sink & sink, Generator<std::span<const char>, Transform> && g)
{
while (g) {
auto bit = g();

View file

@ -371,7 +371,7 @@ void readFile(const Path & path, Sink & sink)
AutoCloseFD fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)};
if (!fd)
throw SysError("opening file '%s'", path);
drainFD(fd.get(), sink);
drainGenerator(drainFDSource(fd.get()), sink);
}
@ -722,12 +722,12 @@ std::string drainFD(int fd, bool block, const size_t reserveSize)
// the parser needs two extra bytes to append terminating characters, other users will
// not care very much about the extra memory.
StringSink sink(reserveSize + 2);
drainFD(fd, sink, block);
sink << drainFDSource(fd, block);
return std::move(sink.s);
}
void drainFD(int fd, Sink & sink, bool block)
Generator<std::span<const char>> drainFDSource(int fd, bool block)
{
// silence GCC maybe-uninitialized warning in finally
int saved = 0;
@ -756,7 +756,7 @@ void drainFD(int fd, Sink & sink, bool block)
throw SysError("reading from file");
}
else if (rd == 0) break;
else sink({(char *) buf.data(), (size_t) rd});
else co_yield std::span{(char *) buf.data(), (size_t) rd};
}
}
@ -1281,7 +1281,7 @@ void runProgram2(const RunOptions & options)
}
if (options.standardOut)
drainFD(out.readSide.get(), *options.standardOut);
*options.standardOut << drainFDSource(out.readSide.get());
/* Wait for the child to finish. */
int status = pid.wait();

View file

@ -1,6 +1,7 @@
#pragma once
///@file
#include "generator.hh"
#include "types.hh"
#include "error.hh"
#include "logging.hh"
@ -296,7 +297,7 @@ MakeError(EndOfFile, Error);
*/
std::string drainFD(int fd, bool block = true, const size_t reserveSize=0);
void drainFD(int fd, Sink & sink, bool block = true);
Generator<std::span<const char>> drainFDSource(int fd, bool block = true);
/**
* If cgroups are active, attempt to calculate the number of CPUs available.