Skip to content

Commit 1b2dedb

Browse files
committed
Merge bitcoin#29040: refactor: Remove pre-C++20 code, fs::path cleanup
6666713 refactor: Rename fs::path::u8string() to fs::path::utf8string() (MarcoFalke) 856c887 ArgsManager: return path by value from GetBlocksDirPath() (Vasil Dimov) fa3d930 refactor: Remove pre-C++20 fs code (MarcoFalke) fa00098 Add tests for C++20 std::u8string (MarcoFalke) fa2bac0 refactor: Avoid copy/move in fs.h (MarcoFalke) faea302 refactor: Use C++20 std::chrono::days (MarcoFalke) Pull request description: This: * Removes dead code. * Avoids unused copies in some places. * Adds copies in other places for safety. ACKs for top commit: achow101: ACK 6666713 ryanofsky: Code review ACK 6666713. Just documentation change since last review. stickies-v: re-ACK 6666713 Tree-SHA512: 6176e44f30b310d51632ec2d3827c3819905d0ddc6a4b57acfcb6cfa1f9735176da75ee8ed4a4abd1296cb0b83bee9374cc6f91ffac87c19b63c435eeadf3f46
2 parents 08e6aaa + 6666713 commit 1b2dedb

13 files changed

+48
-44
lines changed

doc/developer-notes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ A few guidelines for introducing and reviewing new RPC interfaces:
14061406

14071407
- *Rationale*: User-facing consistency.
14081408

1409-
- Use `fs::path::u8string()` and `fs::u8path()` functions when converting path
1409+
- Use `fs::path::u8string()`/`fs::path::utf8string()` and `fs::u8path()` functions when converting path
14101410
to JSON strings, not `fs::PathToString` and `fs::PathFromString`
14111411

14121412
- *Rationale*: JSON strings are Unicode strings, not byte strings, and

src/bench/wallet_create.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void WalletCreate(benchmark::Bench& bench, bool encrypted)
3434

