diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index c72d6476fd3..072ed7e1e82 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4229,8 +4229,19 @@ util::Result MigrateLegacyToDescriptor(std::shared_ptr 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")};