1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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.
5 
6 #include <wallet/bdb.h>
7 #include <wallet/db.h>
8 
9 #include <util/strencodings.h>
10 #include <util/translation.h>
11 
12 #include <stdint.h>
13 
14 #ifndef WIN32
15 #include <sys/stat.h>
16 #endif
17 
18 namespace {
19 
20 //! Make sure database has a unique fileid within the environment. If it
21 //! doesn't, throw an error. BDB caches do not work properly when more than one
22 //! open database has the same fileid (values written to one database may show
23 //! up in reads to other databases).
24 //!
25 //! BerkeleyDB generates unique fileids by default
26 //! (https://docs.oracle.com/cd/E17275_01/html/programmer_reference/program_copy.html),
27 //! so bitcoin should never create different databases with the same fileid, but
28 //! this error can be triggered if users manually copy database files.
CheckUniqueFileid(const BerkeleyEnvironment & env,const std::string & filename,Db & db,WalletDatabaseFileId & fileid)29 void CheckUniqueFileid(const BerkeleyEnvironment& env, const std::string& filename, Db& db, WalletDatabaseFileId& fileid)
30 {
31     if (env.IsMock()) return;
32 
33     int ret = db.get_mpf()->get_fileid(fileid.value);
34     if (ret != 0) {
35         throw std::runtime_error(strprintf("BerkeleyDatabase: Can't open database %s (get_fileid failed with %d)", filename, ret));
36     }
37 
38     for (const auto& item : env.m_fileids) {
39         if (fileid == item.second && &fileid != &item.second) {
40             throw std::runtime_error(strprintf("BerkeleyDatabase: Can't open database %s (duplicates fileid %s from %s)", filename,
41                 HexStr(item.second.value), item.first));
42         }
43     }
44 }
45 
46 RecursiveMutex cs_db;
47 std::map<std::string, std::weak_ptr<BerkeleyEnvironment>> g_dbenvs GUARDED_BY(cs_db); //!< Map from directory name to db environment.
48 } // namespace
49 
operator ==(const WalletDatabaseFileId & rhs) const50 bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId& rhs) const
51 {
52     return memcmp(value, &rhs.value, sizeof(value)) == 0;
53 }
54 
55 /**
56  * @param[in] env_directory Path to environment directory
57  * @return A shared pointer to the BerkeleyEnvironment object for the wallet directory, never empty because ~BerkeleyEnvironment
58  * erases the weak pointer from the g_dbenvs map.
59  * @post A new BerkeleyEnvironment weak pointer is inserted into g_dbenvs if the directory path key was not already in the map.
60  */
GetBerkeleyEnv(const fs::path & env_directory)61 std::shared_ptr<BerkeleyEnvironment> GetBerkeleyEnv(const fs::path& env_directory)
62 {
63     LOCK(cs_db);
64     auto inserted = g_dbenvs.emplace(env_directory.string(), std::weak_ptr<BerkeleyEnvironment>());
65     if (inserted.second) {
66         auto env = std::make_shared<BerkeleyEnvironment>(env_directory.string());
67         inserted.first->second = env;
68         return env;
69     }
70     return inserted.first->second.lock();
71 }
72 
73 //
74 // BerkeleyBatch
75 //
76 
Close()77 void BerkeleyEnvironment::Close()
78 {
79     if (!fDbEnvInit)
80         return;
81 
82     fDbEnvInit = false;
83 
84     for (auto& db : m_databases) {
85         BerkeleyDatabase& database = db.second.get();
86         assert(database.m_refcount <= 0);
87         if (database.m_db) {
88             database.m_db->close(0);
89             database.m_db.reset();
90         }
91     }
92 
93     FILE* error_file = nullptr;
94     dbenv->get_errfile(&error_file);
95 
96     int ret = dbenv->close(0);
97     if (ret != 0)
98         LogPrintf("BerkeleyEnvironment::Close: Error %d closing database environment: %s\n", ret, DbEnv::strerror(ret));
99     if (!fMockDb)
100         DbEnv((u_int32_t)0).remove(strPath.c_str(), 0);
101 
102     if (error_file) fclose(error_file);
103 
104     UnlockDirectory(strPath, ".walletlock");
105 }
106 
Reset()107 void BerkeleyEnvironment::Reset()
108 {
109     dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS));
110     fDbEnvInit = false;
111     fMockDb = false;
112 }
113 
BerkeleyEnvironment(const fs::path & dir_path)114 BerkeleyEnvironment::BerkeleyEnvironment(const fs::path& dir_path) : strPath(dir_path.string())
115 {
116     Reset();
117 }
118 
~BerkeleyEnvironment()119 BerkeleyEnvironment::~BerkeleyEnvironment()
120 {
121     LOCK(cs_db);
122     g_dbenvs.erase(strPath);
123     Close();
124 }
125 
Open(bilingual_str & err)126 bool BerkeleyEnvironment::Open(bilingual_str& err)
127 {
128     if (fDbEnvInit) {
129         return true;
130     }
131 
132     fs::path pathIn = strPath;
133     TryCreateDirectories(pathIn);
134     if (!LockDirectory(pathIn, ".walletlock")) {
135         LogPrintf("Cannot obtain a lock on wallet directory %s. Another instance of bitcoin may be using it.\n", strPath);
136         err = strprintf(_("Error initializing wallet database environment %s!"), Directory());
137         return false;
138     }
139 
140     fs::path pathLogDir = pathIn / "database";
141     TryCreateDirectories(pathLogDir);
142     fs::path pathErrorFile = pathIn / "db.log";
143     LogPrintf("BerkeleyEnvironment::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
144 
145     unsigned int nEnvFlags = 0;
146     if (gArgs.GetBoolArg("-privdb", DEFAULT_WALLET_PRIVDB))
147         nEnvFlags |= DB_PRIVATE;
148 
149     dbenv->set_lg_dir(pathLogDir.string().c_str());
150     dbenv->set_cachesize(0, 0x100000, 1); // 1 MiB should be enough for just the wallet
151     dbenv->set_lg_bsize(0x10000);
152     dbenv->set_lg_max(1048576);
153     dbenv->set_lk_max_locks(40000);
154     dbenv->set_lk_max_objects(40000);
155     dbenv->set_errfile(fsbridge::fopen(pathErrorFile, "a")); /// debug
156     dbenv->set_flags(DB_AUTO_COMMIT, 1);
157     dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
158     dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);
159     int ret = dbenv->open(strPath.c_str(),
160                          DB_CREATE |
161                              DB_INIT_LOCK |
162                              DB_INIT_LOG |
163                              DB_INIT_MPOOL |
164                              DB_INIT_TXN |
165                              DB_THREAD |
166                              DB_RECOVER |
167                              nEnvFlags,
168                          S_IRUSR | S_IWUSR);
169     if (ret != 0) {
170         LogPrintf("BerkeleyEnvironment::Open: Error %d opening database environment: %s\n", ret, DbEnv::strerror(ret));
171         int ret2 = dbenv->close(0);
172         if (ret2 != 0) {
173             LogPrintf("BerkeleyEnvironment::Open: Error %d closing failed database environment: %s\n", ret2, DbEnv::strerror(ret2));
174         }
175         Reset();
176         err = strprintf(_("Error initializing wallet database environment %s!"), Directory());
177         if (ret == DB_RUNRECOVERY) {
178             err += Untranslated(" ") + _("This error could occur if this wallet was not shutdown cleanly and was last loaded using a build with a newer version of Berkeley DB. If so, please use the software that last loaded this wallet");
179         }
180         return false;
181     }
182 
183     fDbEnvInit = true;
184     fMockDb = false;
185     return true;
186 }
187 
188 //! Construct an in-memory mock Berkeley environment for testing
BerkeleyEnvironment()189 BerkeleyEnvironment::BerkeleyEnvironment()
190 {
191     Reset();
192 
193     LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::MakeMock\n");
194 
195     dbenv->set_cachesize(1, 0, 1);
196     dbenv->set_lg_bsize(10485760 * 4);
197     dbenv->set_lg_max(10485760);
198     dbenv->set_lk_max_locks(10000);
199     dbenv->set_lk_max_objects(10000);
200     dbenv->set_flags(DB_AUTO_COMMIT, 1);
201     dbenv->log_set_config(DB_LOG_IN_MEMORY, 1);
202     int ret = dbenv->open(nullptr,
203                          DB_CREATE |
204                              DB_INIT_LOCK |
205                              DB_INIT_LOG |
206                              DB_INIT_MPOOL |
207                              DB_INIT_TXN |
208                              DB_THREAD |
209                              DB_PRIVATE,
210                          S_IRUSR | S_IWUSR);
211     if (ret > 0) {
212         throw std::runtime_error(strprintf("BerkeleyEnvironment::MakeMock: Error %d opening database environment.", ret));
213     }
214 
215     fDbEnvInit = true;
216     fMockDb = true;
217 }
218 
SafeDbt()219 BerkeleyBatch::SafeDbt::SafeDbt()
220 {
221     m_dbt.set_flags(DB_DBT_MALLOC);
222 }
223 
SafeDbt(void * data,size_t size)224 BerkeleyBatch::SafeDbt::SafeDbt(void* data, size_t size)
225     : m_dbt(data, size)
226 {
227 }
228 
~SafeDbt()229 BerkeleyBatch::SafeDbt::~SafeDbt()
230 {
231     if (m_dbt.get_data() != nullptr) {
232         // Clear memory, e.g. in case it was a private key
233         memory_cleanse(m_dbt.get_data(), m_dbt.get_size());
234         // under DB_DBT_MALLOC, data is malloced by the Dbt, but must be
235         // freed by the caller.
236         // https://docs.oracle.com/cd/E17275_01/html/api_reference/C/dbt.html
237         if (m_dbt.get_flags() & DB_DBT_MALLOC) {
238             free(m_dbt.get_data());
239         }
240     }
241 }
242 
get_data() const243 const void* BerkeleyBatch::SafeDbt::get_data() const
244 {
245     return m_dbt.get_data();
246 }
247 
get_size() const248 u_int32_t BerkeleyBatch::SafeDbt::get_size() const
249 {
250     return m_dbt.get_size();
251 }
252 
operator Dbt*()253 BerkeleyBatch::SafeDbt::operator Dbt*()
254 {
255     return &m_dbt;
256 }
257 
Verify(bilingual_str & errorStr)258 bool BerkeleyDatabase::Verify(bilingual_str& errorStr)
259 {
260     fs::path walletDir = env->Directory();
261     fs::path file_path = walletDir / strFile;
262 
263     LogPrintf("Using BerkeleyDB version %s\n", BerkeleyDatabaseVersion());
264     LogPrintf("Using wallet %s\n", file_path.string());
265 
266     if (!env->Open(errorStr)) {
267         return false;
268     }
269 
270     if (fs::exists(file_path))
271     {
272         assert(m_refcount == 0);
273 
274         Db db(env->dbenv.get(), 0);
275         int result = db.verify(strFile.c_str(), nullptr, nullptr, 0);
276         if (result != 0) {
277             errorStr = strprintf(_("%s corrupt. Try using the wallet tool bitcoin-wallet to salvage or restoring a backup."), file_path);
278             return false;
279         }
280     }
281     // also return true if files does not exists
282     return true;
283 }
284 
CheckpointLSN(const std::string & strFile)285 void BerkeleyEnvironment::CheckpointLSN(const std::string& strFile)
286 {
287     dbenv->txn_checkpoint(0, 0, 0);
288     if (fMockDb)
289         return;
290     dbenv->lsn_reset(strFile.c_str(), 0);
291 }
292 
~BerkeleyDatabase()293 BerkeleyDatabase::~BerkeleyDatabase()
294 {
295     if (env) {
296         LOCK(cs_db);
297         env->CloseDb(strFile);
298         assert(!m_db);
299         size_t erased = env->m_databases.erase(strFile);
300         assert(erased == 1);
301         env->m_fileids.erase(strFile);
302     }
303 }
304 
BerkeleyBatch(BerkeleyDatabase & database,const bool read_only,bool fFlushOnCloseIn)305 BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const bool read_only, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database)
306 {
307     database.AddRef();
308     database.Open();
309     fReadOnly = read_only;
310     fFlushOnClose = fFlushOnCloseIn;
311     env = database.env.get();
312     pdb = database.m_db.get();
313     strFile = database.strFile;
314     if (!Exists(std::string("version"))) {
315         bool fTmp = fReadOnly;
316         fReadOnly = false;
317         Write(std::string("version"), CLIENT_VERSION);
318         fReadOnly = fTmp;
319     }
320 }
321 
Open()322 void BerkeleyDatabase::Open()
323 {
324     unsigned int nFlags = DB_THREAD | DB_CREATE;
325 
326     {
327         LOCK(cs_db);
328         bilingual_str open_err;
329         if (!env->Open(open_err))
330             throw std::runtime_error("BerkeleyDatabase: Failed to open database environment.");
331 
332         if (m_db == nullptr) {
333             int ret;
334             std::unique_ptr<Db> pdb_temp = std::make_unique<Db>(env->dbenv.get(), 0);
335 
336             bool fMockDb = env->IsMock();
337             if (fMockDb) {
338                 DbMpoolFile* mpf = pdb_temp->get_mpf();
339                 ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
340                 if (ret != 0) {
341                     throw std::runtime_error(strprintf("BerkeleyDatabase: Failed to configure for no temp file backing for database %s", strFile));
342                 }
343             }
344 
345             ret = pdb_temp->open(nullptr,                             // Txn pointer
346                             fMockDb ? nullptr : strFile.c_str(),      // Filename
347                             fMockDb ? strFile.c_str() : "main",       // Logical db name
348                             DB_BTREE,                                 // Database type
349                             nFlags,                                   // Flags
350                             0);
351 
352             if (ret != 0) {
353                 throw std::runtime_error(strprintf("BerkeleyDatabase: Error %d, can't open database %s", ret, strFile));
354             }
355 
356             // Call CheckUniqueFileid on the containing BDB environment to
357             // avoid BDB data consistency bugs that happen when different data
358             // files in the same environment have the same fileid.
359             CheckUniqueFileid(*env, strFile, *pdb_temp, this->env->m_fileids[strFile]);
360 
361             m_db.reset(pdb_temp.release());
362 
363         }
364     }
365 }
366 
Flush()367 void BerkeleyBatch::Flush()
368 {
369     if (activeTxn)
370         return;
371 
372     // Flush database activity from memory pool to disk log
373     unsigned int nMinutes = 0;
374     if (fReadOnly)
375         nMinutes = 1;
376 
377     if (env) { // env is nullptr for dummy databases (i.e. in tests). Don't actually flush if env is nullptr so we don't segfault
378         env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0);
379     }
380 }
381 
IncrementUpdateCounter()382 void BerkeleyDatabase::IncrementUpdateCounter()
383 {
384     ++nUpdateCounter;
385 }
386 
~BerkeleyBatch()387 BerkeleyBatch::~BerkeleyBatch()
388 {
389     Close();
390     m_database.RemoveRef();
391 }
392 
Close()393 void BerkeleyBatch::Close()
394 {
395     if (!pdb)
396         return;
397     if (activeTxn)
398         activeTxn->abort();
399     activeTxn = nullptr;
400     pdb = nullptr;
401     CloseCursor();
402 
403     if (fFlushOnClose)
404         Flush();
405 }
406 
CloseDb(const std::string & strFile)407 void BerkeleyEnvironment::CloseDb(const std::string& strFile)
408 {
409     {
410         LOCK(cs_db);
411         auto it = m_databases.find(strFile);
412         assert(it != m_databases.end());
413         BerkeleyDatabase& database = it->second.get();
414         if (database.m_db) {
415             // Close the database handle
416             database.m_db->close(0);
417             database.m_db.reset();
418         }
419     }
420 }
421 
ReloadDbEnv()422 void BerkeleyEnvironment::ReloadDbEnv()
423 {
424     // Make sure that no Db's are in use
425     AssertLockNotHeld(cs_db);
426     std::unique_lock<RecursiveMutex> lock(cs_db);
427     m_db_in_use.wait(lock, [this](){
428         for (auto& db : m_databases) {
429             if (db.second.get().m_refcount > 0) return false;
430         }
431         return true;
432     });
433 
434     std::vector<std::string> filenames;
435     for (auto it : m_databases) {
436         filenames.push_back(it.first);
437     }
438     // Close the individual Db's
439     for (const std::string& filename : filenames) {
440         CloseDb(filename);
441     }
442     // Reset the environment
443     Flush(true); // This will flush and close the environment
444     Reset();
445     bilingual_str open_err;
446     Open(open_err);
447 }
448 
Rewrite(const char * pszSkip)449 bool BerkeleyDatabase::Rewrite(const char* pszSkip)
450 {
451     while (true) {
452         {
453             LOCK(cs_db);
454             if (m_refcount <= 0) {
455                 // Flush log data to the dat file
456                 env->CloseDb(strFile);
457                 env->CheckpointLSN(strFile);
458                 m_refcount = -1;
459 
460                 bool fSuccess = true;
461                 LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile);
462                 std::string strFileRes = strFile + ".rewrite";
463                 { // surround usage of db with extra {}
464                     BerkeleyBatch db(*this, true);
465                     std::unique_ptr<Db> pdbCopy = std::make_unique<Db>(env->dbenv.get(), 0);
466 
467                     int ret = pdbCopy->open(nullptr,               // Txn pointer
468                                             strFileRes.c_str(), // Filename
469                                             "main",             // Logical db name
470                                             DB_BTREE,           // Database type
471                                             DB_CREATE,          // Flags
472                                             0);
473                     if (ret > 0) {
474                         LogPrintf("BerkeleyBatch::Rewrite: Can't create database file %s\n", strFileRes);
475                         fSuccess = false;
476                     }
477 
478                     if (db.StartCursor()) {
479                         while (fSuccess) {
480                             CDataStream ssKey(SER_DISK, CLIENT_VERSION);
481                             CDataStream ssValue(SER_DISK, CLIENT_VERSION);
482                             bool complete;
483                             bool ret1 = db.ReadAtCursor(ssKey, ssValue, complete);
484                             if (complete) {
485                                 break;
486                             } else if (!ret1) {
487                                 fSuccess = false;
488                                 break;
489                             }
490                             if (pszSkip &&
491                                 strncmp((const char*)ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
492                                 continue;
493                             if (strncmp((const char*)ssKey.data(), "\x07version", 8) == 0) {
494                                 // Update version:
495                                 ssValue.clear();
496                                 ssValue << CLIENT_VERSION;
497                             }
498                             Dbt datKey(ssKey.data(), ssKey.size());
499                             Dbt datValue(ssValue.data(), ssValue.size());
500                             int ret2 = pdbCopy->put(nullptr, &datKey, &datValue, DB_NOOVERWRITE);
501                             if (ret2 > 0)
502                                 fSuccess = false;
503                         }
504                         db.CloseCursor();
505                     }
506                     if (fSuccess) {
507                         db.Close();
508                         env->CloseDb(strFile);
509                         if (pdbCopy->close(0))
510                             fSuccess = false;
511                     } else {
512                         pdbCopy->close(0);
513                     }
514                 }
515                 if (fSuccess) {
516                     Db dbA(env->dbenv.get(), 0);
517                     if (dbA.remove(strFile.c_str(), nullptr, 0))
518                         fSuccess = false;
519                     Db dbB(env->dbenv.get(), 0);
520                     if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0))
521                         fSuccess = false;
522                 }
523                 if (!fSuccess)
524                     LogPrintf("BerkeleyBatch::Rewrite: Failed to rewrite database file %s\n", strFileRes);
525                 return fSuccess;
526             }
527         }
528         UninterruptibleSleep(std::chrono::milliseconds{100});
529     }
530 }
531 
532 
Flush(bool fShutdown)533 void BerkeleyEnvironment::Flush(bool fShutdown)
534 {
535     int64_t nStart = GetTimeMillis();
536     // Flush log data to the actual data file on all files that are not in use
537     LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: [%s] Flush(%s)%s\n", strPath, fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started");
538     if (!fDbEnvInit)
539         return;
540     {
541         LOCK(cs_db);
542         bool no_dbs_accessed = true;
543         for (auto& db_it : m_databases) {
544             std::string strFile = db_it.first;
545             int nRefCount = db_it.second.get().m_refcount;
546             if (nRefCount < 0) continue;
547             LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: Flushing %s (refcount = %d)...\n", strFile, nRefCount);
548             if (nRefCount == 0) {
549                 // Move log data to the dat file
550                 CloseDb(strFile);
551                 LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s checkpoint\n", strFile);
552                 dbenv->txn_checkpoint(0, 0, 0);
553                 LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s detach\n", strFile);
554                 if (!fMockDb)
555                     dbenv->lsn_reset(strFile.c_str(), 0);
556                 LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s closed\n", strFile);
557                 nRefCount = -1;
558             } else {
559                 no_dbs_accessed = false;
560             }
561         }
562         LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart);
563         if (fShutdown) {
564             char** listp;
565             if (no_dbs_accessed) {
566                 dbenv->log_archive(&listp, DB_ARCH_REMOVE);
567                 Close();
568                 if (!fMockDb) {
569                     fs::remove_all(fs::path(strPath) / "database");
570                 }
571             }
572         }
573     }
574 }
575 
PeriodicFlush()576 bool BerkeleyDatabase::PeriodicFlush()
577 {
578     // Don't flush if we can't acquire the lock.
579     TRY_LOCK(cs_db, lockDb);
580     if (!lockDb) return false;
581 
582     // Don't flush if any databases are in use
583     for (auto& it : env->m_databases) {
584         if (it.second.get().m_refcount > 0) return false;
585     }
586 
587     // Don't flush if there haven't been any batch writes for this database.
588     if (m_refcount < 0) return false;
589 
590     LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile);
591     int64_t nStart = GetTimeMillis();
592 
593     // Flush wallet file so it's self contained
594     env->CloseDb(strFile);
595     env->CheckpointLSN(strFile);
596     m_refcount = -1;
597 
598     LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
599 
600     return true;
601 }
602 
Backup(const std::string & strDest) const603 bool BerkeleyDatabase::Backup(const std::string& strDest) const
604 {
605     while (true)
606     {
607         {
608             LOCK(cs_db);
609             if (m_refcount <= 0)
610             {
611                 // Flush log data to the dat file
612                 env->CloseDb(strFile);
613                 env->CheckpointLSN(strFile);
614 
615                 // Copy wallet file
616                 fs::path pathSrc = env->Directory() / strFile;
617                 fs::path pathDest(strDest);
618                 if (fs::is_directory(pathDest))
619                     pathDest /= strFile;
620 
621                 try {
622                     if (fs::equivalent(pathSrc, pathDest)) {
623                         LogPrintf("cannot backup to wallet source file %s\n", pathDest.string());
624                         return false;
625                     }
626 
627                     fs::copy_file(pathSrc, pathDest, fs::copy_option::overwrite_if_exists);
628                     LogPrintf("copied %s to %s\n", strFile, pathDest.string());
629                     return true;
630                 } catch (const fs::filesystem_error& e) {
631                     LogPrintf("error copying %s to %s - %s\n", strFile, pathDest.string(), fsbridge::get_filesystem_error_message(e));
632                     return false;
633                 }
634             }
635         }
636         UninterruptibleSleep(std::chrono::milliseconds{100});
637     }
638 }
639 
Flush()640 void BerkeleyDatabase::Flush()
641 {
642     env->Flush(false);
643 }
644 
Close()645 void BerkeleyDatabase::Close()
646 {
647     env->Flush(true);
648 }
649 
ReloadDbEnv()650 void BerkeleyDatabase::ReloadDbEnv()
651 {
652     env->ReloadDbEnv();
653 }
654 
StartCursor()655 bool BerkeleyBatch::StartCursor()
656 {
657     assert(!m_cursor);
658     if (!pdb)
659         return false;
660     int ret = pdb->cursor(nullptr, &m_cursor, 0);
661     return ret == 0;
662 }
663 
ReadAtCursor(CDataStream & ssKey,CDataStream & ssValue,bool & complete)664 bool BerkeleyBatch::ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete)
665 {
666     complete = false;
667     if (m_cursor == nullptr) return false;
668     // Read at cursor
669     SafeDbt datKey;
670     SafeDbt datValue;
671     int ret = m_cursor->get(datKey, datValue, DB_NEXT);
672     if (ret == DB_NOTFOUND) {
673         complete = true;
674     }
675     if (ret != 0)
676         return false;
677     else if (datKey.get_data() == nullptr || datValue.get_data() == nullptr)
678         return false;
679 
680     // Convert to streams
681     ssKey.SetType(SER_DISK);
682     ssKey.clear();
683     ssKey.write((char*)datKey.get_data(), datKey.get_size());
684     ssValue.SetType(SER_DISK);
685     ssValue.clear();
686     ssValue.write((char*)datValue.get_data(), datValue.get_size());
687     return true;
688 }
689 
CloseCursor()690 void BerkeleyBatch::CloseCursor()
691 {
692     if (!m_cursor) return;
693     m_cursor->close();
694     m_cursor = nullptr;
695 }
696 
TxnBegin()697 bool BerkeleyBatch::TxnBegin()
698 {
699     if (!pdb || activeTxn)
700         return false;
701     DbTxn* ptxn = env->TxnBegin();
702     if (!ptxn)
703         return false;
704     activeTxn = ptxn;
705     return true;
706 }
707 
TxnCommit()708 bool BerkeleyBatch::TxnCommit()
709 {
710     if (!pdb || !activeTxn)
711         return false;
712     int ret = activeTxn->commit(0);
713     activeTxn = nullptr;
714     return (ret == 0);
715 }
716 
TxnAbort()717 bool BerkeleyBatch::TxnAbort()
718 {
719     if (!pdb || !activeTxn)
720         return false;
721     int ret = activeTxn->abort();
722     activeTxn = nullptr;
723     return (ret == 0);
724 }
725 
BerkeleyDatabaseSanityCheck()726 bool BerkeleyDatabaseSanityCheck()
727 {
728     int major, minor;
729     DbEnv::version(&major, &minor, nullptr);
730 
731     /* If the major version differs, or the minor version of library is *older*
732      * than the header that was compiled against, flag an error.
733      */
734     if (major != DB_VERSION_MAJOR || minor < DB_VERSION_MINOR) {
735         LogPrintf("BerkeleyDB database version conflict: header version is %d.%d, library version is %d.%d\n",
736             DB_VERSION_MAJOR, DB_VERSION_MINOR, major, minor);
737         return false;
738     }
739 
740     return true;
741 }
742 
BerkeleyDatabaseVersion()743 std::string BerkeleyDatabaseVersion()
744 {
745     return DbEnv::version(nullptr, nullptr, nullptr);
746 }
747 
ReadKey(CDataStream && key,CDataStream & value)748 bool BerkeleyBatch::ReadKey(CDataStream&& key, CDataStream& value)
749 {
750     if (!pdb)
751         return false;
752 
753     SafeDbt datKey(key.data(), key.size());
754 
755     SafeDbt datValue;
756     int ret = pdb->get(activeTxn, datKey, datValue, 0);
757     if (ret == 0 && datValue.get_data() != nullptr) {
758         value.write((char*)datValue.get_data(), datValue.get_size());
759         return true;
760     }
761     return false;
762 }
763 
WriteKey(CDataStream && key,CDataStream && value,bool overwrite)764 bool BerkeleyBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite)
765 {
766     if (!pdb)
767         return false;
768     if (fReadOnly)
769         assert(!"Write called on database in read-only mode");
770 
771     SafeDbt datKey(key.data(), key.size());
772 
773     SafeDbt datValue(value.data(), value.size());
774 
775     int ret = pdb->put(activeTxn, datKey, datValue, (overwrite ? 0 : DB_NOOVERWRITE));
776     return (ret == 0);
777 }
778 
EraseKey(CDataStream && key)779 bool BerkeleyBatch::EraseKey(CDataStream&& key)
780 {
781     if (!pdb)
782         return false;
783     if (fReadOnly)
784         assert(!"Erase called on database in read-only mode");
785 
786     SafeDbt datKey(key.data(), key.size());
787 
788     int ret = pdb->del(activeTxn, datKey, 0);
789     return (ret == 0 || ret == DB_NOTFOUND);
790 }
791 
HasKey(CDataStream && key)792 bool BerkeleyBatch::HasKey(CDataStream&& key)
793 {
794     if (!pdb)
795         return false;
796 
797     SafeDbt datKey(key.data(), key.size());
798 
799     int ret = pdb->exists(activeTxn, datKey, 0);
800     return ret == 0;
801 }
802 
AddRef()803 void BerkeleyDatabase::AddRef()
804 {
805     LOCK(cs_db);
806     if (m_refcount < 0) {
807         m_refcount = 1;
808     } else {
809         m_refcount++;
810     }
811 }
812 
RemoveRef()813 void BerkeleyDatabase::RemoveRef()
814 {
815     LOCK(cs_db);
816     m_refcount--;
817     if (env) env->m_db_in_use.notify_all();
818 }
819 
MakeBatch(bool flush_on_close)820 std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(bool flush_on_close)
821 {
822     return std::make_unique<BerkeleyBatch>(*this, false, flush_on_close);
823 }
824 
MakeBerkeleyDatabase(const fs::path & path,const DatabaseOptions & options,DatabaseStatus & status,bilingual_str & error)825 std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
826 {
827     fs::path data_file = BDBDataFile(path);
828     std::unique_ptr<BerkeleyDatabase> db;
829     {
830         LOCK(cs_db); // Lock env.m_databases until insert in BerkeleyDatabase constructor
831         std::string data_filename = data_file.filename().string();
832         std::shared_ptr<BerkeleyEnvironment> env = GetBerkeleyEnv(data_file.parent_path());
833         if (env->m_databases.count(data_filename)) {
834             error = Untranslated(strprintf("Refusing to load database. Data file '%s' is already loaded.", (env->Directory() / data_filename).string()));
835             status = DatabaseStatus::FAILED_ALREADY_LOADED;
836             return nullptr;
837         }
838         db = std::make_unique<BerkeleyDatabase>(std::move(env), std::move(data_filename));
839     }
840 
841     if (options.verify && !db->Verify(error)) {
842         status = DatabaseStatus::FAILED_VERIFY;
843         return nullptr;
844     }
845 
846     status = DatabaseStatus::SUCCESS;
847     return db;
848 }
849