aboutsummaryrefslogtreecommitdiff
path: root/src/wallet
diff options
context:
space:
mode:
authorKiminuo <kiminuo@protonmail.com>2020-06-11 08:58:46 +0200
committerfanquake <fanquake@gmail.com>2022-02-03 18:35:52 +0800
commit41d7166c8a598b604aad6c6b1d88ad46e23be247 (patch)
tree883af805ab387dc7db01602a19ccd70d89b78cb1 /src/wallet
parentffc89d1f21258553c0087b774a9ea1ce84139d4f (diff)
downloadbitcoin-41d7166c8a598b604aad6c6b1d88ad46e23be247.tar.xz
refactor: replace boost::filesystem with std::filesystem
Warning: Replacing fs::system_complete calls with fs::absolute calls in this commit may cause minor changes in behaviour because fs::absolute no longer strips trailing slashes; however these changes are believed to be safe. Co-authored-by: Russell Yanofsky <russ@yanofsky.org> Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
Diffstat (limited to 'src/wallet')
-rw-r--r--src/wallet/bdb.cpp5
-rw-r--r--src/wallet/db.cpp22
-rw-r--r--src/wallet/dump.cpp12
-rw-r--r--src/wallet/dump.h3
-rw-r--r--src/wallet/load.cpp6
-rw-r--r--src/wallet/rpc/backup.cpp9
-rw-r--r--src/wallet/test/db_tests.cpp7
-rw-r--r--src/wallet/test/init_test_fixture.cpp9
-rw-r--r--src/wallet/test/init_tests.cpp3
-rw-r--r--src/wallet/wallet.cpp8
-rw-r--r--src/wallet/walletdb.cpp2
11 files changed, 57 insertions, 29 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp
index cea120a81e..49f0abf9e7 100644
--- a/src/wallet/bdb.cpp
+++ b/src/wallet/bdb.cpp
@@ -3,6 +3,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <fs.h>
#include <wallet/bdb.h>
#include <wallet/db.h>
@@ -620,12 +621,12 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) const
pathDest /= fs::PathFromString(strFile);
try {
- if (fs::equivalent(pathSrc, pathDest)) {
+ if (fs::exists(pathDest) && fs::equivalent(pathSrc, pathDest)) {
LogPrintf("cannot backup to wallet source file %s\n", fs::PathToString(pathDest));
return false;
}
- fs::copy_file(pathSrc, pathDest, fs::copy_option::overwrite_if_exists);
+ fs::copy_file(pathSrc, pathDest, fs::copy_options::overwrite_existing);
LogPrintf("copied %s to %s\n", strFile, fs::PathToString(pathDest));
return true;
} catch (const fs::filesystem_error& e) {
diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp
index 414d0ef5c3..0ed2658129 100644
--- a/src/wallet/db.cpp
+++ b/src/wallet/db.cpp
@@ -8,18 +8,22 @@
#include <logging.h>
#include <wallet/db.h>
+#include <exception>
+#include <fstream>
#include <string>
+#include <system_error>
+#include <vector>
namespace wallet {
std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
{
std::vector<fs::path> paths;
- boost::system::error_code ec;
+ std::error_code ec;
for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
if (ec) {
if (fs::is_directory(*it)) {
- it.no_push();
+ it.disable_recursion_pending();
LogPrintf("%s: %s %s -- skipping.\n", __func__, ec.message(), fs::PathToString(it->path()));
} else {
LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(it->path()));
@@ -30,11 +34,11 @@ std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
try {
const fs::path path{it->path().lexically_relative(wallet_dir)};
- if (it->status().type() == fs::directory_file &&
+ if (it->status().type() == fs::file_type::directory &&
(IsBDBFile(BDBDataFile(it->path())) || IsSQLiteFile(SQLiteDataFile(it->path())))) {
// Found a directory which contains wallet.dat btree file, add it as a wallet.
paths.emplace_back(path);
- } else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && IsBDBFile(it->path())) {
+ } else if (it.depth() == 0 && it->symlink_status().type() == fs::file_type::regular && IsBDBFile(it->path())) {
if (it->path().filename() == "wallet.dat") {
// Found top-level wallet.dat btree file, add top level directory ""
// as a wallet.
@@ -49,7 +53,7 @@ std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
}
} catch (const std::exception& e) {
LogPrintf("%s: Error scanning %s: %s\n", __func__, fs::PathToString(it->path()), e.what());
- it.no_push();
+ it.disable_recursion_pending();
}
}
@@ -81,12 +85,12 @@ bool IsBDBFile(const fs::path& path)
// A Berkeley DB Btree file has at least 4K.
// This check also prevents opening lock files.
- boost::system::error_code ec;
+ std::error_code ec;
auto size = fs::file_size(path, ec);
if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path));
if (size < 4096) return false;
- fsbridge::ifstream file(path, std::ios::binary);
+ std::ifstream file{path, std::ios::binary};
if (!file.is_open()) return false;
file.seekg(12, std::ios::beg); // Magic bytes start at offset 12
@@ -105,12 +109,12 @@ bool IsSQLiteFile(const fs::path& path)
if (!fs::exists(path)) return false;
// A SQLite Database file is at least 512 bytes.
- boost::system::error_code ec;
+ std::error_code ec;
auto size = fs::file_size(path, ec);
if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path));
if (size < 512) return false;
- fsbridge::ifstream file(path, std::ios::binary);
+ std::ifstream file{path, std::ios::binary};
if (!file.is_open()) return false;
// Magic is at beginning and is 16 bytes long
diff --git a/src/wallet/dump.cpp b/src/wallet/dump.cpp
index 3e34a2f776..6d8508fc72 100644
--- a/src/wallet/dump.cpp
+++ b/src/wallet/dump.cpp
@@ -4,9 +4,17 @@
#include <wallet/dump.h>
+#include <fs.h>
#include <util/translation.h>
#include <wallet/wallet.h>
+#include <algorithm>
+#include <fstream>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
namespace wallet {
static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP";
uint32_t DUMP_VERSION = 1;
@@ -26,7 +34,7 @@ bool DumpWallet(CWallet& wallet, bilingual_str& error)
error = strprintf(_("File %s already exists. If you are sure this is what you want, move it out of the way first."), fs::PathToString(path));
return false;
}
- fsbridge::ofstream dump_file;
+ std::ofstream dump_file;
dump_file.open(path);
if (dump_file.fail()) {
error = strprintf(_("Unable to open %s for writing"), fs::PathToString(path));
@@ -121,7 +129,7 @@ bool CreateFromDump(const std::string& name, const fs::path& wallet_path, biling
error = strprintf(_("Dump file %s does not exist."), fs::PathToString(dump_path));
return false;
}
- fsbridge::ifstream dump_file(dump_path);
+ std::ifstream dump_file{dump_path};
// Compute the checksum
CHashWriter hasher(0, 0);
diff --git a/src/wallet/dump.h b/src/wallet/dump.h
index 4effab3bed..a879c4db35 100644
--- a/src/wallet/dump.h
+++ b/src/wallet/dump.h
@@ -7,6 +7,9 @@
#include <fs.h>
+#include <string>
+#include <vector>
+
struct bilingual_str;
namespace wallet {
diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp
index e6f96074d5..4949ed7dc9 100644
--- a/src/wallet/load.cpp
+++ b/src/wallet/load.cpp
@@ -19,6 +19,8 @@
#include <univalue.h>
+#include <system_error>
+
namespace wallet {
bool VerifyWallets(WalletContext& context)
{
@@ -27,9 +29,9 @@ bool VerifyWallets(WalletContext& context)
if (args.IsArgSet("-walletdir")) {
fs::path wallet_dir = fs::PathFromString(args.GetArg("-walletdir", ""));
- boost::system::error_code error;
+ std::error_code error;
// The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
- fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error).remove_trailing_separator();
+ fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
if (error || !fs::exists(wallet_dir)) {
chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), fs::PathToString(wallet_dir)));
return false;
diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp
index c0912ffc70..228564fae4 100644
--- a/src/wallet/rpc/backup.cpp
+++ b/src/wallet/rpc/backup.cpp
@@ -5,6 +5,7 @@
#include <chain.h>
#include <clientversion.h>
#include <core_io.h>
+#include <fs.h>
#include <interfaces/chain.h>
#include <key_io.h>
#include <merkleblock.h>
@@ -20,8 +21,10 @@
#include <wallet/rpc/util.h>
#include <wallet/wallet.h>
-#include <stdint.h>
+#include <cstdint>
+#include <fstream>
#include <tuple>
+#include <string>
#include <boost/algorithm/string.hpp>
@@ -521,7 +524,7 @@ RPCHelpMan importwallet()
EnsureWalletIsUnlocked(*pwallet);
- fsbridge::ifstream file;
+ std::ifstream file;
file.open(fs::u8path(request.params[0].get_str()), std::ios::in | std::ios::ate);
if (!file.is_open()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
@@ -729,7 +732,7 @@ RPCHelpMan dumpwallet()
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");
}
- fsbridge::ofstream file;
+ std::ofstream file;
file.open(filepath);
if (!file.is_open())
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
diff --git a/src/wallet/test/db_tests.cpp b/src/wallet/test/db_tests.cpp
index 825382fe59..35ae3707f8 100644
--- a/src/wallet/test/db_tests.cpp
+++ b/src/wallet/test/db_tests.cpp
@@ -2,14 +2,15 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include <memory>
-
#include <boost/test/unit_test.hpp>
#include <fs.h>
#include <test/util/setup_common.h>
#include <wallet/bdb.h>
+#include <fstream>
+#include <memory>
+#include <string>
namespace wallet {
BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup)
@@ -26,7 +27,7 @@ BOOST_AUTO_TEST_CASE(getwalletenv_file)
std::string test_name = "test_name.dat";
const fs::path datadir = gArgs.GetDataDirNet();
fs::path file_path = datadir / test_name;
- fs::ofstream f(file_path);
+ std::ofstream f{file_path};
f.close();
std::string filename;
diff --git a/src/wallet/test/init_test_fixture.cpp b/src/wallet/test/init_test_fixture.cpp
index b455ab9d9e..be38cebafd 100644
--- a/src/wallet/test/init_test_fixture.cpp
+++ b/src/wallet/test/init_test_fixture.cpp
@@ -7,6 +7,9 @@
#include <util/check.h>
#include <util/system.h>
+#include <fstream>
+#include <string>
+
#include <wallet/test/init_test_fixture.h>
namespace wallet {
@@ -24,8 +27,8 @@ InitWalletDirTestingSetup::InitWalletDirTestingSetup(const std::string& chainNam
m_walletdir_path_cases["custom"] = m_datadir / "my_wallets";
m_walletdir_path_cases["nonexistent"] = m_datadir / "path_does_not_exist";
m_walletdir_path_cases["file"] = m_datadir / "not_a_directory.dat";
- m_walletdir_path_cases["trailing"] = m_datadir / "wallets" / sep;
- m_walletdir_path_cases["trailing2"] = m_datadir / "wallets" / sep / sep;
+ m_walletdir_path_cases["trailing"] = m_datadir / ("wallets" + sep);
+ m_walletdir_path_cases["trailing2"] = m_datadir / ("wallets" + sep + sep);
fs::current_path(m_datadir);
m_walletdir_path_cases["relative"] = "wallets";
@@ -33,7 +36,7 @@ InitWalletDirTestingSetup::InitWalletDirTestingSetup(const std::string& chainNam
fs::create_directories(m_walletdir_path_cases["default"]);
fs::create_directories(m_walletdir_path_cases["custom"]);
fs::create_directories(m_walletdir_path_cases["relative"]);
- fs::ofstream f(m_walletdir_path_cases["file"]);
+ std::ofstream f{m_walletdir_path_cases["file"]};
f.close();
}
diff --git a/src/wallet/test/init_tests.cpp b/src/wallet/test/init_tests.cpp
index c1cae5c5f6..d1df48a312 100644
--- a/src/wallet/test/init_tests.cpp
+++ b/src/wallet/test/init_tests.cpp
@@ -73,6 +73,8 @@ BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_no_trailing)
BOOST_CHECK_EQUAL(walletdir, expected_path);
}
+#ifndef WIN32
+// Windows does not consider "datadir/wallets//" to be a valid directory path.
BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_no_trailing2)
{
SetWalletDir(m_walletdir_path_cases["trailing2"]);
@@ -82,6 +84,7 @@ BOOST_AUTO_TEST_CASE(walletinit_verify_walletdir_no_trailing2)
fs::path expected_path = fs::canonical(m_walletdir_path_cases["default"]);
BOOST_CHECK_EQUAL(walletdir, expected_path);
}
+#endif
BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 84962af906..7e694d1987 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -378,7 +378,7 @@ std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& b
}
auto wallet_file = wallet_path / "wallet.dat";
- fs::copy_file(backup_file, wallet_file, fs::copy_option::fail_if_exists);
+ fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
auto wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
@@ -2651,9 +2651,9 @@ std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, cons
// 4. For backwards compatibility, the name of a data file in -walletdir.
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::PathFromString(name));
fs::file_type path_type = fs::symlink_status(wallet_path).type();
- if (!(path_type == fs::file_not_found || path_type == fs::directory_file ||
- (path_type == fs::symlink_file && fs::is_directory(wallet_path)) ||
- (path_type == fs::regular_file && fs::PathFromString(name).filename() == fs::PathFromString(name)))) {
+ if (!(path_type == fs::file_type::not_found || path_type == fs::file_type::directory ||
+ (path_type == fs::file_type::symlink && fs::is_directory(wallet_path)) ||
+ (path_type == fs::file_type::regular && fs::PathFromString(name).filename() == fs::PathFromString(name)))) {
error_string = Untranslated(strprintf(
"Invalid -wallet path '%s'. -wallet path should point to a directory where wallet.dat and "
"database/log.?????????? files can be stored, a location where such a directory could be created, "
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index 9cef76d803..c11d4b562d 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -1105,7 +1105,7 @@ std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const Databas
{
bool exists;
try {
- exists = fs::symlink_status(path).type() != fs::file_not_found;
+ exists = fs::symlink_status(path).type() != fs::file_type::not_found;
} catch (const fs::filesystem_error& e) {
error = Untranslated(strprintf("Failed to access database path '%s': %s", fs::PathToString(path), fsbridge::get_filesystem_error_message(e)));
status = DatabaseStatus::FAILED_BAD_PATH;