/bitcoin/src/wallet/db.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present 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 <common/args.h> |
8 | | #include <logging.h> |
9 | | #include <util/fs.h> |
10 | | #include <wallet/db.h> |
11 | | |
12 | | #include <algorithm> |
13 | | #include <exception> |
14 | | #include <fstream> |
15 | | #include <string> |
16 | | #include <system_error> |
17 | | #include <vector> |
18 | | |
19 | | namespace wallet { |
20 | 0 | bool operator<(BytePrefix a, std::span<const std::byte> b) { return std::ranges::lexicographical_compare(a.prefix, b.subspan(0, std::min(a.prefix.size(), b.size()))); } |
21 | 0 | bool operator<(std::span<const std::byte> a, BytePrefix b) { return std::ranges::lexicographical_compare(a.subspan(0, std::min(a.size(), b.prefix.size())), b.prefix); } |
22 | | |
23 | | std::vector<std::pair<fs::path, std::string>> ListDatabases(const fs::path& wallet_dir) |
24 | 0 | { |
25 | 0 | std::vector<std::pair<fs::path, std::string>> paths; |
26 | 0 | std::error_code ec; |
27 | |
|
28 | 0 | for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) { Branch (28:70): [True: 0, False: 0]
|
29 | 0 | if (ec) { Branch (29:13): [True: 0, False: 0]
|
30 | 0 | if (fs::is_directory(*it)) { Branch (30:17): [True: 0, False: 0]
|
31 | 0 | it.disable_recursion_pending(); |
32 | 0 | LogPrintf("%s: %s %s -- skipping.\n", __func__, ec.message(), fs::PathToString(it->path())); |
33 | 0 | } else { |
34 | 0 | LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(it->path())); |
35 | 0 | } |
36 | 0 | continue; |
37 | 0 | } |
38 | | |
39 | 0 | try { |
40 | 0 | const fs::path path{it->path().lexically_relative(wallet_dir)}; |
41 | |
|
42 | 0 | if (it->status().type() == fs::file_type::directory) { Branch (42:17): [True: 0, False: 0]
|
43 | 0 | if (IsBDBFile(BDBDataFile(it->path()))) { Branch (43:21): [True: 0, False: 0]
|
44 | | // Found a directory which contains wallet.dat btree file, add it as a wallet with BERKELEY format. |
45 | 0 | paths.emplace_back(path, "bdb"); |
46 | 0 | } else if (IsSQLiteFile(SQLiteDataFile(it->path()))) { Branch (46:28): [True: 0, False: 0]
|
47 | | // Found a directory which contains wallet.dat sqlite file, add it as a wallet with SQLITE format. |
48 | 0 | paths.emplace_back(path, "sqlite"); |
49 | 0 | } |
50 | 0 | } else if (it.depth() == 0 && it->symlink_status().type() == fs::file_type::regular && it->path().extension() != ".bak") { Branch (50:24): [True: 0, False: 0]
Branch (50:24): [True: 0, False: 0]
Branch (50:43): [True: 0, False: 0]
Branch (50:100): [True: 0, False: 0]
|
51 | 0 | if (it->path().filename() == "wallet.dat") { Branch (51:21): [True: 0, False: 0]
|
52 | | // Found top-level wallet.dat file, add top level directory "" |
53 | | // as a wallet. |
54 | 0 | if (IsBDBFile(it->path())) { Branch (54:25): [True: 0, False: 0]
|
55 | 0 | paths.emplace_back(fs::path(), "bdb"); |
56 | 0 | } else if (IsSQLiteFile(it->path())) { Branch (56:32): [True: 0, False: 0]
|
57 | 0 | paths.emplace_back(fs::path(), "sqlite"); |
58 | 0 | } |
59 | 0 | } else if (IsBDBFile(it->path())) { Branch (59:28): [True: 0, False: 0]
|
60 | | // Found top-level btree file not called wallet.dat. Current bitcoin |
61 | | // software will never create these files but will allow them to be |
62 | | // opened in a shared database environment for backwards compatibility. |
63 | | // Add it to the list of available wallets. |
64 | 0 | paths.emplace_back(path, "bdb"); |
65 | 0 | } |
66 | 0 | } |
67 | 0 | } catch (const std::exception& e) { |
68 | 0 | LogPrintf("%s: Error scanning %s: %s\n", __func__, fs::PathToString(it->path()), e.what()); |
69 | 0 | it.disable_recursion_pending(); |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | 0 | return paths; |
74 | 0 | } |
75 | | |
76 | | fs::path BDBDataFile(const fs::path& wallet_path) |
77 | 11.0k | { |
78 | 11.0k | if (fs::is_regular_file(wallet_path)) { Branch (78:9): [True: 0, False: 11.0k]
|
79 | | // Special case for backwards compatibility: if wallet path points to an |
80 | | // existing file, treat it as the path to a BDB data file in a parent |
81 | | // directory that also contains BDB log files. |
82 | 0 | return wallet_path; |
83 | 11.0k | } else { |
84 | | // Normal case: Interpret wallet path as a directory path containing |
85 | | // data and log files. |
86 | 11.0k | return wallet_path / "wallet.dat"; |
87 | 11.0k | } |
88 | 11.0k | } |
89 | | |
90 | | fs::path SQLiteDataFile(const fs::path& path) |
91 | 22.1k | { |
92 | 22.1k | return path / "wallet.dat"; |
93 | 22.1k | } |
94 | | |
95 | | bool IsBDBFile(const fs::path& path) |
96 | 11.0k | { |
97 | 11.0k | if (!fs::exists(path)) return false; Branch (97:9): [True: 11.0k, False: 0]
|
98 | | |
99 | | // A Berkeley DB Btree file has at least 4K. |
100 | | // This check also prevents opening lock files. |
101 | 0 | std::error_code ec; |
102 | 0 | auto size = fs::file_size(path, ec); |
103 | 0 | if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path)); Branch (103:9): [True: 0, False: 0]
|
104 | 0 | if (size < 4096) return false; Branch (104:9): [True: 0, False: 0]
|
105 | | |
106 | 0 | std::ifstream file{path, std::ios::binary}; |
107 | 0 | if (!file.is_open()) return false; Branch (107:9): [True: 0, False: 0]
|
108 | | |
109 | 0 | file.seekg(12, std::ios::beg); // Magic bytes start at offset 12 |
110 | 0 | uint32_t data = 0; |
111 | 0 | file.read((char*) &data, sizeof(data)); // Read 4 bytes of file to compare against magic |
112 | | |
113 | | // Berkeley DB Btree magic bytes, from: |
114 | | // https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75 |
115 | | // - big endian systems - 00 05 31 62 |
116 | | // - little endian systems - 62 31 05 00 |
117 | 0 | return data == 0x00053162 || data == 0x62310500; Branch (117:12): [True: 0, False: 0]
Branch (117:34): [True: 0, False: 0]
|
118 | 0 | } |
119 | | |
120 | | bool IsSQLiteFile(const fs::path& path) |
121 | 11.0k | { |
122 | 11.0k | if (!fs::exists(path)) return false; Branch (122:9): [True: 11.0k, False: 0]
|
123 | | |
124 | | // A SQLite Database file is at least 512 bytes. |
125 | 0 | std::error_code ec; |
126 | 0 | auto size = fs::file_size(path, ec); |
127 | 0 | if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path)); Branch (127:9): [True: 0, False: 0]
|
128 | 0 | if (size < 512) return false; Branch (128:9): [True: 0, False: 0]
|
129 | | |
130 | 0 | std::ifstream file{path, std::ios::binary}; |
131 | 0 | if (!file.is_open()) return false; Branch (131:9): [True: 0, False: 0]
|
132 | | |
133 | | // Magic is at beginning and is 16 bytes long |
134 | 0 | char magic[16]; |
135 | 0 | file.read(magic, 16); |
136 | | |
137 | | // Application id is at offset 68 and 4 bytes long |
138 | 0 | file.seekg(68, std::ios::beg); |
139 | 0 | char app_id[4]; |
140 | 0 | file.read(app_id, 4); |
141 | |
|
142 | 0 | file.close(); |
143 | | |
144 | | // Check the magic, see https://sqlite.org/fileformat.html |
145 | 0 | std::string magic_str(magic, 16); |
146 | 0 | if (magic_str != std::string{"SQLite format 3\000", 16}) { Branch (146:9): [True: 0, False: 0]
|
147 | 0 | return false; |
148 | 0 | } |
149 | | |
150 | | // Check the application id matches our network magic |
151 | 0 | return memcmp(Params().MessageStart().data(), app_id, 4) == 0; |
152 | 0 | } |
153 | | |
154 | | void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options) |
155 | 22.1k | { |
156 | | // Override current options with args values, if any were specified |
157 | 22.1k | options.use_unsafe_sync = args.GetBoolArg("-unsafesqlitesync", options.use_unsafe_sync); |
158 | 22.1k | } |
159 | | |
160 | | } // namespace wallet |