mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-21 07:39:08 +01:00
util: Add ArgsManager::GetPathArg() function
Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
@@ -159,6 +159,98 @@ BOOST_AUTO_TEST_CASE(intarg)
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(patharg)
|
||||
{
|
||||
const auto dir = std::make_pair("-dir", ArgsManager::ALLOW_ANY);
|
||||
SetupArgs({dir});
|
||||
ResetArgs("");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), fs::path{});
|
||||
|
||||
const fs::path root_path{"/"};
|
||||
ResetArgs("-dir=/");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
|
||||
|
||||
ResetArgs("-dir=/.");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
|
||||
|
||||
ResetArgs("-dir=/./");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
|
||||
|
||||
ResetArgs("-dir=/.//");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
|
||||
|
||||
#ifdef WIN32
|
||||
const fs::path win_root_path{"C:\\"};
|
||||
ResetArgs("-dir=C:\\");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
|
||||
|
||||
ResetArgs("-dir=C:/");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
|
||||
|
||||
ResetArgs("-dir=C:\\\\");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
|
||||
|
||||
ResetArgs("-dir=C:\\.");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
|
||||
|
||||
ResetArgs("-dir=C:\\.\\");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
|
||||
|
||||
ResetArgs("-dir=C:\\.\\\\");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
|
||||
#endif
|
||||
|
||||
const fs::path absolute_path{"/home/user/.bitcoin"};
|
||||
ResetArgs("-dir=/home/user/.bitcoin");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
|
||||
|
||||
ResetArgs("-dir=/root/../home/user/.bitcoin");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
|
||||
|
||||
ResetArgs("-dir=/home/./user/.bitcoin");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
|
||||
|
||||
ResetArgs("-dir=/home/user/.bitcoin/");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
|
||||
|
||||
ResetArgs("-dir=/home/user/.bitcoin//");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
|
||||
|
||||
ResetArgs("-dir=/home/user/.bitcoin/.");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
|
||||
|
||||
ResetArgs("-dir=/home/user/.bitcoin/./");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
|
||||
|
||||
ResetArgs("-dir=/home/user/.bitcoin/.//");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
|
||||
|
||||
const fs::path relative_path{"user/.bitcoin"};
|
||||
ResetArgs("-dir=user/.bitcoin");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
|
||||
|
||||
ResetArgs("-dir=somewhere/../user/.bitcoin");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
|
||||
|
||||
ResetArgs("-dir=user/./.bitcoin");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
|
||||
|
||||
ResetArgs("-dir=user/.bitcoin/");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
|
||||
|
||||
ResetArgs("-dir=user/.bitcoin//");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
|
||||
|
||||
ResetArgs("-dir=user/.bitcoin/.");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
|
||||
|
||||
ResetArgs("-dir=user/.bitcoin/./");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
|
||||
|
||||
ResetArgs("-dir=user/.bitcoin/.//");
|
||||
BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(doubledash)
|
||||
{
|
||||
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
|
||||
|
||||
@@ -399,6 +399,13 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
fs::path ArgsManager::GetPathArg(std::string pathlike_arg) const
|
||||
{
|
||||
auto result = fs::PathFromString(GetArg(pathlike_arg, "")).lexically_normal();
|
||||
// Remove trailing slash, if present.
|
||||
return result.has_filename() ? result : result.parent_path();
|
||||
}
|
||||
|
||||
const fs::path& ArgsManager::GetBlocksDirPath() const
|
||||
{
|
||||
LOCK(cs_args);
|
||||
|
||||
@@ -264,6 +264,16 @@ protected:
|
||||
*/
|
||||
std::optional<const Command> GetCommand() const;
|
||||
|
||||
/**
|
||||
* Get a normalized path from a specified pathlike argument
|
||||
*
|
||||
* It is guaranteed that the returned path has no trailing slashes.
|
||||
*
|
||||
* @param pathlike_arg Pathlike argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
|
||||
* @return Normalized path which is get from a specified pathlike argument
|
||||
*/
|
||||
fs::path GetPathArg(std::string pathlike_arg) const;
|
||||
|
||||
/**
|
||||
* Get blocks directory path
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user