| 381 | } |
| 382 | |
| 383 | std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings) |
| 384 | { |
| 385 | DatabaseOptions options; |
| 386 | options.require_existing = true; |
| 387 | |
| 388 | if (!fs::exists(backup_file)) { |
| 389 | error = Untranslated("Backup file does not exist"); |
| 390 | status = DatabaseStatus::FAILED_INVALID_BACKUP_FILE; |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
| 394 | const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::u8path(wallet_name)); |
| 395 | |
| 396 | if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) { |
| 397 | error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path))); |
| 398 | status = DatabaseStatus::FAILED_ALREADY_EXISTS; |
| 399 | return nullptr; |
| 400 | } |
| 401 | |
| 402 | auto wallet_file = wallet_path / "wallet.dat"; |
| 403 | fs::copy_file(backup_file, wallet_file, fs::copy_options::none); |
| 404 | |
| 405 | auto wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings); |
| 406 | |
| 407 | if (!wallet) { |
| 408 | fs::remove(wallet_file); |
| 409 | fs::remove(wallet_path); |
| 410 | } |
| 411 | |
| 412 | return wallet; |
| 413 | } |
| 414 | |
| 415 | /** @defgroup mapWallet |
| 416 | * |
no test coverage detected