Skip to content

Commit 5a6f3c5

Browse files
committed
Merge #20080: Strip any trailing / in -datadir and -blocksdir paths
ad5cef5 doc: Update data directory path comments (Hennadii Stepanov) b19e882 util: Add StripRedundantLastElementsOfPath function (Hennadii Stepanov) Pull request description: Wallet names in `listwalletdir` RPC are correct now, even if the `-datadir` path has any number of trailing `/`. This PR is an alternative to #19933. Fixes #19928. ACKs for top commit: MarcoFalke: review ACK ad5cef5 🔙 promag: Code review ACK ad5cef5. meshcollider: Code review + test run ACK ad5cef5 Tree-SHA512: bccabbd6c18243d48d15b2b27201cc0f5984623dcbc635c8740cf74523f359844c36eadd40391142874fcf452a43880bb6afbf89815ae736e499f9a98143a661
2 parents 42b66a6 + ad5cef5 commit 5a6f3c5

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

4343
BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
4444

45+
BOOST_AUTO_TEST_CASE(util_datadir)
46+
{
47+
ClearDatadirCache();
48+
const fs::path dd_norm = GetDataDir();
49+
50+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/");
51+
ClearDatadirCache();
52+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
53+
54+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/.");
55+
ClearDatadirCache();
56+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
57+
58+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/./");
59+
ClearDatadirCache();
60+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
61+
62+
gArgs.ForceSetArg("-datadir", dd_norm.string() + "/.//");
63+
ClearDatadirCache();
64+
BOOST_CHECK_EQUAL(dd_norm, GetDataDir());
65+
}
66+
4567
BOOST_AUTO_TEST_CASE(util_check)
4668
{
4769
// Check that Assert can forward

src/util/system.cpp

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

3636
#include <algorithm>
37+
#include <cassert>
3738
#include <fcntl.h>
3839
#include <sched.h>
3940
#include <sys/resource.h>
@@ -649,10 +650,9 @@ void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
649650

650651
fs::path GetDefaultDataDir()
651652
{
652-
// Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
653-
// Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
654-
// Mac: ~/Library/Application Support/Bitcoin
655-
// Unix: ~/.bitcoin
653+
// Windows: C:\Users\Username\AppData\Roaming\Bitcoin
654+
// macOS: ~/Library/Application Support/Bitcoin
655+
// Unix-like: ~/.bitcoin
656656
#ifdef WIN32
657657
// Windows
658658
return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
@@ -664,15 +664,28 @@ fs::path GetDefaultDataDir()
664664
else
665665
pathRet = fs::path(pszHome);
666666
#ifdef MAC_OSX
667-
// Mac
667+
// macOS
668668
return pathRet / "Library/Application Support/Bitcoin";
669669
#else
670-
// Unix
670+
// Unix-like
671671
return pathRet / ".bitcoin";
672672
#endif
673673
#endif
674674
}
675675

676+
namespace {
677+
fs::path StripRedundantLastElementsOfPath(const fs::path& path)
678+
{
679+
auto result = path;
680+
while (result.filename().string() == ".") {
681+
result = result.parent_path();
682+
}
683+
684+
assert(fs::equivalent(result, path));
685+
return result;
686+
}
687+
} // namespace
688+
676689
static fs::path g_blocks_path_cache_net_specific;
677690
static fs::path pathCached;
678691
static fs::path pathCachedNetSpecific;
@@ -700,6 +713,7 @@ const fs::path &GetBlocksDir()
700713
path /= BaseParams().DataDir();
701714
path /= "blocks";
702715
fs::create_directories(path);
716+
path = StripRedundantLastElementsOfPath(path);
703717
return path;
704718
}
705719

@@ -730,6 +744,7 @@ const fs::path &GetDataDir(bool fNetSpecific)
730744
fs::create_directories(path / "wallets");
731745
}
732746

747+
path = StripRedundantLastElementsOfPath(path);
733748
return path;
734749
}
735750

0 commit comments

Comments
 (0)