]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Fix util::make_relative_path for trailing slash on Windows
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 6 Nov 2024 20:51:00 +0000 (21:51 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 7 Nov 2024 10:22:48 +0000 (11:22 +0100)
Given an existing directory called "example", std::filesystem apparently
behaves like this on Windows:

    std::filesystem::exists("example")                  -> true
    std::filesystem::exists("example/")                 -> true
    std::filesystem::exists("example\\")                -> true
    std::filesystem::equivalent("example", "example")   -> true
    std::filesystem::equivalent("example", "example/")  -> false
    std::filesystem::equivalent("example", "example\\") -> false

That's... well, unexpected. This makes util::make_relative_path buggy.
Fix this by pruning the trailing slash after normalizing the path.

Fixes #1518.

src/ccache/util/path.cpp
unittest/test_util_path.cpp

index 93f703b9a9ccfd6e357964d9764ead05e4da3701..83db436be7fc941e77765018bf69e4db301bf36b 100644 (file)
@@ -77,6 +77,11 @@ make_relative_path(const fs::path& actual_cwd,
   DEBUG_ASSERT(path.is_absolute());
 
   fs::path normalized_path = path.lexically_normal();
+  if (!normalized_path.has_filename()) {
+    // Skip trailing slash since fs::equivalent doesn't work otherwise on
+    // Windows.
+    normalized_path = normalized_path.parent_path();
+  }
   fs::path closest_existing_path = normalized_path;
   std::vector<fs::path> relpath_candidates;
   fs::path path_suffix;
index 83425c4cabeba1fb26c3366fb8e7b94ecae8acab..0f2d88764e0f505f4670b2b5ecf868f83a1970aa 100644 (file)
@@ -101,6 +101,8 @@ TEST_CASE("util::make_relative_path")
 
   SUBCASE("Match of actual CWD")
   {
+    REQUIRE(fs::create_directory("d"));
+
     CHECK(make_relative_path(actual_cwd, apparent_cwd, actual_cwd + "/x")
           == "x");
     CHECK(make_relative_path(actual_cwd, apparent_cwd, actual_cwd + "/d")