//! The thirty-day Bin for database files, including a removed member's copy. //! Callers close the file and hold their open-file/pass locks across a purge. use crate::{connection::ConnectionFile, layout::Layout}; use chrono::{DateTime, Utc}; /// Purge one expired removal. The connection lock also excludes a concurrent /// re-admission; an IO error leaves the removal in place for the next pass. pub fn purge_removed( layout: &Layout, connection: &ConnectionFile, database: &str, now: DateTime, ) -> Result { connection.purge_removed_with(database, now, |deleted_at| { purge_copy_at(layout, database, deleted_at, now) }) } /// [`purge_removed`] whatever the removal's age: a new admission at the same /// Mac starts a fresh copy, so the removed one goes first. pub fn purge_removed_now( layout: &Layout, connection: &ConnectionFile, database: &str, ) -> Result { connection.purge_removed_now_with(database, |_| purge_copy(layout, database)) } /// One expired removal's files and media. Refuses a copy that is not yet /// due, whatever the caller decided. fn purge_copy_at( layout: &Layout, database: &str, deleted_at: &str, now: DateTime, ) -> Result<(), String> { if !cotton_store::database_bin::purge_due(deleted_at, now) { return Err("The copy is not due for purging.".into()); } purge_copy(layout, database) } /// One database's own files and media only. Both creator deletion and member /// removal use this function: at once for a re-admission, otherwise once /// their retention deadline has elapsed. It does not decide eligibility: the /// lifecycle holder does. pub(crate) fn purge_copy(layout: &Layout, database: &str) -> Result<(), String> { if database.len() != 36 || !database.bytes().enumerate().all(|(at, byte)| { if matches!(at, 8 | 13 | 18 | 23) { byte == b'-' } else { byte.is_ascii_hexdigit() } }) { return Err("Invalid database id for purging a copy.".into()); } let _backup = cotton_store::media_backup_lock::mutating(&layout.client_media_dir()) .map_err(|error| error.to_string())?; // A folder per database holds the SQLite file, its sidecars, interrupted // downloads and saved predecessors. No shared cache root is ever removed. // QUEUE BAD OP 4 and 5: the copy's folder leaves through the one function, which asks the WHOLE folder (every copy // in it, rows the server refused kept beside it, the sites kept on this phone only, anything it does not name): // anything there keeps the folder whole aside, in one rename (a re-admission makes a new copy at once, and nothing // in the old one is lost). Nothing else here removes it. let folder = layout.replica(database).parent().ok_or("The database has no folder")?.to_path_buf(); let media = layout.client_media_dir().join(database); // Kept, its media go with it; a media move that fails fails the purge, so the marker stays and the next try ends it. crate::copy_keep::retire_database(layout, database, &folder, &media, crate::copy_keep::Ending::keep("the database was deleted, or this device removed from it")).map(|_| ()) } #[cfg(test)] #[path = "bin_tests.rs"] mod tests;