]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Move global primary/secondary_config_path into Config (#514)
authorThomas Otto <thomas.otto@pdv-fs.de>
Tue, 21 Jan 2020 18:31:18 +0000 (19:31 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 21 Jan 2020 18:31:18 +0000 (19:31 +0100)
src/Config.cpp
src/Config.hpp
src/ccache.cpp
src/stats.cpp

index a027ef94d1cdadf0dccc3a9dc7471e5b4d057313..7d3883abf3f6580445cacc0db3800c6920c9a783 100644 (file)
@@ -426,6 +426,30 @@ parse_config_file(const std::string& path,
 
 } // namespace
 
+const std::string&
+Config::primary_config_path() const
+{
+  return m_primary_config_path;
+}
+
+const std::string&
+Config::secondary_config_path() const
+{
+  return m_secondary_config_path;
+}
+
+void
+Config::set_primary_config_path(std::string path)
+{
+  m_primary_config_path = std::move(path);
+}
+
+void
+Config::set_secondary_config_path(std::string path)
+{
+  m_secondary_config_path = std::move(path);
+}
+
 bool
 Config::update_from_file(const std::string& file_path)
 {
index f1313b955822bed9aa97f8137a8170873505a00e..82a06501d8641e600cc2923fe20ddf8fb6e92e7c 100644 (file)
@@ -81,6 +81,11 @@ public:
   void set_max_size(uint64_t value);
   void set_run_second_cpp(bool value);
 
+  const std::string& primary_config_path() const;
+  const std::string& secondary_config_path() const;
+  void set_primary_config_path(std::string path);
+  void set_secondary_config_path(std::string path);
+
   typedef std::function<void(const std::string& key,
                              const std::string& value,
                              const std::string& origin)>
@@ -107,6 +112,9 @@ public:
                                 const std::string& value);
 
 private:
+  std::string m_primary_config_path;
+  std::string m_secondary_config_path;
+
   std::string m_base_dir = "";
   std::string m_cache_dir = fmt::format("{}/.ccache", get_home_directory());
   uint32_t m_cache_dir_levels = 2;
index 40be50a00c39194b222e9cbc85f965c95b61f902..87436cbf97a9f3f6753c1df9d6eaf34d9e62db92 100644 (file)
@@ -46,8 +46,6 @@
 using nonstd::string_view;
 
 // Global variables used by other compilation units.
-extern char* primary_config_path;
-extern char* secondary_config_path;
 extern char* current_working_dir;
 extern char* stats_file;
 extern unsigned lock_staleness_limit;
@@ -119,12 +117,6 @@ static const char USAGE_TEXT[] =
   "\n"
   "See also <https://ccache.dev>.\n";
 
-// Where to write configuration changes.
-char* primary_config_path = NULL;
-
-// Secondary, read-only configuration file (if any).
-char* secondary_config_path = NULL;
-
 // Current working directory taken from $PWD, or getcwd() if $PWD is bad.
 char* current_working_dir = NULL;
 
@@ -3521,7 +3513,7 @@ out:
 }
 
 static void
-create_initial_config_file(const char* path)
+create_initial_config_file(const std::string& path)
 {
   if (!Util::create_dir(Util::dir_name(path))) {
     return;
@@ -3541,7 +3533,7 @@ create_initial_config_file(const char* path)
   }
   free(stats_dir);
 
-  FILE* f = fopen(path, "w");
+  FILE* f = fopen(path.c_str(), "w");
   if (!f) {
     return;
   }
@@ -3626,12 +3618,13 @@ initialize(void)
 
   char* p = getenv("CCACHE_CONFIGPATH");
   if (p) {
-    primary_config_path = x_strdup(p);
+    g_config.set_primary_config_path(p);
   } else {
-    secondary_config_path = format("%s/ccache.conf", TO_STRING(SYSCONFDIR));
+    g_config.set_secondary_config_path(
+      fmt::format("{}/ccache.conf", TO_STRING(SYSCONFDIR)));
     MTR_BEGIN("config", "conf_read_secondary");
     // A missing config file in SYSCONFDIR is OK so don't check return value.
-    g_config.update_from_file(secondary_config_path);
+    g_config.update_from_file(g_config.secondary_config_path());
     MTR_END("config", "conf_read_secondary");
 
     if (g_config.cache_dir().empty()) {
@@ -3644,13 +3637,14 @@ initialize(void)
       fatal("CCACHE_DIR must not be the empty string");
     }
 
-    primary_config_path =
-      format("%s/ccache.conf", g_config.cache_dir().c_str());
+    g_config.set_primary_config_path(
+      fmt::format("{}/ccache.conf", g_config.cache_dir()));
   }
 
   bool should_create_initial_config = false;
   MTR_BEGIN("config", "conf_read_primary");
-  if (!g_config.update_from_file(primary_config_path) && !g_config.disable()) {
+  if (!g_config.update_from_file(g_config.primary_config_path())
+      && !g_config.disable()) {
     should_create_initial_config = true;
   }
   MTR_END("config", "conf_read_primary");
@@ -3660,7 +3654,7 @@ initialize(void)
   MTR_END("config", "conf_update_from_environment");
 
   if (should_create_initial_config) {
-    create_initial_config_file(primary_config_path);
+    create_initial_config_file(g_config.primary_config_path());
   }
 
   exitfn_init();
@@ -3698,8 +3692,6 @@ cc_reset(void)
 {
   g_config = Config();
 
-  free_and_nullify(primary_config_path);
-  free_and_nullify(secondary_config_path);
   free_and_nullify(current_working_dir);
   for (size_t i = 0; i < debug_prefix_maps_len; i++) {
     free_and_nullify(debug_prefix_maps[i]);
@@ -4099,7 +4091,8 @@ ccache_main_options(int argc, char* argv[])
 
     case 'F': { // --max-files
       initialize();
-      g_config.set_value_in_file(primary_config_path, "max_files", optarg);
+      g_config.set_value_in_file(
+        g_config.primary_config_path(), "max_files", optarg);
       unsigned files = atoi(optarg);
       if (files == 0) {
         printf("Unset cache file limit\n");
@@ -4115,7 +4108,8 @@ ccache_main_options(int argc, char* argv[])
       if (!parse_size_with_suffix(optarg, &size)) {
         fatal("invalid size: %s", optarg);
       }
-      g_config.set_value_in_file(primary_config_path, "max_size", optarg);
+      g_config.set_value_in_file(
+        g_config.primary_config_path(), "max_size", optarg);
       if (size == 0) {
         printf("Unset cache size limit\n");
       } else {
@@ -4134,7 +4128,7 @@ ccache_main_options(int argc, char* argv[])
       }
       char* key = x_strndup(optarg, p - optarg);
       char* value = p + 1;
-      g_config.set_value_in_file(primary_config_path, key, value);
+      g_config.set_value_in_file(g_config.primary_config_path(), key, value);
       free(key);
       break;
     }
index 9241151624a22bc75e8a6f589711506ad25fb270..8f5ee558d808bf94921f5ca980af2e94dbb5d1db 100644 (file)
@@ -36,8 +36,6 @@
 
 extern char* stats_file;
 extern unsigned lock_staleness_limit;
-extern char* primary_config_path;
-extern char* secondary_config_path;
 
 static struct counters* counter_updates;
 
@@ -443,12 +441,11 @@ stats_summary(void)
   time_t last_updated;
   stats_collect(counters, &last_updated);
 
-  printf("cache directory                     %s\n",
-         g_config.cache_dir().c_str());
-  printf("primary config                      %s\n",
-         primary_config_path ? primary_config_path : "");
-  printf("secondary config (readonly)         %s\n",
-         secondary_config_path ? secondary_config_path : "");
+  fmt::print("cache directory                     {}\n", g_config.cache_dir());
+  fmt::print("primary config                      {}\n",
+             g_config.primary_config_path());
+  fmt::print("secondary config (readonly)         {}\n",
+             g_config.secondary_config_path());
   if (last_updated > 0) {
     struct tm tm;
     localtime_r(&last_updated, &tm);