Skip to content

Commit e6efa6b

Browse files
luke-jrrandom-zebra
authored andcommitted
wallet: Include actual backup filename in recovery warning message
1 parent 647fbc9 commit e6efa6b

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

src/wallet/db.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void CDBEnv::MakeMock()
146146
fMockDb = true;
147147
}
148148

149-
CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, bool (*recoverFunc)(const std::string& strFile))
149+
CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type recoverFunc, std::string& out_backup_filename)
150150
{
151151
LOCK(cs_db);
152152
assert(mapFileUseCount.count(strFile) == 0);
@@ -159,11 +159,11 @@ CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, bool (*recoverFu
159159
return RECOVER_FAIL;
160160

161161
// Try to recover:
162-
bool fRecovered = (*recoverFunc)(strFile);
162+
bool fRecovered = (*recoverFunc)(strFile, out_backup_filename);
163163
return (fRecovered ? RECOVER_OK : RECOVER_FAIL);
164164
}
165165

166-
bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue))
166+
bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename)
167167
{
168168
// Recovery procedure:
169169
// move wallet file to wallet.timestamp.bak
@@ -173,7 +173,7 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco
173173
// Set -rescan so any missing transactions will be
174174
// found.
175175
int64_t now = GetTime();
176-
std::string newFilename = strprintf("wallet.%d.bak", now);
176+
newFilename = strprintf("wallet.%d.bak", now);
177177

178178
int result = bitdb.dbenv->dbrename(NULL, filename.c_str(), NULL,
179179
newFilename.c_str(), DB_AUTO_COMMIT);
@@ -257,16 +257,17 @@ bool CDB::VerifyEnvironment(const std::string& walletFile, const fs::path& dataD
257257
return true;
258258
}
259259

260-
bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, bool (*recoverFunc)(const std::string& strFile))
260+
bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc)
261261
{
262262
if (fs::exists(dataDir / walletFile)) {
263-
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, recoverFunc);
263+
std::string backup_filename;
264+
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, recoverFunc, backup_filename);
264265
if (r == CDBEnv::RECOVER_OK) {
265-
warningStr = strprintf(("Warning: Wallet file corrupt, data salvaged!"
266+
warningStr = strprintf(_("Warning: Wallet file corrupt, data salvaged!"
266267
" Original %s saved as %s in %s; if"
267268
" your balance or transactions are incorrect you should"
268269
" restore from a backup."),
269-
walletFile, "wallet.{timestamp}.bak", dataDir);
270+
walletFile, backup_filename, dataDir);
270271
}
271272
if (r == CDBEnv::RECOVER_FAIL) {
272273
errorStr = strprintf(("%s corrupt, salvage failed"), walletFile);

src/wallet/db.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ class CDBEnv
5555
* Returns true if strFile is OK.
5656
*/
5757
enum VerifyResult { VERIFY_OK,
58-
RECOVER_OK,
59-
RECOVER_FAIL };
60-
VerifyResult Verify(const std::string& strFile, bool (*recoverFunc)(const std::string& strFile));
58+
RECOVER_OK,
59+
RECOVER_FAIL };
60+
typedef bool (*recoverFunc_type)(const std::string& strFile, std::string& out_backup_filename);
61+
VerifyResult Verify(const std::string& strFile, recoverFunc_type recoverFunc, std::string& out_backup_filename);
62+
6163
/**
6264
* Salvage data from a file that Verify says is bad.
6365
* fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
@@ -159,15 +161,15 @@ class CDB
159161

160162
void Flush();
161163
void Close();
162-
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue));
164+
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
163165

164166
/* flush the wallet passively (TRY_LOCK)
165167
ideal to be called periodically */
166168
static bool PeriodicFlush(CWalletDBWrapper& dbw);
167169
/* verifies the database environment */
168170
static bool VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr);
169171
/* verifies the database file */
170-
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, bool (*recoverFunc)(const std::string& strFile));
172+
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc);
171173

172174
private:
173175
CDB(const CDB&);

src/wallet/wallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,12 +2097,13 @@ bool CWallet::Verify()
20972097
if (gArgs.GetBoolArg("-salvagewallet", false)) {
20982098
// Recover readable keypairs:
20992099
CWallet dummyWallet;
2100+
std::string backup_filename;
21002101
// Even if we don't use this lock in this function, we want to preserve
21012102
// lock order in LoadToWallet if query of chain state is needed to know
21022103
// tx status. If lock can't be taken, tx confirmation status may be not
21032104
// reliable.
21042105
LOCK(cs_main);
2105-
if (!CWalletDB::Recover(walletFile, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter))
2106+
if (!CWalletDB::Recover(walletFile, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter, backup_filename))
21062107
return false;
21072108
}
21082109

src/wallet/walletdb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,16 +1063,16 @@ bool AttemptBackupWallet(const CWallet& wallet, const fs::path& pathSrc, const f
10631063
//
10641064
// Try to (very carefully!) recover wallet file if there is a problem.
10651065
//
1066-
bool CWalletDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue))
1066+
bool CWalletDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename)
10671067
{
1068-
return CDB::Recover(filename, callbackDataIn, recoverKVcallback);
1068+
return CDB::Recover(filename, callbackDataIn, recoverKVcallback, out_backup_filename);
10691069
}
10701070

1071-
bool CWalletDB::Recover(const std::string& filename)
1071+
bool CWalletDB::Recover(const std::string& filename, std::string& out_backup_filename)
10721072
{
10731073
// recover without a key filter callback
10741074
// results in recovering all record types
1075-
return CWalletDB::Recover(filename, NULL, NULL);
1075+
return CWalletDB::Recover(filename, NULL, NULL, out_backup_filename);
10761076
}
10771077

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

src/wallet/walletdb.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ class CWalletDB
207207
DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
208208
DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
209209
/* Try to (very carefully!) recover wallet database (with a possible key type filter) */
210-
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue));
211-
/* Recover convenience-function to bypass the key filter callback, called when verify failes, recoveres everything */
212-
static bool Recover(const std::string& filename);
210+
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
211+
/* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
212+
static bool Recover(const std::string& filename, std::string& out_backup_filename);
213213
/* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
214214
static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
215215
/* Function to determin if a certain KV/key-type is a key (cryptographical key) type */

0 commit comments

Comments
 (0)