From: Joel Rosdahl Date: Sat, 1 Jun 2024 11:49:15 +0000 (+0200) Subject: refactor: Convert APIs to use std::filesystem::path for paths X-Git-Tag: v4.11~135 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8e52c07c729d91f39f3c1e7dd450f50feff0d5a2;p=thirdparty%2Fccache.git refactor: Convert APIs to use std::filesystem::path for paths --- diff --git a/src/ccache/InodeCache.cpp b/src/ccache/InodeCache.cpp index cb3952ac..b9fe27e5 100644 --- a/src/ccache/InodeCache.cpp +++ b/src/ccache/InodeCache.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -51,6 +52,8 @@ #include #include +namespace fs = util::filesystem; + using pstr = util::PathString; // The inode cache resides on a file that is mapped into shared memory by @@ -296,7 +299,7 @@ InodeCache::mmap_file(const std::string& inode_cache_file) } bool -InodeCache::hash_inode(const std::string& path, +InodeCache::hash_inode(const fs::path& path, ContentType type, Hash::Digest& digest) { @@ -531,7 +534,7 @@ InodeCache::available(int fd) } std::optional> -InodeCache::get(const std::string& path, ContentType type) +InodeCache::get(const fs::path& path, ContentType type) { if (!initialize()) { return std::nullopt; @@ -580,7 +583,7 @@ InodeCache::get(const std::string& path, ContentType type) } bool -InodeCache::put(const std::string& path, +InodeCache::put(const fs::path& path, ContentType type, const Hash::Digest& file_digest, HashSourceCodeResult return_value) diff --git a/src/ccache/InodeCache.hpp b/src/ccache/InodeCache.hpp index 589c35ca..c4478b73 100644 --- a/src/ccache/InodeCache.hpp +++ b/src/ccache/InodeCache.hpp @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -79,13 +80,13 @@ public: // Get saved hash digest and return value from a previous call to // do_hash_file() in hashutil.cpp. std::optional> - get(const std::string& path, ContentType type); + get(const std::filesystem::path& path, ContentType type); // Put hash digest and return value from a successful call to do_hash_file() // in hashutil.cpp. // // Returns true if values could be stored in the cache, false otherwise. - bool put(const std::string& path, + bool put(const std::filesystem::path& path, ContentType type, const Hash::Digest& file_digest, HashSourceCodeResult return_value); @@ -125,8 +126,9 @@ private: bool mmap_file(const std::string& inode_cache_file); - bool - hash_inode(const std::string& path, ContentType type, Hash::Digest& digest); + bool hash_inode(const std::filesystem::path& path, + ContentType type, + Hash::Digest& digest); bool with_bucket(const Hash::Digest& key_digest, const BucketHandler& bucket_handler); diff --git a/src/ccache/argprocessing.cpp b/src/ccache/argprocessing.cpp index 3ac23323..491e315c 100644 --- a/src/ccache/argprocessing.cpp +++ b/src/ccache/argprocessing.cpp @@ -1766,11 +1766,11 @@ process_args(Context& ctx) } bool -is_precompiled_header(std::string_view path) +is_precompiled_header(const fs::path& path) { - fs::path ext = fs::path(path).extension(); + fs::path ext = path.extension(); return ext == ".gch" || ext == ".pch" || ext == ".pth" - || fs::path(path).parent_path().extension() == ".gch"; + || path.parent_path().extension() == ".gch"; } bool diff --git a/src/ccache/argprocessing.hpp b/src/ccache/argprocessing.hpp index 59c8db42..506e6d24 100644 --- a/src/ccache/argprocessing.hpp +++ b/src/ccache/argprocessing.hpp @@ -23,6 +23,7 @@ #include +#include #include #include #include @@ -48,7 +49,7 @@ tl::expected process_args(Context& ctx); // Return whether `path` represents a precompiled header (see "Precompiled // Headers" in GCC docs). -bool is_precompiled_header(std::string_view path); +bool is_precompiled_header(const std::filesystem::path& path); bool option_should_be_ignored(const std::string& arg, const std::vector& patterns); diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index e2d3dfd1..e1dd3186 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -1616,7 +1616,7 @@ hash_common_info(const Context& ctx, util::split_path_list(ctx.config.extra_files_to_hash())) { LOG("Hashing extra file {}", path); hash.hash_delimiter("extrafile"); - if (!hash_binary_file(ctx, hash, pstr(path))) { + if (!hash_binary_file(ctx, hash, path)) { return tl::unexpected(Statistic::error_hashing_extra_file); } } diff --git a/src/ccache/hashutil.cpp b/src/ccache/hashutil.cpp index 7c0d6096..8878031f 100644 --- a/src/ccache/hashutil.cpp +++ b/src/ccache/hashutil.cpp @@ -27,8 +27,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -53,6 +55,8 @@ # include #endif +namespace fs = util::filesystem; + namespace { // Pre-condition: str[pos - 1] == '_' @@ -176,7 +180,7 @@ check_for_temporal_macros_avx2(std::string_view str) HashSourceCodeResult do_hash_file(const Context& ctx, Hash::Digest& digest, - const std::string& path, + const fs::path& path, size_t size_hint, bool check_temporal_macros) { @@ -233,7 +237,7 @@ check_for_temporal_macros(std::string_view str) HashSourceCodeResult hash_source_code_file(const Context& ctx, Hash::Digest& digest, - const std::string& path, + const fs::path& path, size_t size_hint) { const bool check_temporal_macros = @@ -323,14 +327,14 @@ hash_source_code_file(const Context& ctx, bool hash_binary_file(const Context& ctx, Hash::Digest& digest, - const std::string& path, + const fs::path& path, size_t size_hint) { return do_hash_file(ctx, digest, path, size_hint, false).empty(); } bool -hash_binary_file(const Context& ctx, Hash& hash, const std::string& path) +hash_binary_file(const Context& ctx, Hash& hash, const fs::path& path) { Hash::Digest digest; const bool success = hash_binary_file(ctx, digest, path); diff --git a/src/ccache/hashutil.hpp b/src/ccache/hashutil.hpp index 37746b07..127760cc 100644 --- a/src/ccache/hashutil.hpp +++ b/src/ccache/hashutil.hpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -44,7 +45,7 @@ HashSourceCodeResult check_for_temporal_macros(std::string_view str); // Hash a source code file using the inode cache if enabled. HashSourceCodeResult hash_source_code_file(const Context& ctx, Hash::Digest& digest, - const std::string& path, + const std::filesystem::path& path, size_t size_hint = 0); // Hash a binary file (using the inode cache if enabled) and put its digest in @@ -53,14 +54,16 @@ HashSourceCodeResult hash_source_code_file(const Context& ctx, // Returns true on success, otherwise false. bool hash_binary_file(const Context& ctx, Hash::Digest& digest, - const std::string& path, + const std::filesystem::path& path, size_t size_hint = 0); // Hash a binary file (using the inode cache if enabled) and hash the digest to // `hash`. // // Returns true on success, otherwise false. -bool hash_binary_file(const Context& ctx, Hash& hash, const std::string& path); +bool hash_binary_file(const Context& ctx, + Hash& hash, + const std::filesystem::path& path); // Hash the output of `command` (not executed via a shell). A "%compiler%" // string in `command` will be replaced with `compiler`.