Merge #10574: Remove includes in .cpp files for things the corresponding .h file...
[bitcoinplatinum.git] / src / wallet / db.cpp
blobd4cd30dfac0b3d8ec05775f3cc0ce6fdae80f161
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include <wallet/db.h>
8 #include <addrman.h>
9 #include <hash.h>
10 #include <protocol.h>
11 #include <util.h>
12 #include <utilstrencodings.h>
13 #include <wallet/walletutil.h>
15 #include <stdint.h>
17 #ifndef WIN32
18 #include <sys/stat.h>
19 #endif
21 #include <boost/thread.hpp>
23 namespace {
24 //! Make sure database has a unique fileid within the environment. If it
25 //! doesn't, throw an error. BDB caches do not work properly when more than one
26 //! open database has the same fileid (values written to one database may show
27 //! up in reads to other databases).
28 //!
29 //! BerkeleyDB generates unique fileids by default
30 //! (https://docs.oracle.com/cd/E17275_01/html/programmer_reference/program_copy.html),
31 //! so bitcoin should never create different databases with the same fileid, but
32 //! this error can be triggered if users manually copy database files.
33 void CheckUniqueFileid(const CDBEnv& env, const std::string& filename, Db& db)
35 if (env.IsMock()) return;
37 u_int8_t fileid[DB_FILE_ID_LEN];
38 int ret = db.get_mpf()->get_fileid(fileid);
39 if (ret != 0) {
40 throw std::runtime_error(strprintf("CDB: Can't open database %s (get_fileid failed with %d)", filename, ret));
43 for (const auto& item : env.mapDb) {
44 u_int8_t item_fileid[DB_FILE_ID_LEN];
45 if (item.second && item.second->get_mpf()->get_fileid(item_fileid) == 0 &&
46 memcmp(fileid, item_fileid, sizeof(fileid)) == 0) {
47 const char* item_filename = nullptr;
48 item.second->get_dbname(&item_filename, nullptr);
49 throw std::runtime_error(strprintf("CDB: Can't open database %s (duplicates fileid %s from %s)", filename,
50 HexStr(std::begin(item_fileid), std::end(item_fileid)),
51 item_filename ? item_filename : "(unknown database)"));
55 } // namespace
58 // CDB
61 CDBEnv bitdb;
63 void CDBEnv::EnvShutdown()
65 if (!fDbEnvInit)
66 return;
68 fDbEnvInit = false;
69 int ret = dbenv->close(0);
70 if (ret != 0)
71 LogPrintf("CDBEnv::EnvShutdown: Error %d shutting down database environment: %s\n", ret, DbEnv::strerror(ret));
72 if (!fMockDb)
73 DbEnv((u_int32_t)0).remove(strPath.c_str(), 0);
76 void CDBEnv::Reset()
78 dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS));
79 fDbEnvInit = false;
80 fMockDb = false;
83 CDBEnv::CDBEnv()
85 Reset();
88 CDBEnv::~CDBEnv()
90 EnvShutdown();
93 void CDBEnv::Close()
95 EnvShutdown();
98 bool CDBEnv::Open(const fs::path& pathIn)
100 if (fDbEnvInit)
101 return true;
103 boost::this_thread::interruption_point();
105 strPath = pathIn.string();
106 fs::path pathLogDir = pathIn / "database";
107 TryCreateDirectories(pathLogDir);
108 fs::path pathErrorFile = pathIn / "db.log";
109 LogPrintf("CDBEnv::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
111 unsigned int nEnvFlags = 0;
112 if (gArgs.GetBoolArg("-privdb", DEFAULT_WALLET_PRIVDB))
113 nEnvFlags |= DB_PRIVATE;
115 dbenv->set_lg_dir(pathLogDir.string().c_str());
116 dbenv->set_cachesize(0, 0x100000, 1); // 1 MiB should be enough for just the wallet
117 dbenv->set_lg_bsize(0x10000);
118 dbenv->set_lg_max(1048576);
119 dbenv->set_lk_max_locks(40000);
120 dbenv->set_lk_max_objects(40000);
121 dbenv->set_errfile(fsbridge::fopen(pathErrorFile, "a")); /// debug
122 dbenv->set_flags(DB_AUTO_COMMIT, 1);
123 dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
124 dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);
125 int ret = dbenv->open(strPath.c_str(),
126 DB_CREATE |
127 DB_INIT_LOCK |
128 DB_INIT_LOG |
129 DB_INIT_MPOOL |
130 DB_INIT_TXN |
131 DB_THREAD |
132 DB_RECOVER |
133 nEnvFlags,
134 S_IRUSR | S_IWUSR);
135 if (ret != 0) {
136 dbenv->close(0);
137 return error("CDBEnv::Open: Error %d opening database environment: %s\n", ret, DbEnv::strerror(ret));
140 fDbEnvInit = true;
141 fMockDb = false;
142 return true;
145 void CDBEnv::MakeMock()
147 if (fDbEnvInit)
148 throw std::runtime_error("CDBEnv::MakeMock: Already initialized");
150 boost::this_thread::interruption_point();
152 LogPrint(BCLog::DB, "CDBEnv::MakeMock\n");
154 dbenv->set_cachesize(1, 0, 1);
155 dbenv->set_lg_bsize(10485760 * 4);
156 dbenv->set_lg_max(10485760);
157 dbenv->set_lk_max_locks(10000);
158 dbenv->set_lk_max_objects(10000);
159 dbenv->set_flags(DB_AUTO_COMMIT, 1);
160 dbenv->log_set_config(DB_LOG_IN_MEMORY, 1);
161 int ret = dbenv->open(nullptr,
162 DB_CREATE |
163 DB_INIT_LOCK |
164 DB_INIT_LOG |
165 DB_INIT_MPOOL |
166 DB_INIT_TXN |
167 DB_THREAD |
168 DB_PRIVATE,
169 S_IRUSR | S_IWUSR);
170 if (ret > 0)
171 throw std::runtime_error(strprintf("CDBEnv::MakeMock: Error %d opening database environment.", ret));
173 fDbEnvInit = true;
174 fMockDb = true;
177 CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type recoverFunc, std::string& out_backup_filename)
179 LOCK(cs_db);
180 assert(mapFileUseCount.count(strFile) == 0);
182 Db db(dbenv.get(), 0);
183 int result = db.verify(strFile.c_str(), nullptr, nullptr, 0);
184 if (result == 0)
185 return VERIFY_OK;
186 else if (recoverFunc == nullptr)
187 return RECOVER_FAIL;
189 // Try to recover:
190 bool fRecovered = (*recoverFunc)(strFile, out_backup_filename);
191 return (fRecovered ? RECOVER_OK : RECOVER_FAIL);
194 bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename)
196 // Recovery procedure:
197 // move wallet file to walletfilename.timestamp.bak
198 // Call Salvage with fAggressive=true to
199 // get as much data as possible.
200 // Rewrite salvaged data to fresh wallet file
201 // Set -rescan so any missing transactions will be
202 // found.
203 int64_t now = GetTime();
204 newFilename = strprintf("%s.%d.bak", filename, now);
206 int result = bitdb.dbenv->dbrename(nullptr, filename.c_str(), nullptr,
207 newFilename.c_str(), DB_AUTO_COMMIT);
208 if (result == 0)
209 LogPrintf("Renamed %s to %s\n", filename, newFilename);
210 else
212 LogPrintf("Failed to rename %s to %s\n", filename, newFilename);
213 return false;
216 std::vector<CDBEnv::KeyValPair> salvagedData;
217 bool fSuccess = bitdb.Salvage(newFilename, true, salvagedData);
218 if (salvagedData.empty())
220 LogPrintf("Salvage(aggressive) found no records in %s.\n", newFilename);
221 return false;
223 LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size());
225 std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(bitdb.dbenv.get(), 0);
226 int ret = pdbCopy->open(nullptr, // Txn pointer
227 filename.c_str(), // Filename
228 "main", // Logical db name
229 DB_BTREE, // Database type
230 DB_CREATE, // Flags
232 if (ret > 0) {
233 LogPrintf("Cannot create database file %s\n", filename);
234 pdbCopy->close(0);
235 return false;
238 DbTxn* ptxn = bitdb.TxnBegin();
239 for (CDBEnv::KeyValPair& row : salvagedData)
241 if (recoverKVcallback)
243 CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
244 CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
245 if (!(*recoverKVcallback)(callbackDataIn, ssKey, ssValue))
246 continue;
248 Dbt datKey(&row.first[0], row.first.size());
249 Dbt datValue(&row.second[0], row.second.size());
250 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
251 if (ret2 > 0)
252 fSuccess = false;
254 ptxn->commit(0);
255 pdbCopy->close(0);
257 return fSuccess;
260 bool CDB::VerifyEnvironment(const std::string& walletFile, const fs::path& walletDir, std::string& errorStr)
262 LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0));
263 LogPrintf("Using wallet %s\n", walletFile);
265 // Wallet file must be a plain filename without a directory
266 if (walletFile != fs::basename(walletFile) + fs::extension(walletFile))
268 errorStr = strprintf(_("Wallet %s resides outside wallet directory %s"), walletFile, walletDir.string());
269 return false;
272 if (!bitdb.Open(walletDir))
274 // try moving the database env out of the way
275 fs::path pathDatabase = walletDir / "database";
276 fs::path pathDatabaseBak = walletDir / strprintf("database.%d.bak", GetTime());
277 try {
278 fs::rename(pathDatabase, pathDatabaseBak);
279 LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string());
280 } catch (const fs::filesystem_error&) {
281 // failure is ok (well, not really, but it's not worse than what we started with)
284 // try again
285 if (!bitdb.Open(walletDir)) {
286 // if it still fails, it probably means we can't even create the database env
287 errorStr = strprintf(_("Error initializing wallet database environment %s!"), walletDir);
288 return false;
291 return true;
294 bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& walletDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc)
296 if (fs::exists(walletDir / walletFile))
298 std::string backup_filename;
299 CDBEnv::VerifyResult r = bitdb.Verify(walletFile, recoverFunc, backup_filename);
300 if (r == CDBEnv::RECOVER_OK)
302 warningStr = strprintf(_("Warning: Wallet file corrupt, data salvaged!"
303 " Original %s saved as %s in %s; if"
304 " your balance or transactions are incorrect you should"
305 " restore from a backup."),
306 walletFile, backup_filename, walletDir);
308 if (r == CDBEnv::RECOVER_FAIL)
310 errorStr = strprintf(_("%s corrupt, salvage failed"), walletFile);
311 return false;
314 // also return true if files does not exists
315 return true;
318 /* End of headers, beginning of key/value data */
319 static const char *HEADER_END = "HEADER=END";
320 /* End of key/value data */
321 static const char *DATA_END = "DATA=END";
323 bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<CDBEnv::KeyValPair>& vResult)
325 LOCK(cs_db);
326 assert(mapFileUseCount.count(strFile) == 0);
328 u_int32_t flags = DB_SALVAGE;
329 if (fAggressive)
330 flags |= DB_AGGRESSIVE;
332 std::stringstream strDump;
334 Db db(dbenv.get(), 0);
335 int result = db.verify(strFile.c_str(), nullptr, &strDump, flags);
336 if (result == DB_VERIFY_BAD) {
337 LogPrintf("CDBEnv::Salvage: Database salvage found errors, all data may not be recoverable.\n");
338 if (!fAggressive) {
339 LogPrintf("CDBEnv::Salvage: Rerun with aggressive mode to ignore errors and continue.\n");
340 return false;
343 if (result != 0 && result != DB_VERIFY_BAD) {
344 LogPrintf("CDBEnv::Salvage: Database salvage failed with result %d.\n", result);
345 return false;
348 // Format of bdb dump is ascii lines:
349 // header lines...
350 // HEADER=END
351 // hexadecimal key
352 // hexadecimal value
353 // ... repeated
354 // DATA=END
356 std::string strLine;
357 while (!strDump.eof() && strLine != HEADER_END)
358 getline(strDump, strLine); // Skip past header
360 std::string keyHex, valueHex;
361 while (!strDump.eof() && keyHex != DATA_END) {
362 getline(strDump, keyHex);
363 if (keyHex != DATA_END) {
364 if (strDump.eof())
365 break;
366 getline(strDump, valueHex);
367 if (valueHex == DATA_END) {
368 LogPrintf("CDBEnv::Salvage: WARNING: Number of keys in data does not match number of values.\n");
369 break;
371 vResult.push_back(make_pair(ParseHex(keyHex), ParseHex(valueHex)));
375 if (keyHex != DATA_END) {
376 LogPrintf("CDBEnv::Salvage: WARNING: Unexpected end of file while reading salvage output.\n");
377 return false;
380 return (result == 0);
384 void CDBEnv::CheckpointLSN(const std::string& strFile)
386 dbenv->txn_checkpoint(0, 0, 0);
387 if (fMockDb)
388 return;
389 dbenv->lsn_reset(strFile.c_str(), 0);
393 CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr)
395 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
396 fFlushOnClose = fFlushOnCloseIn;
397 env = dbw.env;
398 if (dbw.IsDummy()) {
399 return;
401 const std::string &strFilename = dbw.strFile;
403 bool fCreate = strchr(pszMode, 'c') != nullptr;
404 unsigned int nFlags = DB_THREAD;
405 if (fCreate)
406 nFlags |= DB_CREATE;
409 LOCK(env->cs_db);
410 if (!env->Open(GetWalletDir()))
411 throw std::runtime_error("CDB: Failed to open database environment.");
413 pdb = env->mapDb[strFilename];
414 if (pdb == nullptr) {
415 int ret;
416 std::unique_ptr<Db> pdb_temp = MakeUnique<Db>(env->dbenv.get(), 0);
418 bool fMockDb = env->IsMock();
419 if (fMockDb) {
420 DbMpoolFile* mpf = pdb_temp->get_mpf();
421 ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
422 if (ret != 0) {
423 throw std::runtime_error(strprintf("CDB: Failed to configure for no temp file backing for database %s", strFilename));
427 ret = pdb_temp->open(nullptr, // Txn pointer
428 fMockDb ? nullptr : strFilename.c_str(), // Filename
429 fMockDb ? strFilename.c_str() : "main", // Logical db name
430 DB_BTREE, // Database type
431 nFlags, // Flags
434 if (ret != 0) {
435 throw std::runtime_error(strprintf("CDB: Error %d, can't open database %s", ret, strFilename));
437 CheckUniqueFileid(*env, strFilename, *pdb_temp);
439 pdb = pdb_temp.release();
440 env->mapDb[strFilename] = pdb;
442 if (fCreate && !Exists(std::string("version"))) {
443 bool fTmp = fReadOnly;
444 fReadOnly = false;
445 WriteVersion(CLIENT_VERSION);
446 fReadOnly = fTmp;
449 ++env->mapFileUseCount[strFilename];
450 strFile = strFilename;
454 void CDB::Flush()
456 if (activeTxn)
457 return;
459 // Flush database activity from memory pool to disk log
460 unsigned int nMinutes = 0;
461 if (fReadOnly)
462 nMinutes = 1;
464 env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0);
467 void CWalletDBWrapper::IncrementUpdateCounter()
469 ++nUpdateCounter;
472 void CDB::Close()
474 if (!pdb)
475 return;
476 if (activeTxn)
477 activeTxn->abort();
478 activeTxn = nullptr;
479 pdb = nullptr;
481 if (fFlushOnClose)
482 Flush();
485 LOCK(env->cs_db);
486 --env->mapFileUseCount[strFile];
490 void CDBEnv::CloseDb(const std::string& strFile)
493 LOCK(cs_db);
494 if (mapDb[strFile] != nullptr) {
495 // Close the database handle
496 Db* pdb = mapDb[strFile];
497 pdb->close(0);
498 delete pdb;
499 mapDb[strFile] = nullptr;
504 bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
506 if (dbw.IsDummy()) {
507 return true;
509 CDBEnv *env = dbw.env;
510 const std::string& strFile = dbw.strFile;
511 while (true) {
513 LOCK(env->cs_db);
514 if (!env->mapFileUseCount.count(strFile) || env->mapFileUseCount[strFile] == 0) {
515 // Flush log data to the dat file
516 env->CloseDb(strFile);
517 env->CheckpointLSN(strFile);
518 env->mapFileUseCount.erase(strFile);
520 bool fSuccess = true;
521 LogPrintf("CDB::Rewrite: Rewriting %s...\n", strFile);
522 std::string strFileRes = strFile + ".rewrite";
523 { // surround usage of db with extra {}
524 CDB db(dbw, "r");
525 std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0);
527 int ret = pdbCopy->open(nullptr, // Txn pointer
528 strFileRes.c_str(), // Filename
529 "main", // Logical db name
530 DB_BTREE, // Database type
531 DB_CREATE, // Flags
533 if (ret > 0) {
534 LogPrintf("CDB::Rewrite: Can't create database file %s\n", strFileRes);
535 fSuccess = false;
538 Dbc* pcursor = db.GetCursor();
539 if (pcursor)
540 while (fSuccess) {
541 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
542 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
543 int ret1 = db.ReadAtCursor(pcursor, ssKey, ssValue);
544 if (ret1 == DB_NOTFOUND) {
545 pcursor->close();
546 break;
547 } else if (ret1 != 0) {
548 pcursor->close();
549 fSuccess = false;
550 break;
552 if (pszSkip &&
553 strncmp(ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
554 continue;
555 if (strncmp(ssKey.data(), "\x07version", 8) == 0) {
556 // Update version:
557 ssValue.clear();
558 ssValue << CLIENT_VERSION;
560 Dbt datKey(ssKey.data(), ssKey.size());
561 Dbt datValue(ssValue.data(), ssValue.size());
562 int ret2 = pdbCopy->put(nullptr, &datKey, &datValue, DB_NOOVERWRITE);
563 if (ret2 > 0)
564 fSuccess = false;
566 if (fSuccess) {
567 db.Close();
568 env->CloseDb(strFile);
569 if (pdbCopy->close(0))
570 fSuccess = false;
571 } else {
572 pdbCopy->close(0);
575 if (fSuccess) {
576 Db dbA(env->dbenv.get(), 0);
577 if (dbA.remove(strFile.c_str(), nullptr, 0))
578 fSuccess = false;
579 Db dbB(env->dbenv.get(), 0);
580 if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0))
581 fSuccess = false;
583 if (!fSuccess)
584 LogPrintf("CDB::Rewrite: Failed to rewrite database file %s\n", strFileRes);
585 return fSuccess;
588 MilliSleep(100);
593 void CDBEnv::Flush(bool fShutdown)
595 int64_t nStart = GetTimeMillis();
596 // Flush log data to the actual data file on all files that are not in use
597 LogPrint(BCLog::DB, "CDBEnv::Flush: Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started");
598 if (!fDbEnvInit)
599 return;
601 LOCK(cs_db);
602 std::map<std::string, int>::iterator mi = mapFileUseCount.begin();
603 while (mi != mapFileUseCount.end()) {
604 std::string strFile = (*mi).first;
605 int nRefCount = (*mi).second;
606 LogPrint(BCLog::DB, "CDBEnv::Flush: Flushing %s (refcount = %d)...\n", strFile, nRefCount);
607 if (nRefCount == 0) {
608 // Move log data to the dat file
609 CloseDb(strFile);
610 LogPrint(BCLog::DB, "CDBEnv::Flush: %s checkpoint\n", strFile);
611 dbenv->txn_checkpoint(0, 0, 0);
612 LogPrint(BCLog::DB, "CDBEnv::Flush: %s detach\n", strFile);
613 if (!fMockDb)
614 dbenv->lsn_reset(strFile.c_str(), 0);
615 LogPrint(BCLog::DB, "CDBEnv::Flush: %s closed\n", strFile);
616 mapFileUseCount.erase(mi++);
617 } else
618 mi++;
620 LogPrint(BCLog::DB, "CDBEnv::Flush: Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart);
621 if (fShutdown) {
622 char** listp;
623 if (mapFileUseCount.empty()) {
624 dbenv->log_archive(&listp, DB_ARCH_REMOVE);
625 Close();
626 if (!fMockDb)
627 fs::remove_all(fs::path(strPath) / "database");
633 bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
635 if (dbw.IsDummy()) {
636 return true;
638 bool ret = false;
639 CDBEnv *env = dbw.env;
640 const std::string& strFile = dbw.strFile;
641 TRY_LOCK(bitdb.cs_db,lockDb);
642 if (lockDb)
644 // Don't do this if any databases are in use
645 int nRefCount = 0;
646 std::map<std::string, int>::iterator mit = env->mapFileUseCount.begin();
647 while (mit != env->mapFileUseCount.end())
649 nRefCount += (*mit).second;
650 mit++;
653 if (nRefCount == 0)
655 boost::this_thread::interruption_point();
656 std::map<std::string, int>::iterator mi = env->mapFileUseCount.find(strFile);
657 if (mi != env->mapFileUseCount.end())
659 LogPrint(BCLog::DB, "Flushing %s\n", strFile);
660 int64_t nStart = GetTimeMillis();
662 // Flush wallet file so it's self contained
663 env->CloseDb(strFile);
664 env->CheckpointLSN(strFile);
666 env->mapFileUseCount.erase(mi++);
667 LogPrint(BCLog::DB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
668 ret = true;
673 return ret;
676 bool CWalletDBWrapper::Rewrite(const char* pszSkip)
678 return CDB::Rewrite(*this, pszSkip);
681 bool CWalletDBWrapper::Backup(const std::string& strDest)
683 if (IsDummy()) {
684 return false;
686 while (true)
689 LOCK(env->cs_db);
690 if (!env->mapFileUseCount.count(strFile) || env->mapFileUseCount[strFile] == 0)
692 // Flush log data to the dat file
693 env->CloseDb(strFile);
694 env->CheckpointLSN(strFile);
695 env->mapFileUseCount.erase(strFile);
697 // Copy wallet file
698 fs::path pathSrc = GetWalletDir() / strFile;
699 fs::path pathDest(strDest);
700 if (fs::is_directory(pathDest))
701 pathDest /= strFile;
703 try {
704 if (fs::equivalent(pathSrc, pathDest)) {
705 LogPrintf("cannot backup to wallet source file %s\n", pathDest.string());
706 return false;
709 fs::copy_file(pathSrc, pathDest, fs::copy_option::overwrite_if_exists);
710 LogPrintf("copied %s to %s\n", strFile, pathDest.string());
711 return true;
712 } catch (const fs::filesystem_error& e) {
713 LogPrintf("error copying %s to %s - %s\n", strFile, pathDest.string(), e.what());
714 return false;
718 MilliSleep(100);
722 void CWalletDBWrapper::Flush(bool shutdown)
724 if (!IsDummy()) {
725 env->Flush(shutdown);