Repository: bitcoin/bitcoin
File: src/wallet/wallet.cpp (C++)

Code before:
                error = Untranslated(strprintf("Failed to restore database path '%s'.", fs::PathToString(wallet_path)));
                status = DatabaseStatus::FAILED_ALREADY_EXISTS;
                return nullptr;
            }
            created_parent_dir = true;
        }

        fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
        wallet_file_copied = true;

        if (load_after_restore) {
            wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
        }
    } catch (const std::exception& e) {
        assert(!wallet);
        if (!error.empty()) error += Untranslated("\n");
        error += Untranslated(strprintf("Unexpected exception: %s", e.what()));
    }

    // Remove created wallet path only when loading fails
    if (load_after_restore && !wallet) {
        if (wallet_file_copied) fs::remove(wallet_file);
        // Clean up the parent directory if we created it during restoration.
        // As we have created it, it must be empty after deleting the wallet file.
        if (created_parent_dir) {
            Assert(fs::is_empty(wallet_path));
            fs::remove(wallet_path);
        }
    }

    return wallet;
}

/** @defgroup mapWallet
 *
 * @{
 */

const CWalletTx* CWallet::GetWalletTx(const Txid& hash) const
{
    AssertLockHeld(cs_wallet);
    const auto it = mapWallet.find(hash);
    if (it == mapWallet.end())
        return nullptr;
    return &(it->second);
}

void CWallet::UpgradeDescriptorCache()
{
    if (!IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) || IsLocked() || IsWalletFlagSet(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED)) {
        return;

Review comment (bug):
In commit "wallet: RestoreWallet failure, erase only what was created" (5ed59557b91f85d2c36d911bd2a334cb80b7494c)

Aborting the node doesn't seem like the best way to handle this error. Letting `fs::remove` just throw an exception on failure here like it would other places seems sufficient.

If the goal is to detect `fs::remove` failing for this specific reason as opposed to other reasons, since this one might indicate a bug, then `Assume` would be appropriate here.

(Same comment also applies to next commit 239f78641256bcce6ca5db3e34c49e50af9df5ca)

Code after:
                return nullptr;
            }

            // Check we are not going to overwrite an existing db file
            if (fs::exists(wallet_file)) {
                error = Untranslated(strprintf("Failed to restore wallet. Database file exists in '%s'.", fs::PathToString(wallet_file)));
                status = DatabaseStatus::FAILED_ALREADY_EXISTS;
                return nullptr;
            }
        } else {
            // The directory doesn't exist, create it
            if (!TryCreateDirectories(wallet_path)) {
                error = Untranslated(strprintf("Failed to restore database path '%s'.", fs::PathToString(wallet_path)));
                status = DatabaseStatus::FAILED_ALREADY_EXISTS;
                return nullptr;
            }
            created_parent_dir = true;
        }

        fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
        wallet_file_copied = true;

        if (load_after_restore) {
            wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
        }
    } catch (const std::exception& e) {
        assert(!wallet);
        if (!error.empty()) error += Untranslated("\n");
        error += Untranslated(strprintf("Unexpected exception: %s", e.what()));
    }

    // Remove created wallet path only when loading fails
    if (load_after_restore && !wallet) {
        if (wallet_file_copied) fs::remove(wallet_file);
        // Clean up the parent directory if we created it during restoration.
        // As we have created it, it must be empty after deleting the wallet file.
        if (created_parent_dir) {
            Assume(fs::is_empty(wallet_path));
            fs::remove(wallet_path);
        }
    }

    return wallet;
}

/** @defgroup mapWallet
 *
 * @{
 */

const CWalletTx* CWallet::GetWalletTx(const Txid& hash) const