Skip to content

Commit

Permalink
Merge #14561: Remove fs::relative call and fix listwalletdir tests
Browse files Browse the repository at this point in the history
Summary:
ed2e18398b3ab657e98e3e1fe135cbf8dd94fda3 Remove fs::relative call and fix listwalletdir tests (João Barbosa)

Pull request description:

  The implementation of `fs::relative` resolves symlinks which is not intended
  in ListWalletDir. The replacement does what is required, and `listwalletdir` RPC
  tests are fixed accordingly.

  Also, `fs::recursive_directory_iterator` iteration is fixed to build with boost 1.47.

  Based on #14559

Tree-SHA512: 1da516226073f195285d10d9d9648c90cce0158c5d1eb9c31217bb4abb575cd37f07c00787c5a850554d6120bbc5a3cbc5cb47d4488b32ac6bcb52bc1882d600

Backport of Core [[bitcoin/bitcoin#14561 | PR14561]]

Depends on D5546

Test Plan:
```
ninja
test_runner.py wallet_multiwallet
```

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D5547
  • Loading branch information
laanwj authored and jonspock committed Sep 4, 2020
1 parent b9cbe54 commit 3be35eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
48 changes: 28 additions & 20 deletions src/wallet/walletutil.cpp
Expand Up @@ -90,29 +90,37 @@ static bool IsBerkeleyBtree(const fs::path &path) {

std::vector<fs::path> ListWalletDir() {
const fs::path wallet_dir = GetWalletDir();
const size_t offset = wallet_dir.string().size() + 1;
std::vector<fs::path> paths;
#ifndef NO_BOOST_FILESYSTEM
for (auto it = fs::recursive_directory_iterator(wallet_dir); it != end(it); ++it) {
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(fs::relative(it->path(), wallet_dir));
} 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(fs::relative(it->path(), wallet_dir));
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
// 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.
paths.emplace_back(path);
}
}
}
}
#endif
return paths;
Expand Down
13 changes: 6 additions & 7 deletions test/functional/wallet_multiwallet.py
Expand Up @@ -72,13 +72,12 @@ def wallet_file(name):
os.path.join(self.options.tmpdir, 'extern/w6'), 'w7_symlink', 'w8', '']
extra_args = ['-wallet={}'.format(n) for n in wallet_names]
self.start_node(0, extra_args)
assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()[
'wallets'])), set(['', 'w3', 'w2', 'sub/w5', 'w7', 'w7', 'w1', 'w8', 'w']))
assert_equal(sorted(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), [
'', os.path.join('sub', 'w5'), 'w', 'w1', 'w2', 'w3', 'w7', 'w7_symlink', 'w8'])

assert_equal(set(node.listwallets()), set(wallet_names))

# check that all requested wallets were created
>>>>>>> 50a1fbc83... Merge #14291: wallet: Add ListWalletDir utility function
self.stop_node(0)
for wallet_name in wallet_names:
if os.path.isdir(wallet_dir(wallet_name)):
Expand Down Expand Up @@ -197,8 +196,8 @@ def wallet_file(name):

self.start_node(0, self.extra_args[0])

assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(
['', 'w3', 'w2', 'sub/w5', 'w7', 'w7', 'w8_copy', 'w1', 'w8', 'w']))
assert_equal(sorted(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), [
'', os.path.join('sub', 'w5'), 'w', 'w1', 'w2', 'w3', 'w7', 'w7_symlink', 'w8', 'w8_copy'])

wallets = [wallet(w) for w in wallet_names]
wallet_bad = wallet("bad")
Expand Down Expand Up @@ -368,8 +367,8 @@ def wallet_file(name):
assert_equal(self.nodes[0].listwallets(), ['w1'])
assert_equal(w1.getwalletinfo()['walletname'], 'w1')

assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(
['', 'w3', 'w2', 'sub/w5', 'w7', 'w9', 'w7', 'w8_copy', 'w1', 'w8', 'w']))
assert_equal(sorted(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), [
'', os.path.join('sub', 'w5'), 'w', 'w1', 'w2', 'w3', 'w7', 'w7_symlink', 'w8', 'w8_copy', 'w9'])

# Test backing up and restoring wallets
self.log.info("Test wallet backup")
Expand Down

0 comments on commit 3be35eb

Please sign in to comment.