Remove fs::relative call and fix listwalletdir tests

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

Also, building with boost 1.47 required 2 changes:
 - replace fs::relative with an alternative implementation;
 - fix fs::recursive_directory_iterator iteration.
pull/643/head
João Barbosa 6 years ago
parent 613fc95ee4
commit ed2e18398b

@ -52,12 +52,17 @@ static bool IsBerkeleyBtree(const fs::path& path)
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); ++it) { 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")) { 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. // Found a directory which contains wallet.dat btree file, add it as a wallet.
paths.emplace_back(fs::relative(it->path(), wallet_dir)); paths.emplace_back(path);
} else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && IsBerkeleyBtree(it->path())) { } else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && 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 directory "" // Found top-level wallet.dat btree file, add top level directory ""
@ -68,7 +73,7 @@ std::vector<fs::path> ListWalletDir()
// software will never create these files but will allow them to be // software will never create these files but will allow them to be
// opened in a shared database environment for backwards compatibility. // opened in a shared database environment for backwards compatibility.
// Add it to the list of available wallets. // Add it to the list of available wallets.
paths.emplace_back(fs::relative(it->path(), wallet_dir)); paths.emplace_back(path);
} }
} }
} }

@ -71,7 +71,7 @@ class MultiWalletTest(BitcoinTestFramework):
wallet_names = ['w1', 'w2', 'w3', 'w', 'sub/w5', os.path.join(self.options.tmpdir, 'extern/w6'), 'w7_symlink', 'w8', ''] wallet_names = ['w1', 'w2', 'w3', 'w', 'sub/w5', os.path.join(self.options.tmpdir, 'extern/w6'), 'w7_symlink', 'w8', '']
extra_args = ['-wallet={}'.format(n) for n in wallet_names] extra_args = ['-wallet={}'.format(n) for n in wallet_names]
self.start_node(0, extra_args) self.start_node(0, extra_args)
assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(['', 'w3', 'w2', os.path.join('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)) assert_equal(set(node.listwallets()), set(wallet_names))
@ -144,7 +144,7 @@ class MultiWalletTest(BitcoinTestFramework):
self.restart_node(0, extra_args) self.restart_node(0, extra_args)
assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(['', 'w3', 'w2', os.path.join('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] wallets = [wallet(w) for w in wallet_names]
wallet_bad = wallet("bad") wallet_bad = wallet("bad")
@ -291,7 +291,7 @@ class MultiWalletTest(BitcoinTestFramework):
assert_equal(self.nodes[0].listwallets(), ['w1']) assert_equal(self.nodes[0].listwallets(), ['w1'])
assert_equal(w1.getwalletinfo()['walletname'], 'w1') assert_equal(w1.getwalletinfo()['walletname'], 'w1')
assert_equal(set(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), set(['', 'w3', 'w2', os.path.join('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 # Test backing up and restoring wallets
self.log.info("Test wallet backup") self.log.info("Test wallet backup")

Loading…
Cancel
Save