//! What a copy on this device holds and what is beside it: the claim that //! makes a whole copy this device's own ([`claim`]), the copy an update set //! aside until its replacement opens (`previous`), the names a download left //! beside a replica ([`file`]) and swept once the real file is there //! ([`sweep`]), why a copy another build stamped was not moved //! ([`not_moved`]), and what this replica owes before it may be erased //! ([`owes_hand_over`], [`may_erase`]). pub mod claim; pub(crate) mod previous; pub mod file; pub mod not_moved; pub(crate) mod unclaimed; use std::path::Path; pub use claim::{claim_snapshot, Claimed}; /// The code of a copy another build stamped that could not be moved in place: /// the page that names why, with no Join beside it (Join discards the file). pub const NOT_MOVED: &str = "not_moved"; /// What this replica holds that the serving Mac does not, in the words a /// refusal says — `None` when deleting the file loses nothing. /// /// Three things, any one of which is enough: /// /// * a queued operation or a held write/delete — a change this device made /// and has not pushed, including one waiting for judge time; /// * a push park in `_parked` — a write the server would not take yet, which /// lives nowhere else; /// * `_meta.handed_over_epoch` below the latest serving epoch this file has /// heard of — a Mac took over and this device has not yet handed it what /// the old one never passed on. "Heard of" is the higher of the latest /// serving fact and `_meta.heard_serving_epoch`, the newest epoch an answer /// named ([`cotton_store::serving::heard_epoch`]): the first answer from a /// Mac that took over names its epoch before its fact arrives. A file that /// has heard of an epoch and holds no mark has never handed over and owes /// too. pub fn owes_hand_over(db: &cotton_store::driver::Driver) -> cotton_store::Result> { let count = |sql: &str| -> cotton_store::Result { Ok(db.get(sql, &[])?.and_then(|row| row.int("n")).unwrap_or(0)) }; let queued = cotton_store::outbox::pending_count(db)? + cotton_store::mint::clock::held_count(db)?; if queued > 0 { return Ok(Some(format!( "this device holds {queued} changes it has not sent yet — they go up first" ))); } let parked = count("SELECT COUNT(*) AS n FROM _parked WHERE direction = 'push'")?; if parked > 0 { return Ok(Some(format!( "this device holds {parked} changes the server has not taken yet — they are settled first" ))); } let Some(meta) = db.get("SELECT workspace_id, handed_over_epoch FROM _meta", &[])? else { return Ok(None); }; let workspace_id = meta.text("workspace_id").unwrap_or_default().to_string(); let Some(latest) = cotton_store::serving::heard_epoch(db, &workspace_id)? else { return Ok(None); }; match meta.int("handed_over_epoch") { Some(mark) if mark >= latest => Ok(None), _ => Ok(Some(format!( "this device has not handed over to the Mac serving since epoch {latest} — it does that first" ))), } } /// MAY A REFUSAL NAMING `named` ERASE THIS REPLICA? (slice 12, the erase /// guard.) Only one from a serving epoch this file has already handed over /// to, and never one from an epoch older than the newest it has heard of: /// /// * `named` above `_meta.handed_over_epoch` (or no mark at all) is a Mac /// that took over and has not yet been handed what only this file holds — a /// device kept at the take-over can have revoked this one there; /// * `named` below [`cotton_store::serving::heard_epoch`] is an old Mac come /// back, whose word about this device is a term nobody reads any more; /// * `named` `None` erases only a file that has heard of no epoch at all — a /// database that never had a serving fact, which is where today's erase /// still holds. /// /// `false` is "ask the person": the screen offers *Keep my copy* or *Erase*. pub fn may_erase(db: &cotton_store::driver::Driver, named: Option) -> cotton_store::Result { let Some(meta) = db.get("SELECT workspace_id, handed_over_epoch FROM _meta", &[])? else { return Ok(named.is_none()); }; let workspace_id = meta.text("workspace_id").unwrap_or_default().to_string(); let heard = cotton_store::serving::heard_epoch(db, &workspace_id)?; Ok(match named { None => heard.is_none(), Some(epoch) => { meta.int("handed_over_epoch").is_some_and(|mark| epoch <= mark) && heard.is_none_or(|heard| epoch >= heard) } }) } /// [`may_erase`] over the replica at `replica`. A missing file holds nothing to /// lose and may be erased; one that will not open or answer may not, because /// "could not tell" is not "nothing to lose". pub fn may_erase_at(replica: &Path, named: Option) -> bool { if !replica.exists() { return true; } cotton_store::driver::Driver::open(replica) .and_then(|db| may_erase(&db, named)) .unwrap_or(false) } /// Delete the leftovers beside a replica that are safe to delete. /// /// Once the real file is back at its name (`previous::restore_set_aside`), /// never before: what is stale depends on whether it is there. pub(crate) fn sweep(replica: &Path) { let Some(directory) = replica.parent() else { return; }; let Some(name) = replica .file_name() .map(|name| name.to_string_lossy().to_string()) else { return; }; let Ok(entries) = std::fs::read_dir(directory) else { // Housekeeping never fails an open. return; }; let names: Vec = entries .filter_map(|entry| entry.ok()) .map(|entry| entry.file_name().to_string_lossy().to_string()) .collect(); for stale in file::stale_downloads(&names, &name, replica.exists()) { // not a copy: a stale download (`file::stale_downloads` never names a copy). let _ = std::fs::remove_file(directory.join(stale)); } } #[cfg(test)] #[path = "mod_tests.rs"] mod tests;