From: Joel Rosdahl Date: Sun, 23 Jul 2023 06:50:56 +0000 (+0200) Subject: enhance: Add util::read_fd variant that returns all data at once X-Git-Tag: v4.9~95 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=baa6cb31e70cd42d7edd22524cefff66c866da0d;p=thirdparty%2Fccache.git enhance: Add util::read_fd variant that returns all data at once --- diff --git a/src/util/file.cpp b/src/util/file.cpp index 1647da49f..4c739ea1d 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -204,6 +204,17 @@ read_fd(int fd, DataReceiver data_receiver) return {}; } +tl::expected +read_fd(int fd) +{ + Bytes output; + return read_fd(fd, + [&](auto data) { + output.insert(output.end(), data.begin(), data.end()); + }) + .transform([&] { return output; }); +} + #ifdef _WIN32 static bool has_utf16_le_bom(std::string_view text) diff --git a/src/util/file.hpp b/src/util/file.hpp index c65514b04..3ee0e1f06 100644 --- a/src/util/file.hpp +++ b/src/util/file.hpp @@ -18,6 +18,7 @@ #pragma once +#include #include #include @@ -59,10 +60,15 @@ tl::expected fallocate(int fd, size_t new_size); // Return how much a file of `size` bytes likely would take on disk. uint64_t likely_size_on_disk(uint64_t size); -// Read data from `fd` until end of file and call `data_receiver` with the read -// data. Returns an error if the underlying read(2) call returned -1. +// Read data from `fd` until end of file and call `data_receiver` repeatedly +// with the read data. Returns an error if the underlying read(2) call returned +// -1. tl::expected read_fd(int fd, DataReceiver data_receiver); +// Read data from `fd`. Returns an error if the underlying read(2) call returned +// -1. +tl::expected read_fd(int fd); + // Return contents of file at `path`. // // `T` should be `util::Bytes` or `std::vector` for binary data and