Skip to content

Commit

Permalink
Abstract VerifyWalletPath and connect it to init and GUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jul 21, 2021
1 parent 23458ca commit d9e1c6b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
15 changes: 5 additions & 10 deletions src/qt/pivx.cpp
Expand Up @@ -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();
}
Expand Down
20 changes: 3 additions & 17 deletions src/wallet/init.cpp
Expand Up @@ -165,24 +165,10 @@ bool WalletVerify()
std::set<fs::path> 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"));
}
Expand Down
22 changes: 22 additions & 0 deletions src/wallet/walletutil.cpp
Expand Up @@ -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};
}
3 changes: 3 additions & 0 deletions src/wallet/walletutil.h
Expand Up @@ -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

0 comments on commit d9e1c6b

Please sign in to comment.