3535
fs::path wallet_path = test_setup->m_path_root / strprintf("test_wallet_%d", random.rand32()).c_str();
3636
bench.run([&] {
37-
auto wallet = CreateWallet(context, wallet_path.u8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
37+
auto wallet = CreateWallet(context, wallet_path.utf8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
3838
assert(status == DatabaseStatus::SUCCESS);
3939
assert(wallet != nullptr);
4040

src/common/args.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value)
277277
return result.has_filename() ? result : result.parent_path();
278278
}
279279

280-
const fs::path& ArgsManager::GetBlocksDirPath() const
280+
fs::path ArgsManager::GetBlocksDirPath() const
281281
{
282282
LOCK(cs_args);
283283
fs::path& path = m_cached_blocks_path;
@@ -302,7 +302,7 @@ const fs::path& ArgsManager::GetBlocksDirPath() const
302302
return path;
303303
}
304304

305-
const fs::path& ArgsManager::GetDataDir(bool net_specific) const
305+
fs::path ArgsManager::GetDataDir(bool net_specific) const
306306
{
307307
LOCK(cs_args);
308308
fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;

src/common/args.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,21 @@ class ArgsManager
215215
*
216216
* @return Blocks path which is network specific
217217
*/
218-
const fs::path& GetBlocksDirPath() const;
218+
fs::path GetBlocksDirPath() const;
219219

220220
/**
221221
* Get data directory path
222222
*
223223
* @return Absolute path on success, otherwise an empty path when a non-directory path would be returned
224224
*/
225-
const fs::path& GetDataDirBase() const { return GetDataDir(false); }
225+
fs::path GetDataDirBase() const { return GetDataDir(false); }
226226

227227
/**
228228
* Get data directory path with appended network identifier
229229
*
230230
* @return Absolute path on success, otherwise an empty path when a non-directory path would be returned
231231
*/
232-
const fs::path& GetDataDirNet() const { return GetDataDir(true); }
232+
fs::path GetDataDirNet() const { return GetDataDir(true); }
233233

234234
/**
235235
* Clear cached directory paths
@@ -420,7 +420,7 @@ class ArgsManager
420420
* @param net_specific Append network identifier to the returned path
421421
* @return Absolute path on success, otherwise an empty path when a non-directory path would be returned
422422
*/
423-
const fs::path& GetDataDir(bool net_specific) const;
423+
fs::path GetDataDir(bool net_specific) const;
424424

425425
/**
426426
* Return -regtest/-signet/-testnet/-chain= setting as a ChainType enum if a

src/qt/guiutil.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ fs::path QStringToPath(const QString &path)
669669

670670
QString PathToQString(const fs::path &path)
671671
{
672-
return QString::fromStdString(path.u8string());
672+
return QString::fromStdString(path.utf8string());
673673
}
674674

675675
QString NetworkToQString(Network net)
@@ -723,8 +723,7 @@ QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction
723723

724724
QString formatDurationStr(std::chrono::seconds dur)
725725
{
726-
using days = std::chrono::duration<int, std::ratio<86400>>; // can remove this line after C++20
727-
const auto d{std::chrono::duration_cast<days>(dur)};
726+
const auto d{std::chrono::duration_cast<std::chrono::days>(dur)};
728727
const auto h{std::chrono::duration_cast<std::chrono::hours>(dur - d)};
729728
const auto m{std::chrono::duration_cast<std::chrono::minutes>(dur - d - h)};
730729
const auto s{std::chrono::duration_cast<std::chrono::seconds>(dur - d - h - m)};

src/rpc/blockchain.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -2601,7 +2601,7 @@ static RPCHelpMan dumptxoutset()
26012601
if (fs::exists(path)) {
26022602
throw JSONRPCError(
26032603
RPC_INVALID_PARAMETER,
2604-
path.u8string() + " already exists. If you are sure this is what you want, "
2604+
path.utf8string() + " already exists. If you are sure this is what you want, "
26052605
"move it out of the way first");
26062606
}
26072607

@@ -2610,15 +2610,15 @@ static RPCHelpMan dumptxoutset()
26102610
if (afile.IsNull()) {
26112611
throw JSONRPCError(
26122612
RPC_INVALID_PARAMETER,
2613-
"Couldn't open file " + temppath.u8string() + " for writing.");
2613+
"Couldn't open file " + temppath.utf8string() + " for writing.");
26142614
}
26152615

26162616
NodeContext& node = EnsureAnyNodeContext(request.context);
26172617
UniValue result = CreateUTXOSnapshot(
26182618
node, node.chainman->ActiveChainstate(), afile, path, temppath);
26192619
fs::rename(temppath, path);
26202620

2621-
result.pushKV("path", path.u8string());
2621+
result.pushKV("path", path.utf8string());
26222622
return result;
26232623
},
26242624
};
@@ -2690,7 +2690,7 @@ UniValue CreateUTXOSnapshot(
26902690
result.pushKV("coins_written", maybe_stats->coins_count);
26912691
result.pushKV("base_hash", tip->GetBlockHash().ToString());
26922692
result.pushKV("base_height", tip->nHeight);
2693-
result.pushKV("path", path.u8string());
2693+
result.pushKV("path", path.utf8string());
26942694
result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString());
26952695
result.pushKV("nchaintx", tip->nChainTx);
26962696
return result;
@@ -2743,7 +2743,7 @@ static RPCHelpMan loadtxoutset()
27432743
if (afile.IsNull()) {
27442744
throw JSONRPCError(
27452745
RPC_INVALID_PARAMETER,
2746-
"Couldn't open file " + path.u8string() + " for reading.");
2746+
"Couldn't open file " + path.utf8string() + " for reading.");
27472747
}
27482748

27492749
SnapshotMetadata metadata;

src/rpc/mempool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ static RPCHelpMan savemempool()
806806
}
807807

808808
UniValue ret(UniValue::VOBJ);
809-
ret.pushKV("filename", dump_path.u8string());
809+
ret.pushKV("filename", dump_path.utf8string());
810810

811811
return ret;
812812
},

src/rpc/server.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static RPCHelpMan getrpcinfo()
245245
UniValue result(UniValue::VOBJ);
246246
result.pushKV("active_commands", active_commands);
247247

248-
const std::string path = LogInstance().m_file_path.u8string();
248+
const std::string path = LogInstance().m_file_path.utf8string();
249249
UniValue log_path(UniValue::VSTR, path);
250250
result.pushKV("logpath", log_path);
251251

src/test/fs_tests.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ BOOST_FIXTURE_TEST_SUITE(fs_tests, BasicTestingSetup)
1717
BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)
1818
{
1919
std::string u8_str = "fs_tests_₿_🏃";
20+
std::u8string str8{u8"fs_tests_₿_🏃"};
2021
BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(u8_str)), u8_str);
21-
BOOST_CHECK_EQUAL(fs::u8path(u8_str).u8string(), u8_str);
22-
BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).u8string(), u8_str);
22+
BOOST_CHECK_EQUAL(fs::u8path(u8_str).utf8string(), u8_str);
23+
BOOST_CHECK_EQUAL(fs::path(str8).utf8string(), u8_str);
24+
BOOST_CHECK(fs::path(str8).u8string() == str8);
25+
BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).utf8string(), u8_str);
2326
BOOST_CHECK_EQUAL(fs::PathToString(fs::u8path(u8_str)), u8_str);
2427
#ifndef WIN32
2528
// On non-windows systems, verify that arbitrary byte strings containing
@@ -46,7 +49,7 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
4649
fs::path tmpfolder = m_args.GetDataDirBase();
4750
// tmpfile1 should be the same as tmpfile2
4851
fs::path tmpfile1 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
49-
fs::path tmpfile2 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
52+
fs::path tmpfile2 = tmpfolder / fs::path(u8"fs_tests_₿_🏃");
5053
{
5154
std::ofstream file{tmpfile1};
5255
file << "bitcoin";

src/util/fs.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2022 The Bitcoin Core developers
1+
// Copyright (c) 2017-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -18,6 +18,7 @@
1818
#endif
1919

2020
#include <cassert>
21+
#include <cerrno>
2122
#include <string>
2223

2324
namespace fsbridge {
@@ -130,4 +131,4 @@ std::string get_filesystem_error_message(const fs::filesystem_error& e)
130131
#endif
131132
}
132133

133-
} // fsbridge
134+
} // namespace fsbridge

src/util/fs.h

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2022 The Bitcoin Core developers
1+
// Copyright (c) 2017-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -14,6 +14,8 @@
1414
#include <ios>
1515
#include <ostream>
1616
#include <string>
17+
#include <system_error>
18+
#include <type_traits>
1719
#include <utility>
1820

1921
/** Filesystem operations and types */
@@ -35,7 +37,7 @@ class path : public std::filesystem::path
3537
// Allow path objects arguments for compatibility.
3638
path(std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
3739
path& operator=(std::filesystem::path path) { std::filesystem::path::operator=(std::move(path)); return *this; }
38-
path& operator/=(std::filesystem::path path) { std::filesystem::path::operator/=(path); return *this; }
40+
path& operator/=(const std::filesystem::path& path) { std::filesystem::path::operator/=(path); return *this; }
3941

4042
// Allow literal string arguments, which are safe as long as the literals are ASCII.
4143
path(const char* c) : std::filesystem::path(c) {}
@@ -52,12 +54,15 @@ class path : public std::filesystem::path
5254
// Disallow std::string conversion method to avoid locale-dependent encoding on windows.
5355
std::string string() const = delete;
5456

55-
std::string u8string() const
57+
/**
58+
* Return a UTF-8 representation of the path as a std::string, for
59+
* compatibility with code using std::string. For code using the newer
60+
* std::u8string type, it is more efficient to call the inherited
61+
* std::filesystem::path::u8string method instead.
62+
*/
63+
std::string utf8string() const
5664
{
57-
const auto& utf8_str{std::filesystem::path::u8string()};
58-
// utf8_str might either be std::string (C++17) or std::u8string
59-
// (C++20). Convert both to std::string. This method can be removed
60-
// after switching to C++20.
65+
const std::u8string& utf8_str{std::filesystem::path::u8string()};
6166
return std::string{utf8_str.begin(), utf8_str.end()};
6267
}
6368

@@ -69,11 +74,7 @@ class path : public std::filesystem::path
6974

7075
static inline path u8path(const std::string& utf8_str)
7176
{
72-
#if __cplusplus < 202002L
73-
return std::filesystem::u8path(utf8_str);
74-
#else
7577
return std::filesystem::path(std::u8string{utf8_str.begin(), utf8_str.end()});
76-
#endif
7778
}
7879

7980
// Disallow implicit std::string conversion for absolute to avoid
@@ -97,9 +98,9 @@ static inline auto quoted(const std::string& s)
9798
}
9899

99100
// Allow safe path append operations.
100-
static inline path operator/(path p1, path p2)
101+
static inline path operator/(path p1, const path& p2)
101102
{
102-
p1 /= std::move(p2);
103+
p1 /= p2;
103104
return p1;
104105
}
105106
static inline path operator/(path p1, const char* p2)
@@ -140,7 +141,7 @@ static inline bool copy_file(const path& from, const path& to, copy_options opti
140141
* Because \ref PathToString and \ref PathFromString functions don't specify an
141142
* encoding, they are meant to be used internally, not externally. They are not
142143
* appropriate to use in applications requiring UTF-8, where
143-
* fs::path::u8string() and fs::u8path() methods should be used instead. Other
144+
* fs::path::u8string() / fs::path::utf8string() and fs::u8path() methods should be used instead. Other
144145
* applications could require still different encodings. For example, JSON, XML,
145146
* or URI applications might prefer to use higher-level escapes (\uXXXX or
146147
* &XXXX; or %XX) instead of multibyte encoding. Rust, Python, Java applications
@@ -154,13 +155,13 @@ static inline std::string PathToString(const path& path)
154155
// use here, because these methods encode the path using C++'s narrow
155156
// multibyte encoding, which on Windows corresponds to the current "code
156157
// page", which is unpredictable and typically not able to represent all
157-
// valid paths. So fs::path::u8string() and
158+
// valid paths. So fs::path::utf8string() and
158159
// fs::u8path() functions are used instead on Windows. On
159-
// POSIX, u8string/u8path functions are not safe to use because paths are
160+
// POSIX, u8string/utf8string/u8path functions are not safe to use because paths are
160161
// not always valid UTF-8, so plain string methods which do not transform
161162
// the path there are used.
162163
#ifdef WIN32
163-
return path.u8string();
164+
return path.utf8string();
164165
#else
165166
static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform");
166167
return path.std::filesystem::path::string();

src/wallet/rpc/backup.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ RPCHelpMan dumpwallet()
734734
* It may also avoid other security issues.
735735
*/
736736
if (fs::exists(filepath)) {
737-
throw JSONRPCError(RPC_INVALID_PARAMETER, filepath.u8string() + " already exists. If you are sure this is what you want, move it out of the way first");
737+
throw JSONRPCError(RPC_INVALID_PARAMETER, filepath.utf8string() + " already exists. If you are sure this is what you want, move it out of the way first");
738738
}
739739

740740
std::ofstream file;
@@ -828,7 +828,7 @@ RPCHelpMan dumpwallet()
828828
file.close();
829829

830830
UniValue reply(UniValue::VOBJ);
831-
reply.pushKV("filename", filepath.u8string());
831+
reply.pushKV("filename", filepath.utf8string());
832832

833833
return reply;
834834
},

src/wallet/rpc/wallet.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static RPCHelpMan listwalletdir()
169169
UniValue wallets(UniValue::VARR);
170170
for (const auto& path : ListDatabases(GetWalletDir())) {
171171
UniValue wallet(UniValue::VOBJ);
172-
wallet.pushKV("name", path.u8string());
172+
wallet.pushKV("name", path.utf8string());
173173
wallets.push_back(wallet);
174174
}
175175

@@ -806,7 +806,7 @@ static RPCHelpMan migratewallet()
806806
if (res->solvables_wallet) {
807807
r.pushKV("solvables_name", res->solvables_wallet->GetName());
808808
}
809-
r.pushKV("backup_path", res->backup_path.u8string());
809+
r.pushKV("backup_path", res->backup_path.utf8string());
810810

811811
return r;
812812
},

0 commit comments

Comments
 (0)