From: Joel Rosdahl Date: Thu, 27 Oct 2022 20:23:50 +0000 (+0200) Subject: fix: Use configured umask for command line operations like --zero-stats X-Git-Tag: v4.7.2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79cb07c0b0f0992092ebef1b3c50b7ac68f6cad2;p=thirdparty%2Fccache.git fix: Use configured umask for command line operations like --zero-stats Closes #1197. --- diff --git a/src/UmaskScope.hpp b/src/UmaskScope.hpp index 02bf8e8cb..d141ad30d 100644 --- a/src/UmaskScope.hpp +++ b/src/UmaskScope.hpp @@ -31,6 +31,8 @@ public: UmaskScope(std::optional new_umask); ~UmaskScope(); + void release(); + private: std::optional m_saved_umask = std::nullopt; }; @@ -47,6 +49,12 @@ inline UmaskScope::UmaskScope(std::optional new_umask) } inline UmaskScope::~UmaskScope() +{ + release(); +} + +inline void +UmaskScope::release() { #ifndef _WIN32 if (m_saved_umask) { @@ -59,6 +67,7 @@ inline UmaskScope::~UmaskScope() # if defined(__GNUC__) && !defined(__clang__) # pragma GCC diagnostic pop # endif + m_saved_umask = std::nullopt; } #endif } diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 30afaa488..1f6e651cc 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -424,6 +425,8 @@ process_main_options(int argc, const char* const* argv) Config config; config.read(); + UmaskScope umask_scope(config.umask()); + const std::string arg = optarg ? optarg : std::string(); switch (c) { @@ -462,6 +465,7 @@ process_main_options(int argc, const char* const* argv) } case EXTRACT_RESULT: { + umask_scope.release(); // Use original umask for files outside cache dir const auto cache_entry_data = read_from_path_or_stdin(arg); if (!cache_entry_data) { PRINT(stderr, "Error: \"{}\"", cache_entry_data.error()); diff --git a/test/suites/base.bash b/test/suites/base.bash index 67a5d42ae..0ce9ce6e1 100644 --- a/test/suites/base.bash +++ b/test/suites/base.bash @@ -1069,6 +1069,24 @@ if ! $HOST_OS_WINDOWS; then export CCACHE_UMASK=002 export CCACHE_TEMPDIR=$CCACHE_DIR/tmp + $CCACHE -C + expect_perm "$CCACHE_DIR" drwxrwxr-x + expect_perm "$CCACHE_DIR/0" drwxrwxr-x + expect_perm "$CCACHE_DIR/0/stats" -rw-rw-r-- + rm -rf $CCACHE_DIR + + $CCACHE -c + expect_perm "$CCACHE_DIR" drwxrwxr-x + expect_perm "$CCACHE_DIR/0" drwxrwxr-x + expect_perm "$CCACHE_DIR/0/stats" -rw-rw-r-- + rm -rf $CCACHE_DIR + + $CCACHE -z + expect_perm "$CCACHE_DIR" drwxrwxr-x + expect_perm "$CCACHE_DIR/0" drwxrwxr-x + expect_perm "$CCACHE_DIR/0/stats" -rw-rw-r-- + rm -rf $CCACHE_DIR + cat <test.c int main() {} EOF