Skip to content

Commit f1bc1c3

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

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
@@ -52,6 +52,28 @@ namespace BCLog {
5252

5353
BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
5454

55+
BOOST_AUTO_TEST_CASE(util_datadir)
56+
{
57+
ClearDatadirCache();
58+
const fs::path dd_norm = GetDataDir();
59+
60+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/");
61+
ClearDatadirCache();
62+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
63+
64+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/.");
65+
ClearDatadirCache();
66+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
67+
68+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/./");
69+
ClearDatadirCache();
70+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
71+
72+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/.//");
73+
ClearDatadirCache();
74+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
75+
}
76+
5577
namespace {
5678
class NoCopyOrMove
5779
{

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>
@@ -693,10 +694,9 @@ void PrintExceptionContinue(const std::exception_ptr pex, const char* pszExcepti
693694

694695
fs::path GetDefaultDataDir()
695696
{
696-
// Windows < Vista: C:\Documents and Settings\Username\Application Data\DashCore
697-
// Windows >= Vista: C:\Users\Username\AppData\Roaming\DashCore
698-
// Mac: ~/Library/Application Support/DashCore
699-
// Unix: ~/.dashcore
697+
// Windows: C:\Users\Username\AppData\Roaming\DashCore
698+
// macOS: ~/Library/Application Support/DashCore
699+
// Unix-like: ~/.dashcore
700700
#ifdef WIN32
701701
// Windows
702702
return GetSpecialFolderPath(CSIDL_APPDATA) / "DashCore";
@@ -708,15 +708,28 @@ fs::path GetDefaultDataDir()
708708
else
709709
pathRet = fs::path(pszHome);
710710
#ifdef MAC_OSX
711-
// Mac
711+
// macOS
712712
return pathRet / "Library/Application Support/DashCore";
713713
#else
714-
// Unix
714+
// Unix-like
715715
return pathRet / ".dashcore";
716716
#endif
717717
#endif
718718
}
719719

720+
namespace {
721+
fs::path StripRedundantLastElementsOfPath(const fs::path& path)
722+
{
723+
auto result = path;
724+
while (result.filename().string() == ".") {
725+
result = result.parent_path();
726+
}
727+
728+
assert(fs::equivalent(result, path));
729+
return result;
730+
}
731+
} // namespace
732+
720733
static fs::path g_blocks_path_cache_net_specific;
721734
static fs::path pathCached;
722735
static fs::path pathCachedNetSpecific;
@@ -744,6 +757,7 @@ const fs::path &GetBlocksDir()
744757
path /= BaseParams().DataDir();
745758
path /= "blocks";
746759
fs::create_directories(path);
760+
path = StripRedundantLastElementsOfPath(path);
747761
return path;
748762
}
749763

@@ -774,6 +788,7 @@ const fs::path &GetDataDir(bool fNetSpecific)
774788
fs::create_directories(path / "wallets");
775789
}
776790

791+
path = StripRedundantLastElementsOfPath(path);
777792
return path;
778793
}
779794

0 commit comments

Comments
 (0)