From 21f781ad7921ebda9f38a6be362e23750d8cd5a6 Mon Sep 17 00:00:00 2001 From: fanquake Date: Fri, 24 Dec 2021 16:19:02 +0800 Subject: [PATCH 1/3] fs: consistently use fsbridge for {i,o}fstream Part of #20744, but this can be done now, and will simplify the diff. --- src/bench/bench.cpp | 4 ++-- src/qt/psbtoperationsdialog.cpp | 2 +- src/qt/sendcoinsdialog.cpp | 2 +- src/qt/walletframe.cpp | 2 +- src/test/fuzz/fuzz.cpp | 2 +- src/util/system.cpp | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp index d7b4228566f..9bd176f0a07 100644 --- a/src/bench/bench.cpp +++ b/src/bench/bench.cpp @@ -4,10 +4,10 @@ #include +#include #include #include -#include #include #include #include @@ -29,7 +29,7 @@ void GenerateTemplateResults(const std::vector& bench // nothing to write, bail out return; } - std::ofstream fout(filename); + fsbridge::ofstream fout{fs::PathFromString(filename)}; if (fout.is_open()) { ankerl::nanobench::render(tpl, benchmarkResults, fout); } else { diff --git a/src/qt/psbtoperationsdialog.cpp b/src/qt/psbtoperationsdialog.cpp index 0962dfe9db5..d328290cbcc 100644 --- a/src/qt/psbtoperationsdialog.cpp +++ b/src/qt/psbtoperationsdialog.cpp @@ -158,7 +158,7 @@ void PSBTOperationsDialog::saveTransaction() { if (filename.isEmpty()) { return; } - std::ofstream out(filename.toLocal8Bit().data(), std::ofstream::out | std::ofstream::binary); + fsbridge::ofstream out{filename.toLocal8Bit().data(), fsbridge::ofstream::out | fsbridge::ofstream::binary}; out << ssTx.str(); out.close(); showStatus(tr("PSBT saved to disk."), StatusLevel::INFO); diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 50436a46d8e..1206f610cd2 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -509,7 +509,7 @@ void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked) if (filename.isEmpty()) { return; } - std::ofstream out(filename.toLocal8Bit().data(), std::ofstream::out | std::ofstream::binary); + fsbridge::ofstream out{filename.toLocal8Bit().data(), fsbridge::ofstream::out | fsbridge::ofstream::binary}; out << ssTx.str(); out.close(); Q_EMIT message(tr("PSBT saved"), "PSBT saved to disk", CClientUIInterface::MSG_INFORMATION); diff --git a/src/qt/walletframe.cpp b/src/qt/walletframe.cpp index 98f5ebce99a..fba83dd510e 100644 --- a/src/qt/walletframe.cpp +++ b/src/qt/walletframe.cpp @@ -210,7 +210,7 @@ void WalletFrame::gotoLoadPSBT(bool from_clipboard) Q_EMIT message(tr("Error"), tr("PSBT file must be smaller than 100 MiB"), CClientUIInterface::MSG_ERROR); return; } - std::ifstream in(filename.toLocal8Bit().data(), std::ios::binary); + fsbridge::ifstream in{filename.toLocal8Bit().data(), std::ios::binary}; data = std::string(std::istreambuf_iterator{in}, {}); } diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp index e9debd8c459..60c48e7c228 100644 --- a/src/test/fuzz/fuzz.cpp +++ b/src/test/fuzz/fuzz.cpp @@ -80,7 +80,7 @@ void initialize() } if (const char* out_path = std::getenv("WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) { std::cout << "Writing all fuzz target names to '" << out_path << "'." << std::endl; - std::ofstream out_stream(out_path, std::ios::binary); + fsbridge::ofstream out_stream{out_path, std::ios::binary}; for (const auto& t : FuzzTargets()) { if (std::get<2>(t.second)) continue; out_stream << t.first << std::endl; diff --git a/src/util/system.cpp b/src/util/system.cpp index e34cdc7fb93..19de08d1ead 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -146,7 +146,7 @@ bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes) } std::streampos GetFileSize(const char* path, std::streamsize max) { - std::ifstream file(path, std::ios::binary); + fsbridge::ifstream file{path, std::ios::binary}; file.ignore(max); return file.gcount(); } From 486261dfcb5ea3ec205a632066298ffa492de466 Mon Sep 17 00:00:00 2001 From: fanquake Date: Fri, 24 Dec 2021 16:24:38 +0800 Subject: [PATCH 2/3] fs: add missing include This is needed to prevent compilation failures once boost is removed, however is still correct to include now, and reduces the diff in #20744. is extracted from the defines because it is used for Windows and non-Windows code, i.e get_filesystem_error_message(). --- src/fs.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/fs.cpp b/src/fs.cpp index 34a0348578d..8fcadcb3ef9 100644 --- a/src/fs.cpp +++ b/src/fs.cpp @@ -7,7 +7,6 @@ #ifndef WIN32 #include #include -#include #include #include #include @@ -20,6 +19,9 @@ #include #endif +#include +#include + namespace fsbridge { FILE *fopen(const fs::path& p, const char *mode) From 5e8975e2694c3178ae73deb28986e1fb5466147e Mon Sep 17 00:00:00 2001 From: fanquake Date: Fri, 24 Dec 2021 16:44:57 +0800 Subject: [PATCH 3/3] fs: consistently use fsbridge for fopen() --- src/bitcoin-tx.cpp | 3 ++- src/test/script_tests.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index edec883264a..8237d7d34f0 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -158,7 +159,7 @@ static void RegisterLoad(const std::string& strInput) std::string key = strInput.substr(0, pos); std::string filename = strInput.substr(pos + 1, std::string::npos); - FILE *f = fopen(filename.c_str(), "r"); + FILE *f = fsbridge::fopen(filename.c_str(), "r"); if (!f) { std::string strErr = "Cannot open file " + filename; throw std::runtime_error(strErr); diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index eacd7ae894f..0da296495f3 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -923,7 +923,7 @@ BOOST_AUTO_TEST_CASE(script_build) } #ifdef UPDATE_JSON_TESTS - FILE* file = fopen("script_tests.json.gen", "w"); + FILE* file = fsbridge::fopen("script_tests.json.gen", "w"); fputs(strGen.c_str(), file); fclose(file); #endif