]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use fs::weakly_canonical in prepare_debug_path
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 23 Oct 2023 18:55:09 +0000 (20:55 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 23 Oct 2023 19:43:52 +0000 (21:43 +0200)
src/Config.cpp
src/Config.hpp
src/ccache.cpp
src/util/filesystem.hpp
src/util/path.cpp
src/util/path.hpp
unittest/test_util_path.cpp

index 74aee464d8c2d4cad3999858145c5687680825a8..064e09822fd4f1c2aa840971404eec07620de647 100644 (file)
@@ -788,7 +788,7 @@ Config::get_string_value(const std::string& key) const
     return format_bool(m_debug);
 
   case ConfigItem::debug_dir:
-    return m_debug_dir;
+    return m_debug_dir.string();
 
   case ConfigItem::debug_level:
     return FMT("{}", m_debug_level);
index 75bd9c2a81519c0ec3bbcd07453d3cab1310cb48..253865a384e1a5780099e4877226371b351feaf4 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <core/Sloppiness.hpp>
 #include <util/NonCopyable.hpp>
+#include <util/filesystem.hpp>
 #include <util/string.hpp>
 
 #include <sys/types.h>
@@ -62,7 +63,7 @@ public:
   int8_t compression_level() const;
   const std::string& cpp_extension() const;
   bool debug() const;
-  const std::string& debug_dir() const;
+  const std::filesystem::path& debug_dir() const;
   uint8_t debug_level() const;
   bool depend_mode() const;
   bool direct_mode() const;
@@ -177,7 +178,7 @@ private:
   int8_t m_compression_level = 0; // Use default level
   std::string m_cpp_extension;
   bool m_debug = false;
-  std::string m_debug_dir;
+  std::filesystem::path m_debug_dir;
   uint8_t m_debug_level = 2;
   bool m_depend_mode = false;
   bool m_direct_mode = true;
@@ -300,7 +301,7 @@ Config::debug() const
   return m_debug;
 }
 
-inline const std::string&
+inline const std::filesystem::path&
 Config::debug_dir() const
 {
   return m_debug_dir;
index 71eefb4090a08bef6f8eae01ca895035504fd44f..7d414f3168dc62caee9a61a67d7be920b709769b 100644 (file)
@@ -183,19 +183,25 @@ add_prefix(const Context& ctx, Args& args, const std::string& prefix_command)
 }
 
 static std::string
