feat: the window and the server say each new state in words; a proof cut short is made again; the proof does not mistake the books' own recovery for a changed head
- books ahead of the folder (as after a restore from an older backup) offer a new backup set beside the old one, at the person's request: books.back_up_now new_set, back-up --new-set, a button in the Backups window - a key file lost from a sealed destination stops backups and asks for the passphrase; plain is written only after an explicit removal, which the record keeps (a damaged record falls back to what the folder itself holds) - a restore that is not of the newest backup says so first, in the warning colour, with both dates and how many backups it passes over - a folder a sync service keeps (or may) is said in plain words at set-up and in the window: one computer only, kept offline, a second computer its own - a drill that finds only an older whole backup fails in words - a manifest whose proof was cut short is marked unproven and proven by the next backup, even one with nothing new; a saved backup file is proven whole before it is given its name - the proof compares the journal its pieces make with the head the manifest records before the books open, and lets opening add only after it - the restore tests are split from backups.rs to keep each under a minute; the clone-of-an-old-git-backup test builds the working tree without git Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
9ac47a3f10
commit
344b3b4abe
21 files changed
+2346
-690
No files matched your search
@@ -19,6 +19,10 @@ pub(crate) const VERSION: u32 = 1;
|
||||
pub(crate) const SET_PREFIX: &str = "wanderbooks-";
|
||||
/// The folder of manifests in a set.
|
||||
pub(crate) const MANIFESTS: &str = "manifests";
|
||||
/// The folder of proof marks in a set: one tiny file per manifest whose
|
||||
/// proof (write.rs) came out whole, so a manifest whose proof was cut short
|
||||
/// is proven by the next backup, even one with nothing new to write.
|
||||
pub(crate) const PROOFS: &str = "proofs";
|
||||
/// The words a person can open.
|
||||
pub(crate) const READ_ME: &str = "READ-ME.txt";
|
||||
/// The sealed form's header: how to derive the key.
|
||||
@@ -210,6 +214,11 @@ pub(crate) fn manifest_path(number: u64, sealed: bool) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// The mark that manifest `number` was proven whole.
|
||||
pub(crate) fn proof_path(number: u64) -> String {
|
||||
format!("{PROOFS}/{number:06}")
|
||||
}
|
||||
|
||||
/// The number a manifest file name carries, if it is one.
|
||||
pub(crate) fn manifest_number(name: &str, sealed: bool) -> Option<u64> {
|
||||
let digits = if sealed {
|
||||
|
||||
@@ -228,6 +228,10 @@ pub struct Ctx {
|
||||
/// Tests: nothing is synced to disk (a destination of thousands of
|
||||
/// backups is made quickly; a real one never is).
|
||||
no_sync: bool,
|
||||
/// Tests: the proof after a backup is left out (for the tests that make
|
||||
/// many backups only to see what is written, which prove what they
|
||||
/// made themselves).
|
||||
no_proof: bool,
|
||||
/// Tests: told each read and length look at a file of the set.
|
||||
trace: Option<Trace>,
|
||||
}
|
||||
@@ -297,6 +301,18 @@ impl Ctx {
|
||||
self
|
||||
}
|
||||
|
||||
/// The proof after a backup is left out (tests that make many backups
|
||||
/// only to see what is written; the proof has tests of its own).
|
||||
#[doc(hidden)]
|
||||
pub fn with_proof_skipped(mut self) -> Self {
|
||||
self.no_proof = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn skips_proof(&self) -> bool {
|
||||
self.no_proof
|
||||
}
|
||||
|
||||
pub(crate) fn syncs(&self) -> bool {
|
||||
!self.no_sync
|
||||
}
|
||||
|
||||
@@ -200,6 +200,11 @@ impl Set {
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
/// Whether the proof after a backup came out whole for manifest `n`.
|
||||
pub(crate) fn proven(&self, n: u64) -> bool {
|
||||
matches!(self.io.len(&proof_path(n)), Ok(Some(_)))
|
||||
}
|
||||
|
||||
fn read_small(&self, rel: &str) -> Result<Vec<u8>, BackupError> {
|
||||
match self.io.len(rel)? {
|
||||
Some(len) if len <= SMALL_MAX => {}
|
||||
@@ -547,6 +552,18 @@ pub struct Found {
|
||||
pub sealed: bool,
|
||||
}
|
||||
|
||||
impl Found {
|
||||
/// A set already open (a backup file just written), to be proven.
|
||||
pub(crate) fn of(set: Set, key: Option<BackupKey>, book: &str) -> Found {
|
||||
Found {
|
||||
sealed: set.sealed(),
|
||||
set,
|
||||
key,
|
||||
book: Some(book.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One set of books in a folder, as listed for a person to choose from.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Candidate {
|
||||
@@ -727,6 +744,9 @@ pub struct Verified {
|
||||
pub number: u64,
|
||||
/// When it was made (UTC), as its manifest says.
|
||||
pub written: String,
|
||||
/// The journal's length in the backup: the books built from it hold
|
||||
/// the journal as it was, and may carry the books' own recovery after.
|
||||
pub journal_bytes: u64,
|
||||
/// How many newer backups there are that were passed over: not whole,
|
||||
/// or damaged. Not zero means this is not the newest backup there is.
|
||||
pub passed_over: usize,
|
||||
@@ -922,15 +942,23 @@ fn prove_books(
|
||||
) -> Result<(u64, (u64, String)), BackupError> {
|
||||
ctx.phase("replay");
|
||||
let _stoppable = ctx.stop_flag().map(crate::cancel::scope);
|
||||
if matches!(depth, Depth::Chain) {
|
||||
// The hash chain, up to the head, before the books are opened.
|
||||
let lines = crate::journal::read_and_verify_journal(&scratch.join(crate::JOURNAL_FILE))?;
|
||||
let last = lines.last().map(|l| l.seq).unwrap_or(0);
|
||||
if last != m.head.seq {
|
||||
return Err(BackupError::Damaged(format!(
|
||||
"its journal ends at event {last}, but the backup records another head"
|
||||
)));
|
||||
}
|
||||
// The hash chain, up to the head, before the books are opened: the
|
||||
// journal the pieces make ends at the head the manifest records. (A
|
||||
// backup taken while a call is under way ends with the call's lines
|
||||
// unfinished: opening the books settles it by appending a line, which
|
||||
// is the books' recovery and not the backup's head.)
|
||||
let lines = crate::journal::read_and_verify_journal(&scratch.join(crate::JOURNAL_FILE))?;
|
||||
let (last_seq, last_hash) = match lines.last() {
|
||||
Some(l) => (
|
||||
l.seq,
|
||||
crate::journal::sha256_hex(l.to_canonical().as_bytes()),
|
||||
),
|
||||
None => (0, String::new()),
|
||||
};
|
||||
if last_seq != m.head.seq || (last_seq > 0 && last_hash != m.head.hash) {
|
||||
return Err(BackupError::Damaged(format!(
|
||||
"its journal ends at event {last_seq}, but the backup records another head"
|
||||
)));
|
||||
}
|
||||
let mut store = crate::Store::open(scratch)?;
|
||||
if matches!(depth, Depth::Whole) {
|
||||
@@ -949,7 +977,7 @@ fn prove_books(
|
||||
Depth::Whole => store.verify_blobs()?,
|
||||
Depth::Chain => audit.files_read,
|
||||
};
|
||||
let head = store.head();
|
||||
let opened = store.head();
|
||||
let tins: Vec<String> = store
|
||||
.ledger()
|
||||
.parties()
|
||||
@@ -982,13 +1010,14 @@ fn prove_books(
|
||||
));
|
||||
}
|
||||
}
|
||||
if head.0 != m.head.seq || head.1 != m.head.hash {
|
||||
// Opening may only have added the books' own recovery after the head.
|
||||
if opened.0 < m.head.seq {
|
||||
return Err(BackupError::Damaged(format!(
|
||||
"its journal ends at event {}, but the backup records another head",
|
||||
head.0
|
||||
opened.0
|
||||
)));
|
||||
}
|
||||
Ok((blobs, head))
|
||||
Ok((blobs, (m.head.seq, m.head.hash.clone())))
|
||||
}
|
||||
|
||||
/// The books built from manifest `m` into `scratch` and proven, whole.
|
||||
@@ -1102,6 +1131,7 @@ fn verify_one(
|
||||
book: Some(m.book.clone()),
|
||||
number: m.number,
|
||||
written: m.written.clone(),
|
||||
journal_bytes: m.journal.bytes,
|
||||
passed_over: passed,
|
||||
newest_written: if n == newest {
|
||||
Some(m.written.clone())
|
||||
@@ -1153,9 +1183,9 @@ pub fn verify_found(found: &Found, scratch: &Path, ctx: &Ctx) -> Result<Verified
|
||||
|
||||
/// Whether the journal at `backup` is a byte prefix of the one at `live`
|
||||
/// (a drill: the backup holds the book's history, nothing else).
|
||||
pub fn is_prefix(live: &Path, backup: &Path, ctx: &Ctx) -> Result<bool, BackupError> {
|
||||
pub fn is_prefix(live: &Path, backup: &Path, len: u64, ctx: &Ctx) -> Result<bool, BackupError> {
|
||||
let mut a = std::fs::File::open(live)?;
|
||||
let mut b = std::fs::File::open(backup)?;
|
||||
let mut b = std::io::Read::take(std::fs::File::open(backup)?, len);
|
||||
let (mut x, mut y) = (vec![0u8; 64 * 1024], vec![0u8; 64 * 1024]);
|
||||
loop {
|
||||
ctx.check("comparing journals")?;
|
||||
@@ -1228,7 +1258,12 @@ pub fn drill(
|
||||
v.head.0, e.0
|
||||
)))
|
||||
} else {
|
||||
match is_prefix(live, &v.tree.join(crate::JOURNAL_FILE), ctx) {
|
||||
match is_prefix(
|
||||
live,
|
||||
&v.tree.join(crate::JOURNAL_FILE),
|
||||
v.journal_bytes,
|
||||
ctx,
|
||||
) {
|
||||
Ok(true) => Ok(v.head),
|
||||
Ok(false) => Err(BackupError::Damaged(
|
||||
"its journal isn't this book's history".into(),
|
||||
@@ -1301,9 +1336,9 @@ pub fn prove_local(
|
||||
}
|
||||
ctx.phase("compare");
|
||||
ctx.check("comparing")?;
|
||||
if std::fs::read(book_dir.join(crate::JOURNAL_FILE)).unwrap_or_default()
|
||||
!= std::fs::read(v.tree.join(crate::JOURNAL_FILE))?
|
||||
{
|
||||
let held = std::fs::read(v.tree.join(crate::JOURNAL_FILE))?;
|
||||
let held = &held[..(v.journal_bytes as usize).min(held.len())];
|
||||
if std::fs::read(book_dir.join(crate::JOURNAL_FILE)).unwrap_or_default() != held {
|
||||
return Err(BackupError::Damaged(
|
||||
"its journal isn't the books' own, byte for byte".into(),
|
||||
));
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
|
||||
use super::cut::Cut;
|
||||
use super::io::{Fill, Put, PutKind, SetIo};
|
||||
use super::read::Set;
|
||||
use super::write::{Written, write_into_set};
|
||||
use super::read::{Found, Set, verify_found};
|
||||
use super::write::{Written, proof_scratch, write_into_set};
|
||||
use super::{BackupError, Ctx};
|
||||
use std::collections::BTreeSet;
|
||||
use std::io::Read;
|
||||
@@ -173,6 +173,19 @@ pub fn save_file(
|
||||
.finish()
|
||||
.map_err(|e| BackupError::Io(std::io::Error::other(e).kind()))?;
|
||||
file.sync_all()?;
|
||||
// Read back as a restore would read it, and proven whole (every
|
||||
// piece re-hashed, the chain replayed, the books opened), before it
|
||||
// is given its name: a file by that name is a backup that proved.
|
||||
let reader = Set {
|
||||
io: open(&aside, ctx)?,
|
||||
keys: key.map(|k| k.keys()),
|
||||
ctx: ctx.clone(),
|
||||
};
|
||||
let found = Found::of(reader, key.cloned(), cut.book());
|
||||
let scratch = proof_scratch(cut)?;
|
||||
let proven = verify_found(&found, &scratch, ctx);
|
||||
let _ = std::fs::remove_dir_all(&scratch);
|
||||
proven?;
|
||||
Ok(written)
|
||||
});
|
||||
match result {
|
||||
|
||||
@@ -804,14 +804,38 @@ fn put_manifest(
|
||||
|
||||
/// A folder for the proof's scratch books: private, in the book's own
|
||||
/// `ops/`, named for this call.
|
||||
fn proof_scratch(cut: &Cut) -> Result<PathBuf, BackupError> {
|
||||
let ops = crate::root::private_book_folder(cut.dir(), crate::ops::OPS_DIR)
|
||||
.map_err(|e| BackupError::Damaged(e.to_string()))?;
|
||||
pub(crate) fn proof_scratch(cut: &Cut) -> Result<PathBuf, BackupError> {
|
||||
let ops = cut.dir().join(crate::ops::OPS_DIR);
|
||||
if !ops.is_dir() {
|
||||
// Made private by the one that makes it; one that lost a race to
|
||||
// make it finds it there.
|
||||
let made = crate::root::private_book_folder(cut.dir(), crate::ops::OPS_DIR);
|
||||
if !ops.is_dir() {
|
||||
return Err(BackupError::Damaged(
|
||||
made.err().map(|e| e.to_string()).unwrap_or_default(),
|
||||
));
|
||||
}
|
||||
}
|
||||
let mut r = [0u8; 6];
|
||||
let _ = getrandom::getrandom(&mut r);
|
||||
getrandom::getrandom(&mut r).map_err(|e| BackupError::Io(std::io::Error::from(e).kind()))?;
|
||||
Ok(ops.join(format!("proof-{}", hex::encode(r))))
|
||||
}
|
||||
|
||||
/// Mark manifest `number` as proven whole: a tiny file put once. A mark
|
||||
/// that can't be made (a full disk) costs only a proof again at the next
|
||||
/// backup; a stop is a stop.
|
||||
fn mark_proven(set: &Set, number: u64) -> Result<(), BackupError> {
|
||||
match set.io.put(&proof_path(number), PutKind::Once, &mut |w| {
|
||||
w.write_all(b"proven whole\n")
|
||||
}) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => match uncarry(e) {
|
||||
stop @ (BackupError::Died | BackupError::Cancelled(_)) => Err(stop),
|
||||
_ => Ok(()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// The most times a backup is written again to repair what its own proof
|
||||
/// found damaged.
|
||||
const REPAIR_ROUNDS: usize = 2;
|
||||
@@ -835,6 +859,7 @@ fn prove_and_repair(
|
||||
ctx.check("proving the backup")?;
|
||||
let proof = super::read::prove_written(set, number, &added, &proof_scratch(cut)?)?;
|
||||
if proof.damaged.is_empty() {
|
||||
mark_proven(set, number)?;
|
||||
return Ok(Written {
|
||||
number,
|
||||
head: cut.head().clone(),
|
||||
@@ -893,6 +918,7 @@ pub(crate) fn write_into_set(
|
||||
prove: bool,
|
||||
) -> Result<Written, BackupError> {
|
||||
ensure_markers(set)?;
|
||||
let prove = prove && !ctx.skips_proof();
|
||||
for _ in 0..ATTEMPTS {
|
||||
ctx.check("backing up")?;
|
||||
let numbers = set.numbers()?;
|
||||
@@ -903,6 +929,15 @@ pub(crate) fn write_into_set(
|
||||
if let Some(p) = &prev
|
||||
&& manifest.same_content(p)
|
||||
{
|
||||
// Nothing new. But a manifest whose proof was cut short (a
|
||||
// close, a stop, a death) is not yet known whole: proven now.
|
||||
if prove && !set.proven(p.number) {
|
||||
let w = prove_and_repair(set, cut, p.number, BTreeSet::new(), Vec::new(), ctx)?;
|
||||
return Ok(Written {
|
||||
new: w.number != p.number,
|
||||
..w
|
||||
});
|
||||
}
|
||||
return Ok(Written {
|
||||
number: p.number,
|
||||
head: cut.head().clone(),
|
||||
|
||||
@@ -1148,8 +1148,13 @@ pub struct RestorePreview {
|
||||
pub origin: Option<String>,
|
||||
/// Whether it is protected by a passphrase.
|
||||
pub sealed: bool,
|
||||
/// Newer backups in it that were not whole and were passed over.
|
||||
/// Newer backups in it that were not whole or were damaged and were
|
||||
/// passed over: not zero means this is not the newest backup there is.
|
||||
pub passed_over: u64,
|
||||
/// When this backup was made (UTC).
|
||||
pub written: Option<String>,
|
||||
/// When the newest backup there is was made (UTC), if it can be read.
|
||||
pub newest_written: Option<String>,
|
||||
}
|
||||
|
||||
/// What [`Root::restore`] did.
|
||||
@@ -1258,6 +1263,8 @@ impl Root {
|
||||
origin: v.book,
|
||||
sealed: v.key.is_some(),
|
||||
passed_over: v.passed_over as u64,
|
||||
written: Some(v.written.clone()),
|
||||
newest_written: v.newest_written.clone(),
|
||||
})
|
||||
})()
|
||||
.map_err(|e| self.hide(e))
|
||||
|
||||
Reference in new issue
Block a user