Skip to content

Commit 0530ba0

Browse files
committed
Add -walletdir parameter to specify custom wallet dir
1 parent 99bc0b4 commit 0530ba0

File tree

12 files changed

+76
-24
lines changed

12 files changed

+76
-24
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ BITCOIN_CORE_H = \
168168
wallet/rpcwallet.h \
169169
wallet/wallet.h \
170170
wallet/walletdb.h \
171+
wallet/walletutil.h \
171172
warnings.h \
172173
zmq/zmqabstractnotifier.h \
173174
zmq/zmqconfig.h\
@@ -249,6 +250,7 @@ libbitcoin_wallet_a_SOURCES = \
249250
wallet/rpcwallet.cpp \
250251
wallet/wallet.cpp \
251252
wallet/walletdb.cpp \
253+
wallet/walletutil.cpp \
252254
$(BITCOIN_CORE_H)
253255

254256
# crypto primitives library

src/bitcoind.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ bool AppInit(int argc, char* argv[])
101101
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
102102
return false;
103103
}
104+
if (gArgs.IsArgSet("-walletdir") && !fs::is_directory(GetWalletDir())) {
105+
fprintf(stderr, "Error: Specified wallet directory \"%s\" does not exist.\n", gArgs.GetArg("-walletdir", "").c_str());
106+
return false;
107+
}
104108
try
105109
{
106110
gArgs.ReadConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12201220
LogPrintf("Startup time: %s\n", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()));
12211221
LogPrintf("Default data directory %s\n", GetDefaultDataDir().string());
12221222
LogPrintf("Using data directory %s\n", GetDataDir().string());
1223+
LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
12231224
LogPrintf("Using config file %s\n", GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)).string());
12241225
LogPrintf("Using at most %i automatic connections (%i file descriptors available)\n", nMaxConnections, nFD);
12251226

src/qt/bitcoin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@ int main(int argc, char *argv[])
626626
QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(gArgs.GetArg("-datadir", ""))));
627627
return EXIT_FAILURE;
628628
}
629+
if (gArgs.IsArgSet("-walletdir") && !fs::is_directory(GetWalletDir())) {
630+
QMessageBox::critical(0, QObject::tr(PACKAGE_NAME),
631+
QObject::tr("Error: Specified wallet directory \"%1\" does not exist.").arg(QString::fromStdString(gArgs.GetArg("-walletdir", ""))));
632+
return EXIT_FAILURE;
633+
}
629634
try {
630635
gArgs.ReadConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
631636
} catch (const std::exception& e) {

src/wallet/db.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <protocol.h>
1212
#include <util.h>
1313
#include <utilstrencodings.h>
14+
#include <wallet/walletutil.h>
1415

1516
#include <stdint.h>
1617

@@ -257,23 +258,23 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco
257258
return fSuccess;
258259
}
259260

260-
bool CDB::VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr)
261+
bool CDB::VerifyEnvironment(const std::string& walletFile, const fs::path& walletDir, std::string& errorStr)
261262
{
262263
LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0));
263264
LogPrintf("Using wallet %s\n", walletFile);
264265

265266
// Wallet file must be a plain filename without a directory
266267
if (walletFile != fs::basename(walletFile) + fs::extension(walletFile))
267268
{
268-
errorStr = strprintf(_("Wallet %s resides outside data directory %s"), walletFile, dataDir.string());
269+
errorStr = strprintf(_("Wallet %s resides outside wallet directory %s"), walletFile, walletDir.string());
269270
return false;
270271
}
271272

272-
if (!bitdb.Open(dataDir))
273+
if (!bitdb.Open(walletDir))
273274
{
274275
// try moving the database env out of the way
275-
fs::path pathDatabase = dataDir / "database";
276-
fs::path pathDatabaseBak = dataDir / strprintf("database.%d.bak", GetTime());
276+
fs::path pathDatabase = walletDir / "database";
277+
fs::path pathDatabaseBak = walletDir / strprintf("database.%d.bak", GetTime());
277278
try {
278279
fs::rename(pathDatabase, pathDatabaseBak);
279280
LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string());
@@ -282,18 +283,18 @@ bool CDB::VerifyEnvironment(const std::string& walletFile, const fs::path& dataD
282283
}
283284

284285
// try again
285-
if (!bitdb.Open(dataDir)) {
286+
if (!bitdb.Open(walletDir)) {
286287
// if it still fails, it probably means we can't even create the database env
287-
errorStr = strprintf(_("Error initializing wallet database environment %s!"), GetDataDir());
288+
errorStr = strprintf(_("Error initializing wallet database environment %s!"), walletDir);
288289
return false;
289290
}
290291
}
291292
return true;
292293
}
293294

294-
bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc)
295+
bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& walletDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc)
295296
{
296-
if (fs::exists(dataDir / walletFile))
297+
if (fs::exists(walletDir / walletFile))
297298
{
298299
std::string backup_filename;
299300
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, recoverFunc, backup_filename);
@@ -303,7 +304,7 @@ bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& data
303304
" Original %s saved as %s in %s; if"
304305
" your balance or transactions are incorrect you should"
305306
" restore from a backup."),
306-
walletFile, backup_filename, dataDir);
307+
walletFile, backup_filename, walletDir);
307308
}
308309
if (r == CDBEnv::RECOVER_FAIL)
309310
{
@@ -407,7 +408,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
407408

408409
{
409410
LOCK(env->cs_db);
410-
if (!env->Open(GetDataDir()))
411+
if (!env->Open(GetWalletDir()))
411412
throw std::runtime_error("CDB: Failed to open database environment.");
412413

413414
pdb = env->mapDb[strFilename];
@@ -695,7 +696,7 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
695696
env->mapFileUseCount.erase(strFile);
696697

697698
// Copy wallet file
698-
fs::path pathSrc = GetDataDir() / strFile;
699+
fs::path pathSrc = GetWalletDir() / strFile;
699700
fs::path pathDest(strDest);
700701
if (fs::is_directory(pathDest))
701702
pathDest /= strFile;

src/wallet/db.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ class CDB
167167
ideal to be called periodically */
168168
static bool PeriodicFlush(CWalletDBWrapper& dbw);
169169
/* verifies the database environment */
170-
static bool VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr);
170+
static bool VerifyEnvironment(const std::string& walletFile, const fs::path& walletDir, std::string& errorStr);
171171
/* verifies the database file */
172-
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc);
172+
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& walletDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc);
173173

