]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Simplify code related to send_to_fd
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 24 Nov 2021 20:26:12 +0000 (21:26 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 24 Nov 2021 20:26:12 +0000 (21:26 +0100)
src/ResultRetriever.cpp
src/Util.cpp
src/Util.hpp
src/ccache.cpp

index 2e0bf4740aa1e3802a7369eeb879a4401705512a..5298c711bd865a8f44a02241c445e782d28e9b0a 100644 (file)
@@ -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();
   }
index dae186293034bd39fc920a65842b1495bb5d3e65..2219048533584b74d01b61d440866c584ce17238 100644 (file)
@@ -26,6 +26,7 @@
 #include "Win32Util.hpp"
 #include "fmtmacros.hpp"
 
+#include <Finalizer.hpp>
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
 #include <util/path.hpp>
@@ -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) {
index ef05852ed4af9039237d791b31a657020c7fab99..3647f3f30801796bec727d1cba10208767b3a370 100644 (file)
 #include <utility>
 #include <vector>
 
-#ifdef HAVE_UNISTD_H
-#  include <unistd.h>
-#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);
index df8e6206841e189f21b9765f943e8cff27d78442..eca0c91bd0d657598e727d56b11b7735faafda5a 100644 (file)
@@ -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;
 }