]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert some std::string paths to fs::path
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Jun 2024 18:20:25 +0000 (20:20 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 30 Jun 2024 15:18:52 +0000 (17:18 +0200)
20 files changed:
src/ccache/ccache.cpp
src/ccache/config.cpp
src/ccache/core/mainoptions.cpp
src/ccache/core/result.cpp
src/ccache/core/result.hpp
src/ccache/core/resultextractor.cpp
src/ccache/core/resultextractor.hpp
src/ccache/hash.cpp
src/ccache/hash.hpp
src/ccache/inodecache.cpp
src/ccache/inodecache.hpp
src/ccache/storage/local/localstorage.cpp
src/ccache/storage/local/localstorage.hpp
src/ccache/storage/local/statsfile.cpp
src/ccache/storage/local/statsfile.hpp
src/ccache/storage/local/util.cpp
src/ccache/storage/local/util.hpp
src/ccache/util/logging.cpp
src/ccache/util/logging.hpp
unittest/test_inodecache.cpp

index f9ab687f0d9e243d0d707253f4b3321de00e90e7..bd1e2cd523189286ccf8dc83fc8ff7ada95065f1 100644 (file)
@@ -890,7 +890,7 @@ update_manifest(Context& ctx,
 struct FindCoverageFileResult
 {
   bool found;
-  std::string path;
+  fs::path path;
   bool mangled;
 };
 
@@ -901,9 +901,9 @@ find_coverage_file(const Context& ctx)
   // (in CWD) if -fprofile-dir=DIR is present (regardless of DIR) instead of the
   // traditional /dir/to/example.gcno.
 
-  std::string mangled_form = core::Result::gcno_file_in_mangled_form(ctx);
-  std::string unmangled_form = core::Result::gcno_file_in_unmangled_form(ctx);
-  std::string found_file;
+  fs::path mangled_form = core::Result::gcno_file_in_mangled_form(ctx);
+  fs::path unmangled_form = core::Result::gcno_file_in_unmangled_form(ctx);
+  fs::path found_file;
   if (DirEntry(mangled_form).is_regular_file()) {
     LOG("Found coverage file {}", mangled_form);
     found_file = mangled_form;
index e9e78dacee5b169f2e598824539368aad4b8d623..9e38994c3d3d898b1271fef0c38a43052744a4e8 100644 (file)
@@ -424,10 +424,10 @@ using ConfigLineHandler = std::function<void(
 
 // Call `config_line_handler` for each line in `path`.
 bool
-parse_config_file(const std::string& path,
+parse_config_file(const fs::path& path,
                   const ConfigLineHandler& config_line_handler)
 {
-  std::ifstream file(path);
+  std::ifstream file(util::pstr(path).c_str());
   if (!file) {
     return false;
   }
@@ -686,8 +686,7 @@ bool
 Config::update_from_file(const fs::path& path)
 {
   return parse_config_file(
-    util::pstr(path),
-    [&](const auto& /*line*/, const auto& key, const auto& value) {
+    path, [&](const auto& /*line*/, const auto& key, const auto& value) {
       if (!key.empty()) {
         set_item(key, value, std::nullopt, false, util::pstr(path));
       }
index f86e0b8c0f6ada9f5277d05c226794ee603346a0..163ce7292d7f8ff68c4ba2ec8e1ab4d975df9add 100644 (file)
@@ -194,7 +194,7 @@ configuration_printer(const std::string& key,
 }
 
 static tl::expected<util::Bytes, std::string>
-read_from_path_or_stdin(const std::string& path)
+read_from_path_or_stdin(const fs::path& path)
 {
   if (path == "-") {
     return util::read_fd(STDIN_FILENO).transform_error([&](auto error) {
@@ -207,7 +207,7 @@ read_from_path_or_stdin(const std::string& path)
 }
 
 static int
-inspect_path(const std::string& path)
+inspect_path(const fs::path& path)
 {
   std::optional<util::DirEntry> orig_dir_entry;
   if (path != "-") {
index 701c92203fb8d660c9ad6415cb35468490b29e23..3599bb5221a6e141deb3b2257f9c879d7c71c7e4 100644 (file)
@@ -161,22 +161,19 @@ file_type_to_string(FileType type)
   return k_unknown_file_type;
 }
 
-std::string
+fs::path
 gcno_file_in_mangled_form(const Context& ctx)
 {
   const auto& output_obj = ctx.args_info.output_obj;
-  const std::string abs_output_obj =
-    output_obj.is_absolute() ? util::pstr(output_obj)
-                             : FMT("{}/{}", ctx.apparent_cwd, output_obj);
-  std::string hashified_obj = abs_output_obj;
+  std::string hashified_obj = (ctx.apparent_cwd / output_obj).generic_string();
   std::replace(hashified_obj.begin(), hashified_obj.end(), '/', '#');
-  return util::pstr(util::with_extension(hashified_obj, ".gcno"));
+  return util::with_extension(hashified_obj, ".gcno");
 }
 
-std::string
+fs::path
 gcno_file_in_unmangled_form(const Context& ctx)
 {
-  return util::pstr(util::with_extension(ctx.args_info.output_obj, ".gcno"));
+  return util::with_extension(ctx.args_info.output_obj, ".gcno");
 }
 
 Deserializer::Deserializer(nonstd::span<const uint8_t> data) : m_data(data)
index 6645feb6972167906dc021a873d6f3a7e615f759..2bf94222c8cfbe376767f2f09c00748549b04dec 100644 (file)
@@ -97,8 +97,8 @@ enum class FileType : UnderlyingFileTypeInt {
 
 const char* file_type_to_string(FileType type);
 
-std::string gcno_file_in_mangled_form(const Context& ctx);
-std::string gcno_file_in_unmangled_form(const Context& ctx);
+std::filesystem::path gcno_file_in_mangled_form(const Context& ctx);
+std::filesystem::path gcno_file_in_unmangled_form(const Context& ctx);
 
 // This class knows how to deserializer a result cache entry.
 class Deserializer
index 339df2190e8ade614a2976665abf6d0ec8cba2ec..ed2cafaf6b6d2f1d1daa0e8bbe79c03515573742 100644 (file)
@@ -23,6 +23,7 @@
 #include <ccache/util/direntry.hpp>
 #include <ccache/util/expected.hpp>
 #include <ccache/util/file.hpp>
+#include <ccache/util/filesystem.hpp>
 #include <ccache/util/format.hpp>
 #include <ccache/util/wincompat.hpp>
 
 
 #include <vector>
 
+namespace fs = util::filesystem;
+
 using util::DirEntry;
 
 namespace core {
 
 ResultExtractor::ResultExtractor(
-  const std::string& output_directory,
+  const fs::path& output_directory,
   std::optional<GetRawFilePathFunction> get_raw_file_path)
   : m_output_directory(output_directory),
     m_get_raw_file_path(get_raw_file_path)
@@ -58,7 +61,7 @@ ResultExtractor::on_embedded_file(uint8_t /*file_number*/,
     suffix.resize(suffix.length() - 1);
   }
 
-  const auto dest_path = FMT("{}/ccache-result{}", m_output_directory, suffix);
+  const auto dest_path = m_output_directory / FMT("ccache-result{}", suffix);
   util::throw_on_error<Error>(util::write_file(dest_path, data),
                               FMT("Failed to write to {}: ", dest_path));
 }
index 50af8b9e35565868e76a98d4512c0bf990009a3b..5be41a05e06af92c1938cff7dcbe2c01e64ed0dd 100644 (file)
@@ -23,6 +23,7 @@
 #include <nonstd/span.hpp>
 
 #include <cstdint>
+#include <filesystem>
 #include <functional>
 #include <optional>
 #include <string>
@@ -33,12 +34,12 @@ namespace core {
 class ResultExtractor : public Result::Deserializer::Visitor
 {
 public:
-  using GetRawFilePathFunction = std::function<std::string(uint8_t)>;
+  using GetRawFilePathFunction = std::function<std::filesystem::path(uint8_t)>;
 
   //`result_path` should be the path to the local result entry file if the
   // result comes from local storage.
   ResultExtractor(
-    const std::string& output_directory,
+    const std::filesystem::path& output_directory,
     std::optional<GetRawFilePathFunction> get_raw_file_path = std::nullopt);
 
   void on_embedded_file(uint8_t file_number,
@@ -49,7 +50,7 @@ public:
                    uint64_t file_size) override;
 
 private:
-  std::string m_output_directory;
+  std::filesystem::path m_output_directory;
   std::optional<GetRawFilePathFunction> m_get_raw_file_path;
 };
 
index 6aac5d3d51f11ec2f5dbf12aa58818f9f46381b0..0fabb2a6a5bfca0e67239abe48ad42277c015ea7 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <ccache/util/fd.hpp>
 #include <ccache/util/file.hpp>
+#include <ccache/util/filesystem.hpp>
 #include <ccache/util/format.hpp>
 #include <ccache/util/logging.hpp>
 #include <ccache/util/string.hpp>
@@ -33,6 +34,8 @@
 #  include <unistd.h>
 #endif
 
+namespace fs = util::filesystem;
+
 const uint8_t HASH_DELIMITER[] = {0, 'c', 'C', 'a', 'C', 'h', 'E', 0};
 
 Hash::Hash()
@@ -99,9 +102,9 @@ Hash::hash_fd(int fd)
 }
 
 tl::expected<void, std::string>
-Hash::hash_file(const std::string& path)
+Hash::hash_file(const fs::path& path)
 {
-  util::Fd fd(open(path.c_str(), O_RDONLY | O_BINARY));
+  util::Fd fd(open(util::pstr(path).c_str(), O_RDONLY | O_BINARY));
   if (!fd) {
     LOG("Failed to open {}: {}", path, strerror(errno));
     return tl::unexpected(strerror(errno));
index 4a1b58b1081cfdedddcbd30f8ad13be20df1d82a..92e0a6970e5c83d5b0f4b894396ddde99f37249f 100644 (file)
@@ -82,7 +82,7 @@ public:
   //
   // If hash debugging is enabled, the data is written verbatim to the text
   // input file.
-  tl::expected<void, std::string> hash_file(const std::string& path);
+  tl::expected<void, std::string> hash_file(const std::filesystem::path& path);
 
   // Add contents read from an open file descriptor to the hash.
   //
index 21c92575272531373bf60b2baf675fe4a300c7ef..d2309f81e8388d537ad0b70d5130c7748ddec09c 100644 (file)
@@ -254,13 +254,13 @@ struct InodeCache::SharedRegion
 };
 
 bool
-InodeCache::mmap_file(const std::string& inode_cache_file)
+InodeCache::mmap_file(const std::filesystem::path& path)
 {
   m_sr = nullptr;
   m_map.unmap();
-  m_fd = util::Fd(open(inode_cache_file.c_str(), O_RDWR));
+  m_fd = util::Fd(open(util::pstr(path).c_str(), O_RDWR));
   if (!m_fd) {
-    LOG("Failed to open inode cache {}: {}", inode_cache_file, strerror(errno));
+    LOG("Failed to open inode cache {}: {}", path, strerror(errno));
     return false;
   }
   if (!fd_is_on_known_to_work_file_system(*m_fd)) {
@@ -269,7 +269,7 @@ InodeCache::mmap_file(const std::string& inode_cache_file)
 
   auto map = util::MemoryMap::map(*m_fd, sizeof(SharedRegion));
   if (!map) {
-    LOG("Failed to map inode cache file {}: {}", inode_cache_file, map.error());
+    LOG("Failed to map inode cache file {}: {}", path, map.error());
     return false;
   }
 
@@ -285,13 +285,13 @@ InodeCache::mmap_file(const std::string& inode_cache_file)
       k_version);
     map->unmap();
     m_fd.close();
-    unlink(inode_cache_file.c_str());
+    fs::remove(path);
     return false;
   }
   m_map = std::move(*map);
   m_sr = sr;
   if (m_config.debug()) {
-    LOG("Inode cache file loaded: {}", inode_cache_file);
+    LOG("Inode cache file loaded: {}", path);
   }
   return true;
 }
@@ -368,11 +368,11 @@ InodeCache::with_bucket(const Hash::Digest& key_digest,
 }
 
 bool
-InodeCache::create_new_file(const std::string& filename)
+InodeCache::create_new_file(const fs::path& path)
 {
   // Create the new file to a temporary name to prevent other processes from
   // mapping it before it is fully initialized.
-  auto tmp_file = util::TemporaryFile::create(filename);
+  auto tmp_file = util::TemporaryFile::create(path);
   if (!tmp_file) {
     LOG("Failed to created inode cache file: {}", tmp_file.error());
     return false;
@@ -416,19 +416,19 @@ InodeCache::create_new_file(const std::string& filename)
   // simultaneously. Thus close the current file handle and reopen a new one,
   // which will make us use the first created file even if we didn't win the
   // race.
-  if (link(tmp_file->path.c_str(), filename.c_str()) != 0) {
-    LOG("Failed to link new inode cache: {}", strerror(errno));
+  if (auto result = fs::create_hard_link(tmp_file->path, path); !result) {
+    LOG("Failed to link new inode cache: {}", result.error());
     return false;
   }
 #else
-  if (MoveFileA(util::PathString(tmp_file->path).c_str(), filename.c_str())
+  if (MoveFileA(util::pstr(tmp_file->path).c_str(), util::pstr(path).c_str())
       == 0) {
     unsigned error = GetLastError();
     if (error == ERROR_FILE_EXISTS) {
       // Not an error, another process won the race. Remove the file we just
       // created.
-      DeleteFileA(util::PathString(tmp_file->path).c_str());
-      LOG("Another process created inode cache {}", filename);
+      DeleteFileA(util::pstr(tmp_file->path).c_str());
+      LOG("Another process created inode cache {}", path);
       return true;
     } else {
       LOG("Failed to move new inode cache: {}", error);
@@ -437,7 +437,7 @@ InodeCache::create_new_file(const std::string& filename)
   }
 #endif
 
-  LOG("Created a new inode cache {}", filename);
+  LOG("Created a new inode cache {}", path);
   return true;
 }
 
@@ -486,19 +486,19 @@ InodeCache::initialize()
     return true;
   }
 
-  std::string filename = get_file();
-  if (m_sr || mmap_file(filename)) {
+  fs::path path = get_path();
+  if (m_sr || mmap_file(path)) {
     return true;
   }
 
   // Try to create a new cache if we failed to map an existing file.
-  create_new_file(filename);
+  create_new_file(path);
 
   // Concurrent processes could try to create new files simultaneously and the
   // file that actually landed on disk will be from the process that won the
   // race. Thus we try to open the file from disk instead of reusing the file
   // handle to the file we just created.
-  if (mmap_file(filename)) {
+  if (mmap_file(path)) {
     return true;
   }
 
@@ -622,20 +622,20 @@ InodeCache::drop()
   m_sr = nullptr;
   m_map.unmap();
   m_fd.close();
-  std::string file = get_file();
-  if (unlink(file.c_str()) != 0 && errno != ENOENT) {
+  fs::path path = get_path();
+  if (!fs::remove(path)) {
     return false;
   }
-  LOG("Dropped inode cache {}", file);
+  LOG("Dropped inode cache {}", path);
   return true;
 }
 
-std::string
-InodeCache::get_file()
+fs::path
+InodeCache::get_path()
 {
   const uint8_t arch_bits = 8 * sizeof(void*);
-  return FMT(
-    "{}/inode-cache-{}.v{}", m_config.temporary_dir(), arch_bits, k_version);
+  return m_config.temporary_dir()
+         / FMT("inode-cache-{}.v{}", arch_bits, k_version);
 }
 
 int64_t
index d2a05b72148c056e4c34c8cf42aa8a8f06f1c2ad..0aee5f9bdf99d0610fb14fb40602f0bd1127c171 100644 (file)
@@ -97,7 +97,7 @@ public:
   bool drop();
 
   // Returns name of the persistent file.
-  std::string get_file();
+  std::filesystem::path get_path();
 
   // Returns total number of cache hits.
   //
@@ -124,7 +124,7 @@ private:
   struct SharedRegion;
   using BucketHandler = std::function<void(Bucket* bucket)>;
 
-  bool mmap_file(const std::string& inode_cache_file);
+  bool mmap_file(const std::filesystem::path& path);
 
   bool hash_inode(const std::filesystem::path& path,
                   ContentType type,
@@ -133,7 +133,7 @@ private:
   bool with_bucket(const Hash::Digest& key_digest,
                    const BucketHandler& bucket_handler);
 
-  static bool create_new_file(const std::string& filename);
+  static bool create_new_file(const std::filesystem::path& path);
 
   bool initialize();
 
index 039fd115abe66bd70066d8b6135f01f08332db86..ec59f5d5f73c41a6668e92166e553a024872735d 100644 (file)
@@ -317,7 +317,7 @@ ratio(T numerator, T denominator)
 
 static CleanDirResult
 clean_dir(
-  const std::string& l2_dir,
+  const fs::path& l2_dir,
   const uint64_t max_size,
   const uint64_t max_files,
   const std::optional<uint64_t> max_age = std::nullopt,
@@ -333,7 +333,7 @@ clean_dir(
   uint64_t files_in_cache = 0;
   auto current_time = util::TimePoint::now();
   std::unordered_map<std::string /*result_file*/,
-                     std::vector<std::string> /*associated_raw_files*/>
+                     std::vector<fs::path> /*associated_raw_files*/>
     raw_files_map;
 
   for (size_t i = 0; i < files.size();
@@ -354,9 +354,9 @@ clean_dir(
 
     if (namespace_ && file_type_from_path(file.path()) == FileType::raw) {
       util::PathString path_str(file.path());
-      const auto result_filename =
+      const std::string result_path =
         FMT("{}R", path_str.str().substr(0, path_str.str().length() - 2));
-      raw_files_map[result_filename].push_back(util::pstr(file.path()));
+      raw_files_map[result_path].push_back(file.path());
     }
 
     cache_size += file.size_on_disk();
@@ -432,7 +432,7 @@ clean_dir(
 FileType
 file_type_from_path(const fs::path& path)
 {
-  std::string filename = util::pstr(path.filename());
+  std::string filename = path.filename().string();
   if (util::ends_with(filename, "M")) {
     return FileType::manifest;
   } else if (util::ends_with(filename, "R")) {
@@ -616,8 +616,8 @@ LocalStorage::remove(const Hash::Digest& key, const core::CacheEntryType type)
     key, -1, -static_cast<int64_t>(cache_file.dir_entry.size_on_disk() / 1024));
 }
 
-std::string
-LocalStorage::get_raw_file_path(std::string_view result_path,
+fs::path
+LocalStorage::get_raw_file_path(const fs::path& result_path,
                                 uint8_t file_number)
 {
   if (file_number >= 10) {
@@ -627,11 +627,12 @@ LocalStorage::get_raw_file_path(std::string_view result_path,
     throw core::Error(FMT("Too high raw file entry number: {}", file_number));
   }
 
-  const auto prefix = result_path.substr(0, result_path.length() - 1);
-  return FMT("{}{}W", prefix, file_number);
+  std::string s = result_path.string();
+  s.pop_back();
+  return FMT("{}{}W", s, file_number);
 }
 
-std::string
+fs::path
 LocalStorage::get_raw_file_path(const Hash::Digest& result_key,
                                 uint8_t file_number) const
 {
@@ -646,7 +647,7 @@ LocalStorage::put_raw_files(
   const std::vector<core::Result::Serializer::RawFile>& raw_files)
 {
   const auto cache_file = look_up_cache_file(key, core::CacheEntryType::result);
-  core::ensure_dir_exists(fs::path(cache_file.path).parent_path());
+  core::ensure_dir_exists(cache_file.path.parent_path());
 
   int64_t files_change = 0;
   int64_t size_kibibyte_change = 0;
@@ -750,7 +751,7 @@ LocalStorage::zero_all_statistics()
   const auto zeroable_fields = core::Statistics::get_zeroable_fields();
 
   for_each_level_1_and_2_stats_file(
-    util::pstr(m_config.cache_dir()), [=](const std::string& path) {
+    m_config.cache_dir(), [=](const fs::path& path) {
       StatsFile(path).update([=](auto& cs) {
         for (const auto statistic : zeroable_fields) {
           cs.set(statistic, 0);
@@ -770,7 +771,7 @@ LocalStorage::get_all_statistics() const
 
   // Add up the stats in each directory.
   for_each_level_1_and_2_stats_file(
-    util::pstr(m_config.cache_dir()), [&](const auto& path) {
+    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),
@@ -932,7 +933,8 @@ LocalStorage::recompress(const std::optional<int8_t> level,
             l2_progress_receiver(0.1 + 0.9 * ratio(i, files.size()));
           }
 
-          if (util::ends_with(l2_dir, "f/f")) {
+          if (l2_dir.filename() == "f"
+              && l2_dir.parent_path().filename() == "f") {
             // Wait here instead of after for_each_cache_subdir to avoid
             // updating the progress bar to 100% before all work is done.
             thread_pool.shut_down();
@@ -1019,16 +1021,16 @@ LocalStorage::recompress(const std::optional<int8_t> level,
 
 // Private methods
 
-std::string
+fs::path
 LocalStorage::get_subdir(uint8_t l1_index) const
 {
-  return FMT("{}/{:x}", m_config.cache_dir(), l1_index);
+  return m_config.cache_dir() / FMT("{:x}", l1_index);
 }
 
-std::string
+fs::path
 LocalStorage::get_subdir(uint8_t l1_index, uint8_t l2_index) const
 {
-  return FMT("{}/{:x}/{:x}", m_config.cache_dir(), l1_index, l2_index);
+  return m_config.cache_dir() / FMT("{:x}/{:x}", l1_index, l2_index);
 }
 
 LocalStorage::LookUpCacheFileResult
@@ -1069,14 +1071,14 @@ void
 LocalStorage::move_to_wanted_cache_level(const StatisticsCounters& counters,
                                          const Hash::Digest& key,
                                          core::CacheEntryType type,
-                                         const std::string& cache_file_path)
+                                         const fs::path& cache_file_path)
 {
   const auto wanted_level =
     calculate_wanted_cache_level(counters.get(Statistic::files_in_cache));
   const auto wanted_path = get_path_in_cache(
     wanted_level, util::format_digest(key) + suffix_from_type(type));
   if (cache_file_path != wanted_path) {
-    core::ensure_dir_exists(fs::path(wanted_path).parent_path());
+    core::ensure_dir_exists(wanted_path.parent_path());
 
     // Note: Two ccache processes may move the file at the same time, so failure
     // to rename is OK.
@@ -1084,9 +1086,7 @@ LocalStorage::move_to_wanted_cache_level(const StatisticsCounters& counters,
     fs::rename(cache_file_path, wanted_path);
     for (const auto& raw_file : m_added_raw_files) {
       fs::rename(raw_file,
-                 FMT("{}/{}",
-                     fs::path(wanted_path).parent_path(),
-                     fs::path(raw_file).filename()));
+                 FMT("{}/{}", wanted_path.parent_path(), raw_file.filename()));
     }
   }
 }
@@ -1477,7 +1477,7 @@ LocalStorage::clean_internal_tempdir()
   util::write_file(cleaned_stamp, "");
 }
 
-std::string
+fs::path
 LocalStorage::get_path_in_cache(const uint8_t level,
                                 const std::string_view name) const
 {
@@ -1492,11 +1492,11 @@ LocalStorage::get_path_in_cache(const uint8_t level,
   return (path / fs::path(name.substr(level))).string();
 }
 
-std::string
+fs::path
 LocalStorage::get_lock_path(const std::string& name) const
 {
-  auto path = FMT("{}/lock/{}", m_config.cache_dir(), name);
-  core::ensure_dir_exists(fs::path(path).parent_path());
+  auto path = m_config.cache_dir() / "lock" / name;
+  core::ensure_dir_exists(path.parent_path());
   return path;
 }
 
index 71b17912e780e1c38d2d4557ac81bb3aee97cb0a..558227271c5430686e2bd39eb257a4f23e51a785 100644 (file)
@@ -80,10 +80,11 @@ public:
 
   void remove(const Hash::Digest& key, core::CacheEntryType type);
 
-  static std::string get_raw_file_path(std::string_view result_path,
-                                       uint8_t file_number);
-  std::string get_raw_file_path(const Hash::Digest& result_key,
-                                uint8_t file_number) const;
+  static std::filesystem::path
+  get_raw_file_path(const std::filesystem::path& result_path,
+                    uint8_t file_number);
+  std::filesystem::path get_raw_file_path(const Hash::Digest& result_key,
+                                          uint8_t file_number) const;
 
   void put_raw_files(
     const Hash::Digest& key,
@@ -137,12 +138,12 @@ private:
   // a statistics file in the finalize method.
   core::StatisticsCounters m_counter_updates;
 
-  std::vector<std::string> m_added_raw_files;
+  std::vector<std::filesystem::path> m_added_raw_files;
   bool m_stored_data = false;
 
   struct LookUpCacheFileResult
   {
-    std::string path;
+    std::filesystem::path path;
     util::DirEntry dir_entry;
     uint8_t level;
   };
@@ -150,8 +151,8 @@ private:
   LookUpCacheFileResult look_up_cache_file(const Hash::Digest& key,
                                            core::CacheEntryType type) const;
 
-  std::string get_subdir(uint8_t l1_index) const;
-  std::string get_subdir(uint8_t l1_index, uint8_t l2_index) const;
+  std::filesystem::path get_subdir(uint8_t l1_index) const;
+  std::filesystem::path get_subdir(uint8_t l1_index, uint8_t l2_index) const;
 
   StatsFile get_stats_file(uint8_t l1_index) const;
   StatsFile get_stats_file(uint8_t l1_index, uint8_t l2_index) const;
@@ -159,7 +160,7 @@ private:
   void move_to_wanted_cache_level(const core::StatisticsCounters& counters,
                                   const Hash::Digest& key,
                                   core::CacheEntryType type,
-                                  const std::string& cache_file_path);
+                                  const std::filesystem::path& cache_file_path);
 
   void recount_level_1_dir(util::LongLivedLockFileManager& lock_manager,
                            uint8_t l1_index);
@@ -180,7 +181,7 @@ private:
   struct EvaluateCleanupResult
   {
     uint8_t l1_index;
-    std::string l1_path;
+    std::filesystem::path l1_path;
     core::StatisticsCounters l1_counters;
     uint64_t total_files;
   };
@@ -195,9 +196,10 @@ private:
   // Join the cache directory, a '/' and `name` into a single path and return
   // it. Additionally, `level` single-character, '/'-separated subpaths are
   // split from the beginning of `name` before joining them all.
-  std::string get_path_in_cache(uint8_t level, std::string_view name) const;
+  std::filesystem::path get_path_in_cache(uint8_t level,
+                                          std::string_view name) const;
 
-  std::string get_lock_path(const std::string& name) const;
+  std::filesystem::path get_lock_path(const std::string& name) const;
 
   util::LockFile get_auto_cleanup_lock() const;
 
index c60869df8a6eb2693e8201a7d47825c3f923ee15..9ce023fd45eedde28515d1085561dfb18598051b 100644 (file)
 #include <ccache/core/atomicfile.hpp>
 #include <ccache/core/exceptions.hpp>
 #include <ccache/util/file.hpp>
+#include <ccache/util/filesystem.hpp>
 #include <ccache/util/format.hpp>
 #include <ccache/util/lockfile.hpp>
 #include <ccache/util/logging.hpp>
 
+namespace fs = util::filesystem;
+
 namespace storage::local {
 
-StatsFile::StatsFile(const std::string& path) : m_path(path)
+StatsFile::StatsFile(const fs::path& path) : m_path(path)
 {
 }
 
index 36df4b84185ddda9384e8371572689de0a5acf74..07d4ef4766f63d0ac8c36154b05f981c89da21bf 100644 (file)
 
 #include <ccache/core/statisticscounters.hpp>
 
+#include <filesystem>
 #include <functional>
 #include <optional>
-#include <string>
 
 namespace storage::local {
 
 class StatsFile
 {
 public:
-  explicit StatsFile(const std::string& path);
+  explicit StatsFile(const std::filesystem::path& path);
 
   // Read counters. No lock is acquired. If the file doesn't exist all returned
   // counters will be zero.
@@ -45,7 +45,7 @@ public:
          OnlyIfChanged only_if_changed = OnlyIfChanged::no) const;
 
 private:
-  std::string m_path;
+  std::filesystem::path m_path;
 };
 
 } // namespace storage::local
index da14b3d296770d0a1a81afc38fb17b0527d4191d..0d23b3f04822fb2d578dcfb8d591076d24fe39bd 100644 (file)
 #include <ccache/core/exceptions.hpp>
 #include <ccache/util/expected.hpp>
 #include <ccache/util/file.hpp>
+#include <ccache/util/filesystem.hpp>
 #include <ccache/util/format.hpp>
 #include <ccache/util/path.hpp>
 #include <ccache/util/string.hpp>
 
+namespace fs = util::filesystem;
+
 using util::DirEntry;
 
 namespace storage::local {
@@ -53,8 +56,8 @@ for_each_cache_subdir(const ProgressReceiver& progress_receiver,
 
 void
 for_each_level_1_and_2_stats_file(
-  const std::string& cache_dir,
-  const std::function<void(const std::string& path)> function)
+  const fs::path& cache_dir,
+  const std::function<void(const fs::path& path)> function)
 {
   for (size_t level_1 = 0; level_1 <= 0xF; ++level_1) {
     function(FMT("{}/{:x}/stats", cache_dir, level_1));
@@ -65,7 +68,7 @@ for_each_level_1_and_2_stats_file(
 }
 
 std::vector<DirEntry>
-get_cache_dir_files(const std::string& dir)
+get_cache_dir_files(const fs::path& dir)
 {
   std::vector<DirEntry> files;
 
index 317c7be1555851cf9744ada614f6e2d5762303c2..b006c36033ee62109bc200359182f4e4ebb94b86 100644 (file)
@@ -20,8 +20,8 @@
 
 #include <ccache/util/direntry.hpp>
 
+#include <filesystem>
 #include <functional>
-#include <string>
 #include <vector>
 
 namespace storage::local {
@@ -37,8 +37,8 @@ void for_each_cache_subdir(const ProgressReceiver& progress_receiver,
                            const SubdirProgressVisitor& visitor);
 
 void for_each_level_1_and_2_stats_file(
-  const std::string& cache_dir,
-  const std::function<void(const std::string& path)> function);
+  const std::filesystem::path& cache_dir,
+  const std::function<void(const std::filesystem::path& path)> function);
 
 // Get a list of files in a subdirectory of the cache.
 //
@@ -50,6 +50,7 @@ void for_each_level_1_and_2_stats_file(
 // - CACHEDIR.TAG
 // - stats
 // - .nfs* (temporary NFS files that may be left for open but deleted files).
-std::vector<util::DirEntry> get_cache_dir_files(const std::string& dir);
+std::vector<util::DirEntry>
+get_cache_dir_files(const std::filesystem::path& dir);
 
 } // namespace storage::local
index 2115dfea0764f2265c21c36bf398e6d31f61712b..178dbd06eaad2ad246c968d795a933e4c90b2dc4 100644 (file)
@@ -174,7 +174,7 @@ bulk_log(std::string_view message)
 }
 
 void
-dump_log(const std::string& path)
+dump_log(const fs::path& path)
 {
   if (!enabled()) {
     return;
index 1919f0cac420262563a272341c482430590a10eb..1628759d26a480ec7e14fdfc6c11cdd2326afd53 100644 (file)
@@ -64,6 +64,6 @@ void log(std::string_view message);
 void bulk_log(std::string_view message);
 
 // Write the current log memory buffer to `path`.
-void dump_log(const std::string& path);
+void dump_log(const std::filesystem::path& path);
 
 } // namespace util::logging
index 878f083ea9e6e63f1073be775fad908b0665409e..79b45927b964d7e93ac1fa6d0bbcb4dddd015fe8 100644 (file)
@@ -172,9 +172,9 @@ TEST_CASE("Drop file")
   InodeCache inode_cache(config, util::Duration(0));
 
   inode_cache.get("a", InodeCache::ContentType::raw);
-  CHECK(util::DirEntry(inode_cache.get_file()));
+  CHECK(util::DirEntry(inode_cache.get_path()));
   CHECK(inode_cache.drop());
-  CHECK(!util::DirEntry(inode_cache.get_file()));
+  CHECK(!util::DirEntry(inode_cache.get_path()));
   CHECK(inode_cache.drop());
 }