Skip to content

Commit d70dc89

Browse files
committed
refactor: Consolidate redundant wallet database path and exists functions
No change in behavior. Just remove a little bit of code, reduce macro usage, remove duplicative functions, and make BDB and SQLite implementations more consistent with each other.
1 parent 6a7a636 commit d70dc89

File tree

8 files changed

+38
-54
lines changed

8 files changed

+38
-54
lines changed

src/wallet/bdb.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,13 @@ bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId& rhs) const
5353
}
5454

5555
/**
56-
* @param[in] wallet_path Path to wallet directory. Or (for backwards compatibility only) a path to a berkeley btree data file inside a wallet directory.
57-
* @param[out] database_filename Filename of berkeley btree data file inside the wallet directory.
56+
* @param[in] env_directory Path to environment directory
5857
* @return A shared pointer to the BerkeleyEnvironment object for the wallet directory, never empty because ~BerkeleyEnvironment
5958
* erases the weak pointer from the g_dbenvs map.
6059
* @post A new BerkeleyEnvironment weak pointer is inserted into g_dbenvs if the directory path key was not already in the map.
6160
*/
62-
std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& wallet_path, std::string& database_filename)
61+
std::shared_ptr<BerkeleyEnvironment> GetBerkeleyEnv(const fs::path& env_directory)
6362
{
64-
fs::path env_directory;
65-
SplitWalletPath(wallet_path, env_directory, database_filename);
6663
LOCK(cs_db);
6764
auto inserted = g_dbenvs.emplace(env_directory.string(), std::weak_ptr<BerkeleyEnvironment>());
6865
if (inserted.second) {
@@ -808,21 +805,14 @@ std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(bool flush_on_close)
808805
return MakeUnique<BerkeleyBatch>(*this, false, flush_on_close);
809806
}
810807

811-
bool ExistsBerkeleyDatabase(const fs::path& path)
812-
{
813-
fs::path env_directory;
814-
std::string data_filename;
815-
SplitWalletPath(path, env_directory, data_filename);
816-
return IsBDBFile(env_directory / data_filename);
817-
}
818-
819808
std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
820809
{
810+
fs::path data_file = BDBDataFile(path);
821811
std::unique_ptr<BerkeleyDatabase> db;
822812
{
823813
LOCK(cs_db); // Lock env.m_databases until insert in BerkeleyDatabase constructor
824-
std::string data_filename;
825-
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(path, data_filename);
814+
std::string data_filename = data_file.filename().string();
815+
std::shared_ptr<BerkeleyEnvironment> env = GetBerkeleyEnv(data_file.parent_path());
826816
if (env->m_databases.count(data_filename)) {
827817
error = Untranslated(strprintf("Refusing to load database. Data file '%s' is already loaded.", (env->Directory() / data_filename).string()));
828818
status = DatabaseStatus::FAILED_ALREADY_LOADED;

src/wallet/bdb.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class BerkeleyEnvironment
8383
}
8484
};
8585

86-
/** Get BerkeleyEnvironment and database filename given a wallet path. */
87-
std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);
86+
/** Get BerkeleyEnvironment given a directory path. */
87+
std::shared_ptr<BerkeleyEnvironment> GetBerkeleyEnv(const fs::path& env_directory);
8888

8989
class BerkeleyBatch;
9090

@@ -223,9 +223,6 @@ class BerkeleyBatch : public DatabaseBatch
223223

224224
std::string BerkeleyDatabaseVersion();
225225

226-
//! Check if Berkeley database exists at specified path.
227-
bool ExistsBerkeleyDatabase(const fs::path& path);
228-
229226
//! Return object giving access to Berkeley database at specified path.
230227
std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
231228

src/wallet/db.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@
1010

1111
#include <string>
1212

13-
#ifdef USE_BDB
14-
bool ExistsBerkeleyDatabase(const fs::path& path);
15-
#else
16-
# define ExistsBerkeleyDatabase(path) (false)
17-
#endif
18-
#ifdef USE_SQLITE
19-
bool ExistsSQLiteDatabase(const fs::path& path);
20-
#else
21-
# define ExistsSQLiteDatabase(path) (false)
22-
#endif
23-
2413
std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
2514
{
2615
const size_t offset = wallet_dir.string().size() + 1;
@@ -39,10 +28,10 @@ std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
3928
const fs::path path = it->path().string().substr(offset);
4029

4130
if (it->status().type() == fs::directory_file &&
42-
(ExistsBerkeleyDatabase(it->path()) || ExistsSQLiteDatabase(it->path()))) {
31+
(IsBDBFile(BDBDataFile(it->path())) || IsSQLiteFile(SQLiteDataFile(it->path())))) {
4332
// Found a directory which contains wallet.dat btree file, add it as a wallet.
4433
paths.emplace_back(path);
45-
} else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && ExistsBerkeleyDatabase(it->path())) {
34+
} else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && IsBDBFile(it->path())) {
4635
if (it->path().filename() == "wallet.dat") {
4736
// Found top-level wallet.dat btree file, add top level directory ""
4837
// as a wallet.
@@ -64,24 +53,31 @@ std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
6453
return paths;
6554
}
6655

67-
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename)
56+
fs::path BDBDataFile(const fs::path& wallet_path)
6857
{
6958
if (fs::is_regular_file(wallet_path)) {
7059
// Special case for backwards compatibility: if wallet path points to an
7160
// existing file, treat it as the path to a BDB data file in a parent
7261
// directory that also contains BDB log files.
73-
env_directory = wallet_path.parent_path();
74-
database_filename = wallet_path.filename().string();
62+
return wallet_path;
7563
} else {
7664
// Normal case: Interpret wallet path as a directory path containing
7765
// data and log files.
78-
env_directory = wallet_path;
79-
database_filename = "wallet.dat";
66+
return wallet_path / "wallet.dat";
8067
}
8168
}
8269

