Merge #20080: Strip any trailing / in -datadir and -blocksdir paths

ad5cef5dfd doc: Update data directory path comments (Hennadii Stepanov)
b19e88230f 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 ad5cef5dfd 🔙
  promag:
    Code review ACK ad5cef5dfd.
  meshcollider:
    Code review + test run ACK ad5cef5dfd

Tree-SHA512: bccabbd6c18243d48d15b2b27201cc0f5984623dcbc635c8740cf74523f359844c36eadd40391142874fcf452a43880bb6afbf89815ae736e499f9a98143a661
This commit is contained in:
Samuel Dobson 2020-11-02 11:40:57 +13:00
commit 5a6f3c5a01
No known key found for this signature in database
GPG key ID: D300116E1C875A3D
2 changed files with 43 additions and 6 deletions

View file

@ -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

View file

@ -34,6 +34,7 @@
#endif // __linux__
#include <algorithm>
#include <cassert>
#include <fcntl.h>
#include <sched.h>
#include <sys/resource.h>
@ -649,10 +650,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";
@ -664,15 +664,28 @@ 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
}
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;
@ -700,6 +713,7 @@ const fs::path &GetBlocksDir()
path /= BaseParams().DataDir();
path /= "blocks";
fs::create_directories(path);
path = StripRedundantLastElementsOfPath(path);
return path;
}
@ -730,6 +744,7 @@ const fs::path &GetDataDir(bool fNetSpecific)
fs::create_directories(path / "wallets");
}
path = StripRedundantLastElementsOfPath(path);
return path;
}