Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2020 The Bitcoin Core developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #include <chainparams.h>
# 7 : : #include <fs.h>
# 8 : : #include <logging.h>
# 9 : : #include <wallet/db.h>
# 10 : :
# 11 : : #include <string>
# 12 : :
# 13 : : std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
# 14 : 13 : {
# 15 : 13 : const size_t offset = wallet_dir.string().size() + 1;
# 16 : 13 : std::vector<fs::path> paths;
# 17 : 13 : boost::system::error_code ec;
# 18 : :
# 19 [ + + ]: 416 : for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
# 20 [ - + ]: 403 : if (ec) {
# 21 [ # # ]: 0 : if (fs::is_directory(*it)) {
# 22 : 0 : it.no_push();
# 23 : 0 : LogPrintf("%s: %s %s -- skipping.\n", __func__, ec.message(), it->path().string());
# 24 : 0 : } else {
# 25 : 0 : LogPrintf("%s: %s %s\n", __func__, ec.message(), it->path().string());
# 26 : 0 : }
# 27 : 0 : continue;
# 28 : 0 : }
# 29 : :
# 30 : 403 : try {
# 31 : : // Get wallet path relative to walletdir by removing walletdir from the wallet path.
# 32 : : // This can be replaced by boost::filesystem::lexically_relative once boost is bumped to 1.60.
# 33 : 403 : const fs::path path = it->path().string().substr(offset);
# 34 : :
# 35 [ + + ][ + + ]: 403 : if (it->status().type() == fs::directory_file &&
# 36 [ + + ][ + + ]: 403 : (IsBDBFile(BDBDataFile(it->path())) || IsSQLiteFile(SQLiteDataFile(it->path())))) {
# 37 : : // Found a directory which contains wallet.dat btree file, add it as a wallet.
# 38 : 70 : paths.emplace_back(path);
# 39 [ + + ][ + + ]: 333 : } else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && IsBDBFile(it->path())) {
# [ + + ][ + + ]
# 40 [ + + ]: 18 : if (it->path().filename() == "wallet.dat") {
# 41 : : // Found top-level wallet.dat btree file, add top level directory ""
# 42 : : // as a wallet.
# 43 : 8 : paths.emplace_back();
# 44 : 10 : } else {
# 45 : : // Found top-level btree file not called wallet.dat. Current bitcoin
# 46 : : // software will never create these files but will allow them to be
# 47 : : // opened in a shared database environment for backwards compatibility.
# 48 : : // Add it to the list of available wallets.
# 49 : 10 : paths.emplace_back(path);
# 50 : 10 : }
# 51 : 18 : }
# 52 : 403 : } catch (const std::exception& e) {
# 53 : 12 : LogPrintf("%s: Error scanning %s: %s\n", __func__, it->path().string(), e.what());
# 54 : 12 : it.no_push();
# 55 : 12 : }
# 56 : 403 : }
# 57 : :
# 58 : 13 : return paths;
# 59 : 13 : }
# 60 : :
# 61 : : fs::path BDBDataFile(const fs::path& wallet_path)
# 62 : 1931 : {
# 63 [ + + ]: 1931 : if (fs::is_regular_file(wallet_path)) {
# 64 : : // Special case for backwards compatibility: if wallet path points to an
# 65 : : // existing file, treat it as the path to a BDB data file in a parent
# 66 : : // directory that also contains BDB log files.
# 67 : 53 : return wallet_path;
# 68 : 1878 : } else {
# 69 : : // Normal case: Interpret wallet path as a directory path containing
# 70 : : // data and log files.
# 71 : 1878 : return wallet_path / "wallet.dat";
# 72 : 1878 : }
# 73 : 1931 : }
# 74 : :
# 75 : : fs::path SQLiteDataFile(const fs::path& path)
# 76 : 1572 : {
# 77 : 1572 : return path / "wallet.dat";
# 78 : 1572 : }
# 79 : :
# 80 : : bool IsBDBFile(const fs::path& path)
# 81 : 1349 : {
# 82 [ + + ]: 1349 : if (!fs::exists(path)) return false;
# 83 : :
# 84 : : // A Berkeley DB Btree file has at least 4K.
# 85 : : // This check also prevents opening lock files.
# 86 : 634 : boost::system::error_code ec;
# 87 : 634 : auto size = fs::file_size(path, ec);
# 88 [ - + ]: 634 : if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), path.string());
# 89 [ + + ]: 634 : if (size < 4096) return false;
# 90 : :
# 91 : 612 : fsbridge::ifstream file(path, std::ios::binary);
# 92 [ - + ]: 612 : if (!file.is_open()) return false;
# 93 : :
# 94 : 612 : file.seekg(12, std::ios::beg); // Magic bytes start at offset 12
# 95 : 612 : uint32_t data = 0;
# 96 : 612 : file.read((char*) &data, sizeof(data)); // Read 4 bytes of file to compare against magic
# 97 : :
# 98 : : // Berkeley DB Btree magic bytes, from:
# 99 : : // https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75
# 100 : : // - big endian systems - 00 05 31 62
# 101 : : // - little endian systems - 62 31 05 00
# 102 [ + + ][ - + ]: 612 : return data == 0x00053162 || data == 0x62310500;
# 103 : 612 : }
# 104 : :
# 105 : : bool IsSQLiteFile(const fs::path& path)
# 106 : 1250 : {
# 107 [ + + ]: 1250 : if (!fs::exists(path)) return false;
# 108 : :
# 109 : : // A SQLite Database file is at least 512 bytes.
# 110 : 509 : boost::system::error_code ec;
# 111 : 509 : auto size = fs::file_size(path, ec);
# 112 [ - + ]: 509 : if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), path.string());
# 113 [ - + ]: 509 : if (size < 512) return false;
# 114 : :
# 115 : 509 : fsbridge::ifstream file(path, std::ios::binary);
# 116 [ - + ]: 509 : if (!file.is_open()) return false;
# 117 : :
# 118 : : // Magic is at beginning and is 16 bytes long
# 119 : 509 : char magic[16];
# 120 : 509 : file.read(magic, 16);
# 121 : :
# 122 : : // Application id is at offset 68 and 4 bytes long
# 123 : 509 : file.seekg(68, std::ios::beg);
# 124 : 509 : char app_id[4];
# 125 : 509 : file.read(app_id, 4);
# 126 : :
# 127 : 509 : file.close();
# 128 : :
# 129 : : // Check the magic, see https://sqlite.org/fileformat2.html
# 130 : 509 : std::string magic_str(magic, 16);
# 131 [ + + ]: 509 : if (magic_str != std::string("SQLite format 3", 16)) {
# 132 : 317 : return false;
# 133 : 317 : }
# 134 : :
# 135 : : // Check the application id matches our network magic
# 136 : 192 : return memcmp(Params().MessageStart(), app_id, 4) == 0;
# 137 : 192 : }
|