//! Copy before you read. Every source, every time. //! //! The call-history daemon and Photos hold their databases open in WAL mode //! and write to them continuously. Reading one in place means contending with //! the process that owns it and, worse, reading a file that changes underneath //! the cursor. So each pass copies the database — and its `-wal` and `-shm`, or //! the copy is missing everything committed since the last checkpoint — into //! `/client/snapshots/.db` and queries THAT. use std::io::ErrorKind; use std::path::{Path, PathBuf}; use rusqlite::{Connection, OpenFlags}; use super::{SourceState, SourceStatus}; use crate::Layout; /// The three files a live SQLite database can be spread across. const SIDECARS: [&str; 2] = ["-wal", "-shm"]; pub fn snapshot_path_for(layout: &Layout, name: &str) -> PathBuf { layout.snapshots_dir().join(name) } /// What a failed open MEANS, in the vocabulary the panel renders. /// /// A permission denial and SQLite's own `SQLITE_CANTOPEN` on a file that EXISTS /// are the same thing to a person: macOS has not been told this app may read /// that file. "Not found" is a different sentence entirely — nothing to read, /// which is the ordinary state of a Mac with no call history. pub fn classify_read_failure(message: &str, source_path: &Path) -> SourceStatus { let lower = message.to_ascii_lowercase(); if lower.contains("no such file") || lower.contains("not found") || lower.contains("nonexistent") { return SourceStatus::not_found(format!("{} is not on this Mac.", source_path.display())); } if lower.contains("permission denied") || lower.contains("operation not permitted") || lower.contains("sqlite_cantopen") || lower.contains("unable to open database") || lower.contains("not authorized") { return SourceStatus::no_permission( "macOS has not given Cotton permission to read this file. \ Add Cotton to Full Disk Access, then try again.", ); } SourceStatus::error(message.to_string()) } /// The same question asked of an `io::Error`, which knows its own kind and does /// not need a message read for it. pub fn classify_io_failure(error: &std::io::Error, source_path: &Path) -> SourceStatus { match error.kind() { ErrorKind::NotFound => { SourceStatus::not_found(format!("{} is not on this Mac.", source_path.display())) } ErrorKind::PermissionDenied => SourceStatus::no_permission( "macOS has not given Cotton permission to read this file. \ Add Cotton to Full Disk Access, then try again.", ), _ => classify_read_failure(&error.to_string(), source_path), } } /// Remove a snapshot and its sidecars — after a pass, and before the next one. pub fn discard_snapshot(destination: &Path) { let _ = std::fs::remove_file(destination); for suffix in SIDECARS { let _ = std::fs::remove_file(sidecar(destination, suffix)); } } fn sidecar(path: &Path, suffix: &str) -> PathBuf { let mut name = path.as_os_str().to_os_string(); name.push(suffix); PathBuf::from(name) } /// Copy a live database (and its sidecars) into the snapshot directory. /// /// The failure carries the OS error so [`classify_io_failure`] can say what it /// meant: a permission failure must reach the panel as a state, not as a stack. pub fn copy_for_read(source_path: &Path, destination: &Path) -> std::io::Result { if let Some(parent) = destination.parent() { std::fs::create_dir_all(parent)?; } // The previous pass's copy goes first, sidecars included: a stale `-wal` // beside a fresh database is a file SQLite will happily replay the wrong // pages out of. discard_snapshot(destination); std::fs::copy(source_path, destination)?; for suffix in SIDECARS { let from = sidecar(source_path, suffix); if from.exists() { std::fs::copy(&from, sidecar(destination, suffix))?; } } Ok(destination.to_path_buf()) } /// Copy a live database into `/client/snapshots/` and open it. /// /// The ONE way to read Messages or the call-history daemon's file. pub fn open_live_copy( layout: &Layout, source_path: &Path, name: &str, ) -> Result { if !source_path.exists() { return Err(SourceStatus::not_found(format!( "{} is not on this Mac.", source_path.display() ))); } let destination = snapshot_path_for(layout, name); let copied = copy_for_read(source_path, &destination) .map_err(|error| classify_io_failure(&error, source_path))?; open_readonly(&copied).map_err(|error| classify_read_failure(&error.to_string(), source_path)) } /// A plain read-only open of a file this process made. pub fn open_readonly(path: &Path) -> rusqlite::Result { Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY) } /// Whether a probe's answer means "ask the person", so nothing retries. pub fn is_waiting_on_the_person(status: &SourceStatus) -> bool { status.state == SourceState::NoPermission } #[cfg(test)] #[path = "snapshot_tests.rs"] mod tests;