Skip to content

Commit 1859aaf

Browse files
committed
Try moving database/ away in case of failure
1 parent e791108 commit 1859aaf

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/init.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,22 @@ bool AppInit2(boost::thread_group& threadGroup)
605605

606606
if (!bitdb.Open(GetDataDir()))
607607
{
608-
string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str());
609-
return InitError(msg);
608+
// try moving the database env out of the way
609+
boost::filesystem::path pathDatabase = GetDataDir() / "database";
610+
boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%"PRI64d".bak", GetTime());
611+
try {
612+
boost::filesystem::rename(pathDatabase, pathDatabaseBak);
613+
printf("Moved old %s to %s. Retrying.\n", pathDatabase.string().c_str(), pathDatabaseBak.string().c_str());
614+
} catch(boost::filesystem::filesystem_error &error) {
615+
// failure is ok (well, not really, but it's not worse than what we started with)
616+
}
617+
618+
// try again
619+
if (!bitdb.Open(GetDataDir())) {
620+
// if it still fails, it probably means we can't even create the database env
621+
string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str());
622+
return InitError(msg);
623+
}
610624
}
611625

612626
if (GetBoolArg("-salvagewallet"))

0 commit comments

Comments
 (0)