Skip to content

Commit

Permalink
Merge pull request #253 from IngCr3at1on/bdb-environments
Browse files Browse the repository at this point in the history
Handle incompatible BDB environments
  • Loading branch information
scroogemcdev committed Feb 23, 2016
2 parents 8ba621b + cbdb157 commit e1f2e07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
20 changes: 8 additions & 12 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@ void CDBEnv::EnvShutdown()
return;

fDbEnvInit = false;
try
{
dbenv.close(0);
}
catch (const DbException& e)
{
printf("EnvShutdown exception: %s (%d)\n", e.what(), e.get_errno());
}
int ret = dbenv.close(0);
if (ret != 0)
printf("EnvShutdown exception: %s (%d)\n", DbEnv::strerror(ret), ret);

if (!fMockDb)
DbEnv(0).remove(GetDataDir().string().c_str(), 0);
}

CDBEnv::CDBEnv() : dbenv(0)
CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS)
{
}

Expand Down Expand Up @@ -96,8 +92,8 @@ bool CDBEnv::Open(const boost::filesystem::path& path)
DB_THREAD |
DB_RECOVER,
S_IRUSR | S_IWUSR);
if (ret > 0)
return error("CDB() : error %d opening database environment", ret);
if (ret != 0)
return error("CDB() : error %s (%d) opening database environment", DbEnv::strerror(ret), ret);

fDbEnvInit = true;
fMockDb = false;
Expand Down Expand Up @@ -186,7 +182,7 @@ CDB::CDB(const char *pszFile, const char* pszMode) :
nFlags, // Flags
0);

if (ret > 0)
if (ret != 0)
{
delete pdb;
pdb = NULL;
Expand Down
18 changes: 15 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,11 @@ bool AppInit2()
FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist.
if (file) fclose(file);
static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
/* Set a cstring to hold the datadir path cause we use it several times
* throughout init. */
const char* pszDataDir = GetDataDir().string().c_str();
if (!lock.try_lock())
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. Paycoin is probably already running."), GetDataDir().string().c_str()));
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. Paycoin is probably already running."), pszDataDir));

#if !defined(WIN32) && !defined(QT_GUI)
if (fDaemon)
Expand Down Expand Up @@ -465,7 +468,7 @@ bool AppInit2()
printf("Using OpenSSL version %s\n", SSLeay_version(SSLEAY_VERSION));
printf("Startup time: %s\n", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
printf("Default data directory %s\n", GetDefaultDataDir().string().c_str());
printf("Used data directory %s\n", GetDataDir().string().c_str());
printf("Used data directory %s\n", pszDataDir);

// Check and update minium version protocol after a given time.
if (time(NULL) >= MICROPRIMES_STAGGER_DOWN)
Expand All @@ -477,6 +480,15 @@ bool AppInit2()
//
if (fDaemon)
fprintf(stdout, "Paycoin server starting\n");

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."), pszDataDir);
return InitError(msg);
}

int64 nStart;

// ********************************************************* Step 5: network initialization
Expand Down Expand Up @@ -590,7 +602,7 @@ bool AppInit2()
printf("Loading block index...\n");
nStart = GetTimeMillis();
if (!LoadBlockIndex())
strErrors << _("Error loading blkindex.dat") << "\n";
return InitError(_("Error loading blkindex.dat"));

// as LoadBlockIndex can take several minutes, it's possible the user
// requested to kill bitcoin-qt during the last operation. If so, exit.
Expand Down

0 comments on commit e1f2e07

Please sign in to comment.