From: Joel Rosdahl Date: Wed, 6 Nov 2024 20:51:00 +0000 (+0100) Subject: fix: Fix util::make_relative_path for trailing slash on Windows X-Git-Tag: v4.11~52 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d3a0adf732a9b834eece077b9dfcaf4ff7bd061b;p=thirdparty%2Fccache.git fix: Fix util::make_relative_path for trailing slash on Windows 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. --- diff --git a/src/ccache/util/path.cpp b/src/ccache/util/path.cpp index 93f703b9..83db436b 100644 --- a/src/ccache/util/path.cpp +++ b/src/ccache/util/path.cpp @@ -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 relpath_candidates; fs::path path_suffix; diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index 83425c4c..0f2d8876 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -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")