Skip to content

Commit b75f1ad

Browse files
committed
partial bitcoin#26005: Fix error handling (copy_file failure in RestoreWallet, and in general via interfaces)
excludes: - c3e5365
1 parent 9f5845c commit b75f1ad

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/wallet/wallet.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -390,25 +390,31 @@ std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& b
390390
ReadDatabaseArgs(*context.args, options);
391391
options.require_existing = true;
392392

393-
if (!fs::exists(backup_file)) {
394-
error = Untranslated("Backup file does not exist");
395-
status = DatabaseStatus::FAILED_INVALID_BACKUP_FILE;
396-
return nullptr;
397-
}
398-
399393
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::u8path(wallet_name));
394+
auto wallet_file = wallet_path / "wallet.dat";
395+
std::shared_ptr<CWallet> wallet;
400396

401-
if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) {
402-
error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path)));
403-
status = DatabaseStatus::FAILED_ALREADY_EXISTS;
404-
return nullptr;
405-
}
397+
try {
398+
if (!fs::exists(backup_file)) {
399+
error = Untranslated("Backup file does not exist");
400+
status = DatabaseStatus::FAILED_INVALID_BACKUP_FILE;
401+
return nullptr;
402+
}
406403

407-
auto wallet_file = wallet_path / "wallet.dat";
408-
fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
404+
if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) {
405+
error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path)));
406+
status = DatabaseStatus::FAILED_ALREADY_EXISTS;
407+
return nullptr;
408+
}
409409

410-
auto wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
410+
fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
411411

412+
wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
413+
} catch (const std::exception& e) {
414+
assert(!wallet);
415+
if (!error.empty()) error += Untranslated("\n");
416+
error += strprintf(Untranslated("Unexpected exception: %s"), e.what());
417+
}
412418
if (!wallet) {
413419
fs::remove(wallet_file);
414420
fs::remove(wallet_path);

0 commit comments

Comments
 (0)