fix(store): a drill and a preview keep the stored files the store reads when it verifies the books

The store's own verification reads the text an extraction recorded as a stored
file, so a verification that left the stored files out would not prove books
with extracted text. Every proof now builds the books with their stored files,
as a restore does, and a test holds it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 17:49:22 -07:00
1 parent a6cd89d2a6
commit 5d5f94e8dc
6 files changed
+69 -56

No files matched your search

+2 -2
View File
@@ -364,7 +364,7 @@ async fn backed_up(c: &Client, within: Duration) -> Value {
fn proven(home: &Home, source: &Path, tag: &str) -> (u64, String) { fn proven(home: &Home, source: &Path, tag: &str) -> (u64, String) {
let scratch = home.outside(&format!("scratch-{tag}")); let scratch = home.outside(&format!("scratch-{tag}"));
let found = find(source, None, &Ctx::new()).unwrap_or_else(|e| panic!("{tag}: {e}")); 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}")) .unwrap_or_else(|e| panic!("{tag}: {e}"))
.head .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). // Its journal is the live journal's beginning (a drill's check).
let scratch = home.outside("scratch-prefix"); let scratch = home.outside("scratch-prefix");
let found = find(&folder, None, &Ctx::new()).unwrap(); 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!( assert!(
std::fs::read(home.book_dir("acme").join(ledger_store::JOURNAL_FILE)) std::fs::read(home.book_dir("acme").join(ledger_store::JOURNAL_FILE))
.unwrap() .unwrap()
+1 -1
View File
@@ -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}; use ledger_store::backup::{Ctx, find, verify_found};
let scratch = home.dir.path().join(format!("scratch-{tag}")); let scratch = home.dir.path().join(format!("scratch-{tag}"));
let found = find(disk, None, &Ctx::new()).unwrap_or_else(|e| panic!("{tag}: {e}")); 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}")) .unwrap_or_else(|e| panic!("{tag}: {e}"))
.head .head
} }
+1 -1
View File
@@ -5185,7 +5185,7 @@ mod tests {
let found = let found =
ledger_store::backup::find(&dir.join(ledger_store::backup::LOCAL_DIR), None, &ctx) ledger_store::backup::find(&dir.join(ledger_store::backup::LOCAL_DIR), None, &ctx)
.expect("a local backup is there"); .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 /// plan/served.md 3.9's `format_upgrade_waits_for_consent`, the
+26 -46
View File
@@ -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 /// 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 /// access log, the vault, the keys and every stored file; every piece
/// stored file; every piece proven as it is read. Without `with_files` the /// proven as it is read. (The store reads some stored files itself when
/// stored files are re-hashed and not kept (a drill). Returns the hashes of /// it verifies the books, so a drill keeps them too, as a restore does.)
/// the stored files proven. /// Returns the hashes of the stored files proven.
fn materialize( fn materialize(set: &Set, m: &Manifest, scratch: &Path) -> Result<BTreeSet<String>, BackupError> {
set: &Set,
m: &Manifest,
scratch: &Path,
with_files: bool,
) -> Result<BTreeSet<String>, BackupError> {
let ctx = &set.ctx; let ctx = &set.ctx;
std::fs::create_dir_all(scratch)?; std::fs::create_dir_all(scratch)?;
for (series, kind, name) in [ for (series, kind, name) in [
@@ -524,23 +519,19 @@ fn materialize(
for line in set.index(m)? { for line in set.index(m)? {
ctx.check("reading the backup")?; ctx.check("reading the backup")?;
let rel = piece_path(set.keys.as_ref(), Kind::File, 0, &line.sha256); let rel = piece_path(set.keys.as_ref(), Kind::File, 0, &line.sha256);
if with_files { let blob = scratch
let blob = scratch .join(crate::store::BLOBS_DIR)
.join(crate::store::BLOBS_DIR) .join(&line.sha256[0..2])
.join(&line.sha256[0..2]) .join(&line.sha256[2..4]);
.join(&line.sha256[2..4]); std::fs::create_dir_all(&blob)?;
std::fs::create_dir_all(&blob)?; let mut f = std::fs::OpenOptions::new()
let mut f = std::fs::OpenOptions::new() .write(true)
.write(true) .create_new(true)
.create_new(true) .open(blob.join(&line.sha256))?;
.open(blob.join(&line.sha256))?; set.read_piece(&rel, Kind::File, line.bytes, &line.sha256, &mut |c| {
set.read_piece(&rel, Kind::File, line.bytes, &line.sha256, &mut |c| { f.write_all(c)
f.write_all(c) })?;
})?; f.sync_all()?;
f.sync_all()?;
} else {
set.read_piece(&rel, Kind::File, line.bytes, &line.sha256, &mut |_| Ok(()))?;
}
proven.insert(line.sha256); proven.insert(line.sha256);
} }
Ok(proven) Ok(proven)
@@ -554,7 +545,6 @@ fn prove_books(
m: &Manifest, m: &Manifest,
scratch: &Path, scratch: &Path,
proven_files: &BTreeSet<String>, proven_files: &BTreeSet<String>,
with_files: bool,
ctx: &Ctx, ctx: &Ctx,
) -> Result<(u64, (u64, String)), BackupError> { ) -> Result<(u64, (u64, String)), BackupError> {
ctx.phase("replay"); ctx.phase("replay");
@@ -570,11 +560,7 @@ fn prove_books(
short(missing) short(missing)
))); )));
} }
let blobs = if with_files { let blobs = store.verify_blobs()?;
store.verify_blobs()?
} else {
proven_files.len() as u64
};
let head = store.head(); let head = store.head();
let tins: Vec<String> = store let tins: Vec<String> = store
.ledger() .ledger()
@@ -618,15 +604,9 @@ fn prove_books(
/// Prove the backup found, building its books in `scratch` (a new folder): /// 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 /// 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 /// passed over and said. Every stored file is re-hashed and kept in the
/// the scratch folder (a restore); without, they are re-hashed and not /// scratch folder (a restore moves them from it; a drill removes it).
/// kept (a drill). pub fn verify_found(found: &Found, scratch: &Path, ctx: &Ctx) -> Result<Verified, BackupError> {
pub fn verify_found(
found: &Found,
scratch: &Path,
with_files: bool,
ctx: &Ctx,
) -> Result<Verified, BackupError> {
if scratch.exists() { if scratch.exists() {
return Err(BackupError::Damaged("its scratch folder is in use".into())); 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()? { for m in set.whole_ones()? {
ctx.phase("fetch"); ctx.phase("fetch");
ctx.check("verifying")?; ctx.check("verifying")?;
let attempt = materialize(set, &m, scratch, with_files) let attempt =
.and_then(|files| prove_books(&m, scratch, &files, with_files, ctx)); materialize(set, &m, scratch).and_then(|files| prove_books(&m, scratch, &files, ctx));
match attempt { match attempt {
Ok((blobs, head)) => { Ok((blobs, head)) => {
return Ok(Verified { return Ok(Verified {
@@ -712,7 +692,7 @@ pub fn drill(
key: dest.key.clone(), key: dest.key.clone(),
set, 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"); ctx.phase("prefix");
if v.book.as_deref() != Some(book) { if v.book.as_deref() != Some(book) {
return Err(BackupError::Elsewhere); return Err(BackupError::Elsewhere);
@@ -755,7 +735,7 @@ pub fn prove_local(
key: None, key: None,
set, 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 { if v.head != *head {
return Err(BackupError::Damaged(format!( return Err(BackupError::Damaged(format!(
"it holds the books through event {}, and they are at event {}", "it holds the books through event {}, and they are at event {}",
+3 -4
View File
@@ -1182,7 +1182,6 @@ impl Root {
books: &Path, books: &Path,
id: &BookId, id: &BookId,
stop: &JobStop, stop: &JobStop,
with_files: bool,
) -> Result<(PathBuf, crate::backup::Verified), RootError> { ) -> Result<(PathBuf, crate::backup::Verified), RootError> {
stop.at("fetch", "restored")?; stop.at("fetch", "restored")?;
let ctx = crate::backup::Ctx::new().with_stop(stop.flag()); 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 scratch = books.join(format!("{RESTORE_PREFIX}{id}-{}", std::process::id()));
let proven = crate::backup::find(source, passphrase, &ctx) 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 { match proven {
Ok(v) => Ok((scratch, v)), Ok(v) => Ok((scratch, v)),
Err(e) => { Err(e) => {
@@ -1241,7 +1240,7 @@ impl Root {
(|| { (|| {
let _lock = self.books_lock(stop, "restored")?; let _lock = self.books_lock(stop, "restored")?;
let (books, _) = self.private_subdir(BOOKS_DIR, false)?; 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 = scratch.join(VAULT_DIR);
let vault_files = if vault.is_dir() { let vault_files = if vault.is_dir() {
std::fs::read_dir(&vault) std::fs::read_dir(&vault)
@@ -1353,7 +1352,7 @@ impl Root {
}; };
stop.at("start", "restored")?; stop.at("start", "restored")?;
let _scope = crate::cancel::scope(stop.flag()); 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 staging = books.join(format!("{RESTORE_PREFIX}{id}-{}-book", std::process::id()));
let built = (|| -> Result<RestoreReport, RootError> { let built = (|| -> Result<RestoreReport, RootError> {
stop.at("copy", "restored")?; stop.at("copy", "restored")?;
+36 -2
View File
@@ -106,7 +106,7 @@ fn prove(
name: &str, name: &str,
) -> Result<ledger_store::backup::Verified, BackupError> { ) -> Result<ledger_store::backup::Verified, BackupError> {
let found = find(source, pass, &Ctx::new())?; 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] #[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). // Found once (a sealed backup's key takes a moment to derive).
let found = find(&disk, pass, &Ctx::new()).unwrap(); let found = find(&disk, pass, &Ctx::new()).unwrap();
let prove = |_: &Path, _: Option<&str>, root: &Path, name: &str| { 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) let set = std::fs::read_dir(&disk)
.unwrap() .unwrap()
@@ -687,3 +687,37 @@ fn a_loser_of_a_manifest_number_goes_on_from_the_winner() {
); );
prove(&disk, None, dir.path(), "v").unwrap(); 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());
}