]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert APIs to use std::filesystem::path for paths
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 1 Jun 2024 11:49:15 +0000 (13:49 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 30 Jun 2024 15:18:46 +0000 (17:18 +0200)
src/ccache/InodeCache.cpp
src/ccache/InodeCache.hpp
src/ccache/argprocessing.cpp
src/ccache/argprocessing.hpp
src/ccache/ccache.cpp
src/ccache/hashutil.cpp
src/ccache/hashutil.hpp

index cb3952acbec3f019b253295257d6db3f316c2af5..b9fe27e538343394e9c6a63ea19fe5dda20c002e 100644 (file)
@@ -27,6 +27,7 @@
 #include <ccache/util/TemporaryFile.hpp>
 #include <ccache/util/conversion.hpp>
 #include <ccache/util/file.hpp>
+#include <ccache/util/filesystem.hpp>
 #include <ccache/util/format.hpp>
 #include <ccache/util/logging.hpp>
 
@@ -51,6 +52,8 @@
 #include <type_traits>
 #include <vector>
 
+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<std::pair<HashSourceCodeResult, Hash::Digest>>
-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)
index 589c35caae718461af0c5c22ac7bdfb5e233db41..c4478b735c4b9e8bffaff3208ab4649feb4f020e 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/types.h>
 
 #include <cstdint>
+#include <filesystem>
 #include <functional>
 #include <optional>
 #include <string>
@@ -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<std::pair<HashSourceCodeResult, Hash::Digest>>
-  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);
index 3ac23323382e34af15d2b366c7013152ddbed20a..491e315ca86b3556448d12059329c963b7564c0a 100644 (file)
@@ -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
index 59c8db42aa08cf5c93ce35c5233636a853a69db3..506e6d249cbe3475f39f7d2b2bcac471d1f3a953 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <tl/expected.hpp>
 
+#include <filesystem>
 #include <string>
 #include <string_view>
 #include <vector>
@@ -48,7 +49,7 @@ tl::expected<ProcessArgsResult, core::Statistic> 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<std::string>& patterns);
index e2d3dfd1f23a1266fc200228a27902bd37ec1979..e1dd3186cd85ab2f3e1b5df17298eb999d650f66 100644 (file)
@@ -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);
       }
     }
index 7c0d60966a044ff6c0f0245d1d550ce5a7cbfa4c..8878031f8b010447466853d6d764163a5ca1fdd3 100644 (file)
 #include <ccache/util/DirEntry.hpp>
 #include <ccache/util/cpu.hpp>
 #include <ccache/util/file.hpp>
+#include <ccache/util/filesystem.hpp>
 #include <ccache/util/format.hpp>
 #include <ccache/util/logging.hpp>
+#include <ccache/util/path.hpp>
 #include <ccache/util/string.hpp>
 #include <ccache/util/time.hpp>
 #include <ccache/util/wincompat.hpp>
@@ -53,6 +55,8 @@
 #  include <immintrin.h>
 #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);
index 37746b07d41b9890e6690ae4c89b8bb972e12186..127760cc4bcc569ec209eb3b0aea55d352add8dc 100644 (file)
@@ -22,6 +22,7 @@
 #include <ccache/util/BitSet.hpp>
 
 #include <cstddef>
+#include <filesystem>
 #include <string>
 #include <string_view>
 
@@ -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`.