]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert usage of util::real_path to std::filesystem
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 31 Dec 2023 14:47:06 +0000 (15:47 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 7 Jan 2024 08:57:37 +0000 (09:57 +0100)
src/Config.cpp
src/Util.cpp
src/ccache.cpp
src/core/common.cpp
src/util/path.cpp
src/util/path.hpp

index 0e9e641fe2a8c5c353bfcc539abfe32210afdfa4..55c44b292d3355adae9e0614b083e6dfdad2a324 100644 (file)
@@ -34,7 +34,6 @@
 #include <util/file.hpp>
 #include <util/filesystem.hpp>
 #include <util/fmtmacros.hpp>
-#include <util/path.hpp>
 #include <util/string.hpp>
 #include <util/wincompat.hpp>
 
@@ -913,9 +912,9 @@ Config::set_value_in_file(const std::string& path,
   Config dummy_config;
   dummy_config.set_item(key, value, std::nullopt, false, "");
 
-  const auto resolved_path = util::real_path(path);
-  if (!DirEntry(resolved_path).is_regular_file()) {
-    core::ensure_dir_exists(Util::dir_name(resolved_path));
+  const fs::path resolved_path = fs::canonical(path).value_or(path);
+  if (!fs::is_regular_file(resolved_path)) {
+    core::ensure_dir_exists(resolved_path.parent_path());
     util::throw_on_error<core::Error>(
       util::write_file(resolved_path, ""),
       FMT("failed to write to {}: ", resolved_path));
index c93e760cbc2f77ee2ed4993c2f8bd3b1c155a4ef..12e16a8b86550563f6d0c64786102d22b14dcbd0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2023 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2024 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -234,7 +234,7 @@ make_relative_path(const std::string& base_dir,
     dir_entry = DirEntry(path);
   }
   const auto path_suffix = std::string(original_path.substr(path.length()));
-  const auto real_path = util::real_path(path);
+  const std::string real_path = fs::canonical(path).value_or(path).string();
 
   const auto add_relpath_candidates = [&](auto p) {
     const std::string normalized_path =
index 6cb38b98a72eb76b54d49c060d781c015ee24e82..37c4122f158ba868fee1e8ad0f9d5a2b1a83d71d 100644 (file)
@@ -1545,7 +1545,8 @@ hash_common_info(const Context& ctx,
     if (!ctx.args_info.profile_path.empty()) {
       dir = ctx.args_info.profile_path;
     } else {
-      dir = util::real_path(Util::dir_name(ctx.args_info.output_obj));
+      const auto output_dir = fs::path(ctx.args_info.output_obj).parent_path();
+      dir = fs::canonical(output_dir).value_or(output_dir).string();
     }
     std::string_view stem = Util::remove_extension(
       fs::path(ctx.args_info.output_obj).filename().string());
index 63a2143465955a5da60dd551429e494c0fe215e9..1a69f815bb8f27336eb71e074fd054d73dac7742 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2023 Joel Rosdahl and other contributors
+// Copyright (C) 2023-2024 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -103,14 +103,10 @@ rewrite_stderr_to_absolute_paths(std::string_view text)
     if (path_end == std::string_view::npos) {
       result.append(line.data(), line.length());
     } else {
-      std::string path(line.substr(0, path_end));
-      if (util::DirEntry(path)) {
-        result += util::real_path(path);
-        auto tail = line.substr(path_end);
-        result.append(tail.data(), tail.length());
-      } else {
-        result.append(line.data(), line.length());
-      }
+      fs::path path(line.substr(0, path_end));
+      result += fs::canonical(path).value_or(path).string();
+      auto tail = line.substr(path_end);
+      result.append(tail.data(), tail.length());
     }
   }
   return result;
index 412c82c06c0ca55c546b927cdaa23da4dbf51bf1..bf2845c9db041584948c3ba3b04a07c98081d02a 100644 (file)
@@ -120,11 +120,4 @@ path_starts_with(std::string_view path, std::string_view prefix)
   return true;
 }
 
-std::string
-real_path(std::string_view path)
-{
-  auto real_path = fs::canonical(path);
-  return real_path ? real_path->string() : std::string(path);
-}
-
 } // namespace util
index 5a29a2d6f4a1da88a07114860b17fc8f40a7596f..a6fc7ab9e7385b44690b68e5017badc6521815f1 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <util/string.hpp>
 
+#include <filesystem>
 #include <string>
 #include <string_view>
 #include <vector>
@@ -54,10 +55,6 @@ bool is_full_path(std::string_view path);
 // Windows
 bool path_starts_with(std::string_view path, std::string_view prefix);
 
-// Return a normalized absolute path of `path`. On error (e.g. if the `path`
-// doesn't exist) path is returned unmodified.
-std::string real_path(std::string_view path);
-
 // --- Inline implementations ---
 
 inline bool