]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::read_fd variant that returns all data at once
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 23 Jul 2023 06:50:56 +0000 (08:50 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 25 Jul 2023 13:58:16 +0000 (15:58 +0200)
src/util/file.cpp
src/util/file.hpp

index 1647da49f499a4d8fc854dd91da81c7875638702..4c739ea1d8797743df54a848a44e2b5742e113aa 100644 (file)
@@ -204,6 +204,17 @@ read_fd(int fd, DataReceiver data_receiver)
   return {};
 }
 
+tl::expected<Bytes, std::string>
+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)
index c65514b0482f236d1fc188a719179be9cdb6a7a1..3ee0e1f06f09716bed7cefb3d54e607aa8f7e62d 100644 (file)
@@ -18,6 +18,7 @@
 
 #pragma once
 
+#include <util/Bytes.hpp>
 #include <util/TimePoint.hpp>
 #include <util/types.hpp>
 
@@ -59,10 +60,15 @@ tl::expected<void, std::string> 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<void, std::string> read_fd(int fd, DataReceiver data_receiver);
 
+// Read data from `fd`. Returns an error if the underlying read(2) call returned
+// -1.
+tl::expected<util::Bytes, std::string> read_fd(int fd);
+
 // Return contents of file at  `path`.
 //
 // `T` should be `util::Bytes` or `std::vector<uint8_t>` for binary data and