From 72396d59f88c0b470a69adf44c80520c363a6ce6 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Fri, 16 Oct 2020 18:06:14 +0300 Subject: [PATCH] merge bitcoin#20080: Strip any trailing / in -datadir and -blocksdir paths --- src/test/util_tests.cpp | 22 ++++++++++++++++++++++ src/util/system.cpp | 27 +++++++++++++++++++++------ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index dd84391585..d06bdec5c0 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -52,6 +52,28 @@ namespace BCLog { BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup) +BOOST_AUTO_TEST_CASE(util_datadir) +{ + ClearDatadirCache(); + const fs::path dd_norm = GetDataDir(); + + gArgs.ForceSetArg("-datadir", dd_norm.string() + "/"); + ClearDatadirCache(); + BOOST_CHECK_EQUAL(dd_norm, GetDataDir()); + + gArgs.ForceSetArg("-datadir", dd_norm.string() + "/."); + ClearDatadirCache(); + BOOST_CHECK_EQUAL(dd_norm, GetDataDir()); + + gArgs.ForceSetArg("-datadir", dd_norm.string() + "/./"); + ClearDatadirCache(); + BOOST_CHECK_EQUAL(dd_norm, GetDataDir()); + + gArgs.ForceSetArg("-datadir", dd_norm.string() + "/.//"); + ClearDatadirCache(); + BOOST_CHECK_EQUAL(dd_norm, GetDataDir()); +} + namespace { class NoCopyOrMove { diff --git a/src/util/system.cpp b/src/util/system.cpp index 9f006b0216..c6c151f210 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -35,6 +35,7 @@ #endif // __linux__ #include +#include #include #include #include @@ -693,10 +694,9 @@ void PrintExceptionContinue(const std::exception_ptr pex, const char* pszExcepti fs::path GetDefaultDataDir() { - // Windows < Vista: C:\Documents and Settings\Username\Application Data\DashCore - // Windows >= Vista: C:\Users\Username\AppData\Roaming\DashCore - // Mac: ~/Library/Application Support/DashCore - // Unix: ~/.dashcore + // Windows: C:\Users\Username\AppData\Roaming\DashCore + // macOS: ~/Library/Application Support/DashCore + // Unix-like: ~/.dashcore #ifdef WIN32 // Windows return GetSpecialFolderPath(CSIDL_APPDATA) / "DashCore"; @@ -708,15 +708,28 @@ fs::path GetDefaultDataDir() else pathRet = fs::path(pszHome); #ifdef MAC_OSX - // Mac + // macOS return pathRet / "Library/Application Support/DashCore"; #else - // Unix + // Unix-like return pathRet / ".dashcore"; #endif #endif } +namespace { +fs::path StripRedundantLastElementsOfPath(const fs::path& path) +{ + auto result = path; + while (result.filename().string() == ".") { + result = result.parent_path(); + } + + assert(fs::equivalent(result, path)); + return result; +} +} // namespace + static fs::path g_blocks_path_cache_net_specific; static fs::path pathCached; static fs::path pathCachedNetSpecific; @@ -744,6 +757,7 @@ const fs::path &GetBlocksDir() path /= BaseParams().DataDir(); path /= "blocks"; fs::create_directories(path); + path = StripRedundantLastElementsOfPath(path); return path; } @@ -774,6 +788,7 @@ const fs::path &GetDataDir(bool fNetSpecific) fs::create_directories(path / "wallets"); } + path = StripRedundantLastElementsOfPath(path); return path; }