From b19e88230f0e93e95e883e65376963cb9c36f606 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Fri, 16 Oct 2020 18:06:14 +0300 Subject: [PATCH 1/2] util: Add StripRedundantLastElementsOfPath function Co-authored-by: saibato Co-authored-by: MarcoFalke --- src/test/util_tests.cpp | 22 ++++++++++++++++++++++ src/util/system.cpp | 16 ++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 241c56934..010b6adf1 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -42,6 +42,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()); +} + BOOST_AUTO_TEST_CASE(util_check) { // Check that Assert can forward diff --git a/src/util/system.cpp b/src/util/system.cpp index a411b73a1..7754ef3e0 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -34,6 +34,7 @@ #endif // __linux__ #include +#include #include #include #include @@ -665,6 +666,19 @@ fs::path GetDefaultDataDir() #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; @@ -692,6 +706,7 @@ const fs::path &GetBlocksDir() path /= BaseParams().DataDir(); path /= "blocks"; fs::create_directories(path); + path = StripRedundantLastElementsOfPath(path); return path; } @@ -722,6 +737,7 @@ const fs::path &GetDataDir(bool fNetSpecific) fs::create_directories(path / "wallets"); } + path = StripRedundantLastElementsOfPath(path); return path; } From ad5cef5dfdd5802fc187a52e74d940a52f420a51 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Fri, 16 Oct 2020 18:32:43 +0300 Subject: [PATCH 2/2] doc: Update data directory path comments --- src/util/system.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/util/system.cpp b/src/util/system.cpp index 7754ef3e0..a3f21f016 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -642,10 +642,9 @@ void PrintExceptionContinue(const std::exception* pex, const char* pszThread) fs::path GetDefaultDataDir() { - // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin - // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin - // Mac: ~/Library/Application Support/Bitcoin - // Unix: ~/.bitcoin + // Windows: C:\Users\Username\AppData\Roaming\Bitcoin + // macOS: ~/Library/Application Support/Bitcoin + // Unix-like: ~/.bitcoin #ifdef WIN32 // Windows return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin"; @@ -657,10 +656,10 @@ fs::path GetDefaultDataDir() else pathRet = fs::path(pszHome); #ifdef MAC_OSX - // Mac + // macOS return pathRet / "Library/Application Support/Bitcoin"; #else - // Unix + // Unix-like return pathRet / ".bitcoin"; #endif #endif