2019-05-06 14:05:28 -04:00
|
|
|
// Copyright (c) 2017-2019 The Bitcoin Core developers
|
2017-10-08 17:48:07 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-17 21:36:37 -03:00
|
|
|
#include <wallet/walletutil.h>
|
2017-10-08 17:48:07 -03:00
|
|
|
|
2019-03-11 19:57:59 -03:00
|
|
|
#include <logging.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/system.h>
|
2018-09-21 20:24:29 -03:00
|
|
|
|
2020-05-26 20:54:05 -04:00
|
|
|
bool ExistsBerkeleyDatabase(const fs::path& path);
|
|
|
|
|
2017-10-08 17:48:07 -03:00
|
|
|
fs::path GetWalletDir()
|
|
|
|
{
|
|
|
|
fs::path path;
|
|
|
|
|
|
|
|
if (gArgs.IsArgSet("-walletdir")) {
|
2018-01-18 15:15:00 -03:00
|
|
|
path = gArgs.GetArg("-walletdir", "");
|
2017-10-08 17:48:07 -03:00
|
|
|
if (!fs::is_directory(path)) {
|
|
|
|
// If the path specified doesn't exist, we return the deliberately
|
|
|
|
// invalid empty string.
|
|
|
|
path = "";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path = GetDataDir();
|
2017-10-26 23:15:40 -03:00
|
|
|
// If a wallets directory exists, use that, otherwise default to GetDataDir
|
|
|
|
if (fs::is_directory(path / "wallets")) {
|
|
|
|
path /= "wallets";
|
|
|
|
}
|
2017-10-08 17:48:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2018-09-21 20:24:29 -03:00
|
|
|
|
|
|
|
std::vector<fs::path> ListWalletDir()
|
|
|
|
{
|
|
|
|
const fs::path wallet_dir = GetWalletDir();
|
2018-10-20 10:29:48 -03:00
|
|
|
const size_t offset = wallet_dir.string().size() + 1;
|
2018-09-21 20:24:29 -03:00
|
|
|
std::vector<fs::path> paths;
|
2019-03-11 19:57:59 -03:00
|
|
|
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;
|
|
|
|
}
|
2018-09-21 20:24:29 -03:00
|
|
|
|
2018-10-20 10:29:48 -03:00
|
|
|
// 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);
|
|
|
|
|
2020-05-26 20:54:05 -04:00
|
|
|
if (it->status().type() == fs::directory_file && ExistsBerkeleyDatabase(it->path())) {
|
2018-09-21 20:24:29 -03:00
|
|
|
// Found a directory which contains wallet.dat btree file, add it as a wallet.
|
2018-10-20 10:29:48 -03:00
|
|
|
paths.emplace_back(path);
|
2020-05-26 20:54:05 -04:00
|
|
|
} else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && ExistsBerkeleyDatabase(it->path())) {
|
2018-09-21 20:24:29 -03:00
|
|
|
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.
|
2018-10-20 10:29:48 -03:00
|
|
|
paths.emplace_back(path);
|
2018-09-21 20:24:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|