70+
fs::path SQLiteDataFile(const fs::path& path)
71+
{
72+
return path / "wallet.dat";
73+
}
74+
8375
bool IsBDBFile(const fs::path& path)
8476
{
77+
#ifndef USE_BDB
78+
return false;
79+
#endif
80+
8581
if (!fs::exists(path)) return false;
8682

8783
// A Berkeley DB Btree file has at least 4K.
@@ -107,6 +103,10 @@ bool IsBDBFile(const fs::path& path)
107103

108104
bool IsSQLiteFile(const fs::path& path)
109105
{
106+
#ifndef USE_SQLITE
107+
return false;
108+
#endif
109+
110110
if (!fs::exists(path)) return false;
111111

112112
// A SQLite Database file is at least 512 bytes.

src/wallet/db.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ std::vector<fs::path> ListDatabases(const fs::path& path);
228228

229229
std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
230230

231+
fs::path BDBDataFile(const fs::path& path);
232+
fs::path SQLiteDataFile(const fs::path& path);
231233
bool IsBDBFile(const fs::path& path);
232234
bool IsSQLiteFile(const fs::path& path);
233235

src/wallet/sqlite.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <sqlite3.h>
1818
#include <stdint.h>
1919

20-
static const char* const DATABASE_FILENAME = "wallet.dat";
2120
static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
2221

2322
static Mutex g_sqlite_mutex;
@@ -568,17 +567,11 @@ bool SQLiteBatch::TxnAbort()
568567
return res == SQLITE_OK;
569568
}
570569

571-
bool ExistsSQLiteDatabase(const fs::path& path)
572-
{
573-
const fs::path file = path / DATABASE_FILENAME;
574-
return fs::symlink_status(file).type() == fs::regular_file && IsSQLiteFile(file);
575-
}
576-
577570
std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
578571
{
579-
const fs::path file = path / DATABASE_FILENAME;
580572
try {
581-
auto db = MakeUnique<SQLiteDatabase>(path, file);
573+
fs::path data_file = SQLiteDataFile(path);
574+
auto db = MakeUnique<SQLiteDatabase>(data_file.parent_path(), data_file);
582575
if (options.verify && !db->Verify(error)) {
583576
status = DatabaseStatus::FAILED_VERIFY;
584577
return nullptr;

src/wallet/sqlite.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ class SQLiteDatabase : public WalletDatabase
113113
sqlite3* m_db{nullptr};
114114
};
115115

116-
bool ExistsSQLiteDatabase(const fs::path& path);
117116
std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
118117

119118
std::string SQLiteDatabaseVersion();

src/wallet/test/db_tests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414
BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup)
1515

16+
static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& path, std::string& database_filename)
17+
{
18+
fs::path data_file = BDBDataFile(path);
19+
database_filename = data_file.filename().string();
20+
return GetBerkeleyEnv(data_file.parent_path());
21+
}
22+
1623
BOOST_AUTO_TEST_CASE(getwalletenv_file)
1724
{
1825
std::string test_name = "test_name.dat";

src/wallet/walletdb.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,21 +1013,17 @@ std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const Databas
10131013

10141014
Optional<DatabaseFormat> format;
10151015
if (exists) {
1016-
#ifdef USE_BDB
1017-
if (ExistsBerkeleyDatabase(path)) {
1016+
if (IsBDBFile(BDBDataFile(path))) {
10181017
format = DatabaseFormat::BERKELEY;
10191018
}
1020-
#endif
1021-
#ifdef USE_SQLITE
1022-
if (ExistsSQLiteDatabase(path)) {
1019+
if (IsSQLiteFile(SQLiteDataFile(path))) {
10231020
if (format) {
10241021
error = Untranslated(strprintf("Failed to load database path '%s'. Data is in ambiguous format.", path.string()));
10251022
status = DatabaseStatus::FAILED_BAD_FORMAT;
10261023
return nullptr;
10271024
}
10281025
format = DatabaseFormat::SQLITE;
10291026
}
1030-
#endif
10311027
} else if (options.require_existing) {
10321028
error = Untranslated(strprintf("Failed to load database path '%s'. Path does not exist.", path.string()));
10331029
status = DatabaseStatus::FAILED_NOT_FOUND;

0 commit comments

Comments
 (0)