From d9e1c6b56def5a6ea9903c244f70ceae40b9ce0c Mon Sep 17 00:00:00 2001 From: furszy Date: Tue, 15 Jun 2021 18:55:34 -0300 Subject: [PATCH] Abstract VerifyWalletPath and connect it to init and GUI. --- src/qt/pivx.cpp | 15 +++++---------- src/wallet/init.cpp | 20 +++----------------- src/wallet/walletutil.cpp | 22 ++++++++++++++++++++++ src/wallet/walletutil.h | 3 +++ 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/qt/pivx.cpp b/src/qt/pivx.cpp index ccd933a1d4e55..15c38ab1c2bd9 100644 --- a/src/qt/pivx.cpp +++ b/src/qt/pivx.cpp @@ -683,17 +683,12 @@ int main(int argc, char* argv[]) bool ret = true; #ifdef ENABLE_WALLET - // Check if the wallet exists or need to be created - std::string strWalletFile = gArgs.GetArg("-wallet", DEFAULT_WALLET_DAT); - std::string strDataDir = GetDataDir().string(); - // Wallet file must be a plain filename without a directory - fs::path wallet_file_path(strWalletFile); - if (strWalletFile != wallet_file_path.filename().string()) { - throw std::runtime_error(strprintf(_("Wallet %s resides outside data directory %s"), strWalletFile, strDataDir)); - } + // Check if at least one wallet exists or needs to be created + const std::string& strWalletFile = gArgs.GetArg("-wallet", ""); + auto opRes = VerifyWalletPath(strWalletFile); + if (!opRes) throw std::runtime_error(opRes.getError()); - fs::path pathBootstrap = GetDataDir() / strWalletFile; - if (!fs::exists(pathBootstrap)) { + if (!fs::exists(fs::absolute(strWalletFile, GetWalletDir()))) { // wallet doesn't exist, popup tutorial screen. ret = app.createTutorialScreen(); } diff --git a/src/wallet/init.cpp b/src/wallet/init.cpp index 00f3efa45c7a2..c2ac22ec133ed 100644 --- a/src/wallet/init.cpp +++ b/src/wallet/init.cpp @@ -165,24 +165,10 @@ bool WalletVerify() std::set wallet_paths; for (const std::string& walletFile : gArgs.GetArgs("-wallet")) { - // Do some checking on wallet path. It should be either a: - // - // 1. Path where a directory can be created. - // 2. Path to an existing directory. - // 3. Path to a symlink to a directory. - // 4. For backwards compatibility, the name of a data file in -walletdir. - fs::path wallet_path = fs::absolute(walletFile, GetWalletDir()); - fs::file_type path_type = fs::symlink_status(wallet_path).type(); - if (!(path_type == fs::file_not_found || path_type == fs::directory_file || - (path_type == fs::symlink_file && fs::is_directory(wallet_path)) || - (path_type == fs::regular_file && fs::path(walletFile).filename() == walletFile))) { - return UIError(strprintf( - _("Invalid -wallet path '%s'. -wallet path should point to a directory where wallet.dat and " - "database/log.?????????? files can be stored, a location where such a directory could be created " - "or (for backwards compatibility) the name of an existing data file in -walletdir (%s)"), - walletFile, GetWalletDir())); - } + auto opRes = VerifyWalletPath(walletFile); + if (!opRes) return UIError(opRes.getError()); + fs::path wallet_path = fs::absolute(walletFile, GetWalletDir()); if (!wallet_paths.insert(wallet_path).second) { return UIError(strprintf(_("Error loading wallet %s. Duplicate %s filename specified."), walletFile, "-wallet")); } diff --git a/src/wallet/walletutil.cpp b/src/wallet/walletutil.cpp index 6bccc7182d985..2d48453f15ac5 100644 --- a/src/wallet/walletutil.cpp +++ b/src/wallet/walletutil.cpp @@ -26,3 +26,25 @@ fs::path GetWalletDir() return path; } + +OperationResult VerifyWalletPath(const std::string& walletFile) +{ + // Do some checking on wallet path. It should be either a: + // + // 1. Path where a directory can be created. + // 2. Path to an existing directory. + // 3. Path to a symlink to a directory. + // 4. For backwards compatibility, the name of a data file in -walletdir. + fs::path wallet_path = fs::absolute(walletFile, GetWalletDir()); + fs::file_type path_type = fs::symlink_status(wallet_path).type(); + if (!(path_type == fs::file_not_found || path_type == fs::directory_file || + (path_type == fs::symlink_file && fs::is_directory(wallet_path)) || + (path_type == fs::regular_file && fs::path(walletFile).filename() == walletFile))) { + return {false, (strprintf( + _("Invalid -wallet path '%s'. -wallet path should point to a directory where wallet.dat and " + "database/log.?????????? files can be stored, a location where such a directory could be created " + "or (for backwards compatibility) the name of an existing data file in -walletdir (%s)"), + walletFile, GetWalletDir()))}; + } + return {true}; +} diff --git a/src/wallet/walletutil.h b/src/wallet/walletutil.h index b1a232f494dfe..91c824ae708a9 100644 --- a/src/wallet/walletutil.h +++ b/src/wallet/walletutil.h @@ -6,8 +6,11 @@ #define BITCOIN_WALLET_UTIL_H #include "fs.h" +#include "operationresult.h" //! Get the path of the wallet directory. fs::path GetWalletDir(); +//! Verify the wallet db's path +OperationResult VerifyWalletPath(const std::string& walletFile); #endif // BITCOIN_WALLET_UTIL_H