Skip to content

Commit b823a4c

Browse files
committed
wallet: Include actual backup filename in recovery warning message
1 parent 84dcb45 commit b823a4c

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

src/wallet/db.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void CDBEnv::MakeMock()
143143
fMockDb = true;
144144
}
145145

146-
CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, bool (*recoverFunc)(const std::string& strFile))
146+
CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type recoverFunc, std::string& out_backup_filename)
147147
{
148148
LOCK(cs_db);
149149
assert(mapFileUseCount.count(strFile) == 0);
@@ -156,11 +156,11 @@ CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, bool (*recoverFu
156156
return RECOVER_FAIL;
157157

158158
// Try to recover:
159-
bool fRecovered = (*recoverFunc)(strFile);
159+
bool fRecovered = (*recoverFunc)(strFile, out_backup_filename);
160160
return (fRecovered ? RECOVER_OK : RECOVER_FAIL);
161161
}
162162

163-
bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue))
163+
bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename)
164164
{
165165
// Recovery procedure:
166166
// move wallet file to wallet.timestamp.bak
@@ -170,7 +170,7 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco
170170
// Set -rescan so any missing transactions will be
171171
// found.
172172
int64_t now = GetTime();
173-
std::string newFilename = strprintf("wallet.%d.bak", now);
173+
newFilename = strprintf("wallet.%d.bak", now);
174174

175175
int result = bitdb.dbenv->dbrename(NULL, filename.c_str(), NULL,
176176
newFilename.c_str(), DB_AUTO_COMMIT);
@@ -261,18 +261,19 @@ bool CDB::VerifyEnvironment(const std::string& walletFile, const fs::path& dataD
261261
return true;
262262
}
263263

264-
bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, bool (*recoverFunc)(const std::string& strFile))
264+
bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc)
265265
{
266266
if (fs::exists(dataDir / walletFile))
267267
{
268-
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, recoverFunc);
268+
std::string backup_filename;
269+
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, recoverFunc, backup_filename);
269270
if (r == CDBEnv::RECOVER_OK)
270271
{
271272
warningStr = strprintf(_("Warning: Wallet file corrupt, data salvaged!"
272273
" Original %s saved as %s in %s; if"
273274
" your balance or transactions are incorrect you should"
274275
" restore from a backup."),
275-
walletFile, "wallet.{timestamp}.bak", dataDir);
276+
walletFile, backup_filename, dataDir);
276277
}
277278
if (r == CDBEnv::RECOVER_FAIL)
278279
{

src/wallet/db.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class CDBEnv
5555
enum VerifyResult { VERIFY_OK,
5656
RECOVER_OK,
5757
RECOVER_FAIL };
58-
VerifyResult Verify(const std::string& strFile, bool (*recoverFunc)(const std::string& strFile));
58+
typedef bool (*recoverFunc_type)(const std::string& strFile, std::string& out_backup_filename);
59+
VerifyResult Verify(const std::string& strFile, recoverFunc_type recoverFunc, std::string& out_backup_filename);
5960
/**
6061
* Salvage data from a file that Verify says is bad.
6162
* fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
@@ -156,15 +157,15 @@ class CDB
156157

157158
void Flush();
158159
void Close();
159-
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue));
160+
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
160161

161162
/* flush the wallet passively (TRY_LOCK)
162163
ideal to be called periodically */
163164
static bool PeriodicFlush(CWalletDBWrapper& dbw);
164165
/* verifies the database environment */
165166
static bool VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr);
166167
/* verifies the database file */
167-
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, bool (*recoverFunc)(const std::string& strFile));
168+
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc);
168169

169170
private:
170171
CDB(const CDB&);

src/wallet/wallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ bool CWallet::Verify()
459459
{
460460
// Recover readable keypairs:
461461
CWallet dummyWallet;
462-
if (!CWalletDB::Recover(walletFile, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter))
462+
std::string backup_filename;
463+
if (!CWalletDB::Recover(walletFile, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter, backup_filename))
463464
return false;
464465
}
465466

src/wallet/walletdb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,16 +783,16 @@ void MaybeCompactWalletDB()
783783
//
784784
// Try to (very carefully!) recover wallet file if there is a problem.
785785
//
786-
bool CWalletDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue))
786+
bool CWalletDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename)
787787
{
788-
return CDB::Recover(filename, callbackDataIn, recoverKVcallback);
788+
return CDB::Recover(filename, callbackDataIn, recoverKVcallback, out_backup_filename);
789789
}
790790

791-
bool CWalletDB::Recover(const std::string& filename)
791+
bool CWalletDB::Recover(const std::string& filename, std::string& out_backup_filename)
792792
{
793793
// recover without a key filter callback
794794
// results in recovering all record types
795-
return CWalletDB::Recover(filename, NULL, NULL);
795+
return CWalletDB::Recover(filename, NULL, NULL, out_backup_filename);
796796
}
797797

798798
bool CWalletDB::RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)

src/wallet/walletdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ class CWalletDB
218218
DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
219219
DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
220220
/* Try to (very carefully!) recover wallet database (with a possible key type filter) */
221-
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue));
221+
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
222222
/* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
223-
static bool Recover(const std::string& filename);
223+
static bool Recover(const std::string& filename, std::string& out_backup_filename);
224224
/* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
225225
static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
226226
/* Function to determine if a certain KV/key-type is a key (cryptographical key) type */

0 commit comments

Comments
 (0)