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

File tree

13 files changed

+48
-44
lines changed

13 files changed

+48
-44
lines changed

doc/developer-notes.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 3 additions & 2 deletions
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

0 commit comments

Comments
 (0)