Minimal fix for #34128.
The issue occurs during the migration of a legacy unnamed wallet
(the legacy “default” wallet). When the migration fails, the cleanup
logic is triggered to roll back the state, which involves erasing the
newly created descriptor wallets directories. Normally, this only
affects the parent directories of named wallets, since they each
reside in their own directories. However, because the unnamed
wallet resides directly in the top-level /wallets/ folder, this
logic accidentally deletes the main directory.
The fix ensures that only the wallet.dat file of the unnamed wallet
is touched and restored, preserving the wallet in BDB format and
leaving the main /wallets/ directory intact.
Story Line:
#32273 fixed a different set of issues and, in doing so, uncovered
this one.
Before the mentioned PR, backups were stored in the same directory
as the wallet.dat file. On a migration failure, the backup was then
copied to the top-level /wallets/ directory. For the unnamed legacy
wallet, the wallet directory is the /wallets/ directory, so the source
and destination paths were identical. As a result, we threw early in the
fs::copy_file call (here) because the file already existed, as we
were trying to copy the file onto itself. This caused the cleanup logic
to abort early on and never reach the removal line.
Testing Notes:
Cherry-pick the test commit on top of master and run it. You will see the failure and realize the reason by reading the test code.