Skip to content

Commit fee79e7

Browse files
davidgumbergluke-jr
authored andcommitted
wallet: Fix migration of wallets with pathnames.
Co-authored-by: Russell Yanofsky <russ@yanofsky.org> Github-Pull: bitcoin#32273 Rebased-From: 70f1c99
1 parent 679d961 commit fee79e7

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/wallet/wallet.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,9 +4546,20 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
45464546
return util::Error{_("Error: This wallet is already a descriptor wallet")};
45474547
}
45484548

4549-
// Make a backup of the DB
4549+
// Make a backup of the DB in the wallet's directory with a unique filename
4550+
// using the wallet name and current timestamp. The backup filename is based
4551+
// on the name of the parent directory containing the wallet data in most
4552+
// cases, but in the case where the wallet name is a path to a data file,
4553+
// the name of the data file is used, and in the case where the wallet name
4554+
// is blank, "default_wallet" is used.
45504555
fs::path this_wallet_dir = fs::absolute(fs::PathFromString(local_wallet->GetDatabase().Filename())).parent_path();
4551-
fs::path backup_filename = fs::PathFromString(strprintf("%s_%d.legacy.bak", MigrationPrefixName(*local_wallet), GetTime()));
4556+
const std::string backup_prefix = wallet_name.empty() ? MigrationPrefixName(*local_wallet) : [&] {
4557+
// fs::weakly_canonical resolves relative specifiers and remove trailing slashes.
4558+
const auto legacy_wallet_path = fs::weakly_canonical(GetWalletDir() / fs::PathFromString(wallet_name));
4559+
return fs::PathToString(legacy_wallet_path.filename());
4560+
}();
4561+
4562+
fs::path backup_filename = fs::PathFromString(strprintf("%s_%d.legacy.bak", backup_prefix, GetTime()));
45524563
fs::path backup_path = this_wallet_dir / backup_filename;
45534564
if (!local_wallet->BackupWallet(fs::PathToString(backup_path))) {
45544565
if (was_loaded) {

test/functional/wallet_migration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def test_migration_failure(self, wallet_name):
636636
assert Path(old_path / "wallet.dat").exists()
637637
assert Path(master_path / "wallet.dat").exists()
638638

639-
backup_prefix = "default_wallet" if is_default else wallet_name
639+
backup_prefix = "default_wallet" if is_default else os.path.basename(os.path.abspath(master_path))
640640
backup_path = master_path / f"{backup_prefix}_{mocked_time}.legacy.bak"
641641
assert backup_path.exists()
642642

0 commit comments

Comments
 (0)