From f6bc047e5a0c33abf599b00d8cfdb42dc7603605 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 21 Nov 2026 18:00:38 -0700 Subject: [PATCH] test(store): what the removed tests of the old form protected, held by the new form Co-Authored-By: Claude Fable 5.2 --- crates/ledger-store/src/backup/read.rs | 1 + crates/ledger-store/tests/backup.rs | 221 +++++++++++++++++++++++++ docs/backups.md | 116 +++++++++++++ 3 files changed, 338 insertions(+) create mode 100644 docs/backups.md diff --git a/crates/ledger-store/src/backup/read.rs b/crates/ledger-store/src/backup/read.rs index 3c4bdaf3..dfaa9b56 100644 --- a/crates/ledger-store/src/backup/read.rs +++ b/crates/ledger-store/src/backup/read.rs @@ -575,6 +575,7 @@ fn prove_books( .collect(); drop(store); ctx.phase("vault"); + ctx.check("reading its vault")?; if !tins.is_empty() { let held: BTreeSet<&str> = m.vault.iter().map(|n| n.piece.sha256.as_str()).collect(); if let Some(missing) = tins.iter().find(|h| !held.contains(h.as_str())) { diff --git a/crates/ledger-store/tests/backup.rs b/crates/ledger-store/tests/backup.rs index 51130528..606fa058 100644 --- a/crates/ledger-store/tests/backup.rs +++ b/crates/ledger-store/tests/backup.rs @@ -727,3 +727,224 @@ fn the_text_read_from_a_stored_file_is_a_stored_file_too() { ); assert_eq!(v.head, s.head()); } + +// --- what the removed tests of the old form protected ------------------------ + +#[test] +fn a_purged_vault_leaves_the_next_backup_and_not_the_earlier_ones() { + let dir = tempfile::tempdir().unwrap(); + let b = dir.path().join("book"); + let s = book(&b); + std::fs::create_dir_all(b.join("vault")).unwrap(); + std::fs::write(b.join("vault").join("tin-1.bin"), b"sealed record").unwrap(); + std::fs::create_dir_all(b.join("keys")).unwrap(); + std::fs::write(b.join("keys").join("legacy.key"), "ab".repeat(32)).unwrap(); + let disk = dir.path().join("disk"); + std::fs::create_dir(&disk).unwrap(); + write_destination(&plain(&disk), &cut(&b, &s), true, &Ctx::new()).unwrap(); + // The vault is purged to nothing: the next backup holds none, the + // earlier one still does (nothing in a destination is deleted). + std::fs::remove_file(b.join("vault").join("tin-1.bin")).unwrap(); + std::fs::remove_dir(b.join("vault")).unwrap(); + let w = write_destination(&plain(&disk), &cut(&b, &s), true, &Ctx::new()).unwrap(); + assert!(w.new && w.number == 2, "{w:?}"); + let now = prove(&disk, None, dir.path(), "now").unwrap(); + assert!(!now.tree.join("vault").exists()); + assert_eq!( + std::fs::read_to_string(now.tree.join("keys").join("legacy.key")).unwrap(), + "ab".repeat(32) + ); + let then = dir.path().join("then"); + copy_tree(&disk, &then); + std::fs::remove_file(then.join("acme/manifests/000002.json")).unwrap(); + let old = prove(&then, None, dir.path(), "old").unwrap(); + assert_eq!( + std::fs::read(old.tree.join("vault").join("tin-1.bin")).unwrap(), + b"sealed record" + ); +} + +#[test] +fn a_scratch_file_left_by_a_dead_program_never_stops_the_next_backup() { + let dir = tempfile::tempdir().unwrap(); + let b = dir.path().join("book"); + let mut s = book(&b); + let disk = dir.path().join("disk"); + std::fs::create_dir(&disk).unwrap(); + write_destination(&plain(&disk), &cut(&b, &s), true, &Ctx::new()).unwrap(); + // What a program that died leaves: a scratch file, a short manifest at + // the next number (its number is spent), a piece cut short. + let set = disk.join("acme"); + std::fs::write(set.join("manifests").join(".wb-tmp-9-abcdef"), b"half").unwrap(); + std::fs::write( + set.join("manifests").join("000002.json"), + b"{\"format\":\"wan", + ) + .unwrap(); + more(&mut s, 1); + let w = write_destination(&plain(&disk), &cut(&b, &s), true, &Ctx::new()).unwrap(); + assert!(w.new && w.number == 3, "{w:?}"); + let v = prove(&disk, None, dir.path(), "v").unwrap(); + assert_eq!(v.head, s.head()); +} + +#[test] +fn a_stored_file_that_does_not_match_its_name_is_refused_and_nothing_is_written() { + let dir = tempfile::tempdir().unwrap(); + let b = dir.path().join("book"); + let s = book(&b); + let sha = ledger_store::journal::sha256_hex(b"%PDF-1.4 r"); + let blob = b.join("blobs").join(&sha[0..2]).join(&sha[2..4]).join(&sha); + std::fs::write(&blob, b"changed under its name").unwrap(); + let disk = dir.path().join("disk"); + std::fs::create_dir(&disk).unwrap(); + let err = write_destination(&plain(&disk), &cut(&b, &s), true, &Ctx::new()).unwrap_err(); + assert!(matches!(err, BackupError::Damaged(_)), "{err:?}"); + // No manifest: nothing there is a backup. + assert!( + !tree(&disk) + .keys() + .any(|n| n.contains("manifests/") && n.ends_with(".json")) + ); +} + +#[test] +fn a_drill_proves_this_books_history_only() { + use ledger_store::backup::drill; + let dir = tempfile::tempdir().unwrap(); + let (b1, b2) = (dir.path().join("one"), dir.path().join("two")); + let (mut s1, s2) = (book(&b1), book(&b2)); + let disk = dir.path().join("disk"); + std::fs::create_dir(&disk).unwrap(); + write_destination(&plain(&disk), &cut(&b1, &s1), true, &Ctx::new()).unwrap(); + let head = s1.head(); + // The books move on: the backup is the beginning of their journal. + more(&mut s1, 1); + let got = drill( + &plain(&disk), + "acme", + &b1.join("journal.jsonl"), + &dir.path().join("d1"), + &Ctx::new(), + ) + .unwrap(); + assert_eq!(got, head); + assert!(!dir.path().join("d1").exists(), "the scratch is removed"); + // Another book's history under the same name: not this book's. + let err = drill( + &plain(&disk), + "acme", + &b2.join("journal.jsonl"), + &dir.path().join("d2"), + &Ctx::new(), + ) + .unwrap_err(); + assert!( + matches!(err, BackupError::Damaged(_)), + "{err:?} {:?}", + s2.head() + ); + assert!(!dir.path().join("d2").exists()); +} + +#[test] +fn the_local_backup_proves_to_hold_the_book_as_it_is_keys_and_all() { + use ledger_store::backup::{prove_local, write_local}; + let dir = tempfile::tempdir().unwrap(); + let b = dir.path().join("book"); + let mut s = book(&b); + std::fs::create_dir_all(b.join("keys")).unwrap(); + std::fs::write(b.join("keys").join("legacy.key"), "ab".repeat(32)).unwrap(); + let scratch = dir.path().join("check"); + let c = cut(&b, &s); + write_local(&b, &c, &Ctx::new()).unwrap(); + assert_eq!( + prove_local(&b, "acme", &s.head(), &scratch, &Ctx::new()).unwrap(), + 1 + ); + assert!(!scratch.exists(), "the scratch folder is removed"); + // A changed key, a changed vault record, an extra one, books that moved + // on, each refused. + std::fs::write(b.join("keys").join("legacy.key"), "cd".repeat(32)).unwrap(); + assert!(prove_local(&b, "acme", &s.head(), &scratch, &Ctx::new()).is_err()); + std::fs::write(b.join("keys").join("legacy.key"), "ab".repeat(32)).unwrap(); + std::fs::create_dir_all(b.join("vault")).unwrap(); + std::fs::write(b.join("vault").join("tin-2.bin"), b"extra").unwrap(); + assert!(prove_local(&b, "acme", &s.head(), &scratch, &Ctx::new()).is_err()); + std::fs::remove_file(b.join("vault").join("tin-2.bin")).unwrap(); + more(&mut s, 1); + assert!(prove_local(&b, "acme", &s.head(), &scratch, &Ctx::new()).is_err()); + // Written again, it proves the books as they are now. + write_local(&b, &cut(&b, &s), &Ctx::new()).unwrap(); + prove_local(&b, "acme", &s.head(), &scratch, &Ctx::new()).unwrap(); + assert!(!scratch.exists()); +} + +#[cfg(unix)] +#[test] +fn a_link_in_a_backup_is_never_followed() { + let dir = tempfile::tempdir().unwrap(); + let b = dir.path().join("book"); + let s = book(&b); + let disk = dir.path().join("disk"); + std::fs::create_dir(&disk).unwrap(); + write_destination(&plain(&disk), &cut(&b, &s), true, &Ctx::new()).unwrap(); + // The journal's piece replaced by a link to a file elsewhere with the + // same bytes: it is no piece of the backup. + let set = disk.join("acme"); + let piece = std::fs::read_dir(set.join("journal")) + .unwrap() + .next() + .unwrap() + .unwrap() + .path(); + let elsewhere = dir.path().join("elsewhere"); + std::fs::copy(&piece, &elsewhere).unwrap(); + std::fs::remove_file(&piece).unwrap(); + std::os::unix::fs::symlink(&elsewhere, &piece).unwrap(); + assert!(prove(&disk, None, dir.path(), "v").is_err()); +} + +#[test] +fn a_drill_stops_inside_the_phase_it_is_told_to_stop_in() { + use ledger_store::backup::drill; + let dir = tempfile::tempdir().unwrap(); + let b = dir.path().join("book"); + let s = book(&b); + let disk = dir.path().join("disk"); + std::fs::create_dir(&disk).unwrap(); + write_destination(&plain(&disk), &cut(&b, &s), true, &Ctx::new()).unwrap(); + // The phases a drill goes through; a stop asked as each begins ends the + // drill there (the observer sees no later phase), scratch removed. + let phases = ["fetch", "replay", "verify", "blobs", "vault", "prefix"]; + for stop_at in phases { + let stop = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let seen = Arc::new(std::sync::Mutex::new(Vec::::new())); + let ctx = { + let (stop, seen) = (stop.clone(), seen.clone()); + Ctx::new() + .with_stop(stop.clone()) + .with_phase_watch(Arc::new(move |p: &str| { + seen.lock().unwrap().push(p.to_string()); + if p == stop_at { + stop.store(true, Ordering::SeqCst); + } + })) + }; + let scratch = dir.path().join(format!("d-{stop_at}")); + let got = drill( + &plain(&disk), + "acme", + &b.join("journal.jsonl"), + &scratch, + &ctx, + ); + assert!( + matches!(got, Err(BackupError::Cancelled(_))), + "{stop_at}: {got:?}" + ); + let seen = seen.lock().unwrap().clone(); + assert_eq!(seen.last().map(String::as_str), Some(stop_at), "{seen:?}"); + assert!(!scratch.exists(), "{stop_at}: scratch left"); + } +} diff --git a/docs/backups.md b/docs/backups.md new file mode 100644 index 00000000..4d288b2c --- /dev/null +++ b/docs/backups.md @@ -0,0 +1,116 @@ +# Backups + +A backup of your books is an ordinary folder of ordinary files. Nothing needs +to be installed to make one or to read one back, and you never need to know how +it works. This page says where it is, what is in it, and how to get your books +back, on this computer, on a new one, or from a backup an older version made. + +## Where it is + +You choose the folder once (in the window: **Back up to...**): another disk, a +USB drive, a folder on your network, or a folder your cloud drive keeps in sync +(Dropbox, iCloud Drive, OneDrive, Google Drive). Your books then back +themselves up a little after each change, when they open and before they +close; **Back up now** does it at once. A backup that is more than a day behind +is flagged, and once a week your books prove they can be restored from it. + +## What is in it + +One folder for your books, inside the folder you chose: + +```text +Backups/ the folder you chose + acme/ your books (the name you gave them) + READ-ME.txt a few words about the folder + manifests/ + 000001.json one per backup that changed something + 000002.json + journal/000001-..., 000002-... the journal, in pieces + access/000001-... who reached the books, in pieces + files-index/000001-... which receipts and files there are + files/ab/abcdef... each receipt or file, once + vault/... keys/... your tax-ID vault and its key +``` + +Files are only ever **added**. Nothing in the folder is changed or deleted by +Wanderbooks, so a sync service never sees a file change under it, and an earlier +state of your books is always still there. A backup counts as complete only +when its small manifest file is in place, written last; an interrupted backup +leaves a few unused files and the last complete backup is still whole. Two +computers backing up to one folder cannot damage it either: whichever writes +the next manifest first wins, and the other goes on from there. If the folder +turns out to hold a different set of books under the same name, or a newer +copy of these than this computer has, Wanderbooks says so and writes nothing. + +If your disk is short of room, you may delete the whole folder of your books +(never parts of it) once you have another backup. + +## A passphrase (optional) + +When you set up backups you may protect them with a passphrase. It is off unless +you turn it on. With one, every file in the folder is sealed (XChaCha20-Poly1305; +the key comes from your passphrase through Argon2id) and the file names say +nothing about your books, only how many files there are and how big. Your +computer keeps the key privately, so backups stay automatic. **A forgotten +passphrase cannot be recovered**, by anyone: write it down somewhere safe. +On another computer, restoring asks for the passphrase. + +Changing the passphrase starts a new set of sealed backups beside the old +ones, which stay as they are and stay sealed with the passphrase they were made +with. Turning it off makes later backups plain. If you want old backups gone +after a change, delete their folder yourself. + +## One file instead of a folder + +**Save a backup...** writes the same backup as a single file ending in +.wbbackup (a zip). Keep it, email it to yourself, or carry it on a USB stick. It +is sealed if your backups are. A file by the same name is never overwritten. + +## Getting your books back + +**Restore...** takes the backup folder you chose (or the folder of one set of +books inside it), or a .wbbackup file. It checks everything first and shows you +what it found: how far the books go, how many files, whether it needed to skip a +newer backup that was damaged. Only then does it put your books back, as new +books, or in place of books with the same name (those are kept whole in a +`legacy` folder, never deleted). + +On a **new computer**: install Wanderbooks, open it, choose **Restore...**, point it +at the backup folder (connect the disk or let your cloud drive finish syncing +first) or at the .wbbackup file, and give the passphrase if it asks. Then choose +a backup folder again to keep backing up. + +If the disk your backups were on is not connected, Wanderbooks says so and does +not start over on another disk by itself: connect the disk, or, if it is a new +disk, say so when you back up and it begins there again. + +## A backup made with an older version + +Older versions kept backups differently, in a way that needed another program +called git. Wanderbooks no longer writes those or reads them, and does not touch +them: they stay exactly where they are. If you still have one and want your books +from it, you can get them back with git yourself: + +1. In a terminal, make a plain folder from the old backup (it is the folder named + after your books, ending in .git, inside the folder you backed up to): + + ```text + git clone /path/to/backups/acme.git acme-old + ``` + +2. Adopt that folder as books (with your vault and its key, which the backup + also kept), and check what it says: + + ```text + wanderbooks books adopt acme-old --id acme --vault acme-old/vault --vault-key acme-old/keys/legacy.key + ``` + + It copies the books in, proves them whole, and leaves the original as it was. + (If your books had a tax-ID vault you did not use, leave out the two vault + options and add `--no-vault`.) + +3. Choose a backup folder for the books you got back, so new backups are made in + the new form beside the old one. + +Your books still list the private repository they used to back up to; it is no +longer used, and you may clear that setting.