174174
public:
175175
template <typename K, typename T>

src/wallet/init.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#include <util.h>
1010
#include <utilmoneystr.h>
1111
#include <validation.h>
12-
#include <wallet/wallet.h>
1312
#include <wallet/rpcwallet.h>
13+
#include <wallet/wallet.h>
14+
#include <wallet/walletutil.h>
1415

1516
std::string GetWalletHelpString(bool showDebug)
1617
{
@@ -34,6 +35,7 @@ std::string GetWalletHelpString(bool showDebug)
3435
strUsage += HelpMessageOpt("-upgradewallet", _("Upgrade wallet to latest format on startup"));
3536
strUsage += HelpMessageOpt("-wallet=<file>", _("Specify wallet file (within data directory)") + " " + strprintf(_("(default: %s)"), DEFAULT_WALLET_DAT));
3637
strUsage += HelpMessageOpt("-walletbroadcast", _("Make the wallet broadcast transactions") + " " + strprintf(_("(default: %u)"), DEFAULT_WALLETBROADCAST));
38+
strUsage += HelpMessageOpt("-walletdir=<dir>", _("Specify directory to hold wallets (default: <datadir>/wallets if it exists, otherwise <datadir>)"));
3739
strUsage += HelpMessageOpt("-walletnotify=<cmd>", _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)"));
3840
strUsage += HelpMessageOpt("-zapwallettxes=<mode>", _("Delete all wallet transactions and only recover those parts of the blockchain through -rescan on startup") +
3941
" " + _("(1 = keep tx meta data e.g. account owner and payment request information, 2 = drop tx meta data)"));
@@ -205,7 +207,7 @@ bool VerifyWallets()
205207
return InitError(strprintf(_("Error loading wallet %s. Invalid characters in -wallet filename."), walletFile));
206208
}
207209

208-
fs::path wallet_path = fs::absolute(walletFile, GetDataDir());
210+
fs::path wallet_path = fs::absolute(walletFile, GetWalletDir());
209211

210212
if (fs::exists(wallet_path) && (!fs::is_regular_file(wallet_path) || fs::is_symlink(wallet_path))) {
211213
return InitError(strprintf(_("Error loading wallet %s. -wallet filename must be a regular file."), walletFile));
@@ -216,7 +218,7 @@ bool VerifyWallets()
216218
}
217219

218220
std::string strError;
219-
if (!CWalletDB::VerifyEnvironment(walletFile, GetDataDir().string(), strError)) {
221+
if (!CWalletDB::VerifyEnvironment(walletFile, GetWalletDir().string(), strError)) {
220222
return InitError(strError);
221223
}
222224

@@ -230,7 +232,7 @@ bool VerifyWallets()
230232
}
231233

232234
std::string strWarning;
233-
bool dbV = CWalletDB::VerifyDatabaseFile(walletFile, GetDataDir().string(), strWarning, strError);
235+
bool dbV = CWalletDB::VerifyDatabaseFile(walletFile, GetWalletDir().string(), strWarning, strError);
234236
if (!strWarning.empty()) {
235237
InitWarning(strWarning);
236238
}

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <wallet/feebumper.h>
2727
#include <wallet/wallet.h>
2828
#include <wallet/walletdb.h>
29+
#include <wallet/walletutil.h>
2930

3031
#include <init.h> // For StartShutdown
3132

src/wallet/walletdb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,14 @@ bool CWalletDB::RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDa
814814
return true;
815815
}
816816

817-
bool CWalletDB::VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr)
817+
bool CWalletDB::VerifyEnvironment(const std::string& walletFile, const fs::path& walletDir, std::string& errorStr)
818818
{
819-
return CDB::VerifyEnvironment(walletFile, dataDir, errorStr);
819+
return CDB::VerifyEnvironment(walletFile, walletDir, errorStr);
820820
}
821821

822-
bool CWalletDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr)
822+
bool CWalletDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& walletDir, std::string& warningStr, std::string& errorStr)
823823
{
824-
return CDB::VerifyDatabaseFile(walletFile, dataDir, warningStr, errorStr, CWalletDB::Recover);
824+
return CDB::VerifyDatabaseFile(walletFile, walletDir, warningStr, errorStr, CWalletDB::Recover);
825825
}
826826

827827
bool CWalletDB::WriteDestData(const std::string &address, const std::string &key, const std::string &value)

src/wallet/walletdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ class CWalletDB
226226
/* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
227227
static bool IsKeyType(const std::string& strType);
228228
/* verifies the database environment */
229-
static bool VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr);
229+
static bool VerifyEnvironment(const std::string& walletFile, const fs::path& walletDir, std::string& errorStr);
230230
/* verifies the database file */
231-
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr);
231+
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& walletDir, std::string& warningStr, std::string& errorStr);
232232

233233
//! write the hdchain model (external chain child index counter)
234234
bool WriteHDChain(const CHDChain& chain);

0 commit comments

Comments
 (0)