Skip to content

Commit

Permalink
wallet: let Listwallets do not iterate trough our blocksdata.
Browse files Browse the repository at this point in the history
When WalletDir == DataDir we would have iterate trough our own node files
to find wallets, that consumes time and could cause an unresponsive node.

Signed-off-by: saibato <saibato.naga@pm.me>
  • Loading branch information
Saibato committed Jul 1, 2020
1 parent 2af56d6 commit 55e5460
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions src/wallet/walletutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,59 @@ std::vector<fs::path> ListWalletDir()
const size_t offset = wallet_dir.string().size() + 1;
std::vector<fs::path> paths;
boost::system::error_code ec;
fs::path sub_path;

for(auto sub_dir = fs::directory_iterator(wallet_dir, ec);sub_dir != fs::directory_iterator(); sub_dir.increment(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());
LogPrintf("%s: %s %s\n", __func__, ec.message(), sub_dir->path().string());
continue;
}

// Get wallet path relative to walletdir by removing walletdir from the wallet path.
// This can be replaced by boost::filesystem::lexically_relative once boost is bumped to 1.60.
const fs::path path = it->path().string().substr(offset);

if (it->status().type() == fs::directory_file && IsBerkeleyBtree(it->path() / "wallet.dat")) {
// 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 && IsBerkeleyBtree(it->path())) {
if (it->path().filename() == "wallet.dat") {
// Found top-level wallet.dat btree file, add top level directory ""
// as a wallet.
paths.emplace_back();
} else {
// Found top-level btree file not called wallet.dat. Current bitcoin
// software will never create these files but will allow them to be
// opened in a shared database environment for backwards compatibility.
// Add it to the list of available wallets.
sub_path = sub_dir->path();

if ( wallet_dir != GetDataDir() ) {
sub_path = wallet_dir;
} else {

if (sub_dir->path() == GetBlocksDir()) continue;
if (sub_dir->path() == GetDataDir() / "testnet3") continue;
if (sub_dir->path() == GetDataDir() / "regtest") continue;
if (sub_dir->status().type() == fs::directory_file && IsBerkeleyBtree( sub_dir->path() / "wallet.dat")) {
// Found a directory which contains wallet.dat btree file, add it as a wallet.
fs::path path = sub_dir->path().string().substr(offset);
paths.emplace_back(path);
}
}

for (auto it = fs::recursive_directory_iterator(sub_path, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
if (ec) {
LogPrintf("%s: %s %s\n", __func__, ec.message(), it->path().string());
continue;
}

// Get wallet path relative to walletdir by removing walletdir from the wallet path.
// This can be replaced by boost::filesystem::lexically_relative once boost is bumped to 1.60.
fs::path path = it->path().string().substr(offset);

if (it->status().type() == fs::directory_file && IsBerkeleyBtree( it->path() / "wallet.dat")) {
// 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 && IsBerkeleyBtree(it->path())) {
if (it->path().filename() == "wallet.dat") {
// Found top-level wallet.dat btree file, add top level directory ""
// as a wallet.
paths.emplace_back();
} else {
// Found top-level btree file not called wallet.dat. Current bitcoin
// software will never create these files but will allow them to be
// opened in a shared database environment for backwards compatibility.
// Add it to the list of available wallets.
paths.emplace_back(path);
}
}
}
if ( wallet_dir != GetDataDir() ) break;
}

return paths;
Expand Down

0 comments on commit 55e5460

Please sign in to comment.