From cec300234cd744af621c285c9931178de06e45b0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 27 Oct 2018 01:20:34 +0200 Subject: [PATCH] fix move_storage with save_path with a trailing slash --- ChangeLog | 1 + include/libtorrent/file.hpp | 2 +- src/file.cpp | 18 ++++++++++++++++++ src/storage.cpp | 2 +- test/test_file.cpp | 24 ++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 19da0505c..970cdba5d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * fix move_storage with save_path with a trailing slash * fix tracker announce issue, advertising port 0 in secondary IPv6 announce * fix missing boost/noncopyable.hpp includes * fix python binding for torrent_info::creation_date() diff --git a/include/libtorrent/file.hpp b/include/libtorrent/file.hpp index 640577057..0eaa406e2 100644 --- a/include/libtorrent/file.hpp +++ b/include/libtorrent/file.hpp @@ -149,7 +149,7 @@ namespace libtorrent TORRENT_EXTRA_EXPORT std::string remove_extension(std::string const& f); TORRENT_EXTRA_EXPORT void replace_extension(std::string& f, std::string const& ext); TORRENT_EXTRA_EXPORT bool is_root_path(std::string const& f); - + TORRENT_EXTRA_EXPORT bool compare_path(std::string const& lhs, std::string const& rhs); // internal used by create_torrent.hpp TORRENT_EXTRA_EXPORT std::string parent_path(std::string const& f); diff --git a/src/file.cpp b/src/file.cpp index 187058779..c2a08bc9e 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -836,6 +836,24 @@ namespace libtorrent return false; } + bool compare_path(std::string const& lhs, std::string const& rhs) + { + std::string::size_type const lhs_size = !lhs.empty() + && (lhs[lhs.size()-1] == '/' +#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2) + || lhs[lhs.size()-1] == '\\' +#endif + ) ? lhs.size() - 1 : lhs.size(); + + std::string::size_type const rhs_size = !rhs.empty() + && (rhs[rhs.size()-1] == '/' +#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2) + || rhs[rhs.size()-1] == '\\' +#endif + ) ? rhs.size() - 1 : rhs.size(); + return lhs.compare(0, lhs_size, rhs, 0, rhs_size) == 0; + } + bool has_parent_path(std::string const& f) { if (f.empty()) return false; diff --git a/src/storage.cpp b/src/storage.cpp index c65611e51..2dd034510 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1421,7 +1421,7 @@ namespace libtorrent { error_code err; std::string subdir = combine_path(old_save_path, *it); - while (subdir != old_save_path && !err) + while (!compare_path(subdir, old_save_path) && !err) { remove(subdir, err); subdir = parent_path(subdir); diff --git a/test/test_file.cpp b/test/test_file.cpp index 08ca9d97b..8f901e187 100644 --- a/test/test_file.cpp +++ b/test/test_file.cpp @@ -201,6 +201,30 @@ TORRENT_TEST(paths) TEST_EQUAL(is_root_path("/"), true); #endif +#ifdef TORRENT_WINDOWS + TEST_CHECK(compare_path("c:\\blah\\", "c:\\blah")); + TEST_CHECK(compare_path("c:\\blah", "c:\\blah")); + TEST_CHECK(compare_path("c:\\blah/", "c:\\blah")); + TEST_CHECK(compare_path("c:\\blah", "c:\\blah\\")); + TEST_CHECK(compare_path("c:\\blah", "c:\\blah")); + TEST_CHECK(compare_path("c:\\blah", "c:\\blah/")); + + TEST_CHECK(!compare_path("c:\\bla", "c:\\blah/")); + TEST_CHECK(!compare_path("c:\\bla", "c:\\blah")); + TEST_CHECK(!compare_path("c:\\blah", "c:\\bla")); + TEST_CHECK(!compare_path("c:\\blah\\sdf", "c:\\blah")); +#else + TEST_CHECK(compare_path("/blah", "/blah")); + TEST_CHECK(compare_path("/blah/", "/blah")); + TEST_CHECK(compare_path("/blah", "/blah")); + TEST_CHECK(compare_path("/blah", "/blah/")); + + TEST_CHECK(!compare_path("/bla", "/blah/")); + TEST_CHECK(!compare_path("/bla", "/blah")); + TEST_CHECK(!compare_path("/blah", "/bla")); + TEST_CHECK(!compare_path("/blah/sdf", "/blah")); +#endif + // if has_parent_path() returns false // parent_path() should return the empty string TEST_EQUAL(parent_path("blah"), "");