| 22 | uint32_t DUMP_VERSION = 1; |
| 23 | |
| 24 | bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error) |
| 25 | { |
| 26 | // Get the dumpfile |
| 27 | std::string dump_filename = args.GetArg("-dumpfile", ""); |
| 28 | if (dump_filename.empty()) { |
| 29 | error = _("No dump file provided. To use dump, -dumpfile=<filename> must be provided."); |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | fs::path path = fs::PathFromString(dump_filename); |
| 34 | path = fs::absolute(path); |
| 35 | if (fs::exists(path)) { |
| 36 | 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)); |
| 37 | return false; |
| 38 | } |
| 39 | std::ofstream dump_file; |
| 40 | dump_file.open(path.std_path()); |
| 41 | if (dump_file.fail()) { |
| 42 | error = strprintf(_("Unable to open %s for writing"), fs::PathToString(path)); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | HashWriter hasher{}; |
| 47 | |
| 48 | std::unique_ptr<DatabaseBatch> batch = db.MakeBatch(); |
| 49 | |
| 50 | bool ret = true; |
| 51 | std::unique_ptr<DatabaseCursor> cursor = batch->GetNewCursor(); |
| 52 | if (!cursor) { |
| 53 | error = _("Error: Couldn't create cursor into database"); |
| 54 | ret = false; |
| 55 | } |
| 56 | |
| 57 | // Write out a magic string with version |
| 58 | std::string line = strprintf("%s,%u\n", DUMP_MAGIC, DUMP_VERSION); |
| 59 | dump_file.write(line.data(), line.size()); |
| 60 | hasher << std::span{line}; |
| 61 | |
| 62 | // Write out the file format |
| 63 | std::string format = db.Format(); |
| 64 | // BDB files that are opened using BerkeleyRODatabase have its format as "bdb_ro" |
| 65 | // We want to override that format back to "bdb" |
| 66 | if (format == "bdb_ro") { |
| 67 | format = "bdb"; |
| 68 | } |
| 69 | line = strprintf("%s,%s\n", "format", format); |
| 70 | dump_file.write(line.data(), line.size()); |
| 71 | hasher << std::span{line}; |
| 72 | |
| 73 | if (ret) { |
| 74 | |
| 75 | // Read the records |
| 76 | while (true) { |
| 77 | DataStream ss_key{}; |
| 78 | DataStream ss_value{}; |
| 79 | DatabaseCursor::Status status = cursor->Next(ss_key, ss_value); |
| 80 | if (status == DatabaseCursor::Status::DONE) { |
| 81 | ret = true; |
no test coverage detected