Browse Source

Merge pull request #2558 from sipa/nodbdir

Some database/-related recovery improvements
0.8
Pieter Wuille 12 years ago
parent
commit
674ae7a26f
  1. 10
      src/db.cpp
  2. 2
      src/db.h
  3. 23
      src/init.cpp

10
src/db.cpp

@ -38,7 +38,7 @@ void CDBEnv::EnvShutdown()
if (ret != 0) if (ret != 0)
printf("EnvShutdown exception: %s (%d)\n", DbEnv::strerror(ret), ret); printf("EnvShutdown exception: %s (%d)\n", DbEnv::strerror(ret), ret);
if (!fMockDb) if (!fMockDb)
DbEnv(0).remove(strPath.c_str(), 0); DbEnv(0).remove(path.string().c_str(), 0);
} }
CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS) CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS)
@ -57,14 +57,14 @@ void CDBEnv::Close()
EnvShutdown(); EnvShutdown();
} }
bool CDBEnv::Open(const boost::filesystem::path& path) bool CDBEnv::Open(const boost::filesystem::path& pathIn)
{ {
if (fDbEnvInit) if (fDbEnvInit)
return true; return true;
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
strPath = path.string(); path = pathIn;
filesystem::path pathLogDir = path / "database"; filesystem::path pathLogDir = path / "database";
filesystem::create_directory(pathLogDir); filesystem::create_directory(pathLogDir);
filesystem::path pathErrorFile = path / "db.log"; filesystem::path pathErrorFile = path / "db.log";
@ -84,7 +84,7 @@ bool CDBEnv::Open(const boost::filesystem::path& path)
dbenv.set_flags(DB_AUTO_COMMIT, 1); dbenv.set_flags(DB_AUTO_COMMIT, 1);
dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1); dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1);
dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1); dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
int ret = dbenv.open(strPath.c_str(), int ret = dbenv.open(path.string().c_str(),
DB_CREATE | DB_CREATE |
DB_INIT_LOCK | DB_INIT_LOCK |
DB_INIT_LOG | DB_INIT_LOG |
@ -456,6 +456,8 @@ void CDBEnv::Flush(bool fShutdown)
{ {
dbenv.log_archive(&listp, DB_ARCH_REMOVE); dbenv.log_archive(&listp, DB_ARCH_REMOVE);
Close(); Close();
if (!fMockDb)
boost::filesystem::remove_all(path / "database");
} }
} }
} }

2
src/db.h

@ -33,7 +33,7 @@ class CDBEnv
private: private:
bool fDbEnvInit; bool fDbEnvInit;
bool fMockDb; bool fMockDb;
std::string strPath; boost::filesystem::path path;
void EnvShutdown(); void EnvShutdown();

23
src/init.cpp

@ -632,9 +632,23 @@ bool AppInit2(boost::thread_group& threadGroup)
if (!bitdb.Open(GetDataDir())) if (!bitdb.Open(GetDataDir()))
{ {
// try moving the database env out of the way
boost::filesystem::path pathDatabase = GetDataDir() / "database";
boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%"PRI64d".bak", GetTime());
try {
boost::filesystem::rename(pathDatabase, pathDatabaseBak);
printf("Moved old %s to %s. Retrying.\n", pathDatabase.string().c_str(), pathDatabaseBak.string().c_str());
} catch(boost::filesystem::filesystem_error &error) {
// failure is ok (well, not really, but it's not worse than what we started with)
}
// try again
if (!bitdb.Open(GetDataDir())) {
// if it still fails, it probably means we can't even create the database env
string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str()); string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str());
return InitError(msg); return InitError(msg);
} }
}
if (GetBoolArg("-salvagewallet")) if (GetBoolArg("-salvagewallet"))
{ {
@ -760,15 +774,6 @@ bool AppInit2(boost::thread_group& threadGroup)
fReindex = GetBoolArg("-reindex"); fReindex = GetBoolArg("-reindex");
// Todo: Check if needed, because in step 5 we do the same
if (!bitdb.Open(GetDataDir()))
{
string msg = strprintf(_("Error initializing database environment %s!"
" To recover, BACKUP THAT DIRECTORY, then remove"
" everything from it except for wallet.dat."), strDataDir.c_str());
return InitError(msg);
}
// Upgrading to 0.8; hard-link the old blknnnn.dat files into /blocks/ // Upgrading to 0.8; hard-link the old blknnnn.dat files into /blocks/
filesystem::path blocksDir = GetDataDir() / "blocks"; filesystem::path blocksDir = GetDataDir() / "blocks";
if (!filesystem::exists(blocksDir)) if (!filesystem::exists(blocksDir))

Loading…
Cancel
Save