Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/walletutil.cpp
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | static bool IsBerkeleyBtree(const fs::path &path) { | ||||
// https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75 | // https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75 | ||||
// - big endian systems - 00 05 31 62 | // - big endian systems - 00 05 31 62 | ||||
// - 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; | |||||
std::vector<fs::path> paths; | std::vector<fs::path> paths; | ||||
for (auto it = fs::recursive_directory_iterator(wallet_dir); it != end(it); | for (auto it = fs::recursive_directory_iterator(wallet_dir); | ||||
++it) { | it != fs::recursive_directory_iterator(); ++it) { | ||||
// 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 && | 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 | ||||
// a wallet. | // a wallet. | ||||
paths.emplace_back(fs::relative(it->path(), wallet_dir)); | paths.emplace_back(path); | ||||
} else if (it.level() == 0 && | } else if (it.level() == 0 && | ||||
it->symlink_status().type() == fs::regular_file && | it->symlink_status().type() == fs::regular_file && | ||||
IsBerkeleyBtree(it->path())) { | IsBerkeleyBtree(it->path())) { | ||||
if (it->path().filename() == "wallet.dat") { | if (it->path().filename() == "wallet.dat") { | ||||
// Found top-level wallet.dat btree file, add top level | // Found top-level wallet.dat btree file, add top level | ||||
// directory "" as a wallet. | // directory "" as a wallet. | ||||
paths.emplace_back(); | paths.emplace_back(); | ||||
} else { | } else { | ||||
// Found top-level btree file not called wallet.dat. Current | // Found top-level btree file not called wallet.dat. Current | ||||
// bitcoin software will never create these files but will allow | // bitcoin software will never create these files but will allow | ||||
// them to be opened in a shared database environment for | // them to be opened in a shared database environment for | ||||
// backwards compatibility. Add it to the list of available | // backwards compatibility. Add it to the list of available | ||||
// wallets. | // wallets. | ||||
paths.emplace_back(fs::relative(it->path(), wallet_dir)); | paths.emplace_back(path); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
return paths; | return paths; | ||||
} | } | ||||
WalletLocation::WalletLocation(const std::string &name) | WalletLocation::WalletLocation(const std::string &name) | ||||
: m_name(name), m_path(fs::absolute(name, GetWalletDir())) {} | : m_name(name), m_path(fs::absolute(name, GetWalletDir())) {} | ||||
bool WalletLocation::Exists() const { | bool WalletLocation::Exists() const { | ||||
return fs::symlink_status(m_path).type() != fs::file_not_found; | return fs::symlink_status(m_path).type() != fs::file_not_found; | ||||
} | } |