diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h --- a/src/wallet/bdb.h +++ b/src/wallet/bdb.h @@ -65,7 +65,7 @@ bool Verify(const std::string &strFile); - bool Open(bool retry); + bool Open(bilingual_str &error); void Close(); void Flush(bool fShutdown); void CheckpointLSN(const std::string &strFile); diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -160,7 +160,7 @@ Close(); } -bool BerkeleyEnvironment::Open(bool retry) { +bool BerkeleyEnvironment::Open(bilingual_str &err) { if (fDbEnvInit) { return true; } @@ -171,6 +171,8 @@ LogPrintf("Cannot obtain a lock on wallet directory %s. Another " "instance of bitcoin may be using it.\n", strPath); + err = strprintf(_("Error initializing wallet database environment %s!"), + Directory()); return false; } @@ -213,27 +215,16 @@ ret2, DbEnv::strerror(ret2)); } Reset(); - if (retry) { - // try moving the database env out of the way - fs::path pathDatabaseBak = - pathIn / strprintf("database.%d.bak", GetTime()); - try { - fs::rename(pathLogDir, pathDatabaseBak); - LogPrintf("Moved old %s to %s. Retrying.\n", - pathLogDir.string(), pathDatabaseBak.string()); - } catch (const fs::filesystem_error &) { - // failure is ok (well, not really, but it's not worse than what - // we started with) - } - // try opening it again one more time - if (!Open(false /* retry */)) { - // if it still fails, it probably means we can't even create the - // database env - return false; - } - } else { - return false; + err = strprintf(_("Error initializing wallet database environment %s!"), + Directory()); + if (ret == DB_RUNRECOVERY) { + err += Untranslated(" ") + + _("This error could occur if this wallet was not shutdown " + "cleanly and was last loaded using a build with a newer " + "version of Berkeley DB. If so, please use the software " + "that last loaded this wallet"); } + return false; } fDbEnvInit = true; @@ -318,9 +309,7 @@ DbEnv::version(nullptr, nullptr, nullptr)); LogPrintf("Using wallet %s\n", file_path.string()); - if (!env->Open(true /* retry */)) { - errorStr = strprintf( - _("Error initializing wallet database environment %s!"), walletDir); + if (!env->Open(errorStr)) { return false; } @@ -365,11 +354,11 @@ { LOCK(cs_db); - if (!env->Open(false /* retry */)) { + bilingual_str open_err; + if (!env->Open(open_err)) { throw std::runtime_error( "BerkeleyBatch: Failed to open database environment."); } - pdb = database.m_db.get(); if (pdb == nullptr) { int ret; @@ -521,7 +510,8 @@ // This will flush and close the environment Flush(true); Reset(); - Open(true); + bilingual_str open_err; + Open(open_err); } bool BerkeleyDatabase::Rewrite(const char *pszSkip) { diff --git a/src/wallet/salvage.cpp b/src/wallet/salvage.cpp --- a/src/wallet/salvage.cpp +++ b/src/wallet/salvage.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -21,10 +22,9 @@ std::shared_ptr env = GetWalletEnv(file_path, filename); - if (!env->Open(true /* retry */)) { - tfm::format(std::cerr, - "Error initializing wallet database environment %s!", - env->Directory()); + bilingual_str open_err; + if (!env->Open(open_err)) { + tfm::format(std::cerr, "%s\n", open_err.original); return false; }