//! What an erasure may unlink. //! //! An erasure is a fact in ONE database, and the bytes it may take are that //! database's alone. Every database's media sits under its own prefix — //! `/…` in the object store, `media//…` on a device — so the //! rule is a wall: a key outside the ERASING file's prefix is never unlinked, //! whatever the erasure's row says. //! //! **The file is the erasing file's own identity, never the key's first //! segment.** A key names a folder; reading the folder off the key and then //! checking the key against it proves nothing — a row carrying another //! database's key would pass its own test. //! //! The media-erasure hook (`crate::media_erasure`) asks [`may_unlink_key`] //! before a dead key enters this device's unlink queue; the halves that remove //! bytes ask these again — one for a key in an object store, one for a file on //! a device. use std::path::{Component, Path, PathBuf}; use rusqlite::types::Value as SqlValue; use crate::driver::Driver; use crate::error::Result; use crate::file_identity::FileId; /// May an erasure in `erasing_file` unlink the object at `key`? /// /// The prefix alone, asked of the ERASING file's id: the key sits under /// `/` and nowhere climbs out of it. Whether the rest of the key is /// well formed is the wall's question (`cotton_core::media::keys`, which this /// crate cannot name); a malformed key under the right prefix is still this /// database's own to take. pub fn may_unlink_key(erasing_file: &FileId, key: &str) -> bool { key.strip_prefix(erasing_file.as_str()) .and_then(|rest| rest.strip_prefix('/')) .is_some_and(|rest| !rest.is_empty() && rest.split('/').all(|part| part != "..")) } /// The file on THIS DEVICE an erasure in `erasing_file` may unlink for `key`, /// or `None` when it may unlink nothing. /// /// Three refusals, each `None`: the key is outside the erasing file's prefix; /// the ledger's path for it is not under `media_dir//` (a path is a /// column, and a column can say anything); or another row of this ledger names /// the same file — the device store is content-addressed, so two keys with the /// same bytes share one file, and taking it for one would take it from the /// other. pub fn may_unlink_local( db: &Driver, erasing_file: &FileId, media_dir: &Path, key: &str, ) -> Result> { if !may_unlink_key(erasing_file, key) { return Ok(None); } let Some(local) = db .get( "SELECT local_path FROM _media WHERE key = ? AND local_path IS NOT NULL", &[SqlValue::Text(key.to_string())], )? .and_then(|row| row.text("local_path").map(str::to_string)) else { return Ok(None); }; let path = PathBuf::from(&local); // No `..` — a path that climbs is not under anything, whatever it starts // with. let climbs = path .components() .any(|part| matches!(part, Component::ParentDir)); if climbs || !path.starts_with(media_dir.join(erasing_file.as_str())) { return Ok(None); } let shared = db .get( "SELECT COUNT(*) AS n FROM _media WHERE local_path = ? AND key <> ?", &[SqlValue::Text(local), SqlValue::Text(key.to_string())], )? .and_then(|row| row.int("n")) .unwrap_or(0); Ok((shared == 0).then_some(path)) } #[cfg(test)] #[path = "media_unlink_tests.rs"] mod tests;