Only remove database log files on shutdown after wallet encryption/rewrite
[novacoin.git] / src / db.cpp
index d769cae..e0f4a01 100644 (file)
@@ -28,13 +28,25 @@ DbEnv dbenv(0);
 static map<string, int> mapFileUseCount;
 static map<string, Db*> mapDb;
 
-static void EnvShutdown(bool fRemoveLogFiles)
+static bool fRemoveLogFiles = false;
+void RemoveLogFilesOnShutdown(bool fIn)
+{
+    fRemoveLogFiles = fIn;
+}
+static void EnvShutdown()
 {
     if (!fDbEnvInit)
         return;
 
     fDbEnvInit = false;
-    dbenv.close(0);
+    try
+    {
+        dbenv.close(0);
+    }
+    catch (const DbException& e)
+    {
+        printf("EnvShutdown exception: %s (%d)\n", e.what(), e.get_errno());
+    }
     DbEnv(0).remove(GetDataDir().c_str(), 0);
 
     if (fRemoveLogFiles)
@@ -44,7 +56,7 @@ static void EnvShutdown(bool fRemoveLogFiles)
         while (it != filesystem::directory_iterator())
         {
             const filesystem::path& p = it->path();
-#if BOOST_FILESYSTEM_VERSION == 3
+#if BOOST_FILESYSTEM_VERSION >= 3
             std::string f = p.filename().generic_string();
 #else
             std::string f = p.filename();
@@ -64,7 +76,7 @@ public:
     }
     ~CDBInit()
     {
-        EnvShutdown(false);
+        EnvShutdown();
     }
 }
 instance_of_cdbinit;
@@ -229,7 +241,10 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
                         CDataStream ssValue;
                         int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT);
                         if (ret == DB_NOTFOUND)
+                        {
+                            pcursor->close();
                             break;
+                        }
                         else if (ret != 0)
                         {
                             pcursor->close();
@@ -253,14 +268,11 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
                     }
                 if (fSuccess)
                 {
-                    Db* pdb = mapDb[strFile];
-                    if (pdb->close(0))
-                        fSuccess = false;
+                    db.Close();
+                    CloseDb(strFile);
                     if (pdbCopy->close(0))
                         fSuccess = false;
-                    delete pdb;
                     delete pdbCopy;
-                    mapDb[strFile] = NULL;
                 }
                 if (fSuccess)
                 {
@@ -282,7 +294,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
 }
 
 
-void DBFlush(bool fShutdown, bool fRemoveLogFiles)
+void DBFlush(bool fShutdown)
 {
     // Flush log data to the actual data file
     //  on all files that are not in use
@@ -315,7 +327,7 @@ void DBFlush(bool fShutdown, bool fRemoveLogFiles)
             if (mapFileUseCount.empty())
             {
                 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
-                EnvShutdown(fRemoveLogFiles);
+                EnvShutdown();
             }
         }
     }