From 379c1188b397342d121a8ba56206043655681882 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 8 Jun 2024 20:31:19 +0200 Subject: [PATCH] refactor: Convert Config::m_cache_dir to fs::path --- src/ccache/Config.cpp | 11 ++++++----- src/ccache/Config.hpp | 10 +++++----- src/ccache/core/Statistics.cpp | 3 ++- src/ccache/storage/local/LocalStorage.cpp | 17 +++++------------ 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/ccache/Config.cpp b/src/ccache/Config.cpp index 6b5835ec..e467318e 100644 --- a/src/ccache/Config.cpp +++ b/src/ccache/Config.cpp @@ -620,7 +620,7 @@ Config::read(const std::vector& 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& 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"); } diff --git a/src/ccache/Config.hpp b/src/ccache/Config.hpp index 60ed5254..77b93bf0 100644 --- a/src/ccache/Config.hpp +++ b/src/ccache/Config.hpp @@ -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) { diff --git a/src/ccache/core/Statistics.cpp b/src/ccache/core/Statistics.cpp index b0db5f97..417d7184 100644 --- a/src/ccache/core/Statistics.cpp +++ b/src/ccache/core/Statistics.cpp @@ -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:", diff --git a/src/ccache/storage/local/LocalStorage.cpp b/src/ccache/storage/local/LocalStorage.cpp index 4f30f098..b84499e4 100644 --- a/src/ccache/storage/local/LocalStorage.cpp +++ b/src/ccache/storage/local/LocalStorage.cpp @@ -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 -- 2.47.2