From 76398a74c0fedb8da17d8666bc5145cacf76d469 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 2 Jun 2024 10:59:38 +0200 Subject: [PATCH] refactor: Convert APIs to use std::filesystem::path for paths --- src/ccache/core/Result.cpp | 5 +++-- src/ccache/core/Result.hpp | 6 ++++-- src/ccache/core/ResultRetriever.cpp | 11 +++++------ src/ccache/core/ResultRetriever.hpp | 6 +++--- src/ccache/storage/local/LocalStorage.cpp | 14 +++++++++----- src/ccache/storage/local/LocalStorage.hpp | 4 ++-- 6 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/ccache/core/Result.cpp b/src/ccache/core/Result.cpp index 82505a0c..5747beb7 100644 --- a/src/ccache/core/Result.cpp +++ b/src/ccache/core/Result.cpp @@ -254,7 +254,8 @@ Serializer::add_data(const FileType file_type, nonstd::span 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; } diff --git a/src/ccache/core/Result.hpp b/src/ccache/core/Result.hpp index f0010a94..9edc5ec5 100644 --- a/src/ccache/core/Result.hpp +++ b/src/ccache/core/Result.hpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -153,7 +154,8 @@ public: void add_data(FileType file_type, nonstd::span 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. diff --git a/src/ccache/core/ResultRetriever.cpp b/src/ccache/core/ResultRetriever.cpp index 1069610a..a4954e9f 100644 --- a/src/ccache/core/ResultRetriever.cpp +++ b/src/ccache/core/ResultRetriever.cpp @@ -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 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)); } diff --git a/src/ccache/core/ResultRetriever.hpp b/src/ccache/core/ResultRetriever.hpp index 9582c47c..704ac574 100644 --- a/src/ccache/core/ResultRetriever.hpp +++ b/src/ccache/core/ResultRetriever.hpp @@ -25,8 +25,8 @@ #include #include +#include #include -#include class Context; @@ -57,9 +57,9 @@ private: const Context& m_ctx; std::optional 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 data); }; diff --git a/src/ccache/storage/local/LocalStorage.cpp b/src/ccache/storage/local/LocalStorage.cpp index 867b4287..4f30f098 100644 --- a/src/ccache/storage/local/LocalStorage.cpp +++ b/src/ccache/storage/local/LocalStorage.cpp @@ -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()) { diff --git a/src/ccache/storage/local/LocalStorage.hpp b/src/ccache/storage/local/LocalStorage.hpp index cda466e5..03677076 100644 --- a/src/ccache/storage/local/LocalStorage.hpp +++ b/src/ccache/storage/local/LocalStorage.hpp @@ -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 --- -- 2.47.2