]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::set_cloexec_flag to util
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 15 Jul 2023 19:04:51 +0000 (21:04 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 15 Jul 2023 20:21:15 +0000 (22:21 +0200)
src/Logging.cpp
src/TemporaryFile.cpp
src/Util.cpp
src/Util.hpp
src/util/file.cpp
src/util/file.hpp

index beac470728cd6f1c32f098105fb92869bedc0e6b..25bdea1a79359af59d6e2f64cb93b84c8bca5dcb 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright (C) 2002 Andrew Tridgell
-// Copyright (C) 2009-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2009-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -27,6 +27,7 @@
 #include "fmtmacros.hpp"
 
 #include <core/wincompat.hpp>
+#include <util/file.hpp>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
@@ -143,7 +144,7 @@ init(const Config& config)
     logfile_path = config.log_file();
     logfile.open(logfile_path, "a");
     if (logfile) {
-      Util::set_cloexec_flag(fileno(*logfile));
+      util::set_cloexec_flag(fileno(*logfile));
     } else {
       print_fatal_error_and_exit();
     }
index 844c261764b05f977de206ea537df48968d135e0..4f52d58e9b82c5575b679af08551e37657dd2257 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
+#include <util/file.hpp>
 #include <util/process.hpp>
 
 #include <cstdlib>
@@ -55,7 +56,7 @@ TemporaryFile::TemporaryFile(std::string_view path_prefix,
       FMT("Failed to create temporary file for {}: {}", path, strerror(errno)));
   }
 
-  Util::set_cloexec_flag(*fd);
+  util::set_cloexec_flag(*fd);
 #ifndef _WIN32
   fchmod(*fd, 0666 & ~util::get_umask());
 #endif
index afd28d4e0918fba920a8a7c6f71a06b824877251..a3858a487e7ebfeb75ea6eb62d48076cfb928767 100644 (file)
@@ -682,19 +682,6 @@ send_to_fd(const Context& ctx, std::string_view text, int fd)
   }
 }
 
-void
-set_cloexec_flag(int fd)
-{
-#ifndef _WIN32
-  int flags = fcntl(fd, F_GETFD, 0);
-  if (flags >= 0) {
-    fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
-  }
-#else
-  (void)fd;
-#endif
-}
-
 std::vector<std::string_view>
 split_into_views(std::string_view string,
                  const char* separators,
index ae8a9d58fdc2f227e6c65c44e5f5eb3270c5bf23..ceed09803759244e15202e40927c0e7b83d3019a 100644 (file)
@@ -173,9 +173,6 @@ std::string_view remove_extension(std::string_view path);
 // `core::Error` on error.
 void send_to_fd(const Context& ctx, std::string_view text, int fd);
 
-// Set the FD_CLOEXEC on file descriptor `fd`. This is a NOP on Windows.
-void set_cloexec_flag(int fd);
-
 // Return size change in KiB between `old_stat`  and `new_stat`.
 inline int64_t
 size_change_kibibyte(const Stat& old_stat, const Stat& new_stat)
index 49c11942d45ac9fd3dbaf3dac296386c40b458f9..b0a718c4483dd6298193bf8015d51f3ac259b169 100644 (file)
@@ -166,6 +166,19 @@ fallocate(int fd, size_t new_size)
   return {};
 }
 
+void
+set_cloexec_flag(int fd)
+{
+#ifndef _WIN32
+  int flags = fcntl(fd, F_GETFD, 0);
+  if (flags >= 0) {
+    fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+  }
+#else
+  (void)fd;
+#endif
+}
+
 nonstd::expected<void, std::string>
 read_fd(int fd, DataReceiver data_receiver)
 {
index 684eb71f2820368ffd84371ccad964150cd42151..858083c21fd204520bbc3734d7604128ccf36764 100644 (file)
@@ -92,6 +92,9 @@ read_file_part(const std::string& path, size_t pos, size_t count);
 nonstd::expected<void, std::string> rename(const std::string& oldpath,
                                            const std::string& newpath);
 
+// Set the FD_CLOEXEC on file descriptor `fd`. This is a NOP on Windows.
+void set_cloexec_flag(int fd);
+
 // Set atime/mtime of `path`. If `mtime` is std::nullopt, set to the current
 // time. If `atime` is std::nullopt, set to what `mtime` specifies.
 void set_timestamps(const std::string& path,