]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Set umask when creating configuration file
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 24 Jul 2021 04:56:23 +0000 (06:56 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 24 Jul 2021 05:00:09 +0000 (07:00 +0200)
This opens up for not having to initialize Context before calling
Config::set_value_in_file.

src/Config.cpp
src/Config.hpp
src/ccache.cpp
unittest/test_Config.cpp

index 6bf54c6672f71659e9e8da16e383a7d0c7f01345..f135b8b6f330da0feeacda5a2944532c0a3976d1 100644 (file)
@@ -24,6 +24,7 @@
 #include "assertions.hpp"
 #include "fmtmacros.hpp"
 
+#include <UmaskScope.hpp>
 #include <compression/types.hpp>
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
@@ -752,8 +753,10 @@ Config::get_string_value(const std::string& key) const
 void
 Config::set_value_in_file(const std::string& path,
                           const std::string& key,
-                          const std::string& value)
+                          const std::string& value) const
 {
+  UmaskScope umask_scope(m_umask);
+
   if (k_config_key_table.find(key) == k_config_key_table.end()) {
     throw core::Error("unknown configuration option \"{}\"", key);
   }
index 053ee75473c49408ab71573d99529d9fb7ef5bb5..f12b65b58ff91683adae178b5e9e1963ecef40dc 100644 (file)
@@ -125,9 +125,9 @@ public:
 
   void visit_items(const ItemVisitor& item_visitor) const;
 
-  static void set_value_in_file(const std::string& path,
-                                const std::string& key,
-                                const std::string& value);
+  void set_value_in_file(const std::string& path,
+                         const std::string& key,
+                         const std::string& value) const;
 
   // Called from unit tests.
   static void check_key_tables_consistency();
index 08d2669727ba92c0a729aa66684a7846dede91b6..4d013e9d57a8f7b64152d5b01949830c3a9a9630 100644 (file)
@@ -2452,7 +2452,7 @@ handle_main_options(int argc, const char* const* argv)
 
     case 'F': { // --max-files
       auto files = util::value_or_throw<core::Error>(util::parse_unsigned(arg));
-      Config::set_value_in_file(
+      ctx.config.set_value_in_file(
         ctx.config.primary_config_path(), "max_files", arg);
       if (files == 0) {
         PRINT_RAW(stdout, "Unset cache file limit\n");
@@ -2464,7 +2464,7 @@ handle_main_options(int argc, const char* const* argv)
 
     case 'M': { // --max-size
       uint64_t size = Util::parse_size(arg);
-      Config::set_value_in_file(
+      ctx.config.set_value_in_file(
         ctx.config.primary_config_path(), "max_size", arg);
       if (size == 0) {
         PRINT_RAW(stdout, "Unset cache size limit\n");
@@ -2485,7 +2485,8 @@ handle_main_options(int argc, const char* const* argv)
       }
       std::string key = arg.substr(0, eq_pos);
       std::string value = arg.substr(eq_pos + 1);
-      Config::set_value_in_file(ctx.config.primary_config_path(), key, value);
+      ctx.config.set_value_in_file(
+        ctx.config.primary_config_path(), key, value);
       break;
     }
 
index ab1ed2003a49a4758f9a45d5786d3ed12dc3a5ad..16b6942cda91a726d1a7c6e55cee7e00bb65ff4e 100644 (file)
@@ -292,11 +292,12 @@ TEST_CASE("Config::update_from_environment")
 TEST_CASE("Config::set_value_in_file")
 {
   TestContext test_context;
+  Config config;
 
   SUBCASE("set new value")
   {
     Util::write_file("ccache.conf", "path = vanilla\n");
-    Config::set_value_in_file("ccache.conf", "compiler", "chocolate");
+    config.set_value_in_file("ccache.conf", "compiler", "chocolate");
     std::string content = Util::read_file("ccache.conf");
     CHECK(content == "path = vanilla\ncompiler = chocolate\n");
   }
@@ -304,7 +305,7 @@ TEST_CASE("Config::set_value_in_file")
   SUBCASE("existing value")
   {
     Util::write_file("ccache.conf", "path = chocolate\nstats = chocolate\n");
-    Config::set_value_in_file("ccache.conf", "path", "vanilla");
+    config.set_value_in_file("ccache.conf", "path", "vanilla");
     std::string content = Util::read_file("ccache.conf");
     CHECK(content == "path = vanilla\nstats = chocolate\n");
   }
@@ -313,7 +314,7 @@ TEST_CASE("Config::set_value_in_file")
   {
     Util::write_file("ccache.conf", "path = chocolate\nstats = chocolate\n");
     try {
-      Config::set_value_in_file("ccache.conf", "foo", "bar");
+      config.set_value_in_file("ccache.conf", "foo", "bar");
       CHECK(false);
     } catch (const core::Error& e) {
       CHECK(std::string(e.what()) == "unknown configuration option \"foo\"");
@@ -326,7 +327,7 @@ TEST_CASE("Config::set_value_in_file")
   SUBCASE("unknown sloppiness")
   {
     Util::write_file("ccache.conf", "path = vanilla\n");
-    Config::set_value_in_file("ccache.conf", "sloppiness", "foo");
+    config.set_value_in_file("ccache.conf", "sloppiness", "foo");
     std::string content = Util::read_file("ccache.conf");
     CHECK(content == "path = vanilla\nsloppiness = foo\n");
   }
@@ -334,8 +335,8 @@ TEST_CASE("Config::set_value_in_file")
   SUBCASE("comments are kept")
   {
     Util::write_file("ccache.conf", "# c1\npath = blueberry\n#c2\n");
-    Config::set_value_in_file("ccache.conf", "path", "vanilla");
-    Config::set_value_in_file("ccache.conf", "compiler", "chocolate");
+    config.set_value_in_file("ccache.conf", "path", "vanilla");
+    config.set_value_in_file("ccache.conf", "compiler", "chocolate");
     std::string content = Util::read_file("ccache.conf");
     CHECK(content == "# c1\npath = vanilla\n#c2\ncompiler = chocolate\n");
   }