From: Joel Rosdahl Date: Wed, 24 Nov 2021 20:26:12 +0000 (+0100) Subject: refactor: Simplify code related to send_to_fd X-Git-Tag: v4.6~73 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e7aa5368373f2caad29063fbd2f12c00dd3da32f;p=thirdparty%2Fccache.git refactor: Simplify code related to send_to_fd --- diff --git a/src/ResultRetriever.cpp b/src/ResultRetriever.cpp index 2e0bf4740..5298c711b 100644 --- a/src/ResultRetriever.cpp +++ b/src/ResultRetriever.cpp @@ -160,7 +160,7 @@ ResultRetriever::on_entry_end() { if (m_dest_file_type == FileType::stderr_output) { LOG("Writing to file descriptor {}", STDERR_FILENO); - Util::send_to_stderr(m_ctx, m_dest_data); + Util::send_to_fd(m_ctx, m_dest_data, STDERR_FILENO); } else if (m_dest_file_type == FileType::dependency && !m_dest_path.empty()) { write_dependency_file(); } diff --git a/src/Util.cpp b/src/Util.cpp index dae186293..221904853 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -26,6 +26,7 @@ #include "Win32Util.hpp" #include "fmtmacros.hpp" +#include #include #include #include @@ -1174,26 +1175,6 @@ same_program_name(nonstd::string_view program_name, #endif } -#ifdef _WIN32 -// Stdout/stderr are normally opened in text mode, which would convert newlines -// a second time, since we treat outputs as binary data. -// Make sure to switch to binary mode. -namespace { -struct BinaryModeHelper -{ - BinaryModeHelper(int fd_) : fd(fd_), oldmode(_setmode(fd, _O_BINARY)) - { - } - ~BinaryModeHelper() - { - _setmode(fd, oldmode); - } - int fd; - int oldmode; -}; -} // namespace -#endif - void send_to_fd(const Context& ctx, const std::string& text, int fd) { @@ -1201,7 +1182,11 @@ send_to_fd(const Context& ctx, const std::string& text, int fd) std::string modified_text; #ifdef _WIN32 - BinaryModeHelper helper(fd); + // stdout/stderr are normally opened in text mode, which would convert + // newlines a second time since we treat output as binary data. Make sure to + // switch to binary mode. + int oldmode = _setmode(fd, _O_BINARY); + Finalizer binary_mode_restorer([=] { _setmode(fd, oldmode); }); #endif if (ctx.args_info.strip_diagnostics_colors) { diff --git a/src/Util.hpp b/src/Util.hpp index ef05852ed..3647f3f30 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -33,10 +33,6 @@ #include #include -#ifdef HAVE_UNISTD_H -# include -#endif - class Context; namespace Util { @@ -322,16 +318,11 @@ void rename(const std::string& oldpath, const std::string& newpath); bool same_program_name(nonstd::string_view program_name, nonstd::string_view canonical_program_name); -// Send `text` to file descriptor 'fd', optionally stripping ANSI color +// Send `text` to file descriptor `fd`, optionally stripping ANSI color // sequences if `ctx.args_info.strip_diagnostics_colors` is true and rewriting // paths to absolute if `ctx.config.absolute_paths_in_stderr` is true. Throws // `core::Error` on error. void send_to_fd(const Context& ctx, const std::string& text, int fd); -inline void -send_to_stderr(const Context& ctx, const std::string& text) -{ - send_to_fd(ctx, text, STDERR_FILENO); -} // Set the FD_CLOEXEC on file descriptor `fd`. This is a NOP on Windows. void set_cloexec_flag(int fd); diff --git a/src/ccache.cpp b/src/ccache.cpp index df8e62068..eca0c91bd 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -974,7 +974,7 @@ to_cache(Context& ctx, LOG("Compiler gave exit status {}", *status); // We can output stderr immediately instead of rerunning the compiler. - Util::send_to_stderr(ctx, Util::read_file(tmp_stderr_path)); + Util::send_to_fd(ctx, Util::read_file(tmp_stderr_path), STDERR_FILENO); auto failure = Failure(Statistic::compile_failed); failure.set_exit_code(*status); @@ -1022,7 +1022,7 @@ to_cache(Context& ctx, } // Everything OK. - Util::send_to_stderr(ctx, Util::read_file(tmp_stderr_path)); + Util::send_to_fd(ctx, Util::read_file(tmp_stderr_path), STDERR_FILENO); return *result_key; }