Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Bounds: grid pictures stop below a free-space floor, contacts read once per run, Paste alone grants, a lock that always holds
Yashwanth Kamireddi056b1beb8fab
diff --git a/.uiverify/stub.mjs b/.uiverify/stub.mjsindex 50ca15e..e907735 100644--- a/.uiverify/stub.mjs+++ b/.uiverify/stub.mjs@@ -1026,7 +1026,8 @@ export function installStub({ screen, services, twoFactor, calendarFixture, notereturn null;}// The desktop's clipboard as the backend reads it: files copied in the file manager, or text.- case "clipboard_read": return { files: window.__clipFiles ?? [], text: window.__clipText ?? null };+ case "clipboard_read":+ case "clipboard_paste": return { files: window.__clipFiles ?? [], text: window.__clipText ?? null };case "drive_upload": (window.__driveUploads ??= []).push(args); return [];case "storage_info": return { usedBytes: 182196857, totalBytes: 5368709120 };// The local copy's room, as settings.rs measures it; clearing takes away only the cached pictures.diff --git a/SECURITY.md b/SECURITY.mdindex 5fababb..4277181 100644--- a/SECURITY.md+++ b/SECURITY.md@@ -111,8 +111,9 @@ never handed to the account signing in.- **The window** loads only Skykeep's own bundled page, never a website, and may askTauri for no more than it uses: events, its version and a window title.- **Files on this computer** are read or written only when you chose them: in an open or save- dialog Skykeep shows from its Rust side, by dropping them on the window, or by opening them- with Skykeep. Each choice is recorded there (`src-tauri/src/grants.rs`) for ten minutes, a+ dialog Skykeep shows from its Rust side, by dropping them on the window, by pasting them into+ Drive after copying them in your file manager (only Paste itself grants them; the window+ looking at what is on the clipboard grants nothing), or by opening them with Skykeep. Each choice is recorded there (`src-tauri/src/grants.rs`) for ten minutes, asave for one write only, and every command that reads or writes a file (uploads, imports,attachments, exports) refuses a path that is not one of them, compared with `..` and linksresolved. So even script running in the window could not name a file of yours on its own.diff --git a/crates/quince-icloud/src/contacts.rs b/crates/quince-icloud/src/contacts.rsindex f8efd0f..bf82d67 100644--- a/crates/quince-icloud/src/contacts.rs+++ b/crates/quince-icloud/src/contacts.rs@@ -541,6 +541,32 @@ pub async fn fetch_with_tokens())}+/// Every card as Apple wrote it, with the tokens a write needs, in one read of the book: what+/// a run of queued changes is sent from, rather than the whole book read again for each one.+pub async fn cards_with_tokens(+ http: &reqwest::Client,+ root: &str,+ params: Vec<(String, String)>,+) -> Result<(Vec<Value>, Tokens)> {+ let body = startup(http, root, params).await?;+ let token = |key: &str| {+ body.get(key)+ .and_then(Value::as_str)+ .map(ToString::to_string)+ .ok_or_else(|| Error::Protocol(format!("/co/startup carried no {key}")))+ };+ let tokens = Tokens {+ sync: token("syncToken")?,+ pref: token("prefToken")?,+ };+ let cards = body+ .get("contacts")+ .and_then(Value::as_array)+ .cloned()+ .unwrap_or_default();+ Ok((cards, tokens))+}+/// [`card_written`] for a card without the rest of its name and work: those stay as Apple/// has them.fn card_merged(contact: &Contact, existing: Option<&Value>, base: Option<&Contact>) -> Value {diff --git a/src-tauri/src/clipboard.rs b/src-tauri/src/clipboard.rsindex bf14872..96bdbc7 100644--- a/src-tauri/src/clipboard.rs+++ b/src-tauri/src/clipboard.rs@@ -2,6 +2,11 @@//! (Files, Dolphin, Thunar) are there as a `text/uri-list`, which the window's own clipboard//! does not hand a page outside an editable field. Read here through GTK, the window's//! toolkit, which speaks Wayland's clipboard and X11's alike.+//!+//! Looking is not pasting. [`clipboard_read`] says what is there — for the folder's menu to+//! offer Paste — and grants nothing; only [`clipboard_paste`], Drive's Paste itself, grants the+//! files copied for reading (grants.rs), as a drop does. A look at the clipboard used to grant+//! every file on it, and the window looks each time a folder's menu opens.use serde::Serialize;@@ -13,11 +18,12 @@ pub struct Clipboard {text: Option<String>,}+/// What is on the clipboard, granting nothing: a look, for the folder's menu.#[tauri::command]pub async fn clipboard_read(app: tauri::AppHandle) -> Result<Clipboard, String> {#[cfg(target_os = "linux")]{- read(&app).await+ read(&app, Use::Look).await}#[cfg(not(target_os = "linux"))]{@@ -26,9 +32,32 @@ pub async fn clipboard_read(app: tauri::AppHandle) -> Result<Clipboard, String>}}+/// Drive's Paste: what is on the clipboard, its files granted for reading so they can be+/// uploaded, as a drop's are.+#[tauri::command]+pub async fn clipboard_paste(app: tauri::AppHandle) -> Result<Clipboard, String> {+ #[cfg(target_os = "linux")]+ {+ read(&app, Use::Paste).await+ }+ #[cfg(not(target_os = "linux"))]+ {+ let _ = app;+ Ok(Clipboard::default())+ }+}++/// Whether the clipboard is read to look at it or to paste it.+#[derive(Clone, Copy, PartialEq, Eq)]+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]+enum Use {+ Look,+ Paste,+}+/// Asked on the thread GTK lives on, without waiting there: the answer comes back to it.#[cfg(target_os = "linux")]-async fn read(app: &tauri::AppHandle) -> Result<Clipboard, String> {+async fn read(app: &tauri::AppHandle, why: Use) -> Result<Clipboard, String> {let (tx, rx) = tokio::sync::oneshot::channel();app.run_on_main_thread(move || {let clipboard = gtk::Clipboard::get(&gdk::SELECTION_CLIPBOARD);@@ -37,6 +66,7 @@ async fn read(app: &tauri::AppHandle) -> Result<Clipboard, String> {uris.iter().filter_map(|uri| glib::filename_from_uri(uri).ok()).map(|(path, _)| path),+ why,);if files.is_empty() {clipboard.request_text(move |_, text| {@@ -55,13 +85,16 @@ async fn read(app: &tauri::AppHandle) -> Result<Clipboard, String> {.map_err(|_| "The clipboard couldn’t be read.".to_string())}-/// Files copied in the file manager were handed over by the person, as a drop is: granted for-/// reading (grants.rs), so Drive's Paste can upload them. Answered as the window names them.+/// Files copied in the file manager, answered as the window names them. Pasted, they were+/// handed over by the person, as a drop is: granted for reading (grants.rs), so Drive's Paste+/// can upload them. Looked at, nothing is granted.#[cfg_attr(not(target_os = "linux"), allow(dead_code))]-fn handed_over(paths: impl Iterator<Item = std::path::PathBuf>) -> Vec<String> {+fn handed_over(paths: impl Iterator<Item = std::path::PathBuf>, why: Use) -> Vec<String> {paths.map(|path| {- crate::grants::grant_read(&path);+ if why == Use::Paste {+ crate::grants::grant_read(&path);+ }path.to_string_lossy().into_owned()}).collect()@@ -69,17 +102,31 @@ fn handed_over(paths: impl Iterator<Item = std::path::PathBuf>) -> Vec<String> {#[cfg(test)]mod tests {- /// Files on the clipboard can be uploaded by Paste: what clipboard_read hands the window- /// is granted for reading, and nothing else is.+ use super::Use;++ /// Files on the clipboard can be uploaded by Paste: what Paste hands the window is granted+ /// for reading, and nothing else is.#[test]fn files_on_the_clipboard_may_be_pasted_and_no_others() {let dir = tempfile::tempdir().unwrap();let (copied, other) = (dir.path().join("copied.jpg"), dir.path().join("other.jpg"));std::fs::write(&copied, b"x").unwrap();std::fs::write(&other, b"x").unwrap();- let named = super::handed_over(std::iter::once(copied.clone()));+ let named = super::handed_over(std::iter::once(copied.clone()), Use::Paste);assert_eq!(named, vec![copied.to_string_lossy().into_owned()]);assert!(crate::grants::take_read(&named[0]).is_ok());assert!(crate::grants::take_read(&other.to_string_lossy()).is_err());}++ /// Looking at the clipboard — the folder's menu asking whether there is anything to+ /// paste — grants nothing: the files are named, and no command may read them.+ #[test]+ fn a_look_at_the_clipboard_grants_nothing() {+ let dir = tempfile::tempdir().unwrap();+ let copied = dir.path().join("copied.jpg");+ std::fs::write(&copied, b"x").unwrap();+ let named = super::handed_over(std::iter::once(copied.clone()), Use::Look);+ assert_eq!(named, vec![copied.to_string_lossy().into_owned()]);+ assert!(crate::grants::take_read(&named[0]).is_err());+ }}diff --git a/src-tauri/src/library_lock.rs b/src-tauri/src/library_lock.rsindex d8830b7..b649016 100644--- a/src-tauri/src/library_lock.rs+++ b/src-tauri/src/library_lock.rs@@ -388,10 +388,161 @@ pub fn hold(dir: &Path) -> std::io::Result<Option<std::fs::File>> {Ok(Some(file))}+/// What keeps a second Skykeep off this library for as long as this one runs.+#[derive(Debug)]+pub enum Hold {+ /// The lock on `skykeep.lock` ([`hold`]): held by being kept open, never read.+ Locked(#[allow(dead_code)] std::fs::File),+ /// Where no lock can be taken: `skykeep.pid`, made only if no other file of that name is+ /// there ([`mark`]).+ Marked(PathBuf),+}++/// [`hold`], and where the folder's disk refuses locks (a network share without them),+/// [`mark`] instead: never a Skykeep running beside another on one library because neither+/// could tell. `Ok(None)`: another Skykeep has it. `Err`: neither could be had, said in words+/// to show before quitting.+pub fn hold_or_mark(dir: &Path) -> Result<Option<Hold>, String> {+ match hold(dir) {+ Ok(Some(lock)) => Ok(Some(Hold::Locked(lock))),+ Ok(None) => Ok(None),+ Err(why) => mark(dir).map_err(|e| {+ format!(+ "Skykeep could not make sure it is the only copy using {}: the disk refused a \+ lock ({why}), and the file that stands in for one could not be made ({e}).",+ dir.display()+ )+ }),+ }+}++/// `skykeep.pid` beside the library, created only if no file of that name exists, holding this+/// process's id: the lock where the disk takes none. One left by a Skykeep that ended without+/// letting go — its process gone, or another program's now — is taken over; one a running+/// Skykeep made answers `None`.+pub fn mark(dir: &Path) -> std::io::Result<Option<Hold>> {+ use std::io::Write;+ std::fs::create_dir_all(dir)?;+ let path = dir.join("skykeep.pid");+ let me = std::process::id();+ for _ in 0..3 {+ match std::fs::OpenOptions::new()+ .write(true)+ .create_new(true)+ .open(&path)+ {+ Ok(mut file) => {+ file.write_all(me.to_string().as_bytes())?;+ file.sync_all()?;+ drop(file);+ // Two starting at once over a mark left behind may both have cleared it; the+ // one whose id is in it now is the one that runs.+ return Ok(match marked_by(&path) {+ Some(pid) if pid == me => Some(Hold::Marked(path)),+ _ => None,+ });+ }+ Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {+ let by = marked_by(&path).filter(|pid| *pid != me);+ if by.is_some_and(is_skykeep) {+ return Ok(None);+ }+ // Where whose it is cannot be asked, it is the person's to say.+ if by.is_some() && !cfg!(target_os = "linux") {+ return Err(std::io::Error::other(format!(+ "{} is there. If no other Skykeep is running, remove it and open \+ Skykeep again",+ path.display()+ )));+ }+ // Left behind: let go of, and made again as this one's.+ match std::fs::remove_file(&path) {+ Ok(()) => {}+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}+ Err(e) => return Err(e),+ }+ }+ Err(e) => return Err(e),+ }+ }+ Err(std::io::Error::other("skykeep.pid kept coming back"))+}++fn marked_by(path: &Path) -> Option<u32> {+ std::fs::read_to_string(path).ok()?.trim().parse().ok()+}++/// Whether process `pid` is running and is a Skykeep: the same program as this one. Asked of+/// Linux's `/proc`; elsewhere it cannot be told, and `false` leaves it to [`mark`] to ask.+/// Its program file, or (one updated while it ran, its file replaced) its name.+fn is_skykeep(pid: u32) -> bool {+ let exe = |p: &str| std::fs::read_link(format!("/proc/{p}/exe")).ok();+ let name = |p: &str| std::fs::read_to_string(format!("/proc/{p}/comm")).ok();+ let theirs = pid.to_string();+ let same_file = matches!((exe(&theirs), exe("self")), (Some(a), Some(b)) if a == b);+ let same_name = matches!((name(&theirs), name("self")), (Some(a), Some(b)) if a == b);+ same_file || same_name+}++impl Drop for Hold {+ fn drop(&mut self) {+ if let Hold::Marked(path) = self {+ if marked_by(path) == Some(std::process::id()) {+ let _ = std::fs::remove_file(path);+ }+ }+ }+}+#[cfg(test)]mod hold_tests {use super::*;+ /// Where the disk takes no lock: the mark stands in. Made by this Skykeep, a second is+ /// told one is running; left by one that ended (its process gone), it is taken over; let+ /// go of, it is gone.+ #[cfg(target_os = "linux")]+ #[test]+ fn where_no_lock_can_be_taken_a_mark_stands_in() {+ let dir = tempfile::tempdir().unwrap();+ let pid = dir.path().join("skykeep.pid");+ // A running Skykeep's mark (this test's own process stands in for one).+ let parent = std::os::unix::process::parent_id();+ std::fs::write(&pid, std::process::id().to_string()).unwrap();+ // Ours: taken again as ours.+ let held = mark(dir.path()).unwrap();+ assert!(matches!(held, Some(Hold::Marked(_))));+ drop(held);+ assert!(!pid.exists(), "let go of with the process");++ // Another running process of this same program: refused. A process that is not+ // this program (the test's parent, a shell or cargo), or none at all: taken over.+ // SAFETY: the child only sleeps and exits, both safe after fork in a threaded process.+ let other = unsafe { libc::fork() };+ if other == 0 {+ unsafe {+ libc::sleep(30);+ libc::_exit(0);+ }+ }+ assert!(other > 0);+ std::fs::write(&pid, other.to_string()).unwrap();+ let refused = mark(dir.path()).unwrap();+ // SAFETY: signals and reaps the child made above, nothing else.+ unsafe {+ libc::kill(other, libc::SIGKILL);+ libc::waitpid(other, std::ptr::null_mut(), 0);+ }+ assert!(refused.is_none(), "another Skykeep has it");+ std::fs::write(&pid, parent.to_string()).unwrap();+ assert!(+ mark(dir.path()).unwrap().is_some(),+ "another program's id: left behind"+ );+ std::fs::write(&pid, "4194999").unwrap();+ assert!(mark(dir.path()).unwrap().is_some(), "a process long gone");+ }+#[test]fn only_one_holds_the_library() {let dir = tempfile::tempdir().unwrap();diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rsindex d042af3..1cc4c95 100644--- a/src-tauri/src/main.rs+++ b/src-tauri/src/main.rs@@ -6670,8 +6670,8 @@ fn main() {keep_private(&dir);// A second Skykeep the session bus could not stop (there is none): it ends here,// before it touches the library, sends anything or mounts a folder.- static HELD: std::sync::OnceLock<std::fs::File> = std::sync::OnceLock::new();- match library_lock::hold(&dir) {+ static HELD: std::sync::OnceLock<library_lock::Hold> = std::sync::OnceLock::new();+ match library_lock::hold_or_mark(&dir) {Ok(Some(lock)) => {let _ = HELD.set(lock);}@@ -6679,8 +6679,8 @@ fn main() {eprintln!("Skykeep is already running.");std::process::exit(0);}- // Somewhere a lock can't be taken (a share that refuses them): run as before.- Err(e) => quiet!("the library's lock could not be taken: {e}"),+ // Neither a lock nor its stand-in: never run beside another unseen.+ Err(why) => cannot_start(&dir, &why),}let client = match Client::new(&dir) {Ok(client) => client,@@ -7044,6 +7044,7 @@ fn main() {drive_duplicate,drive_copy,clipboard::clipboard_read,+ clipboard::clipboard_paste,drive_thumbnails,drive_put_back,drive_delete_forever,diff --git a/src-tauri/src/outbox.rs b/src-tauri/src/outbox.rsindex 54584f6..04a5aa7 100644--- a/src-tauri/src/outbox.rs+++ b/src-tauri/src/outbox.rs@@ -1725,8 +1725,10 @@ async fn drain_contacts(app: &AppHandle) -> Outcome {let Some(account) = crate::asked_as(¶ms) else {return Outcome::Later;};+ // The book read once for the run, not once for each change in it.+ let book = std::sync::Mutex::new(Book::default());let report = drain_as(&data.0, CONTACTS, &crate::CONTACTS_PASS, &account, |p| {- send_contact(&data.0, &http, &root, params.clone(), p)+ send_contact(&data.0, &http, &root, params.clone(), p, &book)}).await;tell_refused(app, CONTACTS, &report);@@ -1737,33 +1739,131 @@ async fn drain_contacts(app: &AppHandle) -> Outcome {report.outcome}+/// The address book a run of queued contact changes is sent from: read once for the run, not+/// once for each change — every read is the whole book, every card in it. A card the run has+/// written since is read again before it is written again (the copy here is from before), and+/// a write Apple says was made against an old book is sent once more from a fresh read.+#[derive(Default)]+struct Book {+ read: Option<std::sync::Arc<BookRead>>,+ written: std::collections::HashSet<String>,+}++/// One read of the book: each card as Apple wrote it, by id, and the tokens a write needs.+struct BookRead {+ cards: std::collections::HashMap<String, serde_json::Value>,+ tokens: quince_icloud::contacts::Tokens,+}++impl Book {+ /// The book to send a change to `record` from, read now if the run has none it can use;+ /// and whether it was read just now.+ async fn for_record(+ book: &std::sync::Mutex<Self>,+ http: &reqwest::Client,+ root: &str,+ params: &[(String, String)],+ record: &str,+ ) -> Result<(std::sync::Arc<BookRead>, bool), Error> {+ let kept = {+ let b = book+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ b.read.clone().filter(|_| !b.written.contains(record))+ };+ if let Some(read) = kept {+ return Ok((read, false));+ }+ let (cards, tokens) =+ quince_icloud::contacts::cards_with_tokens(http, root, params.to_vec()).await?;+ let cards = cards+ .into_iter()+ .filter_map(|card| {+ let id = card.get("contactId")?.as_str()?.to_string();+ Some((id, card))+ })+ .collect();+ let mut b = book+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let read = std::sync::Arc::new(BookRead { cards, tokens });+ b.read = Some(std::sync::Arc::clone(&read));+ b.written.clear();+ Ok((read, true))+ }++ /// `record` written: its copy in the read is old now.+ fn wrote(book: &std::sync::Mutex<Self>, record: &str) {+ book.lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .written+ .insert(record.to_string());+ }++ /// The read let go of: written past it in ways it cannot track (a merge).+ fn forget(book: &std::sync::Mutex<Self>) {+ book.lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .read = None;+ }+}+async fn send_contact(library: &crate::library_lock::LibraryLock,http: &reqwest::Client,root: &str,params: Vec<(String, String)>,p: Pending,+ book: &std::sync::Mutex<Book>,) -> Result<(), Error> {use quince_icloud::contacts as c;if p.kind == "merge" {+ // A merge writes and deletes cards of its own, read by itself.+ Book::forget(book);return crate::contacts_merge::send(library, http, root, params, &p).await;}if p.kind == "address" {let a: ContactAddress = serde_json::from_str(&p.payload).map_err(|e| unreadable(&e))?;+ Book::wrote(book, &p.record);return c::address::edit(http, root, params, &p.record, &a.base, &a.edits).await.map(|_| ());}+ let (read, fresh) = Book::for_record(book, http, root, ¶ms, &p.record).await?;+ let sent = send_contact_from(http, root, params.clone(), &p, &read).await;+ if matches!(sent, Err(Error::Conflict)) && !fresh {+ // Made against a book read earlier in the run, which Apple says is old now: from a+ // fresh read, once.+ Book::forget(book);+ let (read, _) = Book::for_record(book, http, root, ¶ms, &p.record).await?;+ let again = send_contact_from(http, root, params, &p, &read).await;+ Book::wrote(book, &p.record);+ return again;+ }+ Book::wrote(book, &p.record);+ sent+}++/// One queued card change sent, from `read`: Apple's card as it was read, and its tokens.+async fn send_contact_from(+ http: &reqwest::Client,+ root: &str,+ params: Vec<(String, String)>,+ p: &Pending,+ read: &BookRead,+) -> Result<(), Error> {+ use quince_icloud::contacts as c;+ let tokens = &read.tokens;+ let card = read.cards.get(&p.record);if p.kind == "delete" {- let (people, tokens) = c::fetch_with_tokens(http, root, params.clone()).await?;// Gone already — deleted elsewhere, or by an earlier send of this one.- let Some(card) = people.into_iter().find(|x| x.guid == p.record) else {+ let Some(card) = card.and_then(|v| c::parse(&serde_json::json!({ "contacts": [v] })).pop())+ else {return Ok(());};- return c::delete(http, root, params, &tokens, &card).await;+ return c::delete(http, root, params, tokens, &card).await;}let s: ContactSave = serde_json::from_str(&p.payload).map_err(|e| unreadable(&e))?;- let (_, tokens) = c::fetch_with_tokens(http, root, params.clone()).await?;if let Some(details) = &s.details {let whole = |contact: &Contact, details: &ContactDetails| Card {contact: contact.clone(),@@ -1771,14 +1871,13 @@ async fn send_contact(};let mine = whole(&s.contact, details);if s.create {- return c::create_card_named(http, root, params, &tokens, &p.record, &mine).await;+ return c::create_card_named(http, root, params, tokens, &p.record, &mine).await;}- let card = c::card(http, root, params.clone(), &p.record).await?;// A base from before the form showed these has none: compared with Apple's own, so// only what this form changed is written.let base_details = match &s.base_details {Some(d) => Some(d.clone()),- None => card.as_ref().map(|v| {+ None => card.map(|v| {c::parse_cards(&serde_json::json!({ "contacts": [v] })).pop().map(|c| c.details)@@ -1789,28 +1888,18 @@ async fn send_contact(.base.as_ref().map(|b| whole(b, &base_details.clone().unwrap_or_default()));- return c::update_card(- http,- root,- params,- &tokens,- &mine,- card.as_ref(),- base.as_ref(),- )- .await;+ return c::update_card(http, root, params, tokens, &mine, card, base.as_ref()).await;}if s.create {- return c::create_named(http, root, params, &tokens, &p.record, &s.contact).await;+ return c::create_named(http, root, params, tokens, &p.record, &s.contact).await;}- let card = c::card(http, root, params.clone(), &p.record).await?;c::update_from(http,root,params,- &tokens,+ tokens,&s.contact,- card.as_ref(),+ card,s.base.as_ref(),).await@@ -3588,6 +3677,71 @@ mod tests {assert_eq!(lib.lock().await.outbox_pending(REMINDERS).unwrap().len(), 1);}+ /// Three cards edited offline go in one run from one read of the address book — each read+ /// is the whole book — not two reads for each card; a card edited twice is read again+ /// before its second write, its copy from before the first being old.+ #[tokio::test]+ async fn queued_card_edits_are_sent_from_one_read_of_the_book() {+ use wiremock::matchers::{method, path, path_regex};+ use wiremock::{Mock, MockServer, ResponseTemplate};+ let server = MockServer::start().await;+ let card = |id: &str| serde_json::json!({ "contactId": id, "firstName": id, "etag": "e" });+ Mock::given(method("GET"))+ .and(path("/co/startup"))+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({+ "syncToken": "S", "prefToken": "P",+ "contacts": [card("A"), card("B"), card("C")]+ })))+ .mount(&server)+ .await;+ Mock::given(path_regex(r".*/co/contacts/card$"))+ .respond_with(+ ResponseTemplate::new(200).set_body_json(serde_json::json!({ "contacts": [] })),+ )+ .mount(&server)+ .await;++ let mut l = library();+ for (id, name) in [("A", "Ada"), ("B", "Bea"), ("C", "Cy")] {+ let was = person(id, id);+ l.put_contacts(std::slice::from_ref(&was)).unwrap();+ record_contact_save(&mut l, &person(id, name), Some(was)).unwrap();+ }+ // Behind a save of A that is on its way: a second edit of A, sent after it.+ l.outbox_take(CONTACTS).unwrap();+ record_contact_save(&mut l, &person("A", "Ada L"), Some(person("A", "Ada"))).unwrap();+ l.set_state(crate::ACCOUNT_KEY, "me").unwrap();+ let lib = crate::library_lock::LibraryLock::new(l, None);+ let http = reqwest::Client::new();+ let book = std::sync::Mutex::new(Book::default());+ let root = server.uri();+ let report = drain_as(&lib, CONTACTS, &Mutex::new(()), "me", |p| {+ send_contact(&lib, &http, &root, Vec::new(), p, &book)+ })+ .await;+ assert!(report.refused.is_empty(), "{:?}", report.refused);+ assert!(lib+ .lock()+ .await+ .outbox_pending(CONTACTS)+ .unwrap()+ .is_empty());+ let asked = server.received_requests().await.unwrap();+ let reads = asked+ .iter()+ .filter(|r| r.url.path() == "/co/startup")+ .count();+ let writes = asked+ .iter()+ .filter(|r| r.url.path().ends_with("/co/contacts/card"))+ .count();+ assert_eq!(writes, 4);+ assert_eq!(+ reads, 2,+ "one read for the run, and A's again before its second write"+ );+ }+/// A pretend Apple that holds one event: a save sent with its current tag is taken, one sent/// with any other is answered "changed since" (412), as Apple's calendar does.mod apple_calendar {diff --git a/src-tauri/src/photo_cache.rs b/src-tauri/src/photo_cache.rsindex 0113e97..20bbd0f 100644--- a/src-tauri/src/photo_cache.rs+++ b/src-tauri/src/photo_cache.rs@@ -4,7 +4,10 @@//! What it holds, by folder under `photos/`://!//! - `thumb/`, and `fit/<step>/` up to [`THUMB_STEP`]: the grid's pictures. Small, drawn by every-//! view, and never let go of — the library always scrolls from disk.+//! view, and kept — the library scrolls from disk — but only while the disk has room: below+//! [`GRID_FLOOR`] free, no more are kept and the longest unseen are let go of until there is+//! room again ([`trim_grid`]). A library of a hundred thousand photos makes gigabytes of+//! them, and nothing else held them to anything.//! - `original/`, `full/`, `medium/`, `video/`, `live/`, the larger `fit/` steps, and `open/`//! (a video's name for the system's player, a hard link to its original): originals and//! previews. These are what the cap holds, the longest unseen let go first.@@ -47,6 +50,12 @@ pub const RECENT: Duration = Duration::from_secs(5 * 60);/// A download's `.part` untouched for this long belongs to a download that died with the app./// One still landing is written to every moment.pub const STALE_PART: Duration = Duration::from_secs(60 * 60);+/// Free room on the disk below which the grid's pictures stop being kept, and the longest+/// unseen of them let go of: a full disk is never the price of scrolling.+pub const GRID_FLOOR: u64 = 2 * GB;+/// How far over [`GRID_FLOOR`] a trim of the grid's pictures brings the disk back, so the next+/// picture made does not trim again at once.+const GRID_SLACK: u64 = GRID_FLOOR / 4;/// The folders of originals and previews.const PREVIEWS: [&str; 6] = ["original", "full", "medium", "video", "live", "open"];@@ -215,8 +224,8 @@ fn scan(root: &Path) -> Scan {scan}-/// Room the grid's pictures take (thumbnails, and `fit/` steps up to [`THUMB_STEP`]).-fn thumbnails(root: &Path) -> u64 {+/// The grid's pictures on disk (thumbnails, and `fit/` steps up to [`THUMB_STEP`]).+fn grid_files(root: &Path) -> Vec<(PathBuf, std::fs::Metadata)> {let mut files = Vec::new();walk(&root.join("thumb"), 0, &mut files);for e in std::fs::read_dir(root.join("fit"))@@ -228,11 +237,36 @@ fn thumbnails(root: &Path) -> u64 {walk(&e.path(), 0, &mut files);}}+ files.retain(|(p, _)| !is_part(p));files- .iter()- .filter(|(p, _)| !is_part(p))- .map(|(_, m)| on_disk(m))- .sum()+}++/// Room the grid's pictures take.+fn thumbnails(root: &Path) -> u64 {+ grid_files(root).iter().map(|(_, m)| on_disk(m)).sum()+}++/// The grid's pictures let go of, the longest unseen first (last written, or read where the+/// disk keeps that), until the disk's `free` room has come back to `want` — none a reader+/// holds. Answers how many went.+pub fn trim_grid(root: &Path, free: u64, want: u64) -> usize {+ if free >= want {+ return 0;+ }+ let mut files = grid_files(root);+ files.sort_by_key(|(_, m)| last_seen(m));+ let mut freed = 0u64;+ let mut removed = 0;+ for (path, meta) in files {+ if free.saturating_add(freed) >= want {+ break;+ }+ if remove_unless_in_use(&path, &[]) {+ freed = freed.saturating_add(on_disk(&meta));+ removed += 1;+ }+ }+ removed}// ---------------------------------------------------------------------------------------------@@ -654,7 +688,11 @@ pub fn trim_now(app: &AppHandle) -> (Outcome, u64) {now: SystemTime::now(),},);- if out.removed > 0 {+ // The grid's pictures too, once the disk is short of room.+ let grid = free_space(&root)+ .filter(|free| *free < GRID_FLOOR)+ .map_or(0, |free| trim_grid(&root, free, GRID_FLOOR + GRID_SLACK));+ if out.removed + grid > 0 {let _ = app.emit(EVENT, status_of(&root, cap, out.after));}// Short of the limit only for what was on screen: tried again once that has had its time.@@ -676,6 +714,19 @@ pub fn grew(app: &AppHandle) {request(app, Duration::from_secs(2));}+/// Whether a grid picture made now may be kept on disk: while the disk has [`GRID_FLOOR`]+/// free (or cannot say how much it has).+pub fn grid_room(app: &AppHandle) -> bool {+ free_space(&root(app)).is_none_or(|free| free >= GRID_FLOOR)+}++/// A grid picture landed: nothing to do while the disk has room; short of it, a trim soon.+pub fn grid_grew(app: &AppHandle) {+ if !grid_room(app) {+ grew(app);+ }+}+/// A trim in the background after `after`; requests that come while one waits or runs are one.fn request(app: &AppHandle, after: Duration) {if RUNNING.swap(true, Ordering::SeqCst) {@@ -796,6 +847,33 @@ mod side_tests {path}+ /// The grid's pictures, kept while the disk has room, are let go of once it is short: the+ /// longest unseen first, only until the room wanted is back, never a picture a reader holds+ /// and never a preview (those are the cap's).+ #[test]+ fn the_grid_gives_way_when_the_disk_is_short() {+ let dir = tempfile::tempdir().unwrap();+ let root = dir.path();+ let hour = Duration::from_secs(3600);+ let oldest = put(root, "thumb/a", 40_000, hour * 30);+ let old_fit = put(root, "fit/256/b", 40_000, hour * 20);+ let held = put(root, "thumb/c", 40_000, hour * 10);+ let newest = put(root, "thumb/d", 40_000, hour);+ let preview = put(root, "fit/2048/e", 40_000, hour * 40);+ hold(&held);+ let each = on_disk(&std::fs::metadata(&oldest).unwrap());++ assert_eq!(trim_grid(root, GRID_FLOOR, GRID_FLOOR), 0, "room enough");+ assert!(oldest.exists());++ // Short by a little more than two pictures: three go, the held one passed over.+ let free = GRID_FLOOR - 2 * each - 1;+ assert_eq!(trim_grid(root, free, GRID_FLOOR), 2 + 1);+ assert!(!oldest.exists() && !old_fit.exists() && !newest.exists());+ assert!(held.exists(), "a picture being read stays");+ assert!(preview.exists(), "a preview is the cap's, not the grid's");+ }+/// Over its size, a cache lets go of the longest unseen first, never one just seen, and/// sweeps an abandoned download.#[test]diff --git a/src-tauri/src/photos.rs b/src-tauri/src/photos.rsindex ed99c78..f1bc4a0 100644--- a/src-tauri/src/photos.rs+++ b/src-tauri/src/photos.rs@@ -285,15 +285,24 @@ pub async fn fitted(app: &AppHandle, id: &str, long: u32) -> Result<Vec<u8>, Str.map_err(|e| e.to_string())?.map_err(|e| e.to_string())?;if let Some(bytes) = made {+ // A grid picture is kept only while the disk has room (photo_cache::GRID_FLOOR);+ // short of it, it is shown and made again when next wanted.+ if !preview && !crate::photo_cache::grid_room(app) {+ crate::photo_cache::grid_grew(app);+ return Ok(bytes);+ }if let Some(dir) = path.parent() {let _ = tokio::fs::create_dir_all(dir).await;}let part = path.with_extension("part");if tokio::fs::write(&part, &bytes).await.is_ok()&& tokio::fs::rename(&part, &path).await.is_ok()- && preview{- crate::photo_cache::grew(app);+ if preview {+ crate::photo_cache::grew(app);+ } else {+ crate::photo_cache::grid_grew(app);+ }}return Ok(bytes);}@@ -450,10 +459,12 @@ async fn from_apple(if !done && !fetch(new_link(&http, &root, params, &photo, size).await?).await? {return Err("Apple did not send that picture.".into());}- // Held for whoever asked, and the cache checked against its cap soon after (thumbnails are- // never let go of, so a thumbnail landing asks nothing).+ // Held for whoever asked, and the cache checked against its cap soon after (a thumbnail is+ // held only to the disk's room: photo_cache::GRID_FLOOR).crate::photo_cache::hold(&path);- if size != Size::Thumb {+ if size == Size::Thumb {+ crate::photo_cache::grid_grew(app);+ } else {crate::photo_cache::grew(app);}// A video's picture that is black (Apple makes them from the first frame) is made againdiff --git a/src/lib/ipc.ts b/src/lib/ipc.tsindex 987eff6..5245bba 100644--- a/src/lib/ipc.ts+++ b/src/lib/ipc.ts@@ -449,7 +449,10 @@ export const api = {driveCopy: (drivewsid: string, destinationId: string) =>invoke<void>("drive_copy", { drivewsid, destinationId }),/** The desktop's clipboard: files copied in the file manager, as paths, or else its text. */+ /** What is on the clipboard, to offer Paste; grants nothing. */clipboardRead: () => invoke<{ files: string[]; text: string | null }>("clipboard_read"),+ /** Drive's Paste: what is on the clipboard, its files granted for uploading. */+ clipboardPaste: () => invoke<{ files: string[]; text: string | null }>("clipboard_paste"),};/** Tauri rejects with a plain string from our commands; keep it readable either way. */diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsxindex b141799..5f4dbb4 100644--- a/src/screens/DriveView.tsx+++ b/src/screens/DriveView.tsx@@ -1239,7 +1239,7 @@ export function DriveView() {*/// Each render's own, as the keys are: it uploads into the folder open now.const paste = async (move: boolean) => {- const board = await api.clipboardRead().catch(() => null);+ const board = await api.clipboardPaste().catch(() => null);if (board?.files.length) {await upload(board.files);return;