From d70dc89e78ee6355e0bc37cc36cfc04ef7a86885 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Fri, 30 Oct 2020 16:41:23 -0400 Subject: 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. --- src/wallet/db.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/wallet/db.cpp') diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index e8b8b8abc1..425b14b8f9 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -10,17 +10,6 @@ #include -#ifdef USE_BDB -bool ExistsBerkeleyDatabase(const fs::path& path); -#else -# define ExistsBerkeleyDatabase(path) (false) -#endif -#ifdef USE_SQLITE -bool ExistsSQLiteDatabase(const fs::path& path); -#else -# define ExistsSQLiteDatabase(path) (false) -#endif - std::vector ListDatabases(const fs::path& wallet_dir) { const size_t offset = wallet_dir.string().size() + 1; @@ -39,10 +28,10 @@ std::vector ListDatabases(const fs::path& wallet_dir) const fs::path path = it->path().string().substr(offset); if (it->status().type() == fs::directory_file && - (ExistsBerkeleyDatabase(it->path()) || ExistsSQLiteDatabase(it->path()))) { + (IsBDBFile(BDBDataFile(it->path())) || IsSQLiteFile(SQLiteDataFile(it->path())))) { // Found a directory which contains wallet.dat btree file, add it as a wallet. paths.emplace_back(path); - } else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && ExistsBerkeleyDatabase(it->path())) { + } else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && IsBDBFile(it->path())) { if (it->path().filename() == "wallet.dat") { // Found top-level wallet.dat btree file, add top level directory "" // as a wallet. @@ -64,24 +53,31 @@ std::vector ListDatabases(const fs::path& wallet_dir) return paths; } -void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename) +fs::path BDBDataFile(const fs::path& wallet_path) { if (fs::is_regular_file(wallet_path)) { // Special case for backwards compatibility: if wallet path points to an // existing file, treat it as the path to a BDB data file in a parent // directory that also contains BDB log files. - env_directory = wallet_path.parent_path(); - database_filename = wallet_path.filename().string(); + return wallet_path; } else { // Normal case: Interpret wallet path as a directory path containing // data and log files. - env_directory = wallet_path; - database_filename = "wallet.dat"; + return wallet_path / "wallet.dat"; } } +fs::path SQLiteDataFile(const fs::path& path) +{ + return path / "wallet.dat"; +} + bool IsBDBFile(const fs::path& path) { +#ifndef USE_BDB + return false; +#endif + if (!fs::exists(path)) return false; // A Berkeley DB Btree file has at least 4K. @@ -107,6 +103,10 @@ bool IsBDBFile(const fs::path& path) bool IsSQLiteFile(const fs::path& path) { +#ifndef USE_SQLITE + return false; +#endif + if (!fs::exists(path)) return false; // A SQLite Database file is at least 512 bytes. -- cgit v1.2.3