Bitcoin Core  22.99.0
P2P Digital Currency
db.cpp
Go to the documentation of this file.
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 {
15  const size_t offset = wallet_dir.native().size() + (wallet_dir == wallet_dir.root_name() ? 0 : 1);
16  std::vector<fs::path> paths;
17  boost::system::error_code ec;
18 
19  for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
20  if (ec) {
21  if (fs::is_directory(*it)) {
22  it.no_push();
23  LogPrintf("%s: %s %s -- skipping.\n", __func__, ec.message(), fs::PathToString(it->path()));
24  } else {
25  LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(it->path()));
26  }
27  continue;
28  }
29 
30  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  const auto path_str = it->path().native().substr(offset);
34  const fs::path path{path_str.begin(), path_str.end()};
35 
36  if (it->status().type() == fs::directory_file &&
37  (IsBDBFile(BDBDataFile(it->path())) || IsSQLiteFile(SQLiteDataFile(it->path())))) {
38  // Found a directory which contains wallet.dat btree file, add it as a wallet.
39  paths.emplace_back(path);
40  } else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && IsBDBFile(it->path())) {
41  if (it->path().filename() == "wallet.dat") {
42  // Found top-level wallet.dat btree file, add top level directory ""
43  // as a wallet.
44  paths.emplace_back();
45  } else {
46  // Found top-level btree file not called wallet.dat. Current bitcoin
47  // software will never create these files but will allow them to be
48  // opened in a shared database environment for backwards compatibility.
49  // Add it to the list of available wallets.
50  paths.emplace_back(path);
51  }
52  }
53  } catch (const std::exception& e) {
54  LogPrintf("%s: Error scanning %s: %s\n", __func__, fs::PathToString(it->path()), e.what());
55  it.no_push();
56  }
57  }
58 
59  return paths;
60 }
61 
62 fs::path BDBDataFile(const fs::path& wallet_path)
63 {
64  if (fs::is_regular_file(wallet_path)) {
65  // Special case for backwards compatibility: if wallet path points to an
66  // existing file, treat it as the path to a BDB data file in a parent
67  // directory that also contains BDB log files.
68  return wallet_path;
69  } else {
70  // Normal case: Interpret wallet path as a directory path containing
71  // data and log files.
72  return wallet_path / "wallet.dat";
73  }
74 }
75 
77 {
78  return path / "wallet.dat";
79 }
80 
81 bool IsBDBFile(const fs::path& path)
82 {
83  if (!fs::exists(path)) return false;
84 
85  // A Berkeley DB Btree file has at least 4K.
86  // This check also prevents opening lock files.
87  boost::system::error_code ec;
88  auto size = fs::file_size(path, ec);
89  if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path));
90  if (size < 4096) return false;
91 
92  fsbridge::ifstream file(path, std::ios::binary);
93  if (!file.is_open()) return false;
94 
95  file.seekg(12, std::ios::beg); // Magic bytes start at offset 12
96  uint32_t data = 0;
97  file.read((char*) &data, sizeof(data)); // Read 4 bytes of file to compare against magic
98 
99  // Berkeley DB Btree magic bytes, from:
100  // https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75
101  // - big endian systems - 00 05 31 62
102  // - little endian systems - 62 31 05 00
103  return data == 0x00053162 || data == 0x62310500;
104 }
105 
106 bool IsSQLiteFile(const fs::path& path)
107 {
108  if (!fs::exists(path)) return false;
109 
110  // A SQLite Database file is at least 512 bytes.
111  boost::system::error_code ec;
112  auto size = fs::file_size(path, ec);
113  if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path));
114  if (size < 512) return false;
115 
116  fsbridge::ifstream file(path, std::ios::binary);
117  if (!file.is_open()) return false;
118 
119  // Magic is at beginning and is 16 bytes long
120  char magic[16];
121  file.read(magic, 16);
122 
123  // Application id is at offset 68 and 4 bytes long
124  file.seekg(68, std::ios::beg);
125  char app_id[4];
126  file.read(app_id, 4);
127 
128  file.close();
129 
130  // Check the magic, see https://sqlite.org/fileformat2.html
131  std::string magic_str(magic, 16);
132  if (magic_str != std::string("SQLite format 3", 16)) {
133  return false;
134  }
135 
136  // Check the application id matches our network magic
137  return memcmp(Params().MessageStart(), app_id, 4) == 0;
138 }
fs::exists
static bool exists(const path &p)
Definition: fs.h:77
fsbridge::ifstream
fs::ifstream ifstream
Definition: fs.h:224
fs.h
IsSQLiteFile
bool IsSQLiteFile(const fs::path &path)
Definition: db.cpp:106
fs::PathToString
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:120
chainparams.h
db.h
fs::path
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:33
IsBDBFile
bool IsBDBFile(const fs::path &path)
Definition: db.cpp:81
LogPrintf
#define LogPrintf(...)
Definition: logging.h:187
BDBDataFile
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:62
SQLiteDataFile
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:76
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
ListDatabases
std::vector< fs::path > ListDatabases(const fs::path &wallet_dir)
Recursively list database paths in directory.
Definition: db.cpp:13