]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::{g,s}et_umask to util
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 14 Jul 2023 18:43:28 +0000 (20:43 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 15 Jul 2023 19:03:53 +0000 (21:03 +0200)
src/Context.cpp
src/TemporaryFile.cpp
src/UmaskScope.hpp
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/core/mainoptions.cpp
src/storage/local/LocalStorage.cpp
src/util/CMakeLists.txt
src/util/process.cpp [new file with mode: 0644]
src/util/process.hpp [new file with mode: 0644]

index 0a45d9e613ea1ba9562e2436d89d4582ea7e1fa3..cdf79ea1f4016b7f663e2464c0f46227cfd56152 100644 (file)
@@ -27,6 +27,7 @@
 #include <core/wincompat.hpp>
 #include <util/TimePoint.hpp>
 #include <util/path.hpp>
+#include <util/process.hpp>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
@@ -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());
   }
 }
 
index 257481234499b80ea6fd950c394d7df11b4da14f..844c261764b05f977de206ea537df48968d135e0 100644 (file)
@@ -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 <core/exceptions.hpp>
 #include <fmtmacros.hpp>
+#include <util/process.hpp>
 
 #include <cstdlib>
 
@@ -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
 }
 
index 79113503bbb35cd7d0db21874a09bcd61b8e8ad3..88867ed3af57b7d36508643a6f89f47573b3a957 100644 (file)
@@ -18,7 +18,7 @@
 
 #pragma once
 
-#include <Util.hpp>
+#include <util/process.hpp>
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -43,7 +43,7 @@ inline UmaskScope::UmaskScope(std::optional<mode_t> 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
index a24ff3b641d0fe6906883687aba1bf16c3cfab18..c5f6693bd984815603b446c11edb64623d7583b1 100644 (file)
@@ -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<size_t>
 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<std::string_view>
 split_into_views(std::string_view string,
                  const char* separators,
index e8f3f0c27814d629188b50ea93be71b0db9e53a8..14008c928215cfc650bd11a976386ae2b2384be8 100644 (file)
@@ -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<size_t> 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)
index d43cc0ddcf112f79996d91fc9abbe318e6e6274f..c7e85826420ef3b7a4de77f7a9c0d14d34fcdb0b 100644 (file)
@@ -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);
index 01cf78102705d17f17052b15ae575e2406c40e13..9703f21e6b9da72afa113906174186ff8d972672 100644 (file)
@@ -28,6 +28,7 @@
 #include <TemporaryFile.hpp>
 #include <ThreadPool.hpp>
 #include <UmaskScope.hpp>
+#include <Util.hpp>
 #include <assertions.hpp>
 #include <ccache.hpp>
 #include <core/CacheEntry.hpp>
index e46abcaa3891eec8448c4b408f73bcdb6457a578..5410bc9623a885e9c54b9f37cd2ca35554b4b6f1 100644 (file)
@@ -39,6 +39,7 @@
 #include <util/TextTable.hpp>
 #include <util/expected.hpp>
 #include <util/file.hpp>
+#include <util/process.hpp>
 #include <util/string.hpp>
 
 #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
index 234318bea124389b261450cc9bb8df971ce821c7..d1d5925a84bc73266ef0a050e548044d53f377b0 100644 (file)
@@ -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 (file)
index 0000000..64c6bfd
--- /dev/null
@@ -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 <core/wincompat.hpp>
+
+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 (file)
index 0000000..a37f3d5
--- /dev/null
@@ -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 <sys/stat.h>
+
+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