//! A cut of the books: what a backup is made from, taken in one look //! under the store's lock, and read from the book's folder after it. //! //! The journal is append-only and a stored file never changes once it is //! there, so what the lock has to fix is small: how long the journal is //! (and that its last line is the head), how much of the access log is //! whole lines, and the vault and the keys, which can change. Everything //! else is read afterwards, outside the lock, and cannot move: the bytes //! of the journal before the cut's length are the same bytes a moment //! later. use super::form::{IndexLine, is_plain_name, is_sha}; use super::{BackupError, Ctx}; use sha2::{Digest, Sha256}; use std::io::{Read, Seek, SeekFrom}; use std::path::{Path, PathBuf}; /// A cut of one book. Take it with the store's lock held ([`Cut::take`]). #[derive(Debug, Clone)] pub struct Cut { book: String, dir: PathBuf, head: (u64, String), pub(crate) journal_len: u64, pub(crate) access_len: u64, pub(crate) vault: Vec<(String, Vec)>, pub(crate) keys: Vec<(String, Vec)>, } /// The bytes of the last line of `path`, which is `len` long and ends in a /// newline, read from the end. fn last_line(path: &Path, len: u64) -> std::io::Result> { let mut f = std::fs::File::open(path)?; let mut tail: Vec = Vec::new(); let mut at = len; // Without the final newline; looking back for the one before it. loop { let step = at.min(64 * 1024); at -= step; f.seek(SeekFrom::Start(at))?; let mut chunk = vec![0u8; step as usize]; f.read_exact(&mut chunk)?; chunk.extend_from_slice(&tail); tail = chunk; if tail.last() != Some(&b'\n') { return Err(std::io::Error::new( std::io::ErrorKind::InvalidData, "the journal does not end in a whole line", )); } let body = &tail[..tail.len() - 1]; if let Some(i) = body.iter().rposition(|b| *b == b'\n') { return Ok(body[i + 1..].to_vec()); } if at == 0 { return Ok(body.to_vec()); } } } /// How much of `path`, `len` long, is whole lines. fn whole_lines(path: &Path, len: u64) -> std::io::Result { let mut f = std::fs::File::open(path)?; let mut at = len; while at > 0 { let step = at.min(64 * 1024); at -= step; f.seek(SeekFrom::Start(at))?; let mut chunk = vec![0u8; step as usize]; f.read_exact(&mut chunk)?; if let Some(i) = chunk.iter().rposition(|b| *b == b'\n') { return Ok(at + i as u64 + 1); } } Ok(0) } /// What the exactly-once writes of the vault (ledger-vault `stage`, /// `settle`) leave in its folder while a call is between its first step /// and its last: a new record staged under `NAME.staged-TAG`, an old one /// set aside under `NAME.purging-TAG`. enum Scratch<'a> { /// A record staged for a call that has not committed: not the books' /// yet, and nothing the journal at the cut names. Staged, /// A record set aside for a purge that has not committed: still the /// books' record as the journal at the cut stands, under the name it /// has when it is not set aside (`own`). Purging { own: &'a str }, } fn scratch_of(name: &str) -> Option> { if name.contains(".staged-") { return Some(Scratch::Staged); } name.split_once(".purging-") .map(|(stem, _)| Scratch::Purging { own: stem }) } /// The plain files of a flat folder of a book, by name, with their bytes. /// With `vault`, the vault's scratch files are met as they are meant (see /// [`Scratch`]): never backed up as records of their own, so a backup cut /// while a write of the vault is under way holds the vault as the journal /// at the cut says it is. fn flat_files(dir: &Path, vault: bool) -> Result)>, BackupError> { let entries = match std::fs::read_dir(dir) { Ok(e) => e, Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()), Err(e) => return Err(e.into()), }; let mut named: Vec<(String, PathBuf)> = Vec::new(); for entry in entries { let entry = entry?; let name = entry.file_name().to_string_lossy().into_owned(); let meta = std::fs::symlink_metadata(entry.path())?; if !meta.is_file() || !is_plain_name(&name) { return Err(BackupError::Damaged(format!( "a folder of the books holds something no book keeps ({name:?})" ))); } named.push((name, entry.path())); } let held: std::collections::BTreeSet = named.iter().map(|(n, _)| n.clone()).collect(); let mut out = Vec::new(); for (name, path) in named { let name = match vault.then(|| scratch_of(&name)).flatten() { None => name, Some(Scratch::Staged) => continue, Some(Scratch::Purging { own }) => { let own = format!("{own}.bin"); if held.contains(&own) { // The record was written anew: that one is the books'. continue; } own } }; out.push((name, std::fs::read(path)?)); } out.sort(); Ok(out) } impl Cut { /// Take a cut of the book `book` kept in `dir`, whose journal's head /// is `head`. The caller holds the store's lock, so the journal is /// whole lines and the vault matches it. pub fn take(book: &str, dir: &Path, head: (u64, String)) -> Result { let journal = dir.join(crate::JOURNAL_FILE); let journal_len = match std::fs::metadata(&journal) { Ok(m) => m.len(), Err(e) if e.kind() == std::io::ErrorKind::NotFound => 0, Err(e) => return Err(e.into()), }; if head.0 == 0 { if journal_len != 0 { return Err(BackupError::Damaged( "the journal has lines the books don't count".into(), )); } } else { let line = last_line(&journal, journal_len)?; if hex::encode(Sha256::digest(&line)) != head.1 { return Err(BackupError::Damaged( "the journal's last line isn't the books' head".into(), )); } } let access = dir.join(crate::access::ACCESS_FILE); let access_len = match std::fs::metadata(&access) { Ok(m) => whole_lines(&access, m.len())?, Err(e) if e.kind() == std::io::ErrorKind::NotFound => 0, Err(e) => return Err(e.into()), }; Ok(Cut { book: book.to_string(), dir: dir.to_path_buf(), head, journal_len, access_len, vault: flat_files(&dir.join(crate::root::VAULT_DIR), true)?, keys: flat_files(&dir.join(crate::root::KEYS_DIR), false)?, }) } /// The book's folder the cut was taken from. pub(crate) fn dir(&self) -> &Path { &self.dir } /// The books' id. pub fn book(&self) -> &str { &self.book } /// The journal's head at the cut: seq and hash. pub fn head(&self) -> &(u64, String) { &self.head } pub(crate) fn journal_path(&self) -> PathBuf { self.dir.join(crate::JOURNAL_FILE) } pub(crate) fn access_path(&self) -> PathBuf { self.dir.join(crate::access::ACCESS_FILE) } /// The stored files, by hash, with their lengths: those named as this /// program names them (`blobs/ab/cd/`), nothing else. pub(crate) fn stored_files(&self, ctx: &Ctx) -> Result, BackupError> { let root = self.dir.join(crate::store::BLOBS_DIR); let mut out = Vec::new(); let level = |p: &Path| -> std::io::Result> { match std::fs::read_dir(p) { Ok(e) => e.collect(), Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()), Err(e) => Err(e), } }; for a in level(&root)? { ctx.check("listing the stored files")?; let an = a.file_name().to_string_lossy().into_owned(); if an.len() != 2 || !a.path().is_dir() { continue; } for b in level(&a.path())? { let bn = b.file_name().to_string_lossy().into_owned(); if bn.len() != 2 || !b.path().is_dir() { continue; } for f in level(&b.path())? { let name = f.file_name().to_string_lossy().into_owned(); let meta = std::fs::symlink_metadata(f.path())?; if meta.is_file() && is_sha(&name) && name.starts_with(&an) && name[2..].starts_with(&bn) { out.push(IndexLine { sha256: name, bytes: meta.len(), }); } } } } out.sort_by(|x, y| x.sha256.cmp(&y.sha256)); Ok(out) } /// Where a stored file is in the book. pub(crate) fn blob_path(&self, sha: &str) -> PathBuf { self.dir .join(crate::store::BLOBS_DIR) .join(&sha[0..2]) .join(&sha[2..4]) .join(sha) } }