Skip to content

Commit b19e882

Browse files
hebastoSaibatoMarcoFalke
committed
util: Add StripRedundantLastElementsOfPath function
Co-authored-by: saibato <[email protected]> Co-authored-by: MarcoFalke <[email protected]>
1 parent 9e8d2bd commit b19e882

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
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: 16 additions & 0 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>
@@ -665,6 +666,19 @@ fs::path GetDefaultDataDir()
665666
#endif
666667
}
667668

669+
namespace {
670+
fs::path StripRedundantLastElementsOfPath(const fs::path& path)
671+
{
672+
auto result = path;
673+
while (result.filename().string() == ".") {
674+
result = result.parent_path();
675+
}
676+
677+
assert(fs::equivalent(result, path));
678+
return result;
679+
}
680+
} // namespace
681+
668682
static fs::path g_blocks_path_cache_net_specific;
669683
static fs::path pathCached;
670684
static fs::path pathCachedNetSpecific;
@@ -692,6 +706,7 @@ const fs::path &GetBlocksDir()
692706
path /= BaseParams().DataDir();
693707
path /= "blocks";
694708
fs::create_directories(path);
709+
path = StripRedundantLastElementsOfPath(path);
695710
return path;
696711
}
697712

@@ -722,6 +737,7 @@ const fs::path &GetDataDir(bool fNetSpecific)
722737
fs::create_directories(path / "wallets");
723738
}
724739

740+
path = StripRedundantLastElementsOfPath(path);
725741
return path;
726742
}
727743

0 commit comments

Comments
 (0)