diff --git a/crates/ledger-cli/tests/backups.rs b/crates/ledger-cli/tests/backups.rs index bc04aea7..b94215e4 100644 --- a/crates/ledger-cli/tests/backups.rs +++ b/crates/ledger-cli/tests/backups.rs @@ -364,7 +364,7 @@ async fn backed_up(c: &Client, within: Duration) -> Value { fn proven(home: &Home, source: &Path, tag: &str) -> (u64, String) { let scratch = home.outside(&format!("scratch-{tag}")); let found = find(source, None, &Ctx::new()).unwrap_or_else(|e| panic!("{tag}: {e}")); - verify_found(&found, &scratch, false, &Ctx::new()) + verify_found(&found, &scratch, &Ctx::new()) .unwrap_or_else(|e| panic!("{tag}: {e}")) .head } @@ -412,7 +412,7 @@ async fn a_book_backs_itself_up_to_its_folder_soon_after_a_write() { // Its journal is the live journal's beginning (a drill's check). let scratch = home.outside("scratch-prefix"); let found = find(&folder, None, &Ctx::new()).unwrap(); - let v = verify_found(&found, &scratch, false, &Ctx::new()).unwrap(); + let v = verify_found(&found, &scratch, &Ctx::new()).unwrap(); assert!( std::fs::read(home.book_dir("acme").join(ledger_store::JOURNAL_FILE)) .unwrap() diff --git a/crates/ledger-cli/tests/book_open_close.rs b/crates/ledger-cli/tests/book_open_close.rs index 076a1e91..089eb4e0 100644 --- a/crates/ledger-cli/tests/book_open_close.rs +++ b/crates/ledger-cli/tests/book_open_close.rs @@ -1159,7 +1159,7 @@ fn proven_head(home: &Home, disk: &std::path::Path, tag: &str) -> (u64, String) use ledger_store::backup::{Ctx, find, verify_found}; let scratch = home.dir.path().join(format!("scratch-{tag}")); let found = find(disk, None, &Ctx::new()).unwrap_or_else(|e| panic!("{tag}: {e}")); - verify_found(&found, &scratch, false, &Ctx::new()) + verify_found(&found, &scratch, &Ctx::new()) .unwrap_or_else(|e| panic!("{tag}: {e}")) .head } diff --git a/crates/ledger-mcp/src/local.rs b/crates/ledger-mcp/src/local.rs index 5df519dd..3642cb3f 100644 --- a/crates/ledger-mcp/src/local.rs +++ b/crates/ledger-mcp/src/local.rs @@ -5185,7 +5185,7 @@ mod tests { let found = ledger_store::backup::find(&dir.join(ledger_store::backup::LOCAL_DIR), None, &ctx) .expect("a local backup is there"); - ledger_store::backup::verify_found(&found, &scratch, true, &ctx).expect("it proves whole") + ledger_store::backup::verify_found(&found, &scratch, &ctx).expect("it proves whole") } /// plan/served.md 3.9's `format_upgrade_waits_for_consent`, the diff --git a/crates/ledger-store/src/backup/read.rs b/crates/ledger-store/src/backup/read.rs index 6b5a0a04..3c4bdaf3 100644 --- a/crates/ledger-store/src/backup/read.rs +++ b/crates/ledger-store/src/backup/read.rs @@ -478,16 +478,11 @@ fn put_scratch(dir: &Path, name: &str, bytes: &[u8]) -> std::io::Result<()> { } /// Build the books of `m` in `scratch` (a new folder): the journal, the -/// access log, the vault and the keys, and, with `with_files`, every -/// stored file; every piece proven as it is read. Without `with_files` the -/// stored files are re-hashed and not kept (a drill). Returns the hashes of -/// the stored files proven. -fn materialize( - set: &Set, - m: &Manifest, - scratch: &Path, - with_files: bool, -) -> Result, BackupError> { +/// access log, the vault, the keys and every stored file; every piece +/// proven as it is read. (The store reads some stored files itself when +/// it verifies the books, so a drill keeps them too, as a restore does.) +/// Returns the hashes of the stored files proven. +fn materialize(set: &Set, m: &Manifest, scratch: &Path) -> Result, BackupError> { let ctx = &set.ctx; std::fs::create_dir_all(scratch)?; for (series, kind, name) in [ @@ -524,23 +519,19 @@ fn materialize( for line in set.index(m)? { ctx.check("reading the backup")?; let rel = piece_path(set.keys.as_ref(), Kind::File, 0, &line.sha256); - if with_files { - let blob = scratch - .join(crate::store::BLOBS_DIR) - .join(&line.sha256[0..2]) - .join(&line.sha256[2..4]); - std::fs::create_dir_all(&blob)?; - let mut f = std::fs::OpenOptions::new() - .write(true) - .create_new(true) - .open(blob.join(&line.sha256))?; - set.read_piece(&rel, Kind::File, line.bytes, &line.sha256, &mut |c| { - f.write_all(c) - })?; - f.sync_all()?; - } else { - set.read_piece(&rel, Kind::File, line.bytes, &line.sha256, &mut |_| Ok(()))?; - } + let blob = scratch + .join(crate::store::BLOBS_DIR) + .join(&line.sha256[0..2]) + .join(&line.sha256[2..4]); + std::fs::create_dir_all(&blob)?; + let mut f = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(blob.join(&line.sha256))?; + set.read_piece(&rel, Kind::File, line.bytes, &line.sha256, &mut |c| { + f.write_all(c) + })?; + f.sync_all()?; proven.insert(line.sha256); } Ok(proven) @@ -554,7 +545,6 @@ fn prove_books( m: &Manifest, scratch: &Path, proven_files: &BTreeSet, - with_files: bool, ctx: &Ctx, ) -> Result<(u64, (u64, String)), BackupError> { ctx.phase("replay"); @@ -570,11 +560,7 @@ fn prove_books( short(missing) ))); } - let blobs = if with_files { - store.verify_blobs()? - } else { - proven_files.len() as u64 - }; + let blobs = store.verify_blobs()?; let head = store.head(); let tins: Vec = store .ledger() @@ -618,15 +604,9 @@ fn prove_books( /// Prove the backup found, building its books in `scratch` (a new folder): /// the newest manifest that proves whole; a newer one that does not is -/// passed over and said. With `with_files`, every stored file is kept in -/// the scratch folder (a restore); without, they are re-hashed and not -/// kept (a drill). -pub fn verify_found( - found: &Found, - scratch: &Path, - with_files: bool, - ctx: &Ctx, -) -> Result { +/// passed over and said. Every stored file is re-hashed and kept in the +/// scratch folder (a restore moves them from it; a drill removes it). +pub fn verify_found(found: &Found, scratch: &Path, ctx: &Ctx) -> Result { if scratch.exists() { return Err(BackupError::Damaged("its scratch folder is in use".into())); } @@ -636,8 +616,8 @@ pub fn verify_found( for m in set.whole_ones()? { ctx.phase("fetch"); ctx.check("verifying")?; - let attempt = materialize(set, &m, scratch, with_files) - .and_then(|files| prove_books(&m, scratch, &files, with_files, ctx)); + let attempt = + materialize(set, &m, scratch).and_then(|files| prove_books(&m, scratch, &files, ctx)); match attempt { Ok((blobs, head)) => { return Ok(Verified { @@ -712,7 +692,7 @@ pub fn drill( key: dest.key.clone(), set, }; - let checked = verify_found(&found, scratch, false, ctx).and_then(|v| { + let checked = verify_found(&found, scratch, ctx).and_then(|v| { ctx.phase("prefix"); if v.book.as_deref() != Some(book) { return Err(BackupError::Elsewhere); @@ -755,7 +735,7 @@ pub fn prove_local( key: None, set, }; - let proven = verify_found(&found, scratch, false, ctx).and_then(|v| { + let proven = verify_found(&found, scratch, ctx).and_then(|v| { if v.head != *head { return Err(BackupError::Damaged(format!( "it holds the books through event {}, and they are at event {}", diff --git a/crates/ledger-store/src/root.rs b/crates/ledger-store/src/root.rs index ddb9abfe..1a21944b 100644 --- a/crates/ledger-store/src/root.rs +++ b/crates/ledger-store/src/root.rs @@ -1182,7 +1182,6 @@ impl Root { books: &Path, id: &BookId, stop: &JobStop, - with_files: bool, ) -> Result<(PathBuf, crate::backup::Verified), RootError> { stop.at("fetch", "restored")?; let ctx = crate::backup::Ctx::new().with_stop(stop.flag()); @@ -1200,7 +1199,7 @@ impl Root { } let scratch = books.join(format!("{RESTORE_PREFIX}{id}-{}", std::process::id())); let proven = crate::backup::find(source, passphrase, &ctx) - .and_then(|found| crate::backup::verify_found(&found, &scratch, with_files, &ctx)); + .and_then(|found| crate::backup::verify_found(&found, &scratch, &ctx)); match proven { Ok(v) => Ok((scratch, v)), Err(e) => { @@ -1241,7 +1240,7 @@ impl Root { (|| { let _lock = self.books_lock(stop, "restored")?; let (books, _) = self.private_subdir(BOOKS_DIR, false)?; - let (scratch, v) = self.fetch_backup(source, passphrase, &books, id, stop, false)?; + let (scratch, v) = self.fetch_backup(source, passphrase, &books, id, stop)?; let vault = scratch.join(VAULT_DIR); let vault_files = if vault.is_dir() { std::fs::read_dir(&vault) @@ -1353,7 +1352,7 @@ impl Root { }; stop.at("start", "restored")?; let _scope = crate::cancel::scope(stop.flag()); - let (scratch, v) = self.fetch_backup(source, passphrase, &books, id, stop, true)?; + let (scratch, v) = self.fetch_backup(source, passphrase, &books, id, stop)?; let staging = books.join(format!("{RESTORE_PREFIX}{id}-{}-book", std::process::id())); let built = (|| -> Result { stop.at("copy", "restored")?; diff --git a/crates/ledger-store/tests/backup.rs b/crates/ledger-store/tests/backup.rs index 35f3d289..75303ca5 100644 --- a/crates/ledger-store/tests/backup.rs +++ b/crates/ledger-store/tests/backup.rs @@ -106,7 +106,7 @@ fn prove( name: &str, ) -> Result { let found = find(source, pass, &Ctx::new())?; - verify_found(&found, &root.join(name), true, &Ctx::new()) + verify_found(&found, &root.join(name), &Ctx::new()) } #[test] @@ -462,7 +462,7 @@ fn a_flipped_or_cut_piece_is_found_and_never_restored() { // Found once (a sealed backup's key takes a moment to derive). let found = find(&disk, pass, &Ctx::new()).unwrap(); let prove = |_: &Path, _: Option<&str>, root: &Path, name: &str| { - verify_found(&found, &root.join(name), true, &Ctx::new()) + verify_found(&found, &root.join(name), &Ctx::new()) }; let set = std::fs::read_dir(&disk) .unwrap() @@ -687,3 +687,37 @@ fn a_loser_of_a_manifest_number_goes_on_from_the_winner() { ); prove(&disk, None, dir.path(), "v").unwrap(); } + +// --- what the store reads of a backup ----------------------------------- + +#[test] +fn the_text_read_from_a_stored_file_is_a_stored_file_too() { + // The store's own verification reads the text an extraction recorded as + // a stored file; a backup that left it out would not prove whole, so it + // is carried and proven with the rest. + let dir = tempfile::tempdir().unwrap(); + let b = dir.path().join("book"); + let mut s = book(&b); + let (attachment, _, _) = s + .attach_receipt(None, "words.txt", "text/plain", b"a receipt with words", None) + .unwrap(); + s.extract_attachment(attachment).unwrap(); + let text_sha = s + .extraction(attachment) + .unwrap() + .and_then(|x| x.text_sha256) + .expect("the text is a stored file"); + let disk = dir.path().join("disk"); + std::fs::create_dir(&disk).unwrap(); + write_destination(&plain(&disk), &cut(&b, &s), true, &Ctx::new()).unwrap(); + let v = prove(&disk, None, dir.path(), "v").unwrap(); + assert!( + v.tree + .join("blobs") + .join(&text_sha[0..2]) + .join(&text_sha[2..4]) + .join(&text_sha) + .is_file() + ); + assert_eq!(v.head, s.head()); +}