Bitcoin Core  0.21.1
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules
walletutil.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2019 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <wallet/walletutil.h>
6 
7 #include <logging.h>
8 #include <util/system.h>
9 
10 bool ExistsBerkeleyDatabase(const fs::path& path);
11 #ifdef USE_SQLITE
12 bool ExistsSQLiteDatabase(const fs::path& path);
13 #else
14 # define ExistsSQLiteDatabase(path) (false)
15 #endif
16 
17 fs::path GetWalletDir()
18 {
19  fs::path path;
20 
21  if (gArgs.IsArgSet("-walletdir")) {
22  path = gArgs.GetArg("-walletdir", "");
23  if (!fs::is_directory(path)) {
24  // If the path specified doesn't exist, we return the deliberately
25  // invalid empty string.
26  path = "";
27  }
28  } else {
29  path = GetDataDir();
30  // If a wallets directory exists, use that, otherwise default to GetDataDir
31  if (fs::is_directory(path / "wallets")) {
32  path /= "wallets";
33  }
34  }
35 
36  return path;
37 }
38 
39 std::vector<fs::path> ListWalletDir()
40 {
41  const fs::path wallet_dir = GetWalletDir();
42  const size_t offset = wallet_dir.string().size() + 1;
43  std::vector<fs::path> paths;
44  boost::system::error_code ec;
45 
46  for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
47  if (ec) {
48  LogPrintf("%s: %s %s\n", __func__, ec.message(), it->path().string());
49  continue;
50  }
51 
52  try {
53  // Get wallet path relative to walletdir by removing walletdir from the wallet path.
54  // This can be replaced by boost::filesystem::lexically_relative once boost is bumped to 1.60.
55  const fs::path path = it->path().string().substr(offset);
56 
57  if (it->status().type() == fs::directory_file &&
58  (ExistsBerkeleyDatabase(it->path()) || ExistsSQLiteDatabase(it->path()))) {
59  // Found a directory which contains wallet.dat btree file, add it as a wallet.
60  paths.emplace_back(path);
61  } else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && ExistsBerkeleyDatabase(it->path())) {
62  if (it->path().filename() == "wallet.dat") {
63  // Found top-level wallet.dat btree file, add top level directory ""
64  // as a wallet.
65  paths.emplace_back();
66  } else {
67  // Found top-level btree file not called wallet.dat. Current bitcoin
68  // software will never create these files but will allow them to be
69  // opened in a shared database environment for backwards compatibility.
70  // Add it to the list of available wallets.
71  paths.emplace_back(path);
72  }
73  }
74  } catch (const std::exception& e) {
75  LogPrintf("%s: Error scanning %s: %s\n", __func__, it->path().string(), e.what());
76  it.no_push();
77  }
78  }
79 
80  return paths;
81 }
82 
83 bool IsFeatureSupported(int wallet_version, int feature_version)
84 {
85  return wallet_version >= feature_version;
86 }
87 
89 {
91  for (const WalletFeature& wf : wallet_features) {
92  if (version >= wf) return wf;
93  }
94  return static_cast<WalletFeature>(0);
95 }
bool IsFeatureSupported(int wallet_version, int feature_version)
Definition: walletutil.cpp:83
static void LogPrintf(const char *fmt, const Args &...args)
Definition: logging.h:166
bool ExistsBerkeleyDatabase(const fs::path &path)
Check if Berkeley database exists at specified path.
Definition: bdb.cpp:811
WalletFeature GetClosestWalletFeature(int version)
Definition: walletutil.cpp:88
WalletFeature
(client) version numbers for particular wallet features
Definition: walletutil.h:14
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:371
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition: walletutil.cpp:17
const fs::path & GetDataDir(bool fNetSpecific)
Definition: system.cpp:720
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:467
ArgsManager gArgs
Definition: system.cpp:77
std::vector< fs::path > ListWalletDir()
Get wallets in wallet directory.
Definition: walletutil.cpp:39
#define ExistsSQLiteDatabase(path)
Definition: walletutil.cpp:14
auto it
Definition: validation.cpp:381