From: Joel Rosdahl Date: Sat, 15 Jul 2023 19:04:51 +0000 (+0200) Subject: refactor: Move Util::set_cloexec_flag to util X-Git-Tag: v4.9~121 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f5c4696988240835590b3046888b0bbae68c611;p=thirdparty%2Fccache.git refactor: Move Util::set_cloexec_flag to util --- diff --git a/src/Logging.cpp b/src/Logging.cpp index beac47072..25bdea1a7 100644 --- a/src/Logging.cpp +++ b/src/Logging.cpp @@ -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 +#include #ifdef HAVE_UNISTD_H # include @@ -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(); } diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index 844c26176..4f52d58e9 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -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 diff --git a/src/Util.cpp b/src/Util.cpp index afd28d4e0..a3858a487 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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 split_into_views(std::string_view string, const char* separators, diff --git a/src/Util.hpp b/src/Util.hpp index ae8a9d58f..ceed09803 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -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) diff --git a/src/util/file.cpp b/src/util/file.cpp index 49c11942d..b0a718c44 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -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 read_fd(int fd, DataReceiver data_receiver) { diff --git a/src/util/file.hpp b/src/util/file.hpp index 684eb71f2..858083c21 100644 --- a/src/util/file.hpp +++ b/src/util/file.hpp @@ -92,6 +92,9 @@ read_file_part(const std::string& path, size_t pos, size_t count); nonstd::expected 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,