-prepare_debug_path(const std::string& debug_dir,
+prepare_debug_path(const fs::path& cwd,
+                   const fs::path& debug_dir,
                    const util::TimePoint& time_of_invocation,
-                   const std::string& output_obj,
+                   const fs::path& output_obj,
                    std::string_view suffix)
 {
-  auto prefix = debug_dir.empty()
-                  ? output_obj
-                  : debug_dir + util::to_absolute_path_no_drive(output_obj);
+  auto prefix =
+    debug_dir.empty()
+      ? output_obj
+      : (debug_dir
+         / (output_obj.is_absolute()
+              ? output_obj
+              : fs::weakly_canonical(cwd / output_obj).value_or(output_obj))
+             .relative_path());
 
   // Ignore any error from fs::create_directories since we can't handle an error
   // in another way in this context. The caller takes care of logging when
   // trying to open the path for writing.
-  fs::create_directories(Util::dir_name(prefix));
+  fs::create_directories(prefix.parent_path());
 
   char timestamp[100];
   const auto tm = util::localtime(time_of_invocation);
@@ -208,7 +214,7 @@ prepare_debug_path(const std::string& debug_dir,
              static_cast<long long unsigned int>(time_of_invocation.sec()));
   }
   return FMT("{}.{}_{:06}.ccache-{}",
-             prefix,
+             prefix.string(),
              timestamp,
              time_of_invocation.nsec_decimal_part() / 1000,
              suffix);
@@ -225,7 +231,8 @@ init_hash_debug(Context& ctx,
     return;
   }
 
-  const auto path = prepare_debug_path(ctx.config.debug_dir(),
+  const auto path = prepare_debug_path(ctx.apparent_cwd,
+                                       ctx.config.debug_dir(),
                                        ctx.time_of_invocation,
                                        ctx.args_info.output_obj,
                                        FMT("input-{}", type));
@@ -2304,7 +2311,8 @@ finalize_at_exit(Context& ctx)
 
   // Dump log buffer last to not lose any logs.
   if (ctx.config.debug() && !ctx.args_info.output_obj.empty()) {
-    util::logging::dump_log(prepare_debug_path(ctx.config.debug_dir(),
+    util::logging::dump_log(prepare_debug_path(ctx.apparent_cwd,
+                                               ctx.config.debug_dir(),
                                                ctx.time_of_invocation,
                                                ctx.args_info.output_obj,
                                                "log"));
@@ -2506,7 +2514,8 @@ do_cache_compilation(Context& ctx)
   MTR_META_THREAD_NAME(ctx.args_info.output_obj.c_str());
 
   if (ctx.config.debug() && ctx.config.debug_level() >= 2) {
-    const auto path = prepare_debug_path(ctx.config.debug_dir(),
+    const auto path = prepare_debug_path(ctx.apparent_cwd,
+                                         ctx.config.debug_dir(),
                                          ctx.time_of_invocation,
                                          ctx.args_info.orig_output_obj,
                                          "input-text");
index edea2c66d6584dd16f91b43a303efdb4de1d196b..a7bd257722493d442e9f68e7b20904d71508fd51 100644 (file)
@@ -114,6 +114,7 @@ DEF_WRAP_1_R(read_symlink,        path,           const path&, p)
 DEF_WRAP_1_R(remove,              bool,           const path&, p)
 DEF_WRAP_1_R(remove_all,          std::uintmax_t, const path&, p)
 DEF_WRAP_0_R(temp_directory_path, path)
+DEF_WRAP_1_R(weakly_canonical,    path,           const path&, p)
 
 // clang-format on
 
index 3473cdd019956d84c38c3ab905ac532155592452..bfa955aa0a3e3cda87a875236137fe1c9e2332ef 100644 (file)
@@ -139,27 +139,4 @@ real_path(std::string_view path)
   return real_path ? real_path->string() : std::string(path);
 }
 
-std::string
-to_absolute_path(std::string_view path)
-{
-  if (is_absolute_path(path)) {
-    return std::string(path);
-  } else {
-    return Util::normalize_abstract_absolute_path(
-      FMT("{}/{}", actual_cwd(), path));
-  }
-}
-
-std::string
-to_absolute_path_no_drive(std::string_view path)
-{
-  std::string abs_path = to_absolute_path(path);
-#ifdef _WIN32
-  if (abs_path.length() >= 2 && abs_path[1] == ':') {
-    abs_path.erase(0, 2);
-  }
-#endif
-  return abs_path;
-}
-
 } // namespace util
index 6d43135e3d86d5bab2126591767de5f526b58540..68c120708655840d6b04c97de3d27030ed3d20c0 100644 (file)
@@ -61,12 +61,6 @@ bool path_starts_with(std::string_view path, std::string_view prefix);
 // doesn't exist) path is returned unmodified.
 std::string real_path(std::string_view path);
 
-// Make `path` an absolute path.
-std::string to_absolute_path(std::string_view path);
-
-// Make `path` an absolute path, but do not include Windows drive.
-std::string to_absolute_path_no_drive(std::string_view path);
-
 // --- Inline implementations ---
 
 inline bool
index 1e6ff03c46d7e68750ea9ee655b11337f8f8b5ab..f52a50f374ac4f8c224b4ef52676c200ffaa5488 100644 (file)
@@ -74,45 +74,6 @@ TEST_CASE("util::is_dev_null_path")
 #endif
 }
 
-TEST_CASE("util::to_absolute_path")
-{
-  CHECK(util::to_absolute_path("/foo/bar") == "/foo/bar");
-
-#ifdef _WIN32
-  CHECK(util::to_absolute_path("C:\\foo\\bar") == "C:\\foo\\bar");
-#endif
-
-  const auto cwd = util::actual_cwd();
-
-  CHECK(util::to_absolute_path("") == cwd);
-  CHECK(util::to_absolute_path(".") == cwd);
-  CHECK(util::to_absolute_path("..") == Util::dir_name(cwd));
-  CHECK(util::to_absolute_path("foo") == FMT("{}/foo", cwd));
-  CHECK(util::to_absolute_path("../foo/bar")
-        == FMT("{}/foo/bar", Util::dir_name(cwd)));
-}
-
-TEST_CASE("util::to_absolute_path_no_drive")
-{
-  CHECK(util::to_absolute_path_no_drive("/foo/bar") == "/foo/bar");
-
-#ifdef _WIN32
-  CHECK(util::to_absolute_path_no_drive("C:\\foo\\bar") == "\\foo\\bar");
-#endif
-
-  auto cwd = util::actual_cwd();
-#ifdef _WIN32
-  cwd = cwd.substr(2);
-#endif
-
-  CHECK(util::to_absolute_path_no_drive("") == cwd);
-  CHECK(util::to_absolute_path_no_drive(".") == cwd);
-  CHECK(util::to_absolute_path_no_drive("..") == Util::dir_name(cwd));
-  CHECK(util::to_absolute_path_no_drive("foo") == FMT("{}/foo", cwd));
-  CHECK(util::to_absolute_path_no_drive("../foo/bar")
-        == FMT("{}/foo/bar", Util::dir_name(cwd)));
-}
-
 TEST_CASE("util::path_starts_with")
 {
   CHECK(!util::path_starts_with("", ""));