Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Drive: changes made elsewhere show within seconds, folder by folder

Yashwanth Kamireddie6f6e9381517
diff --git a/.uiverify/drive-thumbs.mjs b/.uiverify/drive-thumbs.mjs
index efbfc07..2230873 100644
--- a/.uiverify/drive-thumbs.mjs
+++ b/.uiverify/drive-thumbs.mjs
@@ -35,20 +35,25 @@ const ok = (p, m) => { console.log(`${p ? "ok " : "FAIL"} ${m}`); if (!p) failu
const thumbs = () => page.evaluate(() => window.__thumbs ?? 0);
const checks = () => page.evaluate(() => window.__checks ?? 0);
+const look = async () => {
+ await page.evaluate(() => window.dispatchEvent(new Event("focus")));
+ await page.waitForTimeout(500);
+};
ok((await thumbs()) === 1, "opening the folder asks for its pictures");
ok((await page.locator("img.drive-thumb").count()) > 0, "and draws them");
-// Nothing new in ten seconds: the folder is looked at, its pictures are not asked for again.
+// Nothing new at a look (the window coming to the front): the folder is looked at, its
+// pictures are not asked for again.
let before = await thumbs();
let looked = await checks();
-await page.waitForTimeout(10_600);
-ok((await checks()) > looked, "the open folder is checked again after ten seconds");
+await look();
+ok((await checks()) > looked, "the open folder is looked at again when the window comes to the front");
ok((await thumbs()) === before, "and a check that found nothing new asks for no pictures");
ok((await page.locator("img.drive-thumb").count()) > 0, "which stay on screen");
// Something new: its picture is asked for.
await page.evaluate(() => { window.__phone = true; });
-await page.waitForTimeout(10_600);
+await look();
ok((await thumbs()) === before + 1, "a file arriving asks for the pictures again");
// Pictures that never came (offline) are asked for at the next look, changed or not.
@@ -58,10 +63,10 @@ await page.locator(".drive-row-hit", { hasText: "Downloads" }).dblclick();
await page.waitForTimeout(800);
await page.evaluate(() => { window.__thumbsFail = false; });
before = await thumbs();
-await page.waitForTimeout(10_600);
+await look();
ok((await thumbs()) === before + 1, "a folder whose pictures failed is asked again");
before = await thumbs();
-await page.waitForTimeout(10_600);
+await look();
ok((await thumbs()) === before, "and, once they came, not again while nothing changes");
await b.close();
diff --git a/.uiverify/drive-watch.mjs b/.uiverify/drive-watch.mjs
index 0793f2f..91c9d2a 100644
--- a/.uiverify/drive-watch.mjs
+++ b/.uiverify/drive-watch.mjs
@@ -1,6 +1,7 @@
-// The folder in front is checked again while it shows: a file saved on the phone appears
-// within ten seconds, unasked — and a check that finds nothing new redraws nothing, and stands
-// aside while a change of this computer's own is on its way.
+// The folder in front follows Drive's change feed: told by the backend that it changed
+// (quince://drive-changed), it redraws from the library with no request of its own; told of
+// other folders, or nothing, it does nothing — no timed checks — and it stands aside while a
+// change of this computer's own is on its way. Coming back to the window still looks once.
import { engine } from "./engine.mjs";
import { SERVICES, installStub } from "./stub.mjs";
@@ -31,40 +32,57 @@ let failures = 0;
const ok = (p, m) => { console.log(`${p ? "PASS" : "FAIL"} ${m}`); if (!p) failures++; };
const names = () => page.locator(".drive-name").allInnerTexts();
-// Nothing new: the rows stay the very same elements, so a selection or a hover is never lost.
+const told = (folders) => page.evaluate((f) => window.__emit("quince://drive-changed", f), folders);
+const reads = () => page.evaluate(() => window.__reads ?? 0);
+await page.evaluate(() => {
+ const invoke = window.__TAURI_INTERNALS__.invoke;
+ window.__TAURI_INTERNALS__.invoke = (cmd, args) => {
+ if (cmd === "drive_children") window.__reads = (window.__reads ?? 0) + 1;
+ return invoke(cmd, args);
+ };
+});
+const ROOT = "FOLDER::com.apple.CloudDocs::root";
+
+// No timed checks: a folder nobody changed is never asked about again.
await page.evaluate(() => { window.__firstRow = document.querySelector(".drive-name"); });
const before = await page.evaluate(() => window.__checks);
await page.waitForTimeout(10_600);
-ok((await page.evaluate(() => window.__checks)) > before, "the open folder is checked again after ten seconds");
-ok(await page.evaluate(() => document.querySelector(".drive-name") === window.__firstRow), "and a check that found nothing redraws nothing");
+ok((await page.evaluate(() => window.__checks)) === before, "no timed check of the open folder");
+
+// Another folder changed: nothing here moves.
+const r0 = await reads();
+await told(["FOLDER::com.apple.CloudDocs::elsewhere"]);
+await page.waitForTimeout(300);
+ok((await reads()) === r0, "a change in another folder reads nothing");
-// Saved on the phone: here without anyone opening the folder again.
+// Saved on the phone: told, read from the library, and drawn — without asking Apple.
await page.evaluate(() => { window.__phone = true; });
-await page.waitForTimeout(10_600);
-ok((await names()).some((n) => n.startsWith("Scan from phone")), "a file saved elsewhere appears unasked");
+await told([ROOT]);
+await page.waitForFunction(() => [...document.querySelectorAll(".drive-name")].some((e) => e.textContent.startsWith("Scan from phone")), null, { timeout: 3000 }).catch(() => {});
+ok((await names()).some((n) => n.startsWith("Scan from phone")), "a file saved elsewhere appears when the backend says so");
+ok((await page.evaluate(() => window.__checks)) === before, "and the folder was not asked of Apple again");
+ok(await page.evaluate(() => document.querySelector(".drive-name") === window.__firstRow), "and the rows already there stay the same elements");
-// A rename on its way (Apple slow to answer): the timed check stands aside.
+// A rename on its way (Apple slow to answer): a change told meanwhile stands aside.
await page.evaluate(() => { window.__phone = false; });
await page.locator(".drive-row-hit", { hasText: "Downloads" }).click({ button: "right" });
await page.getByRole("menuitem", { name: "Rename" }).click();
await page.keyboard.press("Control+a");
await page.keyboard.type("Downloads 2");
await page.keyboard.press("Enter");
-const during = await page.evaluate(() => window.__checks);
-await page.waitForTimeout(10_600);
-ok((await page.evaluate(() => window.__checks)) === during, "no check while a rename is on its way");
+await page.waitForTimeout(200);
+const during = await reads();
+await told([ROOT]);
+await page.waitForTimeout(500);
+ok((await reads()) === during, "nothing read over a rename on its way");
ok((await names()).some((n) => n.startsWith("Downloads 2")), "and the new name stays on screen meanwhile");
-// The window open but not in front (behind another, on another workspace): no timed checks —
-// and focus coming back checks at once.
-await page.waitForTimeout(2500); // the rename's answer lands
-await page.evaluate(() => { document.hasFocus = () => false; });
+// Coming back to the window looks once, for what the feed does not cover.
+await page.waitForTimeout(12_500); // the rename's answer lands
const away = await page.evaluate(() => window.__checks);
-await page.waitForTimeout(10_600);
-ok((await page.evaluate(() => window.__checks)) === away, "no timed check while the window is in the background");
-await page.evaluate(() => { document.hasFocus = () => true; window.dispatchEvent(new Event("focus")); });
+await page.evaluate(() => window.dispatchEvent(new Event("focus")));
await page.waitForTimeout(300);
-ok((await page.evaluate(() => window.__checks)) > away, "and one at once when it comes to the front");
+ok((await page.evaluate(() => window.__checks)) > away, "and one look at once when the window comes to the front");
await b.close();
if (failures) process.exitCode = 1;
diff --git a/crates/quince-icloud/src/drive_changes.rs b/crates/quince-icloud/src/drive_changes.rs
new file mode 100644
index 0000000..dc4383a
--- /dev/null
+++ b/crates/quince-icloud/src/drive_changes.rs
@@ -0,0 +1,289 @@
+//! What changed in iCloud Drive, item by item, from CloudKit's own change feed.
+//!
+//! Apple's push socket says nothing when Drive changes (measured 2026-10-29), and Drive's web
+//! service only lists a folder at a time, so a change on the phone could only be found by
+//! listing folders again and again. CloudKit holds Drive too, in the `com.apple.clouddocs`
+//! container (measured live, 2026-10-29):
+//!
+//! - its `changes/database` token moves within half a second of a change anywhere in Drive;
+//! - the `com.apple.CloudDocs` zone's `changes/zone` feed then names each record that changed:
+//! `directory/<id>` for a folder, `documentStructure/<id>` and `documentContent/<id>` for a
+//! file, `<id>` being the same one Drive's own `FOLDER::com.apple.CloudDocs::<id>` carries;
+//! - a structure record's `parent` field is a reference to its folder's `directory/<id>`; a
+//! deletion is a move into Recently Deleted's directory, a purge a `deleted` record.
+//!
+//! Names are encrypted there, so the feed is read for *which* folders changed — never what
+//! is in them: those folders are then listed again through Drive's web service, as before.
+
+use crate::{Error, Result};
+use serde_json::{json, Value};
+
+/// Drive's container and its main zone.
+pub const CONTAINER: &str = "com.apple.clouddocs";
+const ZONE: &str = "com.apple.CloudDocs";
+
+/// One record the feed named, as Drive knows it.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Changed {
+ /// Drive's own identifier for the item.
+ pub drivewsid: String,
+ /// The folder it is in now, when the record says (a structure record does).
+ pub parent: Option<String>,
+ /// Purged: no longer anywhere.
+ pub gone: bool,
+}
+
+/// One page of the feed.
+#[derive(Debug, Default, Clone, PartialEq, Eq)]
+pub struct Page {
+ pub changed: Vec<Changed>,
+ /// Where to read from next time.
+ pub token: Option<String>,
+ /// More waits behind this page.
+ pub more: bool,
+}
+
+/// Drive's identifier for a record of the main zone, or `None` for anything else it holds.
+pub fn drivewsid_of(record_name: &str) -> Option<String> {
+ let (kind, id) = record_name.split_once('/')?;
+ if id.is_empty() {
+ return None;
+ }
+ let kind = match kind {
+ "directory" => "FOLDER",
+ "documentStructure" | "documentContent" => "FILE",
+ _ => return None,
+ };
+ Some(format!("{kind}::{ZONE}::{id}"))
+}
+
+/// A page of `changes/zone`, as Drive knows its items. The same item named twice (its
+/// structure and its content) is told once, with what the structure said of its folder.
+pub fn parse_page(body: &Value) -> Result<Page> {
+ let zone = body["zones"]
+ .get(0)
+ .ok_or_else(|| Error::Protocol("changes/zone answered no zone".into()))?;
+ if let Some(code) = zone["serverErrorCode"].as_str() {
+ return Err(match code {
+ "CHANGE_TOKEN_EXPIRED" | "ZONE_NOT_FOUND" | "BAD_REQUEST" => Error::StaleCursor,
+ "AUTHENTICATION_REQUIRED" => Error::ReauthRequired,
+ other => Error::Protocol(format!("changes/zone answered {other}")),
+ });
+ }
+ let mut changed: Vec<Changed> = Vec::new();
+ for record in zone["records"].as_array().into_iter().flatten() {
+ let Some(drivewsid) = record["recordName"].as_str().and_then(drivewsid_of) else {
+ continue;
+ };
+ let parent = record["fields"]["parent"]["value"]["recordName"]
+ .as_str()
+ .and_then(drivewsid_of);
+ let gone = record["deleted"].as_bool().unwrap_or(false);
+ match changed.iter_mut().find(|c| c.drivewsid == drivewsid) {
+ Some(seen) => {
+ seen.parent = seen.parent.take().or(parent);
+ seen.gone |= gone;
+ }
+ None => changed.push(Changed {
+ drivewsid,
+ parent,
+ gone,
+ }),
+ }
+ }
+ Ok(Page {
+ changed,
+ token: zone["syncToken"].as_str().map(str::to_string),
+ more: zone["moreComing"].as_bool().unwrap_or(false),
+ })
+}
+
+/// One page of Drive's change feed from `token` (from the start with none). Read-only; asks
+/// only for the `parent` field, so a page is small whatever the files hold.
+pub async fn page(
+ http: &reqwest::Client,
+ ckdatabasews_root: &str,
+ params: &[(String, String)],
+ token: Option<&str>,
+) -> Result<Page> {
+ let url = format!(
+ "{}/database/1/{CONTAINER}/production/private/changes/zone",
+ ckdatabasews_root.trim_end_matches('/')
+ );
+ let mut zone = json!({
+ "zoneID": { "zoneName": ZONE, "zoneType": "REGULAR_CUSTOM_ZONE" },
+ "resultsLimit": 200,
+ "desiredKeys": ["parent"],
+ });
+ if let Some(t) = token.filter(|t| !t.is_empty()) {
+ zone["syncToken"] = Value::String(t.to_string());
+ }
+ // As CloudKit JS asks, the way every other CloudKit read here does.
+ let mut query = params.to_vec();
+ query.push(("ckjsBuildVersion".into(), "17DProjectDev77".into()));
+ query.push(("ckjsVersion".into(), "2.6.1".into()));
+ let response = http
+ .post(url)
+ .query(&query)
+ .json(&json!({ "zones": [zone] }))
+ .send()
+ .await?;
+ let status = response.status();
+ if matches!(status.as_u16(), 401 | 421 | 450) {
+ return Err(Error::ReauthRequired);
+ }
+ let text = response.text().await.unwrap_or_default();
+ if !status.is_success() {
+ if text.contains("CHANGE_TOKEN_EXPIRED") || status.as_u16() == 400 && token.is_some() {
+ return Err(Error::StaleCursor);
+ }
+ return Err(Error::Protocol(format!("changes/zone answered {status}")));
+ }
+ let body: Value = serde_json::from_str(&text)
+ .map_err(|e| Error::Protocol(format!("changes/zone sent invalid JSON: {e}")))?;
+ parse_page(&body)
+}
+
+/// The folders to list again for what changed: where each item is now, and where it was —
+/// `was_in` answers from what this computer stored — of those this computer has seen
+/// (`known`). A folder never seen here is listed when someone opens it, so it is left be.
+pub fn folders_to_list(
+ changed: &[Changed],
+ was_in: impl Fn(&str) -> Option<String>,
+ known: impl Fn(&str) -> bool,
+) -> Vec<String> {
+ let mut folders: Vec<String> = Vec::new();
+ for c in changed {
+ for folder in [c.parent.clone(), was_in(&c.drivewsid)]
+ .into_iter()
+ .flatten()
+ {
+ if !folders.contains(&folder) && known(&folder) {
+ folders.push(folder);
+ }
+ }
+ }
+ folders
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const ROOT: &str = "FOLDER::com.apple.CloudDocs::root";
+
+ fn record(name: &str, parent: Option<&str>, deleted: bool) -> Value {
+ let mut r = json!({ "recordName": name, "recordType": "structure", "deleted": deleted });
+ if let Some(p) = parent {
+ r["fields"] = json!({ "parent": { "value": {
+ "recordName": p, "action": "VALIDATE",
+ "zoneID": { "zoneName": "com.apple.CloudDocs" } } } });
+ }
+ r
+ }
+
+ #[test]
+ fn records_are_read_as_drive_knows_them() {
+ assert_eq!(drivewsid_of("directory/root").as_deref(), Some(ROOT));
+ assert_eq!(
+ drivewsid_of("directory/12121DA4").as_deref(),
+ Some("FOLDER::com.apple.CloudDocs::12121DA4")
+ );
+ assert_eq!(
+ drivewsid_of("documentStructure/7D85").as_deref(),
+ Some("FILE::com.apple.CloudDocs::7D85")
+ );
+ assert_eq!(
+ drivewsid_of("documentContent/7D85").as_deref(),
+ Some("FILE::com.apple.CloudDocs::7D85")
+ );
+ assert_eq!(drivewsid_of("AppContainer/x"), None);
+ assert_eq!(drivewsid_of("directory/"), None);
+ assert_eq!(drivewsid_of("nothing"), None);
+ }
+
+ /// The shape measured live when a folder was made in the top level and then deleted.
+ #[test]
+ fn a_page_names_each_item_once_with_its_folder() {
+ let body = json!({ "zones": [{
+ "syncToken": "T2", "moreComing": false,
+ "records": [
+ record("directory/NEW", Some("directory/root"), false),
+ record("directory/root", None, false),
+ record("documentContent/F", None, false),
+ record("documentStructure/F", Some("directory/NEW"), false),
+ record("documentStructure/P", None, true),
+ { "recordName": "AppContainer/x", "recordType": "AppContainer" },
+ ] }] });
+ let page = parse_page(&body).unwrap();
+ assert_eq!(page.token.as_deref(), Some("T2"));
+ assert!(!page.more);
+ assert_eq!(
+ page.changed,
+ vec![
+ Changed {
+ drivewsid: "FOLDER::com.apple.CloudDocs::NEW".into(),
+ parent: Some(ROOT.into()),
+ gone: false
+ },
+ Changed {
+ drivewsid: ROOT.into(),
+ parent: None,
+ gone: false
+ },
+ Changed {
+ drivewsid: "FILE::com.apple.CloudDocs::F".into(),
+ parent: Some("FOLDER::com.apple.CloudDocs::NEW".into()),
+ gone: false
+ },
+ Changed {
+ drivewsid: "FILE::com.apple.CloudDocs::P".into(),
+ parent: None,
+ gone: true
+ },
+ ]
+ );
+ }
+
+ #[test]
+ fn a_token_apple_no_longer_knows_starts_over() {
+ let body = json!({ "zones": [{ "serverErrorCode": "CHANGE_TOKEN_EXPIRED" }] });
+ assert!(matches!(parse_page(&body), Err(Error::StaleCursor)));
+ assert!(matches!(
+ parse_page(&json!({ "zones": [] })),
+ Err(Error::Protocol(_))
+ ));
+ }
+
+ /// A file moved from one folder to another: both are listed again, once each; a folder
+ /// this computer never saw is left for when it is opened.
+ #[test]
+ fn the_folders_listed_again_are_where_it_went_and_where_it_was() {
+ let a = "FOLDER::com.apple.CloudDocs::A";
+ let b = "FOLDER::com.apple.CloudDocs::B";
+ let unseen = "FOLDER::com.apple.CloudDocs::U";
+ let changed = vec![
+ Changed {
+ drivewsid: "FILE::com.apple.CloudDocs::F".into(),
+ parent: Some(b.into()),
+ gone: false,
+ },
+ Changed {
+ drivewsid: "FILE::com.apple.CloudDocs::G".into(),
+ parent: Some(unseen.into()),
+ gone: false,
+ },
+ Changed {
+ drivewsid: "FILE::com.apple.CloudDocs::P".into(),
+ parent: None,
+ gone: true,
+ },
+ ];
+ let was_in = |id: &str| match id {
+ "FILE::com.apple.CloudDocs::F" | "FILE::com.apple.CloudDocs::P" => Some(a.to_string()),
+ _ => None,
+ };
+ let known = |id: &str| id == a || id == b;
+ assert_eq!(folders_to_list(&changed, was_in, known), vec![b, a]);
+ }
+}
diff --git a/crates/quince-icloud/src/lib.rs b/crates/quince-icloud/src/lib.rs
index 370cbe8..99cd83b 100644
--- a/crates/quince-icloud/src/lib.rs
+++ b/crates/quince-icloud/src/lib.rs
@@ -22,6 +22,7 @@ pub mod cloudkit;
pub mod contacts;
pub mod courier;
pub mod drive;
+pub mod drive_changes;
pub mod markdown;
pub mod mergeable;
pub mod note_media;
diff --git a/probe/examples/drive_watch_live.rs b/probe/examples/drive_watch_live.rs
new file mode 100644
index 0000000..62ba70e
--- /dev/null
+++ b/probe/examples/drive_watch_live.rs
@@ -0,0 +1,87 @@
+//! Live: Skykeep's Drive change reading (quince_icloud::drive_changes) end to end. Reads the
+//! feed through to now (timed), makes a throwaway folder ("Quince mount test …") in the top
+//! level, and shows which folders the watcher would list again; then deletes it and shows the
+//! same. Clears it from Recently Deleted at the end. Ids and timings only.
+use anyhow::{anyhow, Result};
+use quince_icloud::drive_changes::{folders_to_list, page, Changed};
+use std::time::{Duration, Instant};
+
+const ROOT: &str = "FOLDER::com.apple.CloudDocs::root";
+
+async fn read(
+ http: &reqwest::Client,
+ ck: &str,
+ p: &[(String, String)],
+ token: &mut Option<String>,
+) -> Result<Vec<Changed>> {
+ let mut all = Vec::new();
+ loop {
+ let pg = page(http, ck, p, token.as_deref()).await?;
+ all.extend(pg.changed);
+ *token = pg.token.or(token.take());
+ if !pg.more {
+ return Ok(all);
+ }
+ }
+}
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let s = client.session();
+ let ck = s
+ .service_url("ckdatabasews")
+ .ok_or_else(|| anyhow!("no ck"))?;
+ let drivews = s
+ .service_url("drivews")
+ .ok_or_else(|| anyhow!("no drivews"))?;
+ let (http, p) = (client.http().clone(), client.service_params());
+ let t = Instant::now();
+ let mut token = None;
+ let n = read(&http, &ck, &p, &mut token).await?.len();
+ println!("first read: {n} items in {:?}", t.elapsed());
+ let name = format!("Quince mount test {}", std::process::id());
+ let made = quince_icloud::drive::create_folder(&http, &drivews, p.clone(), ROOT, &name).await?;
+ tokio::time::sleep(Duration::from_secs(1)).await;
+ let t = Instant::now();
+ let changed = read(&http, &ck, &p, &mut token).await?;
+ let mine = made.drivewsid.clone();
+ let folders = folders_to_list(&changed, |_| None, |id| id == ROOT);
+ println!(
+ "after make ({:?}): {} changed, made one named: {}, list again: {:?}",
+ t.elapsed(),
+ changed.len(),
+ changed
+ .iter()
+ .any(|c| c.drivewsid == mine && c.parent.as_deref() == Some(ROOT)),
+ folders
+ );
+ quince_icloud::drive::delete(&http, &drivews, p.clone(), &made).await?;
+ tokio::time::sleep(Duration::from_secs(1)).await;
+ let changed = read(&http, &ck, &p, &mut token).await?;
+ // As the library would answer: the folder was in the top level.
+ let folders = folders_to_list(
+ &changed,
+ |id| (id == mine).then(|| ROOT.to_string()),
+ |id| id == ROOT,
+ );
+ println!(
+ "after delete: {} changed, list again: {:?}",
+ changed.len(),
+ folders
+ );
+ let trash = quince_icloud::drive::trash(&http, &drivews, p.clone()).await?;
+ let mine: Vec<_> = trash
+ .into_iter()
+ .filter(|i| i.name.starts_with("Quince mount test"))
+ .collect();
+ println!("throwaways in Recently Deleted: {}", mine.len());
+ if !mine.is_empty() {
+ quince_icloud::drive::delete_forever_many(&http, &drivews, p, &mine).await?;
+ }
+ Ok(())
+}
diff --git a/src-tauri/src/drive_watch.rs b/src-tauri/src/drive_watch.rs
new file mode 100644
index 0000000..26a7bf3
--- /dev/null
+++ b/src-tauri/src/drive_watch.rs
@@ -0,0 +1,209 @@
+//! iCloud Drive's changes, heard as they happen and applied folder by folder.
+//!
+//! Drive has no push (measured 2026-10-29), so a file added on the phone used to show only
+//! when its folder was listed again: the open folder every ten seconds, the rest when opened,
+//! and the Drive folder on this computer not until then either. Now one tiny read-only check —
+//! whether Drive's CloudKit database moved — runs as often as the other services' checks do;
+//! when it moves, Drive's own change feed says which items changed and where they are, and
+//! only the folders they left or went into are listed again (quince_icloud::drive_changes).
+//! Stored, every view reads them at once: the window is told which folders changed, and the
+//! Drive folder on this computer sees the library move.
+//!
+//! The feed's position is kept in the library, so changes made while the app was closed are
+//! read when it starts. The first run reads the feed through to now once — identifiers only,
+//! a few hundred bytes a record — before it can tell what is new.
+
+use crate::push::{poll_every, presence, sleep_or_wake, WAKE};
+use crate::Data;
+use quince_auth::Client;
+use quince_icloud::drive_changes::{self, Changed, CONTAINER};
+use quince_icloud::Error;
+use std::sync::Arc;
+use std::time::Duration;
+use tauri::{Emitter, Manager};
+use tokio::sync::Mutex;
+
+/// Told to the window with the folders listed again, so an open one redraws.
+pub const CHANGED: &str = "quince://drive-changed";
+
+/// Where the feed was last read to, with the account it belongs to: `<dsid>\t<token>`.
+const POSITION: &str = "drive.changes";
+
+/// The most pages one catch-up reads before it stores what it has and lets the next check go
+/// on. A first read of a large Drive is a few dozen pages.
+const PAGES_AT_ONCE: usize = 500;
+
+pub async fn watch(app: tauri::AppHandle, client: Arc<Mutex<Client>>) {
+ let mut database: Option<String> = None;
+ // Read the feed on the first check whatever the database says: changes made while the
+ // app was closed are behind a database token seeded only now.
+ let mut catch_up = true;
+ let mut backoff: Option<Duration> = None;
+ let mut woken = WAKE.subscribe();
+ loop {
+ let session = {
+ let guard = client.lock().await;
+ let s = guard.session();
+ s.webservices
+ .is_some()
+ .then(|| {
+ Some((
+ s.service_url("ckdatabasews")?,
+ s.service_url("drivews")?,
+ s.dsid.clone()?,
+ ))
+ })
+ .flatten()
+ .map(|(ck, drivews, dsid)| {
+ (
+ guard.http().clone(),
+ guard.service_params(),
+ ck,
+ drivews,
+ dsid,
+ )
+ })
+ };
+ let Some((http, params, ck, drivews, dsid)) = session else {
+ (database, catch_up) = (None, true);
+ sleep_or_wake(Duration::from_secs(30), &mut woken).await;
+ continue;
+ };
+ let moved = match quince_icloud::push::database_changed(
+ &http,
+ &ck,
+ &params,
+ CONTAINER,
+ database.as_deref(),
+ )
+ .await
+ {
+ Ok((moved, next)) => {
+ database = next;
+ Ok(moved)
+ }
+ Err(Error::StaleCursor) => {
+ database = None;
+ Ok(true)
+ }
+ Err(e) => Err(e),
+ };
+ let result = match moved {
+ Ok(moved) if moved || catch_up => {
+ let read = read_feed(&app, &http, &params, &ck, &drivews, &dsid).await;
+ if read.is_ok() {
+ catch_up = false;
+ }
+ read
+ }
+ Ok(_) => Ok(()),
+ Err(e) => Err(e),
+ };
+ backoff = match result {
+ Ok(()) => None,
+ Err(e) => {
+ let next = backoff.map_or(Duration::from_secs(5), |b| {
+ (b * 2).min(Duration::from_secs(5 * 60))
+ });
+ if backoff.is_none() {
+ quiet!("drive watch: {e}; trying again in {}s", next.as_secs());
+ }
+ Some(next)
+ }
+ };
+ let wait = backoff.unwrap_or_else(|| poll_every(presence(&app)));
+ sleep_or_wake(wait, &mut woken).await;
+ }
+}
+
+/// Read the feed from where it was left, list again the folders it touched, and keep the new
+/// position — only once they are stored, so a failure part way reads the same changes again.
+async fn read_feed(
+ app: &tauri::AppHandle,
+ http: &reqwest::Client,
+ params: &[(String, String)],
+ ck: &str,
+ drivews: &str,
+ dsid: &str,
+) -> quince_icloud::Result<()> {
+ let data = app.state::<Data>();
+ let kept = data
+ .0
+ .lock()
+ .await
+ .state(POSITION)
+ .ok()
+ .flatten()
+ .and_then(|v| {
+ let (who, token) = v.split_once('\t')?;
+ (who == dsid).then(|| token.to_string())
+ });
+ let first = kept.is_none();
+ let mut token = kept;
+ let mut changed: Vec<Changed> = Vec::new();
+ for _ in 0..PAGES_AT_ONCE {
+ let page = match drive_changes::page(http, ck, params, token.as_deref()).await {
+ Err(Error::StaleCursor) if token.is_some() => {
+ // Apple no longer knows this position: forgotten, so the next check reads
+ // through to now as a first run does. What changed in between is found as
+ // before this watch, when each folder is next listed.
+ quiet!("drive watch: position expired; reading Drive's changes afresh");
+ let _ = data.0.lock().await.set_state(POSITION, "");
+ return Err(Error::StaleCursor);
+ }
+ other => other?,
+ };
+ // A first read is everything there is, not news: only its end position is kept.
+ if !first {
+ changed.extend(page.changed);
+ }
+ token = page.token.or(token);
+ if !page.more {
+ break;
+ }
+ }
+ let folders = {
+ let library = data.0.lock().await;
+ drive_changes::folders_to_list(
+ &changed,
+ |id| {
+ library
+ .drive_item(id)
+ .ok()
+ .flatten()
+ .and_then(|i| i.parent_id)
+ },
+ |id| id == quince_core::drive::ROOT || library.drive_item(id).ok().flatten().is_some(),
+ )
+ };
+ let mut listed = Vec::new();
+ for folder in folders {
+ let pass = crate::drive_pass(&folder);
+ let _one = pass.lock().await;
+ match quince_icloud::drive::fetch_folder(http, drivews, params.to_vec(), &folder).await {
+ Ok(items) => {
+ let mut library = data.0.lock().await;
+ quince_icloud::drive::store_folder(&mut library, &folder, &items)?;
+ }
+ // Gone since: forgotten here too, as when the window finds it gone.
+ Err(Error::NotFound) => {
+ let mut library = data.0.lock().await;
+ library.forget_drive_folder(&folder)?;
+ }
+ Err(e) => return Err(e),
+ }
+ listed.push(folder);
+ }
+ if let Some(t) = token {
+ let _ = data
+ .0
+ .lock()
+ .await
+ .set_state(POSITION, &format!("{dsid}\t{t}"));
+ }
+ if !listed.is_empty() {
+ quiet!("drive watch: {} folder(s) changed", listed.len());
+ app.emit(CHANGED, &listed).ok();
+ }
+ Ok(())
+}
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index c88f6a3..c33152a 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -18,6 +18,7 @@ mod apps;
mod contacts_cmds;
mod contacts_merge;
mod cpu;
+mod drive_watch;
mod export;
mod grants;
mod intake;
diff --git a/src-tauri/src/push.rs b/src-tauri/src/push.rs
index be268d1..dd6f127 100644
--- a/src-tauri/src/push.rs
+++ b/src-tauri/src/push.rs
@@ -60,7 +60,7 @@ static LIVE: AtomicBool = AtomicBool::new(false);
static CATCH_UP: AtomicBool = AtomicBool::new(false);
/// Bumped by [`wake`]; the channel and the poller each watch it.
-static WAKE: LazyLock<watch::Sender<u64>> = LazyLock::new(|| watch::channel(0).0);
+pub(crate) static WAKE: LazyLock<watch::Sender<u64>> = LazyLock::new(|| watch::channel(0).0);
/// Something happened that makes now a good moment to look: a window gained focus, or the
/// network came back. A channel that failed for a passing reason is tried again at once, and
@@ -70,7 +70,7 @@ pub fn wake() {
}
/// Sleep for `wait`, or until [`wake`] — whichever is first. True when woken.
-async fn sleep_or_wake(wait: Duration, woken: &mut watch::Receiver<u64>) -> bool {
+pub(crate) async fn sleep_or_wake(wait: Duration, woken: &mut watch::Receiver<u64>) -> bool {
tokio::select! {
() = tokio::time::sleep(wait) => false,
r = woken.changed() => r.is_ok(),
@@ -83,6 +83,7 @@ pub fn spawn(app: tauri::AppHandle, client: Arc<Mutex<Client>>) {
tauri::async_runtime::spawn(refresher(app.clone(), Arc::clone(&client), rx));
tauri::async_runtime::spawn(watch_calendar(app.clone(), Arc::clone(&client), tx.clone()));
tauri::async_runtime::spawn(watch_contacts(app.clone(), Arc::clone(&client), tx.clone()));
+ tauri::async_runtime::spawn(crate::drive_watch::watch(app.clone(), Arc::clone(&client)));
tauri::async_runtime::spawn(poll_cloudkit(app, Arc::clone(&client), tx.clone()));
tauri::async_runtime::spawn(listen(client, tx));
}
@@ -143,7 +144,7 @@ async fn watch_contacts(
/// How present the app is: a window with focus, one merely on screen, or none (the tray).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-enum Presence {
+pub(crate) enum Presence {
Focused,
Open,
Hidden,
@@ -151,7 +152,7 @@ enum Presence {
/// Every window counts — the whole-library one and each app's own. A minimized window is
/// still "visible" to Windows (and to Tauri there), so it is counted as hidden.
-fn presence(app: &tauri::AppHandle) -> Presence {
+pub(crate) fn presence(app: &tauri::AppHandle) -> Presence {
let mut open = false;
for w in app.webview_windows().values() {
if !w.is_visible().unwrap_or(false) || w.is_minimized().unwrap_or(false) {
@@ -516,7 +517,7 @@ async fn run(
/// How often the containers are asked whether anything changed while no live channel is up:
/// often with a window in focus, less with one merely open, rarely from the tray.
/// On the battery, what no one is looking at is asked half as often (power.rs).
-fn poll_every(presence: Presence) -> Duration {
+pub(crate) fn poll_every(presence: Presence) -> Duration {
let every = match presence {
Presence::Focused => Duration::from_secs(5),
Presence::Open => Duration::from_secs(15),
diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsx
index 96e649b..da3ab83 100644
--- a/src/screens/DriveView.tsx
+++ b/src/screens/DriveView.tsx
@@ -672,11 +672,12 @@ export function DriveView() {
* "loading", and the pictures stay: re-opening the folder wiped the thumbnails and drew
* them back, a blink across the grid every time a file arrived.
*/
- const refresh = useCallback(async () => {
+ const refresh = useCallback(async (ask = true) => {
const folderId = hereRef.current;
const token = openToken.current;
try {
- await api.driveSyncFolder(folderId);
+ // Not asked again when the backend has just listed it (`quince://drive-changed`).
+ if (ask) await api.driveSyncFolder(folderId);
if (token !== openToken.current) return;
const fresh = await api.driveChildren(folderId);
if (token !== openToken.current) return;
@@ -702,11 +703,11 @@ export function DriveView() {
// A quiet refresh that fails changes nothing; opening the folder says what is wrong.
}
}, []);
- // Apple sends no push for iCloud Drive (measured: none in 25 s for a folder made or
- // deleted), and icloud.com reads a folder only as it is opened. The folder in front is
- // checked again every ten seconds while the window has the focus, and at once when the
- // window comes back — one small request; none while hidden or in the background — so a file saved on the phone appears
- // here as it would in Finder.
+ // Apple sends no push for iCloud Drive, but Drive's change feed names each item changed
+ // anywhere in it within a second (src-tauri drive_watch.rs). The backend lists again the
+ // folders it touched and says which: the one in front is redrawn from the library, with no
+ // request of its own. Coming back to the window still looks at once, for the app libraries
+ // (Pages, Numbers…) the feed does not cover.
const shown = useScreenActive();
const shownNow = useRef(shown);
shownNow.current = shown;
@@ -717,16 +718,16 @@ export function DriveView() {
const look = () => {
if (document.visibilityState === "visible" && !writingRef.current) void refresh();
};
- // The timed check only while the window has the focus: one merely open behind another, or
- // on another workspace, asked Apple every ten seconds all day for a folder nobody was
- // looking at. Coming back to it (focus) looks at once, so nothing is missed.
- const t = window.setInterval(() => {
- if (document.hasFocus()) look();
- }, 10_000);
+ let gone = false;
+ let stop: (() => void) | undefined;
+ void listen<string[]>("quince://drive-changed", (e) => {
+ if (!writingRef.current && e.payload.includes(hereRef.current)) void refresh(false);
+ }).then((un) => (gone ? un() : (stop = un)));
document.addEventListener("visibilitychange", look);
window.addEventListener("focus", look);
return () => {
- window.clearInterval(t);
+ gone = true;
+ stop?.();
document.removeEventListener("visibilitychange", look);
window.removeEventListener("focus", look);
};