From: Joel Rosdahl Date: Fri, 14 Jul 2023 18:43:28 +0000 (+0200) Subject: refactor: Move Util::{g,s}et_umask to util X-Git-Tag: v4.9~126 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b53a79a96c74b89220e89b532313c233a0d5f82;p=thirdparty%2Fccache.git refactor: Move Util::{g,s}et_umask to util --- diff --git a/src/Context.cpp b/src/Context.cpp index 0a45d9e61..cdf79ea1f 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef HAVE_UNISTD_H # include @@ -66,7 +67,7 @@ Context::initialize(Args&& compiler_and_args, // in the cache directory should be affected by the configured umask and that // no other files and directories should. if (config.umask()) { - original_umask = Util::set_umask(*config.umask()); + original_umask = util::set_umask(*config.umask()); } } diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index 257481234..844c26176 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2022 Joel Rosdahl and other contributors +// Copyright (C) 2020-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -22,6 +22,7 @@ #include #include +#include #include @@ -56,7 +57,7 @@ TemporaryFile::TemporaryFile(std::string_view path_prefix, Util::set_cloexec_flag(*fd); #ifndef _WIN32 - fchmod(*fd, 0666 & ~Util::get_umask()); + fchmod(*fd, 0666 & ~util::get_umask()); #endif } diff --git a/src/UmaskScope.hpp b/src/UmaskScope.hpp index 79113503b..88867ed3a 100644 --- a/src/UmaskScope.hpp +++ b/src/UmaskScope.hpp @@ -18,7 +18,7 @@ #pragma once -#include +#include #include #include @@ -43,7 +43,7 @@ inline UmaskScope::UmaskScope(std::optional new_umask) { #ifndef _WIN32 if (new_umask) { - m_saved_umask = Util::set_umask(*new_umask); + m_saved_umask = util::set_umask(*new_umask); } #else (void)new_umask; @@ -65,7 +65,7 @@ UmaskScope::release() # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" # endif - Util::set_umask(*m_saved_umask); + util::set_umask(*m_saved_umask); # if defined(__GNUC__) && !defined(__clang__) # pragma GCC diagnostic pop # endif diff --git a/src/Util.cpp b/src/Util.cpp index a24ff3b64..c5f6693bd 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -55,13 +55,6 @@ namespace fs = std::filesystem; namespace { -// Process umask, read and written by get_umask and set_umask. -mode_t g_umask = [] { - const mode_t mask = umask(0); - umask(mask); - return mask; -}(); - // Search for the first match of the following regular expression: // // \x1b\[[\x30-\x3f]*[\x20-\x2f]*[Km] @@ -464,12 +457,6 @@ get_relative_path(std::string_view dir, std::string_view path) return result.empty() ? "." : result; } -mode_t -get_umask() -{ - return g_umask; -} - std::optional is_absolute_path_with_prefix(std::string_view path) { @@ -743,13 +730,6 @@ set_cloexec_flag(int fd) #endif } -mode_t -set_umask(mode_t mask) -{ - g_umask = mask; - return umask(mask); -} - std::vector split_into_views(std::string_view string, const char* separators, diff --git a/src/Util.hpp b/src/Util.hpp index e8f3f0c27..14008c928 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -104,9 +104,6 @@ const char* get_hostname(); // resolve to the same file as `path`. std::string get_relative_path(std::string_view dir, std::string_view path); -// Get process umask. -mode_t get_umask(); - // Determine if `path` is an absolute path with prefix, returning the split // point. std::optional is_absolute_path_with_prefix(std::string_view path); @@ -188,9 +185,6 @@ 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); -// Set process umask. Returns the previous mask. -mode_t set_umask(mode_t mask); - // 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/ccache.cpp b/src/ccache.cpp index d43cc0ddc..c7e858264 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2393,7 +2393,7 @@ cache_compilation(int argc, const char* const* argv) if (fall_back_to_original_compiler) { if (original_umask) { - Util::set_umask(*original_umask); + util::set_umask(*original_umask); } auto execv_argv = saved_orig_args.to_argv(); execute_noreturn(execv_argv.data(), saved_temp_dir); diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 01cf78102..9703f21e6 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index e46abcaa3..5410bc962 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #ifdef INODE_CACHE_SUPPORTED @@ -655,7 +656,7 @@ LocalStorage::clone_hard_link_or_copy_file(const std::string& source, fs::create_hard_link(source, dest, ec); if (!ec) { #ifndef _WIN32 - if (chmod(dest.c_str(), 0444 & ~Util::get_umask()) != 0) { + if (chmod(dest.c_str(), 0444 & ~util::get_umask()) != 0) { LOG("Failed to chmod {}: {}", dest.c_str(), strerror(errno)); } #endif diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 234318bea..d1d5925a8 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -9,6 +9,7 @@ set( environment.cpp file.cpp path.cpp + process.cpp string.cpp zstd.cpp ) diff --git a/src/util/process.cpp b/src/util/process.cpp new file mode 100644 index 000000000..64c6bfdfd --- /dev/null +++ b/src/util/process.cpp @@ -0,0 +1,49 @@ +// Copyright (C) 2023 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include "process.hpp" + +#include + +namespace { + +// Process umask, read and written by get_umask and set_umask. +mode_t g_umask = [] { + const mode_t mask = umask(0); + umask(mask); + return mask; +}(); + +} // namespace + +namespace util { + +mode_t +get_umask() +{ + return g_umask; +} + +mode_t +set_umask(mode_t mask) +{ + g_umask = mask; + return umask(mask); +} + +} // namespace util diff --git a/src/util/process.hpp b/src/util/process.hpp new file mode 100644 index 000000000..a37f3d59f --- /dev/null +++ b/src/util/process.hpp @@ -0,0 +1,31 @@ +// Copyright (C) 2023 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#pragma once + +#include + +namespace util { + +// Get process umask. +mode_t get_umask(); + +// Set process umask. Returns the previous mask. +mode_t set_umask(mode_t mask); + +} // namespace util