From 0e3cf9862eef4abab498beae03449047aace93a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 2 Jan 2023 22:07:14 +0100 Subject: [PATCH] ComputePathTo: Handle case where from == to. --- Common/File/Path.cpp | 5 +++++ UI/MemStickScreen.cpp | 10 +++++++--- unittest/UnitTest.cpp | 3 +++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Common/File/Path.cpp b/Common/File/Path.cpp index 39a31e164e..489ab692b4 100644 --- a/Common/File/Path.cpp +++ b/Common/File/Path.cpp @@ -346,6 +346,11 @@ bool Path::IsAbsolute() const { } bool Path::ComputePathTo(const Path &other, std::string &path) const { + if (other == *this) { + path.clear(); + return true; + } + if (!other.StartsWith(*this)) { // Can't do this. Should return an error. return false; diff --git a/UI/MemStickScreen.cpp b/UI/MemStickScreen.cpp index 78c4cf24fd..374c4683a0 100644 --- a/UI/MemStickScreen.cpp +++ b/UI/MemStickScreen.cpp @@ -507,15 +507,19 @@ static bool ListFileSuffixesRecursively(const Path &root, Path folder, std::vect if (file.isDirectory) { std::string dirSuffix; if (root.ComputePathTo(file.fullName, dirSuffix)) { - dirSuffixes.push_back(dirSuffix); - ListFileSuffixesRecursively(root, folder / file.name, dirSuffixes, fileSuffixes); + if (!dirSuffix.empty()) { + dirSuffixes.push_back(dirSuffix); + ListFileSuffixesRecursively(root, folder / file.name, dirSuffixes, fileSuffixes); + } } else { ERROR_LOG_REPORT(SYSTEM, "Failed to compute PathTo from '%s' to '%s'", root.c_str(), folder.c_str()); } } else { std::string fileSuffix; if (root.ComputePathTo(file.fullName, fileSuffix)) { - fileSuffixes.push_back(FileSuffix{ fileSuffix, file.size }); + if (!fileSuffix.empty()) { + fileSuffixes.push_back(FileSuffix{ fileSuffix, file.size }); + } } } } diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index 1284584607..05d2d35475 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -661,6 +661,9 @@ static bool TestPath() { EXPECT_TRUE(Path("/").ComputePathTo(Path("/home/foo/bar"), computedPath)); EXPECT_EQ_STR(computedPath, std::string("home/foo/bar")); + EXPECT_TRUE(Path("/a/b").ComputePathTo(Path("/a/b"), computedPath)); + EXPECT_EQ_STR(computedPath, std::string()); + return true; }