From: Joel Rosdahl Date: Fri, 14 Jul 2023 18:46:37 +0000 (+0200) Subject: refactor: Move UmaskScope to util X-Git-Tag: v4.9~125 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3c98cd28de7362cbcc94a040b587292c90079523;p=thirdparty%2Fccache.git refactor: Move UmaskScope to util --- diff --git a/src/Config.cpp b/src/Config.cpp index f7fef61ae..b508ae4dc 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -23,12 +23,12 @@ #include "Util.hpp" #include "assertions.hpp" -#include #include #include #include #include #include +#include #include #include #include @@ -859,7 +859,7 @@ Config::set_value_in_file(const std::string& path, const std::string& key, const std::string& value) const { - UmaskScope umask_scope(m_umask); + util::UmaskScope umask_scope(m_umask); if (k_config_key_table.find(key) == k_config_key_table.end()) { throw core::Error(FMT("unknown configuration option \"{}\"", key)); diff --git a/src/ccache.cpp b/src/ccache.cpp index c7e858264..75dc41b60 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1,5 +1,5 @@ -// Copyright (C) 2002-2007 Andrew Tridgell // Copyright (C) 2009-2023 Joel Rosdahl and other contributors +// Copyright (C) 2002-2007 Andrew Tridgell // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -31,7 +31,6 @@ #include "MiniTrace.hpp" #include "SignalHandler.hpp" #include "TemporaryFile.hpp" -#include "UmaskScope.hpp" #include "Util.hpp" #include "Win32Util.hpp" #include "argprocessing.hpp" @@ -54,10 +53,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include "third_party/fmt/core.h" @@ -767,7 +768,7 @@ result_key_from_includes(Context& ctx, Hash& hash, std::string_view stdout_data) static nonstd::expected do_execute(Context& ctx, Args& args, const bool capture_stdout = true) { - UmaskScope umask_scope(ctx.original_umask); + util::UmaskScope umask_scope(ctx.original_umask); if (ctx.diagnostics_color_failed) { DEBUG_ASSERT(ctx.config.compiler_type() == CompilerType::gcc); @@ -2121,7 +2122,7 @@ from_cache(Context& ctx, FromCacheCallMode mode, const Hash::Digest& result_key) cache_entry.verify_checksum(); core::Result::Deserializer deserializer(cache_entry.payload()); core::ResultRetriever result_retriever(ctx, result_key); - UmaskScope umask_scope(ctx.original_umask); + util::UmaskScope umask_scope(ctx.original_umask); deserializer.visit(result_retriever); } catch (core::ResultRetriever::WriteError& e) { LOG("Write error when retrieving result from {}: {}", diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 9703f21e6..3ec634f6f 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -44,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -561,7 +561,7 @@ process_main_options(int argc, const char* const* argv) config.read(); Logging::init(config); - UmaskScope umask_scope(config.umask()); + util::UmaskScope umask_scope(config.umask()); const std::string arg = optarg ? optarg : std::string(); diff --git a/src/storage/remote/FileStorage.cpp b/src/storage/remote/FileStorage.cpp index 648e9504c..4b6125068 100644 --- a/src/storage/remote/FileStorage.cpp +++ b/src/storage/remote/FileStorage.cpp @@ -20,12 +20,12 @@ #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -145,7 +145,7 @@ FileStorageBackend::put(const Hash::Digest& key, } { - UmaskScope umask_scope(m_umask); + util::UmaskScope umask_scope(m_umask); const auto dir = Util::dir_name(path); if (!Util::create_dir(dir)) { diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index d1d5925a8..1739c1daf 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -6,6 +6,7 @@ set( TextTable.cpp TimePoint.cpp Tokenizer.cpp + UmaskScope.cpp environment.cpp file.cpp path.cpp diff --git a/src/UmaskScope.hpp b/src/util/UmaskScope.cpp similarity index 72% rename from src/UmaskScope.hpp rename to src/util/UmaskScope.cpp index 88867ed3a..add2a7ca4 100644 --- a/src/UmaskScope.hpp +++ b/src/util/UmaskScope.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2023 Joel Rosdahl and other contributors +// Copyright (C) 2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -16,30 +16,15 @@ // this program; if not, write to the Free Software Foundation, Inc., 51 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -#pragma once +#include "UmaskScope.hpp" #include #include -#include -#include +namespace util { -// This class sets a new (process-global) umask and restores the previous umask -// when destructed. -class UmaskScope -{ -public: - UmaskScope(std::optional new_umask); - ~UmaskScope(); - - void release(); - -private: - std::optional m_saved_umask = std::nullopt; -}; - -inline UmaskScope::UmaskScope(std::optional new_umask) +UmaskScope::UmaskScope(std::optional new_umask) { #ifndef _WIN32 if (new_umask) { @@ -50,12 +35,7 @@ inline UmaskScope::UmaskScope(std::optional new_umask) #endif } -inline UmaskScope::~UmaskScope() -{ - release(); -} - -inline void +void UmaskScope::release() { #ifndef _WIN32 @@ -73,3 +53,5 @@ UmaskScope::release() } #endif } + +} // namespace util diff --git a/src/util/UmaskScope.hpp b/src/util/UmaskScope.hpp new file mode 100644 index 000000000..1bdbabd32 --- /dev/null +++ b/src/util/UmaskScope.hpp @@ -0,0 +1,46 @@ +// Copyright (C) 2020-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 + +#include + +namespace util { + +// This class sets a new (process-global) umask and restores the previous umask +// when destructed. +class UmaskScope +{ +public: + UmaskScope(std::optional new_umask); + ~UmaskScope(); + + void release(); + +private: + std::optional m_saved_umask = std::nullopt; +}; + +inline UmaskScope::~UmaskScope() +{ + release(); +} + +} // namespace util