]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Improve util::read_fd usage
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 23 Jul 2023 14:53:25 +0000 (16:53 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 25 Jul 2023 13:58:24 +0000 (15:58 +0200)
src/Hash.cpp
src/core/mainoptions.cpp

index 0ea8c456c40a478ebbf46cc48abdd0f59904ba74..c84670fc66c0570b65f9dfdba68fe7db275bae1d 100644 (file)
@@ -110,8 +110,7 @@ Hash::hash(int64_t x)
 tl::expected<void, std::string>
 Hash::hash_fd(int fd)
 {
-  return util::read_fd(
-    fd, [this](nonstd::span<const uint8_t> data) { hash(data); });
+  return util::read_fd(fd, [this](auto data) { hash(data); });
 }
 
 tl::expected<void, std::string>
index d83940970b22b60254a2d5e7b0f80cea7adc2ae7..46370a41e5bb45fb77a8fc259f6b4d1779ff7f3a 100644 (file)
@@ -182,26 +182,16 @@ configuration_printer(const std::string& key,
   PRINT(stdout, "({}) {} = {}\n", origin, key, value);
 }
 
-static tl::expected<std::vector<uint8_t>, std::string>
+static tl::expected<util::Bytes, std::string>
 read_from_path_or_stdin(const std::string& path)
 {
   if (path == "-") {
-    std::vector<uint8_t> output;
-    const auto result =
-      util::read_fd(STDIN_FILENO, [&](nonstd::span<const uint8_t> data) {
-        output.insert(output.end(), data.begin(), data.end());
-      });
-    if (!result) {
-      return tl::unexpected(
-        FMT("Failed to read from stdin: {}", result.error()));
-    }
-    return output;
+    return util::read_fd(STDIN_FILENO).transform_error([&](auto error) {
+      return FMT("Failed to read from stdin: {}", error);
+    });
   } else {
-    const auto result = util::read_file<std::vector<uint8_t>>(path);
-    if (!result) {
-      return tl::unexpected(FMT("Failed to read {}: {}", path, result.error()));
-    }
-    return *result;
+    return util::read_file<util::Bytes>(path).transform_error(
+      [&](auto error) { return FMT("Failed to read {}: {}", path, error); });
   }
 }
 
@@ -580,9 +570,7 @@ process_main_options(int argc, const char* const* argv)
       util::XXH3_128 checksum;
       Fd fd(arg == "-" ? STDIN_FILENO : open(arg.c_str(), O_RDONLY));
       if (fd) {
-        util::read_fd(*fd, [&checksum](nonstd::span<const uint8_t> data) {
-          checksum.update(data);
-        });
+        util::read_fd(*fd, [&checksum](auto data) { checksum.update(data); });
         const auto digest = checksum.digest();
         PRINT(stdout, "{}\n", util::format_base16(digest));
       } else {