//! 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//! `<dataDir>/client/snapshots/<source>.db` and queries THAT.use std::io::ErrorKind;use std::path::{Path, PathBuf};use rusqlite::{Connection, OpenFlags};usesuper::{SourceState, SourceStatus};usecrate::Layout;/// The three files a live SQLite database can be spread across.const SIDECARS: [&str; 2] = ["-wal", "-shm"];pubfn 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.pubfn 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") {returnSourceStatus::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") {returnSourceStatus::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.pubfn 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.pubfn 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 {letmut 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.pubfn copy_for_read(source_path: &Path, destination: &Path) -> std::io::Result<PathBuf> {ifletSome(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 `<dataDir>/client/snapshots/<name>` and open it.////// The ONE way to read Messages or the call-history daemon's file.pubfn open_live_copy( layout: &Layout, source_path: &Path, name: &str,) -> Result<Connection, SourceStatus> {if !source_path.exists() {returnErr(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.pubfn open_readonly(path: &Path) -> rusqlite::Result<Connection> {Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY)}/// Whether a probe's answer means "ask the person", so nothing retries.pubfn is_waiting_on_the_person(status: &SourceStatus) -> bool { status.state == SourceState::NoPermission}#[cfg(test)]#[path = "snapshot_tests.rs"]mod tests;