From: Joel Rosdahl Date: Sun, 23 Jul 2023 14:53:25 +0000 (+0200) Subject: refactor: Improve util::read_fd usage X-Git-Tag: v4.9~94 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3efbc7bd2219ae5bade9b9e873157f53b24a6504;p=thirdparty%2Fccache.git refactor: Improve util::read_fd usage --- diff --git a/src/Hash.cpp b/src/Hash.cpp index 0ea8c456c..c84670fc6 100644 --- a/src/Hash.cpp +++ b/src/Hash.cpp @@ -110,8 +110,7 @@ Hash::hash(int64_t x) tl::expected Hash::hash_fd(int fd) { - return util::read_fd( - fd, [this](nonstd::span data) { hash(data); }); + return util::read_fd(fd, [this](auto data) { hash(data); }); } tl::expected diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index d83940970..46370a41e 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -182,26 +182,16 @@ configuration_printer(const std::string& key, PRINT(stdout, "({}) {} = {}\n", origin, key, value); } -static tl::expected, std::string> +static tl::expected read_from_path_or_stdin(const std::string& path) { if (path == "-") { - std::vector output; - const auto result = - util::read_fd(STDIN_FILENO, [&](nonstd::span 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>(path); - if (!result) { - return tl::unexpected(FMT("Failed to read {}: {}", path, result.error())); - } - return *result; + return util::read_file(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 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 {