Skip to content

Commit 209488d

Browse files
committed
merge bitcoin#29040: Remove pre-C++20 code, fs::path cleanup
1 parent e2eb625 commit 209488d

File tree

13 files changed

+43
-39
lines changed

13 files changed

+43
-39
lines changed

doc/developer-notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ A few guidelines for introducing and reviewing new RPC interfaces:
14081408

14091409
- *Rationale*: User-facing consistency.
14101410

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

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

src/bench/wallet_create.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void WalletCreate(benchmark::Bench& bench, bool encrypted)
3535

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

src/fs.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#endif
1919

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

2324
namespace fsbridge {
@@ -135,4 +136,4 @@ std::string get_filesystem_error_message(const fs::filesystem_error& e)
135136
#endif
136137
}
137138

138-
} // fsbridge
139+
} // namespace fsbridge

src/fs.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <ios>
1414
#include <ostream>
1515
#include <string>
16+
#include <system_error>
17+
#include <type_traits>
1618
#include <utility>
1719

1820
/** Filesystem operations and types */
@@ -34,7 +36,7 @@ class path : public std::filesystem::path
3436
// Allow path objects arguments for compatibility.
3537
path(std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
3638
path& operator=(std::filesystem::path path) { std::filesystem::path::operator=(std::move(path)); return *this; }
37-
path& operator/=(std::filesystem::path path) { std::filesystem::path::operator/=(path); return *this; }
39+
path& operator/=(const std::filesystem::path& path) { std::filesystem::path::operator/=(path); return *this; }
3840

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

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

@@ -68,11 +73,7 @@ class path : public std::filesystem::path
6873

6974
static inline path u8path(const std::string& utf8_str)
7075
{
71-
#if __cplusplus < 202002L
72-
return std::filesystem::u8path(utf8_str);
73-
#else
7476
return std::filesystem::path(std::u8string{utf8_str.begin(), utf8_str.end()});
75-
#endif
7677
}
7778

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

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

src/qt/guiutil.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ fs::path QStringToPath(const QString &path)
16661666

16671667
QString PathToQString(const fs::path &path)
16681668
{
1669-
return QString::fromStdString(path.u8string());
1669+
return QString::fromStdString(path.utf8string());
16701670
}
16711671

16721672
QString NetworkToQString(Network net)
@@ -1715,8 +1715,7 @@ QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction
17151715

17161716
QString formatDurationStr(std::chrono::seconds dur)
17171717
{
1718-
using days = std::chrono::duration<int, std::ratio<86400>>; // can remove this line after C++20
1719-
const auto d{std::chrono::duration_cast<days>(dur)};
1718+
const auto d{std::chrono::duration_cast<std::chrono::days>(dur)};
17201719
const auto h{std::chrono::duration_cast<std::chrono::hours>(dur - d)};
17211720
const auto m{std::chrono::duration_cast<std::chrono::minutes>(dur - d - h)};
17221721
const auto s{std::chrono::duration_cast<std::chrono::seconds>(dur - d - h - m)};

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,7 +2631,7 @@ static RPCHelpMan dumptxoutset()
26312631
if (fs::exists(path)) {
26322632
throw JSONRPCError(
26332633
RPC_INVALID_PARAMETER,
2634-
path.u8string() + " already exists. If you are sure this is what you want, "
2634+
path.utf8string() + " already exists. If you are sure this is what you want, "
26352635
"move it out of the way first");
26362636
}
26372637

@@ -2642,7 +2642,7 @@ static RPCHelpMan dumptxoutset()
26422642
node, node.chainman->ActiveChainstate(), afile, path, temppath);
26432643
fs::rename(temppath, path);
26442644

2645-
result.pushKV("path", path.u8string());
2645+
result.pushKV("path", path.utf8string());
26462646
return result;
26472647
},
26482648
};
@@ -2713,7 +2713,7 @@ UniValue CreateUTXOSnapshot(
27132713
result.pushKV("coins_written", stats.coins_count);
27142714
result.pushKV("base_hash", tip->GetBlockHash().ToString());
27152715
result.pushKV("base_height", tip->nHeight);
2716-
result.pushKV("path", path.u8string());
2716+
result.pushKV("path", path.utf8string());
27172717
result.pushKV("txoutset_hash", stats.hashSerialized.ToString());
27182718
// Cast required because univalue doesn't have serialization specified for
27192719
// `unsigned int`, nChainTx's type.

src/rpc/mempool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ RPCHelpMan savemempool()
459459
}
460460

461461
UniValue ret(UniValue::VOBJ);
462-
ret.pushKV("filename", fs::path((args.GetDataDirNet() / "mempool.dat")).u8string());
462+
ret.pushKV("filename", fs::path((args.GetDataDirNet() / "mempool.dat")).utf8string());
463463

464464
return ret;
465465
},

src/rpc/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static RPCHelpMan getrpcinfo()
258258
UniValue result(UniValue::VOBJ);
259259
result.pushKV("active_commands", active_commands);
260260

261-
const std::string path = LogInstance().m_file_path.u8string();
261+
const std::string path = LogInstance().m_file_path.utf8string();
262262
UniValue log_path(UniValue::VSTR, path);
263263
result.pushKV("logpath", log_path);
264264

src/test/fs_tests.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ BOOST_FIXTURE_TEST_SUITE(fs_tests, BasicTestingSetup)
1818
BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)
1919
{
2020
std::string u8_str = "fs_tests_∋_🏃";
21+
std::u8string str8{u8"fs_tests_∋_🏃"};
2122
BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(u8_str)), u8_str);
22-
BOOST_CHECK_EQUAL(fs::u8path(u8_str).u8string(), u8_str);
23-
BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).u8string(), u8_str);
23+
BOOST_CHECK_EQUAL(fs::u8path(u8_str).utf8string(), u8_str);
24+
BOOST_CHECK_EQUAL(fs::path(str8).utf8string(), u8_str);
25+
BOOST_CHECK(fs::path(str8).u8string() == str8);
26+
BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).utf8string(), u8_str);
2427
BOOST_CHECK_EQUAL(fs::PathToString(fs::u8path(u8_str)), u8_str);
2528
#ifndef WIN32
2629
// On non-windows systems, verify that arbitrary byte strings containing
@@ -47,7 +50,7 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
4750
fs::path tmpfolder = m_args.GetDataDirBase();
4851
// tmpfile1 should be the same as tmpfile2
4952
fs::path tmpfile1 = tmpfolder / fs::u8path("fs_tests_∋_🏃");
50-
fs::path tmpfile2 = tmpfolder / fs::u8path("fs_tests_∋_🏃");
53+
fs::path tmpfile2 = tmpfolder / fs::path(u8"fs_tests_∋_🏃");
5154
{
5255
std::ofstream file{tmpfile1};
5356
file << "bitcoin";

src/util/system.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value)
418418
return result.has_filename() ? result : result.parent_path();
419419
}
420420

421-
const fs::path ArgsManager::GetBlocksDirPath() const
421+
fs::path ArgsManager::GetBlocksDirPath() const
422422
{
423423
LOCK(cs_args);
424424
fs::path& path = m_cached_blocks_path;
@@ -443,7 +443,7 @@ const fs::path ArgsManager::GetBlocksDirPath() const
443443
return path;
444444
}
445445

446-
const fs::path ArgsManager::GetDataDir(bool net_specific) const
446+
fs::path ArgsManager::GetDataDir(bool net_specific) const
447447
{
448448
LOCK(cs_args);
449449
fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;

0 commit comments

Comments
 (0)