Move BackupWallet to CWallet::BackupWallet

This commit is contained in:
Patrick Strateman 2016-05-16 17:31:16 -07:00
parent ecb9741ec3
commit 380498aba4
6 changed files with 44 additions and 43 deletions

View file

@ -447,7 +447,7 @@ bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureStri
bool WalletModel::backupWallet(const QString &filename) bool WalletModel::backupWallet(const QString &filename)
{ {
return BackupWallet(*wallet, filename.toLocal8Bit().data()); return wallet->BackupWallet(filename.toLocal8Bit().data());
} }
// Handlers for core signals // Handlers for core signals

View file

@ -1804,7 +1804,7 @@ UniValue backupwallet(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwalletMain->cs_wallet);
string strDest = params[0].get_str(); string strDest = params[0].get_str();
if (!BackupWallet(*pwalletMain, strDest)) if (!pwalletMain->BackupWallet(strDest))
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!"); throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
return NullUniValue; return NullUniValue;

View file

@ -3283,6 +3283,46 @@ bool CWallet::ParameterInteraction()
return true; return true;
} }
bool CWallet::BackupWallet(const std::string& strDest)
{
if (!fFileBacked)
return false;
while (true)
{
{
LOCK(bitdb.cs_db);
if (!bitdb.mapFileUseCount.count(strWalletFile) || bitdb.mapFileUseCount[strWalletFile] == 0)
{
// Flush log data to the dat file
bitdb.CloseDb(strWalletFile);
bitdb.CheckpointLSN(strWalletFile);
bitdb.mapFileUseCount.erase(strWalletFile);
// Copy wallet file
boost::filesystem::path pathSrc = GetDataDir() / strWalletFile;
boost::filesystem::path pathDest(strDest);
if (boost::filesystem::is_directory(pathDest))
pathDest /= strWalletFile;
try {
#if BOOST_VERSION >= 104000
boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists);
#else
boost::filesystem::copy_file(pathSrc, pathDest);
#endif
LogPrintf("copied %s to %s\n", strWalletFile, pathDest.string());
return true;
} catch (const boost::filesystem::filesystem_error& e) {
LogPrintf("error copying %s to %s - %s\n", strWalletFile, pathDest.string(), e.what());
return false;
}
}
}
MilliSleep(100);
}
return false;
}
CKeyPool::CKeyPool() CKeyPool::CKeyPool()
{ {
nTime = GetTime(); nTime = GetTime();

View file

@ -885,6 +885,8 @@ public:
/* Wallets parameter interaction */ /* Wallets parameter interaction */
static bool ParameterInteraction(); static bool ParameterInteraction();
bool BackupWallet(const std::string& strDest);
}; };
/** A key allocated from the key pool. */ /** A key allocated from the key pool. */

View file

@ -903,46 +903,6 @@ void ThreadFlushWalletDB(const string& strFile)
} }
} }
bool BackupWallet(const CWallet& wallet, const string& strDest)
{
if (!wallet.fFileBacked)
return false;
while (true)
{
{
LOCK(bitdb.cs_db);
if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
{
// Flush log data to the dat file
bitdb.CloseDb(wallet.strWalletFile);
bitdb.CheckpointLSN(wallet.strWalletFile);
bitdb.mapFileUseCount.erase(wallet.strWalletFile);
// Copy wallet file
boost::filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
boost::filesystem::path pathDest(strDest);
if (boost::filesystem::is_directory(pathDest))
pathDest /= wallet.strWalletFile;
try {
#if BOOST_VERSION >= 104000
boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists);
#else
boost::filesystem::copy_file(pathSrc, pathDest);
#endif
LogPrintf("copied %s to %s\n", wallet.strWalletFile, pathDest.string());
return true;
} catch (const boost::filesystem::filesystem_error& e) {
LogPrintf("error copying %s to %s - %s\n", wallet.strWalletFile, pathDest.string(), e.what());
return false;
}
}
}
MilliSleep(100);
}
return false;
}
// //
// Try to (very carefully!) recover wallet file if there is a problem. // Try to (very carefully!) recover wallet file if there is a problem.
// //

View file

@ -141,7 +141,6 @@ private:
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry); bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
}; };
bool BackupWallet(const CWallet& wallet, const std::string& strDest);
void ThreadFlushWalletDB(const std::string& strFile); void ThreadFlushWalletDB(const std::string& strFile);
#endif // BITCOIN_WALLET_WALLETDB_H #endif // BITCOIN_WALLET_WALLETDB_H