Merge remote branch 'refs/remotes/svn/trunk' into svn
[bitcoinplatinum.git] / db.cpp
blobdf500cadb87bcfd5a1d8c4cabbdbdf7c81786d0f
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 #include "headers.h"
7 void ThreadFlushWalletDB(void* parg);
10 unsigned int nWalletDBUpdated;
16 // CDB
19 static CCriticalSection cs_db;
20 static bool fDbEnvInit = false;
21 DbEnv dbenv(0);
22 static map<string, int> mapFileUseCount;
23 static map<string, Db*> mapDb;
25 class CDBInit
27 public:
28 CDBInit()
31 ~CDBInit()
33 if (fDbEnvInit)
35 dbenv.close(0);
36 fDbEnvInit = false;
40 instance_of_cdbinit;
43 CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
45 int ret;
46 if (pszFile == NULL)
47 return;
49 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
50 bool fCreate = strchr(pszMode, 'c');
51 unsigned int nFlags = DB_THREAD;
52 if (fCreate)
53 nFlags |= DB_CREATE;
55 CRITICAL_BLOCK(cs_db)
57 if (!fDbEnvInit)
59 if (fShutdown)
60 return;
61 string strDataDir = GetDataDir();
62 string strLogDir = strDataDir + "/database";
63 filesystem::create_directory(strLogDir.c_str());
64 string strErrorFile = strDataDir + "/db.log";
65 printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
67 dbenv.set_lg_dir(strLogDir.c_str());
68 dbenv.set_lg_max(10000000);
69 dbenv.set_lk_max_locks(10000);
70 dbenv.set_lk_max_objects(10000);
71 dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
72 dbenv.set_flags(DB_AUTO_COMMIT, 1);
73 ret = dbenv.open(strDataDir.c_str(),
74 DB_CREATE |
75 DB_INIT_LOCK |
76 DB_INIT_LOG |
77 DB_INIT_MPOOL |
78 DB_INIT_TXN |
79 DB_THREAD |
80 DB_RECOVER,
81 S_IRUSR | S_IWUSR);
82 if (ret > 0)
83 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
84 fDbEnvInit = true;
87 strFile = pszFile;
88 ++mapFileUseCount[strFile];
89 pdb = mapDb[strFile];
90 if (pdb == NULL)
92 pdb = new Db(&dbenv, 0);
94 ret = pdb->open(NULL, // Txn pointer
95 pszFile, // Filename
96 "main", // Logical db name
97 DB_BTREE, // Database type
98 nFlags, // Flags
99 0);
101 if (ret > 0)
103 delete pdb;
104 pdb = NULL;
105 CRITICAL_BLOCK(cs_db)
106 --mapFileUseCount[strFile];
107 strFile = "";
108 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
111 if (fCreate && !Exists(string("version")))
113 bool fTmp = fReadOnly;
114 fReadOnly = false;
115 WriteVersion(VERSION);
116 fReadOnly = fTmp;
119 mapDb[strFile] = pdb;
124 void CDB::Close()
126 if (!pdb)
127 return;
128 if (!vTxn.empty())
129 vTxn.front()->abort();
130 vTxn.clear();
131 pdb = NULL;
133 // Flush database activity from memory pool to disk log
134 unsigned int nMinutes = 0;
135 if (strFile == "addr.dat")
136 nMinutes = 2;
137 if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
138 nMinutes = 1;
139 dbenv.txn_checkpoint(0, nMinutes, 0);
141 CRITICAL_BLOCK(cs_db)
142 --mapFileUseCount[strFile];
145 void CloseDb(const string& strFile)
147 CRITICAL_BLOCK(cs_db)
149 if (mapDb[strFile] != NULL)
151 // Close the database handle
152 Db* pdb = mapDb[strFile];
153 pdb->close(0);
154 delete pdb;
155 mapDb[strFile] = NULL;
160 void DBFlush(bool fShutdown)
162 // Flush log data to the actual data file
163 // on all files that are not in use
164 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
165 if (!fDbEnvInit)
166 return;
167 CRITICAL_BLOCK(cs_db)
169 map<string, int>::iterator mi = mapFileUseCount.begin();
170 while (mi != mapFileUseCount.end())
172 string strFile = (*mi).first;
173 int nRefCount = (*mi).second;
174 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
175 if (nRefCount == 0)
177 // Move log data to the dat file
178 CloseDb(strFile);
179 dbenv.txn_checkpoint(0, 0, 0);
180 printf("%s flush\n", strFile.c_str());
181 dbenv.lsn_reset(strFile.c_str(), 0);
182 mapFileUseCount.erase(mi++);
184 else
185 mi++;
187 if (fShutdown)
189 char** listp;
190 if (mapFileUseCount.empty())
191 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
192 dbenv.close(0);
193 fDbEnvInit = false;
204 // CTxDB
207 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
209 assert(!fClient);
210 txindex.SetNull();
211 return Read(make_pair(string("tx"), hash), txindex);
214 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
216 assert(!fClient);
217 return Write(make_pair(string("tx"), hash), txindex);
220 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
222 assert(!fClient);
224 // Add to tx index
225 uint256 hash = tx.GetHash();
226 CTxIndex txindex(pos, tx.vout.size());
227 return Write(make_pair(string("tx"), hash), txindex);
230 bool CTxDB::EraseTxIndex(const CTransaction& tx)
232 assert(!fClient);
233 uint256 hash = tx.GetHash();
235 return Erase(make_pair(string("tx"), hash));
238 bool CTxDB::ContainsTx(uint256 hash)
240 assert(!fClient);
241 return Exists(make_pair(string("tx"), hash));
244 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
246 assert(!fClient);
247 vtx.clear();
249 // Get cursor
250 Dbc* pcursor = GetCursor();
251 if (!pcursor)
252 return false;
254 unsigned int fFlags = DB_SET_RANGE;
255 loop
257 // Read next record
258 CDataStream ssKey;
259 if (fFlags == DB_SET_RANGE)
260 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
261 CDataStream ssValue;
262 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
263 fFlags = DB_NEXT;
264 if (ret == DB_NOTFOUND)
265 break;
266 else if (ret != 0)
268 pcursor->close();
269 return false;
272 // Unserialize
273 string strType;
274 uint160 hashItem;
275 CDiskTxPos pos;
276 ssKey >> strType >> hashItem >> pos;
277 int nItemHeight;
278 ssValue >> nItemHeight;
280 // Read transaction
281 if (strType != "owner" || hashItem != hash160)
282 break;
283 if (nItemHeight >= nMinHeight)
285 vtx.resize(vtx.size()+1);
286 if (!vtx.back().ReadFromDisk(pos))
288 pcursor->close();
289 return false;
294 pcursor->close();
295 return true;
298 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
300 assert(!fClient);
301 tx.SetNull();
302 if (!ReadTxIndex(hash, txindex))
303 return false;
304 return (tx.ReadFromDisk(txindex.pos));
307 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
309 CTxIndex txindex;
310 return ReadDiskTx(hash, tx, txindex);
313 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
315 return ReadDiskTx(outpoint.hash, tx, txindex);
318 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
320 CTxIndex txindex;
321 return ReadDiskTx(outpoint.hash, tx, txindex);
324 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
326 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
329 bool CTxDB::EraseBlockIndex(uint256 hash)
331 return Erase(make_pair(string("blockindex"), hash));
334 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
336 return Read(string("hashBestChain"), hashBestChain);
339 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
341 return Write(string("hashBestChain"), hashBestChain);
344 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
346 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
349 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
351 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
354 CBlockIndex* InsertBlockIndex(uint256 hash)
356 if (hash == 0)
357 return NULL;
359 // Return existing
360 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
361 if (mi != mapBlockIndex.end())
362 return (*mi).second;
364 // Create new
365 CBlockIndex* pindexNew = new CBlockIndex();
366 if (!pindexNew)
367 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
368 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
369 pindexNew->phashBlock = &((*mi).first);
371 return pindexNew;
374 bool CTxDB::LoadBlockIndex()
376 // Get database cursor
377 Dbc* pcursor = GetCursor();
378 if (!pcursor)
379 return false;
381 // Load mapBlockIndex
382 unsigned int fFlags = DB_SET_RANGE;
383 loop
385 // Read next record
386 CDataStream ssKey;
387 if (fFlags == DB_SET_RANGE)
388 ssKey << make_pair(string("blockindex"), uint256(0));
389 CDataStream ssValue;
390 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
391 fFlags = DB_NEXT;
392 if (ret == DB_NOTFOUND)
393 break;
394 else if (ret != 0)
395 return false;
397 // Unserialize
398 string strType;
399 ssKey >> strType;
400 if (strType == "blockindex")
402 CDiskBlockIndex diskindex;
403 ssValue >> diskindex;
405 // Construct block index object
406 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
407 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
408 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
409 pindexNew->nFile = diskindex.nFile;
410 pindexNew->nBlockPos = diskindex.nBlockPos;
411 pindexNew->nHeight = diskindex.nHeight;
412 pindexNew->nVersion = diskindex.nVersion;
413 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
414 pindexNew->nTime = diskindex.nTime;
415 pindexNew->nBits = diskindex.nBits;
416 pindexNew->nNonce = diskindex.nNonce;
418 // Watch for genesis block
419 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
420 pindexGenesisBlock = pindexNew;
422 if (!pindexNew->CheckIndex())
423 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
425 else
427 break;
430 pcursor->close();
432 // Calculate bnChainWork
433 vector<pair<int, CBlockIndex*> > vSortedByHeight;
434 vSortedByHeight.reserve(mapBlockIndex.size());
435 foreach(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
437 CBlockIndex* pindex = item.second;
438 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
440 sort(vSortedByHeight.begin(), vSortedByHeight.end());
441 foreach(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
443 CBlockIndex* pindex = item.second;
444 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
447 // Load hashBestChain pointer to end of best chain
448 if (!ReadHashBestChain(hashBestChain))
450 if (pindexGenesisBlock == NULL)
451 return true;
452 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
454 if (!mapBlockIndex.count(hashBestChain))
455 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
456 pindexBest = mapBlockIndex[hashBestChain];
457 nBestHeight = pindexBest->nHeight;
458 bnBestChainWork = pindexBest->bnChainWork;
459 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
461 // Load bnBestInvalidWork, OK if it doesn't exist
462 ReadBestInvalidWork(bnBestInvalidWork);
464 // Verify blocks in the best chain
465 CBlockIndex* pindexFork = NULL;
466 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
468 if (pindex->nHeight < 74000 && !mapArgs.count("-checkblocks"))
469 break;
470 CBlock block;
471 if (!block.ReadFromDisk(pindex))
472 return error("LoadBlockIndex() : block.ReadFromDisk failed");
473 if (!block.CheckBlock())
475 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
476 pindexFork = pindex->pprev;
479 if (pindexFork)
481 // Reorg back to the fork
482 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
483 CBlock block;
484 if (!block.ReadFromDisk(pindexFork))
485 return error("LoadBlockIndex() : block.ReadFromDisk failed");
486 CTxDB txdb;
487 block.SetBestChain(txdb, pindexFork);
490 return true;
498 // CAddrDB
501 bool CAddrDB::WriteAddress(const CAddress& addr)
503 return Write(make_pair(string("addr"), addr.GetKey()), addr);
506 bool CAddrDB::LoadAddresses()
508 CRITICAL_BLOCK(cs_mapAddresses)
510 // Load user provided addresses
511 CAutoFile filein = fopen((GetDataDir() + "/addr.txt").c_str(), "rt");
512 if (filein)
516 char psz[1000];
517 while (fgets(psz, sizeof(psz), filein))
519 CAddress addr(psz, NODE_NETWORK);
520 addr.nTime = 0; // so it won't relay unless successfully connected
521 if (addr.IsValid())
522 AddAddress(addr);
525 catch (...) { }
528 // Get cursor
529 Dbc* pcursor = GetCursor();
530 if (!pcursor)
531 return false;
533 loop
535 // Read next record
536 CDataStream ssKey;
537 CDataStream ssValue;
538 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
539 if (ret == DB_NOTFOUND)
540 break;
541 else if (ret != 0)
542 return false;
544 // Unserialize
545 string strType;
546 ssKey >> strType;
547 if (strType == "addr")
549 CAddress addr;
550 ssValue >> addr;
551 mapAddresses.insert(make_pair(addr.GetKey(), addr));
554 pcursor->close();
556 printf("Loaded %d addresses\n", mapAddresses.size());
558 // Fix for possible bug that manifests in mapAddresses.count in irc.cpp,
559 // just need to call count here and it doesn't happen there. The bug was the
560 // pack pragma in irc.cpp and has been fixed, but I'm not in a hurry to delete this.
561 mapAddresses.count(vector<unsigned char>(18));
564 return true;
567 bool LoadAddresses()
569 return CAddrDB("cr+").LoadAddresses();
576 // CWalletDB
579 bool CWalletDB::LoadWallet()
581 vchDefaultKey.clear();
582 int nFileVersion = 0;
584 // Modify defaults
585 #ifndef __WXMSW__
586 // Tray icon sometimes disappears on 9.10 karmic koala 64-bit, leaving no way to access the program
587 fMinimizeToTray = false;
588 fMinimizeOnClose = false;
589 #endif
591 //// todo: shouldn't we catch exceptions and try to recover and continue?
592 CRITICAL_BLOCK(cs_mapKeys)
593 CRITICAL_BLOCK(cs_mapWallet)
595 // Get cursor
596 Dbc* pcursor = GetCursor();
597 if (!pcursor)
598 return false;
600 loop
602 // Read next record
603 CDataStream ssKey;
604 CDataStream ssValue;
605 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
606 if (ret == DB_NOTFOUND)
607 break;
608 else if (ret != 0)
609 return false;
611 // Unserialize
612 // Taking advantage of the fact that pair serialization
613 // is just the two items serialized one after the other
614 string strType;
615 ssKey >> strType;
616 if (strType == "name")
618 string strAddress;
619 ssKey >> strAddress;
620 ssValue >> mapAddressBook[strAddress];
622 else if (strType == "tx")
624 uint256 hash;
625 ssKey >> hash;
626 CWalletTx& wtx = mapWallet[hash];
627 ssValue >> wtx;
629 if (wtx.GetHash() != hash)
630 printf("Error in wallet.dat, hash mismatch\n");
632 //// debug print
633 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
634 //printf(" %12I64d %s %s %s\n",
635 // wtx.vout[0].nValue,
636 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
637 // wtx.hashBlock.ToString().substr(0,20).c_str(),
638 // wtx.mapValue["message"].c_str());
640 else if (strType == "key" || strType == "wkey")
642 vector<unsigned char> vchPubKey;
643 ssKey >> vchPubKey;
644 CWalletKey wkey;
645 if (strType == "key")
646 ssValue >> wkey.vchPrivKey;
647 else
648 ssValue >> wkey;
650 mapKeys[vchPubKey] = wkey.vchPrivKey;
651 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
653 else if (strType == "defaultkey")
655 ssValue >> vchDefaultKey;
657 else if (strType == "version")
659 ssValue >> nFileVersion;
660 if (nFileVersion == 10300)
661 nFileVersion = 300;
663 else if (strType == "setting")
665 string strKey;
666 ssKey >> strKey;
668 // Menu state
669 if (strKey == "fGenerateBitcoins") ssValue >> fGenerateBitcoins;
671 // Options
672 if (strKey == "nTransactionFee") ssValue >> nTransactionFee;
673 if (strKey == "addrIncoming") ssValue >> addrIncoming;
674 if (strKey == "fLimitProcessors") ssValue >> fLimitProcessors;
675 if (strKey == "nLimitProcessors") ssValue >> nLimitProcessors;
676 if (strKey == "fMinimizeToTray") ssValue >> fMinimizeToTray;
677 if (strKey == "fMinimizeOnClose") ssValue >> fMinimizeOnClose;
678 if (strKey == "fUseProxy") ssValue >> fUseProxy;
679 if (strKey == "addrProxy") ssValue >> addrProxy;
683 pcursor->close();
686 printf("nFileVersion = %d\n", nFileVersion);
687 printf("fGenerateBitcoins = %d\n", fGenerateBitcoins);
688 printf("nTransactionFee = %"PRI64d"\n", nTransactionFee);
689 printf("addrIncoming = %s\n", addrIncoming.ToString().c_str());
690 printf("fMinimizeToTray = %d\n", fMinimizeToTray);
691 printf("fMinimizeOnClose = %d\n", fMinimizeOnClose);
692 printf("fUseProxy = %d\n", fUseProxy);
693 printf("addrProxy = %s\n", addrProxy.ToString().c_str());
696 // The transaction fee setting won't be needed for many years to come.
697 // Setting it to zero here in case they set it to something in an earlier version.
698 if (nTransactionFee != 0)
700 nTransactionFee = 0;
701 WriteSetting("nTransactionFee", nTransactionFee);
704 // Upgrade
705 if (nFileVersion < VERSION)
707 // Get rid of old debug.log file in current directory
708 if (nFileVersion <= 105 && !pszSetDataDir[0])
709 unlink("debug.log");
711 WriteVersion(VERSION);
714 return true;
717 bool LoadWallet(bool& fFirstRunRet)
719 fFirstRunRet = false;
720 if (!CWalletDB("cr+").LoadWallet())
721 return false;
722 fFirstRunRet = vchDefaultKey.empty();
724 if (mapKeys.count(vchDefaultKey))
726 // Set keyUser
727 keyUser.SetPubKey(vchDefaultKey);
728 keyUser.SetPrivKey(mapKeys[vchDefaultKey]);
730 else
732 // Create new keyUser and set as default key
733 RandAddSeedPerfmon();
734 keyUser.MakeNewKey();
735 if (!AddKey(keyUser))
736 return false;
737 if (!SetAddressBookName(PubKeyToAddress(keyUser.GetPubKey()), "Your Address"))
738 return false;
739 CWalletDB().WriteDefaultKey(keyUser.GetPubKey());
742 CreateThread(ThreadFlushWalletDB, NULL);
743 return true;
746 void ThreadFlushWalletDB(void* parg)
748 static bool fOneThread;
749 if (fOneThread)
750 return;
751 fOneThread = true;
752 if (mapArgs.count("-noflushwallet"))
753 return;
755 unsigned int nLastSeen = nWalletDBUpdated;
756 unsigned int nLastFlushed = nWalletDBUpdated;
757 int64 nLastWalletUpdate = GetTime();
758 while (!fShutdown)
760 Sleep(500);
762 if (nLastSeen != nWalletDBUpdated)
764 nLastSeen = nWalletDBUpdated;
765 nLastWalletUpdate = GetTime();
768 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
770 TRY_CRITICAL_BLOCK(cs_db)
772 // Don't do this if any databases are in use
773 int nRefCount = 0;
774 map<string, int>::iterator mi = mapFileUseCount.begin();
775 while (mi != mapFileUseCount.end())
777 nRefCount += (*mi).second;
778 mi++;
781 if (nRefCount == 0 && !fShutdown)
783 string strFile = "wallet.dat";
784 map<string, int>::iterator mi = mapFileUseCount.find(strFile);
785 if (mi != mapFileUseCount.end())
787 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
788 printf("Flushing wallet.dat\n");
789 nLastFlushed = nWalletDBUpdated;
790 int64 nStart = GetTimeMillis();
792 // Flush wallet.dat so it's self contained
793 CloseDb(strFile);
794 dbenv.txn_checkpoint(0, 0, 0);
795 dbenv.lsn_reset(strFile.c_str(), 0);
797 mapFileUseCount.erase(mi++);
798 printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
806 void BackupWallet(const string& strDest)
808 while (!fShutdown)
810 CRITICAL_BLOCK(cs_db)
812 const string strFile = "wallet.dat";
813 if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
815 // Flush log data to the dat file
816 CloseDb(strFile);
817 dbenv.txn_checkpoint(0, 0, 0);
818 dbenv.lsn_reset(strFile.c_str(), 0);
819 mapFileUseCount.erase(strFile);
821 // Copy wallet.dat
822 filesystem::path pathSrc(GetDataDir() + "/" + strFile);
823 filesystem::path pathDest(strDest);
824 if (filesystem::is_directory(pathDest))
825 pathDest = pathDest / strFile;
826 #if BOOST_VERSION >= 104000
827 filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
828 #else
829 filesystem::copy_file(pathSrc, pathDest);
830 #endif
831 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
833 return;
836 Sleep(100);