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 
GetWalletDir()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 
ListWalletDir()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 
IsFeatureSupported(int wallet_version,int feature_version)83 bool IsFeatureSupported(int wallet_version, int feature_version)
84 {
85     return wallet_version >= feature_version;
86 }
87 
GetClosestWalletFeature(int version)88 WalletFeature GetClosestWalletFeature(int version)
89 {
90     const std::array<WalletFeature, 8> wallet_features{{FEATURE_LATEST, FEATURE_PRE_SPLIT_KEYPOOL, FEATURE_NO_DEFAULT_KEY, FEATURE_HD_SPLIT, FEATURE_HD, FEATURE_COMPRPUBKEY, FEATURE_WALLETCRYPT, FEATURE_BASE}};
91     for (const WalletFeature& wf : wallet_features) {
92         if (version >= wf) return wf;
93     }
94     return static_cast<WalletFeature>(0);
95 }
96