Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/walletutil.cpp
// Copyright (c) 2017 The Bitcoin Core developers | // Copyright (c) 2017 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <wallet/walletutil.h> | #include <wallet/walletutil.h> | ||||
#include <logging.h> | |||||
#include <util/system.h> | #include <util/system.h> | ||||
fs::path GetWalletDir() { | fs::path GetWalletDir() { | ||||
fs::path path; | fs::path path; | ||||
if (gArgs.IsArgSet("-walletdir")) { | if (gArgs.IsArgSet("-walletdir")) { | ||||
path = gArgs.GetArg("-walletdir", ""); | path = gArgs.GetArg("-walletdir", ""); | ||||
if (!fs::is_directory(path)) { | if (!fs::is_directory(path)) { | ||||
Show All 12 Lines | fs::path GetWalletDir() { | ||||
return path; | return path; | ||||
} | } | ||||
static bool IsBerkeleyBtree(const fs::path &path) { | static bool IsBerkeleyBtree(const fs::path &path) { | ||||
// A Berkeley DB Btree file has at least 4K. | // A Berkeley DB Btree file has at least 4K. | ||||
// This check also prevents opening lock files. | // This check also prevents opening lock files. | ||||
boost::system::error_code ec; | boost::system::error_code ec; | ||||
if (fs::file_size(path, ec) < 4096) { | auto size = fs::file_size(path, ec); | ||||
if (ec) { | |||||
LogPrintf("%s: %s %s\n", __func__, ec.message(), path.string()); | |||||
} | |||||
if (size < 4096) { | |||||
return false; | return false; | ||||
} | } | ||||
fsbridge::ifstream file(path, std::ios::binary); | fsbridge::ifstream file(path, std::ios::binary); | ||||
if (!file.is_open()) { | if (!file.is_open()) { | ||||
return false; | return false; | ||||
} | } | ||||
Show All 9 Lines | static bool IsBerkeleyBtree(const fs::path &path) { | ||||
// - little endian systems - 62 31 05 00 | // - little endian systems - 62 31 05 00 | ||||
return data == 0x00053162 || data == 0x62310500; | return data == 0x00053162 || data == 0x62310500; | ||||
} | } | ||||
std::vector<fs::path> ListWalletDir() { | std::vector<fs::path> ListWalletDir() { | ||||
const fs::path wallet_dir = GetWalletDir(); | const fs::path wallet_dir = GetWalletDir(); | ||||
const size_t offset = wallet_dir.string().size() + 1; | const size_t offset = wallet_dir.string().size() + 1; | ||||
std::vector<fs::path> paths; | std::vector<fs::path> paths; | ||||
boost::system::error_code ec; | |||||
for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); | |||||
it != fs::recursive_directory_iterator(); it.increment(ec)) { | |||||
if (ec) { | |||||
LogPrintf("%s: %s %s\n", __func__, ec.message(), | |||||
it->path().string()); | |||||
continue; | |||||
} | |||||
for (auto it = fs::recursive_directory_iterator(wallet_dir); | |||||
it != fs::recursive_directory_iterator(); ++it) { | |||||
// Get wallet path relative to walletdir by removing walletdir from the | // Get wallet path relative to walletdir by removing walletdir from the | ||||
// wallet path. This can be replaced by | // wallet path. This can be replaced by | ||||
// boost::filesystem::lexically_relative once boost is bumped to 1.60. | // boost::filesystem::lexically_relative once boost is bumped to 1.60. | ||||
const fs::path path = it->path().string().substr(offset); | const fs::path path = it->path().string().substr(offset); | ||||
if (it->status().type() == fs::directory_file && | if (it->status().type() == fs::directory_file && | ||||
IsBerkeleyBtree(it->path() / "wallet.dat")) { | IsBerkeleyBtree(it->path() / "wallet.dat")) { | ||||
// Found a directory which contains wallet.dat btree file, add it as | // Found a directory which contains wallet.dat btree file, add it as | ||||
Show All 29 Lines |