From: Joel Rosdahl Date: Sat, 19 Feb 2022 15:52:26 +0000 (+0100) Subject: refactor: Extract get_umask function to Util X-Git-Tag: v4.6~14 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=783ffde9e83f2f3b490074d4df14bf39f0831fd8;p=thirdparty%2Fccache.git refactor: Extract get_umask function to Util --- diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index 8c9e3868d..3b6ccabd1 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2021 Joel Rosdahl and other contributors +// Copyright (C) 2020-2022 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -28,25 +28,6 @@ using nonstd::string_view; -namespace { - -#ifndef _WIN32 -mode_t -get_umask() -{ - static bool mask_retrieved = false; - static mode_t mask; - if (!mask_retrieved) { - mask = umask(0); - umask(mask); - mask_retrieved = true; - } - return mask; -} -#endif - -} // namespace - TemporaryFile::TemporaryFile(string_view path_prefix) : path(std::string(path_prefix) + ".XXXXXX") { @@ -69,6 +50,6 @@ TemporaryFile::TemporaryFile(string_view path_prefix) Util::set_cloexec_flag(*fd); #ifndef _WIN32 - fchmod(*fd, 0666 & ~get_umask()); + fchmod(*fd, 0666 & ~Util::get_umask()); #endif } diff --git a/src/Util.cpp b/src/Util.cpp index 55254489b..f9636dbc5 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -742,6 +742,21 @@ get_relative_path(string_view dir, string_view path) return result.empty() ? "." : result; } +#ifndef _WIN32 +mode_t +get_umask() +{ + static bool mask_retrieved = false; + static mode_t mask; + if (!mask_retrieved) { + mask = umask(0); + umask(mask); + mask_retrieved = true; + } + return mask; +} +#endif + void hard_link(const std::string& oldpath, const std::string& newpath) { diff --git a/src/Util.hpp b/src/Util.hpp index 67cd164e4..6e0aea833 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -188,6 +188,11 @@ const char* get_hostname(); std::string get_relative_path(nonstd::string_view dir, nonstd::string_view path); +#ifndef _WIN32 +// Get process umask. +mode_t get_umask(); +#endif + // Hard-link `oldpath` to `newpath`. Throws `core::Error` on error. void hard_link(const std::string& oldpath, const std::string& newpath);