wallet: Fix migration of wallets with pathnames.

Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
This commit is contained in:
David Gumberg
2025-04-12 00:03:40 -07:00
parent f6ee59b6e2
commit 70f1c99c90

View File

@@ -4229,8 +4229,19 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
return util::Error{_("Error: This wallet is already a descriptor wallet")};
}
// Make a backup of the DB
fs::path backup_filename = fs::PathFromString(strprintf("%s_%d.legacy.bak", (wallet_name.empty() ? "default_wallet" : wallet_name), GetTime()));
// Make a backup of the DB in the wallet's directory with a unique filename
// using the wallet name and current timestamp. The backup filename is based
// on the name of the parent directory containing the wallet data in most
// cases, but in the case where the wallet name is a path to a data file,
// the name of the data file is used, and in the case where the wallet name
// is blank, "default_wallet" is used.
const std::string backup_prefix = wallet_name.empty() ? "default_wallet" : [&] {
// fs::weakly_canonical resolves relative specifiers and remove trailing slashes.
const auto legacy_wallet_path = fs::weakly_canonical(GetWalletDir() / fs::PathFromString(wallet_name));
return fs::PathToString(legacy_wallet_path.filename());
}();
fs::path backup_filename = fs::PathFromString(strprintf("%s_%d.legacy.bak", backup_prefix, GetTime()));
fs::path backup_path = fsbridge::AbsPathJoin(GetWalletDir(), backup_filename);
if (!local_wallet->BackupWallet(fs::PathToString(backup_path))) {
return util::Error{_("Error: Unable to make a backup of your wallet")};