]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert APIs to use std::filesystem::path for paths
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 2 Jun 2024 08:59:38 +0000 (10:59 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 30 Jun 2024 15:18:49 +0000 (17:18 +0200)
src/ccache/core/Result.cpp
src/ccache/core/Result.hpp
src/ccache/core/ResultRetriever.cpp
src/ccache/core/ResultRetriever.hpp
src/ccache/storage/local/LocalStorage.cpp
src/ccache/storage/local/LocalStorage.hpp

index 82505a0c670cd0543a5e6428bda0b666fb5ded7f..5747beb7a658667b44942e6ba9a65a985cb5ecbf 100644 (file)
@@ -254,7 +254,8 @@ Serializer::add_data(const FileType file_type, nonstd::span<const uint8_t> data)
 }
 
 bool
-Serializer::add_file(const FileType file_type, const std::string& path)
+Serializer::add_file(const FileType file_type,
+                     const std::filesystem::path& path)
 {
   m_serialized_size += 1 + 1 + 8; // marker + file_type + file_size
   if (!should_store_raw_file(m_config, file_type)) {
@@ -264,7 +265,7 @@ Serializer::add_file(const FileType file_type, const std::string& path)
     }
     m_serialized_size += entry.size();
   }
-  m_file_entries.push_back(FileEntry{file_type, path});
+  m_file_entries.push_back(FileEntry{file_type, path.string()});
   return true;
 }
 
index f0010a9444173754f91a4552c16d51cce9eb5b4b..9edc5ec510a03c0b25b8fbbc2077124e62638720 100644 (file)
@@ -24,6 +24,7 @@
 #include <nonstd/span.hpp>
 
 #include <cstdint>
+#include <filesystem>
 #include <string>
 #include <variant>
 #include <vector>
@@ -153,7 +154,8 @@ public:
   void add_data(FileType file_type, nonstd::span<const uint8_t> data);
 
   // Register a file path whose content should be included in the result.
-  [[nodiscard]] bool add_file(FileType file_type, const std::string& path);
+  [[nodiscard]] bool add_file(FileType file_type,
+                              const std::filesystem::path& path);
 
   // core::Serializer
   uint32_t serialized_size() const override;
@@ -164,7 +166,7 @@ public:
   struct RawFile
   {
     uint8_t file_number;
-    std::string path;
+    std::filesystem::path path;
   };
 
   // Get raw files to store in local storage.
index 1069610a6cb137a067ba4a53fa44729107a803f1..a4954e9fcddfafb7ee1c3c71403bf66bb178069d 100644 (file)
@@ -144,7 +144,7 @@ ResultRetriever::on_raw_file(uint8_t file_number,
   }
 }
 
-std::string
+fs::path
 ResultRetriever::get_dest_path(FileType file_type) const
 {
   switch (file_type) {
@@ -164,8 +164,7 @@ ResultRetriever::get_dest_path(FileType file_type) const
 
   case FileType::coverage_unmangled:
     if (m_ctx.args_info.generating_coverage) {
-      return util::pstr(
-        util::with_extension(m_ctx.args_info.output_obj, ".gcno"));
+      return util::with_extension(m_ctx.args_info.output_obj, ".gcno");
     }
     break;
 
@@ -216,13 +215,13 @@ ResultRetriever::get_dest_path(FileType file_type) const
 }
 
 void
-ResultRetriever::write_dependency_file(const std::string& path,
+ResultRetriever::write_dependency_file(const std::filesystem::path& path,
                                        nonstd::span<const uint8_t> data)
 {
   ASSERT(m_ctx.args_info.dependency_target);
 
-  util::Fd fd(
-    open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
+  util::Fd fd(open(
+    util::pstr(path).c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
   if (!fd) {
     throw WriteError(FMT("Failed to open {} for writing", path));
   }
index 9582c47c1b63251129ddef03a283dd27d574b3bc..704ac574e8742d2cde396fc6448666b8d43d41c0 100644 (file)
@@ -25,8 +25,8 @@
 #include <nonstd/span.hpp>
 
 #include <cstdint>
+#include <filesystem>
 #include <optional>
-#include <string>
 
 class Context;
 
@@ -57,9 +57,9 @@ private:
   const Context& m_ctx;
   std::optional<Hash::Digest> m_result_key;
 
-  std::string get_dest_path(Result::FileType file_type) const;
+  std::filesystem::path get_dest_path(Result::FileType file_type) const;
 
-  void write_dependency_file(const std::string& path,
+  void write_dependency_file(const std::filesystem::path& path,
                              nonstd::span<const uint8_t> data);
 };
 
index 867b428738277ae5c6fb66e782383c85ad7d94e7..4f30f0988805b627a4bf6a281322a2bb60432700 100644 (file)
@@ -240,10 +240,12 @@ delete_file(const DirEntry& dir_entry,
 // to a temporary file and then renamed to `dest`. Throws `core::Error` on
 // error.
 static void
-clone_file(const std::string& src, const std::string& dest, bool via_tmp_file)
+clone_file(const std::filesystem::path& src,
+           const std::filesystem::path& dest,
+           bool via_tmp_file)
 {
 #  if defined(__linux__)
-  util::Fd src_fd(open(src.c_str(), O_RDONLY));
+  util::Fd src_fd(open(util::pstr(src).c_str(), O_RDONLY));
   if (!src_fd) {
     throw core::Error(FMT("{}: {}", src, strerror(errno)));
   }
@@ -281,7 +283,9 @@ clone_file(const std::string& src, const std::string& dest, bool via_tmp_file)
   }
 #  elif defined(__APPLE__)
   (void)via_tmp_file;
-  if (clonefile(src.c_str(), dest.c_str(), CLONE_NOOWNERCOPY) != 0) {
+  if (clonefile(
+        util::pstr(src).c_str(), util::pstr(dest).c_str(), CLONE_NOOWNERCOPY)
+      != 0) {
     throw core::Error(strerror(errno));
   }
 #  else
@@ -670,8 +674,8 @@ LocalStorage::put_raw_files(
 }
 
 void
-LocalStorage::clone_hard_link_or_copy_file(const std::string& source,
-                                           const std::string& dest,
+LocalStorage::clone_hard_link_or_copy_file(const fs::path& source,
+                                           const fs::path& dest,
                                            bool via_tmp_file) const
 {
   if (m_config.file_clone()) {
index cda466e5fe7d412d1b0350234a143209795f5bf5..03677076e0c891fd139ab7128162f709a00c7cc3 100644 (file)
@@ -92,8 +92,8 @@ public:
   // Clone, hard link or copy a file from `source` to `dest` depending on
   // settings in `ctx`. If cloning or hard linking cannot and should not be done
   // the file will be copied instead. Throws `core::Error` on error.
-  void clone_hard_link_or_copy_file(const std::string& source,
-                                    const std::string& dest,
+  void clone_hard_link_or_copy_file(const std::filesystem::path& source,
+                                    const std::filesystem::path& dest,
                                     bool via_tmp_file = false) const;
 
   // --- Statistics ---