Skip to content

Commit b2930f5

Browse files
committed
merge bitcoin#20080: Strip any trailing / in -datadir and -blocksdir paths
1 parent afaa5da commit b2930f5

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/test/util_tests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ namespace BCLog {
5454

5555
BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
5656

57+
BOOST_AUTO_TEST_CASE(util_datadir)
58+
{
59+
ClearDatadirCache();
60+
const fs::path dd_norm = GetDataDir();
61+
62+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/");
63+
ClearDatadirCache();
64+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
65+
66+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/.");
67+
ClearDatadirCache();
68+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
69+
70+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/./");
71+
ClearDatadirCache();
72+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
73+
74+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/.//");
75+
ClearDatadirCache();
76+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
77+
}
78+
5779
BOOST_AUTO_TEST_CASE(util_check)
5880
{
5981
// Check that Assert can forward

src/util/system.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#endif // __linux__
3636

3737
#include <algorithm>
38+
#include <cassert>
3839
#include <fcntl.h>
3940
#include <sched.h>
4041
#include <sys/resource.h>
@@ -687,10 +688,9 @@ void PrintExceptionContinue(const std::exception_ptr pex, const char* pszExcepti
687688

688689
fs::path GetDefaultDataDir()
689690
{
690-
// Windows < Vista: C:\Documents and Settings\Username\Application Data\DashCore
691-
// Windows >= Vista: C:\Users\Username\AppData\Roaming\DashCore
692-
// Mac: ~/Library/Application Support/DashCore
693-
// Unix: ~/.dashcore
691+
// Windows: C:\Users\Username\AppData\Roaming\DashCore
692+
// macOS: ~/Library/Application Support/DashCore
693+
// Unix-like: ~/.dashcore
694694
#ifdef WIN32
695695
// Windows
696696
return GetSpecialFolderPath(CSIDL_APPDATA) / "DashCore";
@@ -702,15 +702,28 @@ fs::path GetDefaultDataDir()
702702
else
703703
pathRet = fs::path(pszHome);
704704
#ifdef MAC_OSX
705-
// Mac
705+
// macOS
706706
return pathRet / "Library/Application Support/DashCore";
707707
#else
708-
// Unix
708+
// Unix-like
709709
return pathRet / ".dashcore";
710710
#endif
711711
#endif
712712
}
713713

714+
namespace {
715+
fs::path StripRedundantLastElementsOfPath(const fs::path& path)
716+
{
717+
auto result = path;
718+
while (result.filename().string() == ".") {
719+
result = result.parent_path();
720+
}
721+
722+
assert(fs::equivalent(result, path));
723+
return result;
724+
}
725+
} // namespace
726+
714727
static fs::path g_blocks_path_cache_net_specific;
715728
static fs::path pathCached;
716729
static fs::path pathCachedNetSpecific;
@@ -738,6 +751,7 @@ const fs::path &GetBlocksDir()
738751
path /= BaseParams().DataDir();
739752
path /= "blocks";
740753
fs::create_directories(path);
754+
path = StripRedundantLastElementsOfPath(path);
741755
return path;
742756
}
743757

@@ -768,6 +782,7 @@ const fs::path &GetDataDir(bool fNetSpecific)
768782
fs::create_directories(path / "wallets");
769783
}
770784

785+
path = StripRedundantLastElementsOfPath(path);
771786
return path;
772787
}
773788

0 commit comments

Comments
 (0)