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