]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert Config::m_cache_dir to fs::path
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 8 Jun 2024 18:31:19 +0000 (20:31 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 30 Jun 2024 15:18:51 +0000 (17:18 +0200)
src/ccache/Config.cpp
src/ccache/Config.hpp
src/ccache/core/Statistics.cpp
src/ccache/storage/local/LocalStorage.cpp

index 6b5835ec982f105b135e0833a9388356531ce025..e467318ebee40db9d7e72cc2131c7a0ba7e64793 100644 (file)
@@ -620,7 +620,7 @@ Config::read(const std::vector<std::string>& cmdline_config_settings)
     set_config_path(config_dir / "ccache.conf");
   }
 
-  const std::string& cache_dir_before_config_file_was_read = cache_dir();
+  const fs::path& cache_dir_before_config_file_was_read = cache_dir();
 
   update_from_file(config_path());
 
@@ -634,10 +634,10 @@ Config::read(const std::vector<std::string>& cmdline_config_settings)
 
   if (cache_dir().empty()) {
     if (legacy_ccache_dir_exists) {
-      set_cache_dir(util::pstr(legacy_ccache_dir));
+      set_cache_dir(legacy_ccache_dir);
 #ifdef _WIN32
     } else if (env_local_appdata) {
-      set_cache_dir(util::pstr(fs::path(env_local_appdata) / "ccache"));
+      set_cache_dir(fs::path(env_local_appdata) / "ccache");
     } else {
       throw core::Fatal(
         "could not find cache directory and the LOCALAPPDATA environment"
@@ -760,7 +760,7 @@ Config::get_string_value(const std::string& key) const
     return util::pstr(m_base_dir);
 
   case ConfigItem::cache_dir:
-    return m_cache_dir;
+    return m_cache_dir.string();
 
   case ConfigItem::compiler:
     return m_compiler;
@@ -1204,5 +1204,6 @@ Config::default_temporary_dir() const
 #endif
     return std::string();
   }();
-  return !run_user_tmp_dir.empty() ? run_user_tmp_dir : m_cache_dir + "/tmp";
+  return !run_user_tmp_dir.empty() ? run_user_tmp_dir
+                                   : util::pstr(m_cache_dir / "tmp");
 }
index 60ed5254d656a18327c07269e5c9db34bc5f435c..77b93bf0ff14a91c137f90e12de21ee3bb91f812 100644 (file)
@@ -54,7 +54,7 @@ public:
 
   bool absolute_paths_in_stderr() const;
   const std::filesystem::path& base_dir() const;
-  const std::string& cache_dir() const;
+  const std::filesystem::path& cache_dir() const;
   const std::string& compiler() const;
   const std::string& compiler_check() const;
   CompilerType compiler_type() const;
@@ -107,7 +107,7 @@ public:
   std::string default_temporary_dir() const;
 
   void set_base_dir(const std::filesystem::path& value);
-  void set_cache_dir(const std::string& value);
+  void set_cache_dir(const std::filesystem::path& value);
   void set_compiler(const std::string& value);
   void set_compiler_type(CompilerType value);
   void set_cpp_extension(const std::string& value);
@@ -169,7 +169,7 @@ private:
 
   bool m_absolute_paths_in_stderr = false;
   std::filesystem::path m_base_dir;
-  std::string m_cache_dir;
+  std::filesystem::path m_cache_dir;
   std::string m_compiler;
   std::string m_compiler_check = "mtime";
   CompilerType m_compiler_type = CompilerType::auto_guess;
@@ -242,7 +242,7 @@ Config::base_dir() const
   return m_base_dir;
 }
 
-inline const std::string&
+inline const std::filesystem::path&
 Config::cache_dir() const
 {
   return m_cache_dir;
@@ -522,7 +522,7 @@ Config::set_base_dir(const std::filesystem::path& value)
 }
 
 inline void
-Config::set_cache_dir(const std::string& value)
+Config::set_cache_dir(const std::filesystem::path& value)
 {
   m_cache_dir = value;
   if (!m_temporary_dir_configured_explicitly) {
index b0db5f97afed71deebe41d6bc3398aa577d0a269..417d7184d982b832b7122d4894bc47fc0c51c0b4 100644 (file)
@@ -389,7 +389,8 @@ Statistics::format_human_readable(const Config& config,
   };
 
   if (verbosity > 0 && !from_log) {
-    table.add_row({"Cache directory:", C(config.cache_dir()).colspan(4)});
+    table.add_row(
+      {"Cache directory:", C(util::pstr(config.cache_dir())).colspan(4)});
     table.add_row(
       {"Config file:", C(util::pstr(config.config_path())).colspan(4)});
     table.add_row({"System config file:",
index 4f30f0988805b627a4bf6a281322a2bb60432700..b84499e4ea288e229f887e6976e47778dbe0e6c8 100644 (file)
@@ -750,7 +750,7 @@ LocalStorage::zero_all_statistics()
   const auto zeroable_fields = core::Statistics::get_zeroable_fields();
 
   for_each_level_1_and_2_stats_file(
-    m_config.cache_dir(), [=](const std::string& path) {
+    util::pstr(m_config.cache_dir()), [=](const std::string& path) {
       StatsFile(path).update([=](auto& cs) {
         for (const auto statistic : zeroable_fields) {
           cs.set(statistic, 0);
@@ -770,7 +770,7 @@ LocalStorage::get_all_statistics() const
 
   // Add up the stats in each directory.
   for_each_level_1_and_2_stats_file(
-    m_config.cache_dir(), [&](const auto& path) {
+    util::pstr(m_config.cache_dir()), [&](const auto& path) {
       counters.set(Statistic::stats_zeroed_timestamp, 0); // Don't add
       counters.increment(StatsFile(path).read());
       zero_timestamp = std::max(counters.get(Statistic::stats_zeroed_timestamp),
@@ -1484,19 +1484,12 @@ LocalStorage::get_path_in_cache(const uint8_t level,
   ASSERT(level >= 1 && level <= 8);
   ASSERT(name.length() >= level);
 
-  std::string path(m_config.cache_dir());
-  path.reserve(path.size() + level * 2 + 1 + name.length() - level);
-
+  fs::path path(m_config.cache_dir());
   for (uint8_t i = 0; i < level; ++i) {
-    path.push_back('/');
-    path.push_back(name.at(i));
+    path /= std::string(1, name.at(i));
   }
 
-  path.push_back('/');
-  const std::string_view name_remaining = name.substr(level);
-  path.append(name_remaining.data(), name_remaining.length());
-
-  return path;
+  return (path / fs::path(name.substr(level))).string();
 }
 
 std::string