@@ -359,13 +359,12 @@ void CDBEnv::CheckpointLSN(const std::string& strFile)
359359}
360360
361361
362- CDB::CDB (const std::string& strFilename , const char * pszMode, bool fFlushOnCloseIn ) : pdb(NULL ), activeTxn(NULL )
362+ CDB::CDB (CWalletDBWrapper& dbw , const char * pszMode, bool fFlushOnCloseIn ) : pdb(NULL ), activeTxn(NULL )
363363{
364364 int ret;
365365 fReadOnly = (!strchr (pszMode, ' +' ) && !strchr (pszMode, ' w' ));
366366 fFlushOnClose = fFlushOnCloseIn ;
367- if (strFilename.empty ())
368- return ;
367+ const std::string& strFilename = dbw.strFile ;
369368
370369 bool fCreate = strchr (pszMode, ' c' ) != NULL ;
371370 unsigned int nFlags = DB_THREAD;
@@ -472,8 +471,12 @@ bool CDBEnv::RemoveDb(const std::string& strFile)
472471 return (rc == 0 );
473472}
474473
475- bool CDB::Rewrite (const std::string& strFile , const char * pszSkip)
474+ bool CDB::Rewrite (CWalletDBWrapper& dbw , const char * pszSkip)
476475{
476+ if (!dbw.env ) {
477+ return true ;
478+ }
479+ const std::string& strFile = dbw.strFile ;
477480 while (true ) {
478481 {
479482 LOCK (bitdb.cs_db );
@@ -487,7 +490,7 @@ bool CDB::Rewrite(const std::string& strFile, const char* pszSkip)
487490 LogPrintf (" CDB::Rewrite: Rewriting %s...\n " , strFile);
488491 std::string strFileRes = strFile + " .rewrite" ;
489492 { // surround usage of db with extra {}
490- CDB db (strFile. c_str () , " r" );
493+ CDB db (dbw , " r" );
491494 Db* pdbCopy = new Db (bitdb.dbenv , 0 );
492495
493496 int ret = pdbCopy->open (NULL , // Txn pointer
@@ -596,9 +599,10 @@ void CDBEnv::Flush(bool fShutdown)
596599 }
597600}
598601
599- bool CDB::PeriodicFlush (std::string strFile )
602+ bool CDB::PeriodicFlush (CWalletDBWrapper& dbw )
600603{
601604 bool ret = false ;
605+ const std::string& strFile = dbw.strFile ;
602606 TRY_LOCK (bitdb.cs_db ,lockDb);
603607 if (lockDb)
604608 {
@@ -633,3 +637,45 @@ bool CDB::PeriodicFlush(std::string strFile)
633637
634638 return ret;
635639}
640+
641+ bool CWalletDBWrapper::Rewrite (const char * pszSkip)
642+ {
643+ return CDB::Rewrite (*this , pszSkip);
644+ }
645+
646+ bool CWalletDBWrapper::Backup (const std::string& strDest)
647+ {
648+ if (!env) {
649+ return false ;
650+ }
651+ while (true )
652+ {
653+ {
654+ LOCK (bitdb.cs_db );
655+ if (!bitdb.mapFileUseCount .count (strFile) || bitdb.mapFileUseCount [strFile] == 0 )
656+ {
657+ // Flush log data to the dat file
658+ bitdb.CloseDb (strFile);
659+ bitdb.CheckpointLSN (strFile);
660+ bitdb.mapFileUseCount .erase (strFile);
661+
662+ // Copy wallet file
663+ fs::path pathSrc = GetDataDir () / strFile;
664+ fs::path pathDest (strDest);
665+ if (fs::is_directory (pathDest))
666+ pathDest /= strFile;
667+
668+ try {
669+ fs::copy_file (pathSrc, pathDest, fs::copy_option::overwrite_if_exists);
670+ LogPrintf (" copied %s to %s\n " , strFile, pathDest.string ());
671+ return true ;
672+ } catch (const fs::filesystem_error& e) {
673+ LogPrintf (" error copying %s to %s - %s\n " , strFile, pathDest.string (), e.what ());
674+ return false ;
675+ }
676+ }
677+ }
678+ MilliSleep (100 );
679+ }
680+ return false ;
681+ }
0 commit comments