Skip to content

Commit 45952da

Browse files
author
MarcoFalke
committed
Merge #20932: refactor: Replace fs::absolute calls with AbsPathJoin calls
da9caa1 Replace fs::absolute calls with AbsPathJoin calls (Kiminuo) 66576c4 test: Clear forced -walletdir setting after wallet init_tests (Kiminuo) Pull request description: This adds better test coverage and will make it easier in #20744 to remove our dependency on the two-argument boost::filesystem::absolute() function which does not have a direct equivalent in C++17. This PR doesn't change behavior aside from adding an assert and fixing a test bug. ACKs for top commit: jonatack: Code review ACK da9caa1 only doxygen improvements since my last review per `git diff d867d7a da9caa1` MarcoFalke: review ACK da9caa1 📯 ryanofsky: Code review ACK da9caa1. Just comment and test tweaks since previous review. Tree-SHA512: c940ee60f3ba374d4927cf34cf12d27c4c735c94af591fbc0ca408c641b30f8f8fbcfe521d66bfbddf9877a1fc8cd99bd8a47ebcd2fa59789de6bd87a7b9cf4d
2 parents 11cbd4b + da9caa1 commit 45952da

File tree

9 files changed

+45
-7
lines changed

9 files changed

+45
-7
lines changed

src/fs.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ FILE *fopen(const fs::path& p, const char *mode)
3131
#endif
3232
}
3333

34+
fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
35+
{
36+
assert(base.is_absolute());
37+
return fs::absolute(path, base);
38+
}
39+
3440
#ifndef WIN32
3541

3642
static std::string GetErrorReason()

src/fs.h

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ namespace fs = boost::filesystem;
2121
namespace fsbridge {
2222
FILE *fopen(const fs::path& p, const char *mode);
2323

24+
/**
25+
* Helper function for joining two paths
26+
*
27+
* @param[in] base Base path
28+
* @param[in] path Path to combine with base
29+
* @returns path unchanged if it is an absolute path, otherwise returns base joined with path. Returns base unchanged if path is empty.
30+
* @pre Base path must be absolute
31+
* @post Returned path will always be absolute
32+
*/
33+
fs::path AbsPathJoin(const fs::path& base, const fs::path& path);
34+
2435
class FileLock
2536
{
2637
public:

src/rpc/blockchain.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2399,10 +2399,10 @@ static RPCHelpMan dumptxoutset()
23992399
},
24002400
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
24012401
{
2402-
fs::path path = fs::absolute(request.params[0].get_str(), GetDataDir());
2402+
const fs::path path = fsbridge::AbsPathJoin(GetDataDir(), request.params[0].get_str());
24032403
// Write to a temporary path and then move into `path` on completion
24042404
// to avoid confusion due to an interruption.
2405-
fs::path temppath = fs::absolute(request.params[0].get_str() + ".incomplete", GetDataDir());
2405+
const fs::path temppath = fsbridge::AbsPathJoin(GetDataDir(), request.params[0].get_str() + ".incomplete");
24062406

24072407
if (fs::exists(path)) {
24082408
throw JSONRPCError(

src/test/fs_tests.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
5252
file >> input_buffer;
5353
BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
5454
}
55+
{
56+
// Join an absolute path and a relative path.
57+
fs::path p = fsbridge::AbsPathJoin(tmpfolder, "fs_tests_₿_🏃");
58+
BOOST_CHECK(p.is_absolute());
59+
BOOST_CHECK_EQUAL(tmpfile1, p);
60+
}
61+
{
62+
// Join two absolute paths.
63+
fs::path p = fsbridge::AbsPathJoin(tmpfile1, tmpfile2);
64+
BOOST_CHECK(p.is_absolute());
65+
BOOST_CHECK_EQUAL(tmpfile2, p);
66+
}
67+
{
68+
// Ensure joining with empty paths does not add trailing path components.
69+
BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, ""));
70+
BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, {}));
71+
}
5572
}
5673

5774
BOOST_AUTO_TEST_SUITE_END()

src/util/system.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const
398398
}
399399
if (filepath) {
400400
std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME);
401-
*filepath = fs::absolute(temp ? settings + ".tmp" : settings, GetDataDir(/* net_specific= */ true));
401+
*filepath = fsbridge::AbsPathJoin(GetDataDir(/* net_specific= */ true), temp ? settings + ".tmp" : settings);
402402
}
403403
return true;
404404
}
@@ -1311,7 +1311,7 @@ fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
13111311
if (path.is_absolute()) {
13121312
return path;
13131313
}
1314-
return fs::absolute(path, GetDataDir(net_specific));
1314+
return fsbridge::AbsPathJoin(GetDataDir(net_specific), path);
13151315
}
13161316

13171317
void ScheduleBatchPriority()

src/wallet/load.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ bool VerifyWallets(interfaces::Chain& chain)
6262
std::set<fs::path> wallet_paths;
6363

6464
for (const auto& wallet_file : gArgs.GetArgs("-wallet")) {
65-
const fs::path path = fs::absolute(wallet_file, GetWalletDir());
65+
const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), wallet_file);
6666

6767
if (!wallet_paths.insert(path).second) {
6868
chain.initWarning(strprintf(_("Ignoring duplicate -wallet %s."), wallet_file));

src/wallet/test/init_test_fixture.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <fs.h>
6+
#include <univalue.h>
67
#include <util/check.h>
78
#include <util/system.h>
89

@@ -37,6 +38,9 @@ InitWalletDirTestingSetup::InitWalletDirTestingSetup(const std::string& chainNam
3738

3839
InitWalletDirTestingSetup::~InitWalletDirTestingSetup()
3940
{
41+
gArgs.LockSettings([&](util::Settings& settings) {
42+
settings.forced_settings.erase("walletdir");
43+
});
4044
fs::current_path(m_cwd);
4145
}
4246

src/wallet/wallet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3780,7 +3780,7 @@ std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, cons
37803780
// 2. Path to an existing directory.
37813781
// 3. Path to a symlink to a directory.
37823782
// 4. For backwards compatibility, the name of a data file in -walletdir.
3783-
const fs::path& wallet_path = fs::absolute(name, GetWalletDir());
3783+
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), name);
37843784
fs::file_type path_type = fs::symlink_status(wallet_path).type();
37853785
if (!(path_type == fs::file_not_found || path_type == fs::directory_file ||
37863786
(path_type == fs::symlink_file && fs::is_directory(wallet_path)) ||

src/wallet/wallettool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static void WalletShowInfo(CWallet* wallet_instance)
105105

106106
bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command, const std::string& name)
107107
{
108-
fs::path path = fs::absolute(name, GetWalletDir());
108+
const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), name);
109109

110110
if (args.IsArgSet("-format") && command != "createfromdump") {
111111
tfm::format(std::cerr, "The -format option can only be used with the \"createfromdump\" command.\n");

0 commit comments

Comments
 (0)