| 20 | uint32_t DUMP_VERSION = 1; |
| 21 | |
| 22 | bool DumpWallet(CWallet& wallet, bilingual_str& error) |
| 23 | { |
| 24 | // Get the dumpfile |
| 25 | std::string dump_filename = gArgs.GetArg("-dumpfile", ""); |
| 26 | if (dump_filename.empty()) { |
| 27 | error = _("No dump file provided. To use dump, -dumpfile=<filename> must be provided."); |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | fs::path path = fs::PathFromString(dump_filename); |
| 32 | path = fs::absolute(path); |
| 33 | if (fs::exists(path)) { |
| 34 | error = strprintf(_("File %s already exists. If you are sure this is what you want, move it out of the way first."), fs::PathToString(path)); |
| 35 | return false; |
| 36 | } |
| 37 | std::ofstream dump_file; |
| 38 | dump_file.open(path); |
| 39 | if (dump_file.fail()) { |
| 40 | error = strprintf(_("Unable to open %s for writing"), fs::PathToString(path)); |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | CHashWriter hasher(0, 0); |
| 45 | |
| 46 | WalletDatabase& db = wallet.GetDatabase(); |
| 47 | std::unique_ptr<DatabaseBatch> batch = db.MakeBatch(); |
| 48 | |
| 49 | bool ret = true; |
| 50 | if (!batch->StartCursor()) { |
| 51 | error = _("Error: Couldn't create cursor into database"); |
| 52 | ret = false; |
| 53 | } |
| 54 | |
| 55 | // Write out a magic string with version |
| 56 | std::string line = strprintf("%s,%u\n", DUMP_MAGIC, DUMP_VERSION); |
| 57 | dump_file.write(line.data(), line.size()); |
| 58 | hasher.write(MakeByteSpan(line)); |
| 59 | |
| 60 | // Write out the file format |
| 61 | line = strprintf("%s,%s\n", "format", db.Format()); |
| 62 | dump_file.write(line.data(), line.size()); |
| 63 | hasher.write(MakeByteSpan(line)); |
| 64 | |
| 65 | if (ret) { |
| 66 | |
| 67 | // Read the records |
| 68 | while (true) { |
| 69 | CDataStream ss_key(SER_DISK, CLIENT_VERSION); |
| 70 | CDataStream ss_value(SER_DISK, CLIENT_VERSION); |
| 71 | bool complete; |
| 72 | ret = batch->ReadAtCursor(ss_key, ss_value, complete); |
| 73 | if (complete) { |
| 74 | ret = true; |
| 75 | break; |
| 76 | } else if (!ret) { |
| 77 | error = _("Error reading next record from wallet database"); |
| 78 | break; |
| 79 | } |
no test coverage detected