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: a folder deleted elsewhere is never read as empty; no second copy on a lost reply

Yashwanth Kamireddi96b66133196d
diff --git a/.uiverify/drive-gone.mjs b/.uiverify/drive-gone.mjs
new file mode 100644
index 0000000..213016b
--- /dev/null
+++ b/.uiverify/drive-gone.mjs
@@ -0,0 +1,23 @@
+// A folder deleted on another device while it is open here: Drive steps back out to the
+// folder above, says why, and never shows the gone folder as merely empty.
+import { engine } from "./engine.mjs";
+import { SERVICES, installStub } from "./stub.mjs";
+const b = await engine.launch();
+const ctx = await b.newContext({ viewport: { width: 1280, height: 800 } });
+await ctx.addInitScript(installStub, { screen: "library", services: SERVICES, twoFactor: null, driveFixture: true });
+const page = await ctx.newPage();
+await page.goto("http://localhost:4173", { waitUntil: "networkidle" });
+await page.locator(".nav-item", { hasText: "iCloud Drive" }).click();
+await page.waitForTimeout(400);
+let failures = 0;
+const ok = (pass, what) => { console.log(`${pass ? "PASS" : "FAIL"} ${what}`); if (!pass) failures++; };
+const crumbs = () => page.locator(".crumb").allInnerTexts();
+
+await page.evaluate(() => { window.__driveGone = "FOLDER::a"; });
+await page.locator(".drive-row-hit", { hasText: "Downloads" }).dblclick();
+await page.waitForTimeout(600);
+ok(!(await crumbs()).some((t) => t.includes("Downloads")), `stepped back out of the gone folder ${JSON.stringify(await crumbs())}`);
+ok((await page.locator(".notice", { hasText: "deleted or moved on another device" }).count()) === 1, "and says why");
+ok((await page.locator(".drive-row-hit", { hasText: "Semester Plan" }).count()) === 1, "showing the folder above");
+await b.close();
+process.exitCode = failures ? 1 : 0;
diff --git a/.uiverify/stub.mjs b/.uiverify/stub.mjs
index 66bffda..f15e97b 100644
--- a/.uiverify/stub.mjs
+++ b/.uiverify/stub.mjs
@@ -294,7 +294,10 @@ export function installStub({ screen, services, twoFactor, calendarFixture, note
window.__trash = (window.__trash ?? []).filter((i) => i.drivewsid !== args.item.drivewsid);
(window.__trashCalls ??= []).push([cmd, args.item.drivewsid]);
return null;
- case "drive_sync_folder": return driveItems.length;
+ case "drive_sync_folder":
+ // A folder deleted on another device: the backend forgets it and says so.
+ if (args.folderId === window.__driveGone) throw "This folder was deleted or moved on another device.";
+ return driveItems.length;
case "drive_download": return "/home/user/Downloads/file.pdf";
case "drive_move": (window.__moves ??= []).push(args); await new Promise((r) => setTimeout(r, 600)); return null;
case "drive_create_folder": case "drive_rename": case "drive_delete": return null;
diff --git a/crates/quince-core/src/drive.rs b/crates/quince-core/src/drive.rs
index 8959ae9..0fc8cf9 100644
--- a/crates/quince-core/src/drive.rs
+++ b/crates/quince-core/src/drive.rs
@@ -110,6 +110,20 @@ impl Library {
Ok(())
}
+ /// Forget a folder Apple no longer has — it, and everything stored beneath it, at any
+ /// depth. Only for a folder Apple has said is gone: nothing here asks Apple anything.
+ pub fn forget_drive_folder(&mut self, drivewsid: &str) -> Result<()> {
+ self.conn.execute(
+ "WITH RECURSIVE gone(id) AS (
+ SELECT ?1
+ UNION SELECT d.drivewsid FROM drive_items d JOIN gone ON d.parent_id = gone.id
+ )
+ DELETE FROM drive_items WHERE drivewsid IN (SELECT id FROM gone)",
+ params![drivewsid],
+ )?;
+ Ok(())
+ }
+
/// What is inside a folder, folders first and then by name — how a file browser reads.
///
/// Named the way Finder orders them: numbers as numbers and accents ignored, so "Scan 10"
@@ -192,6 +206,37 @@ mod tests {
}
}
+ #[test]
+ fn a_forgotten_folder_takes_everything_beneath_it_and_nothing_else() {
+ let mut lib = Library::in_memory().unwrap();
+ lib.put_drive_children(
+ ROOT,
+ &[item("F::a", "a", "FOLDER"), item("F::b", "b", "FOLDER")],
+ )
+ .unwrap();
+ lib.put_drive_children(
+ "F::a",
+ &[item("F::a1", "a1", "FOLDER"), item("D::x", "x", "FILE")],
+ )
+ .unwrap();
+ lib.put_drive_children("F::a1", &[item("D::deep", "deep", "FILE")])
+ .unwrap();
+ lib.put_drive_children("F::b", &[item("D::y", "y", "FILE")])
+ .unwrap();
+ lib.forget_drive_folder("F::a").unwrap();
+ let names = |p: &str| {
+ lib.drive_children(p)
+ .unwrap()
+ .into_iter()
+ .map(|i| i.name)
+ .collect::<Vec<_>>()
+ };
+ assert_eq!(names(ROOT), ["b"]);
+ assert!(names("F::a").is_empty() && names("F::a1").is_empty());
+ assert!(lib.drive_item("D::deep").unwrap().is_none());
+ assert_eq!(names("F::b"), ["y"]);
+ }
+
#[test]
fn folders_come_before_files_then_alphabetical() {
let mut lib = Library::in_memory().unwrap();
diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rs
index fbf63cc..dc55f62 100644
--- a/crates/quince-icloud/src/drive.rs
+++ b/crates/quince-icloud/src/drive.rs
@@ -54,6 +54,35 @@ pub fn parse_folder(body: &Value) -> Vec<DriveItem> {
.unwrap_or_default()
}
+/// A folder's contents, from a `retrieveItemDetailsInFolders` answer that has been checked
+/// to be one: a stored folder is replaced by whatever this returns, so an answer that is not
+/// a listing must never read as an empty folder.
+///
+/// Measured: a folder that no longer exists comes back `200` with `"items": []` and
+/// `"status": "ID_INVALID"` — exactly what an empty folder looks like, but for the status.
+/// A real folder's status is `"OK"`.
+fn listing(body: &Value) -> Result<Vec<DriveItem>> {
+ let folder = body
+ .as_array()
+ .and_then(|folders| folders.first())
+ .ok_or_else(|| Error::Protocol("Drive answered without the folder asked for".into()))?;
+ match folder.get("status").and_then(Value::as_str) {
+ None | Some("OK") => {}
+ Some("ID_INVALID") => return Err(Error::NotFound),
+ Some(other) => {
+ return Err(Error::Protocol(format!(
+ "Drive could not list the folder: {other}"
+ )))
+ }
+ }
+ if !folder.get("items").is_some_and(Value::is_array) {
+ return Err(Error::Protocol(
+ "Drive answered without the folder's items".into(),
+ ));
+ }
+ Ok(parse_folder(body))
+}
+
/// Fetch one folder's contents and store them.
pub async fn sync_folder(
http: &reqwest::Client,
@@ -110,7 +139,7 @@ pub async fn fetch_folder(
let parsed: Value = serde_json::from_str(&text)
.map_err(|e| Error::Protocol(format!("Drive returned invalid JSON: {e}")))?;
- Ok(parse_folder(&parsed))
+ listing(&parsed)
}
/// Bare docwsid: `FILE::com.apple.CloudDocs::<uuid>` carries it after the last `::`.
@@ -325,7 +354,7 @@ async fn folder_listing(
}
let parsed: Value = serde_json::from_str(&text)
.map_err(|e| Error::Protocol(format!("Drive returned invalid JSON: {e}")))?;
- Ok(parse_folder(&parsed))
+ listing(&parsed)
}
/// The parent a write on `item` should be verified against: its own folder if it has one,
@@ -693,7 +722,7 @@ pub async fn trash_items(
.and_then(Value::as_array)
.map(|a| a.iter().collect())
.unwrap_or_default();
- Ok(parse_folder(&parsed)
+ Ok(listing(&parsed)?
.into_iter()
.map(|item| {
let found = raw.iter().find(|r| {
@@ -1005,6 +1034,74 @@ mod tests {
assert_eq!(children.len(), 3);
assert_eq!(children[0].name, "Downloads", "folders sort first");
}
+
+ /// A folder deleted elsewhere answers like an empty one but for its status. Read as empty,
+ /// it would wipe the folder this computer holds; it must fail instead.
+ #[tokio::test]
+ async fn a_folder_apple_no_longer_has_never_empties_the_stored_one() {
+ use wiremock::matchers::{method, path_regex};
+ use wiremock::{Mock, MockServer, ResponseTemplate};
+
+ let mut library = quince_core::Library::in_memory().unwrap();
+ let root = quince_core::drive::ROOT;
+ library
+ .put_drive_children(root, &parse_folder(&response()))
+ .unwrap();
+
+ for (answer, gone) in [
+ (
+ json!([{ "items": [], "numberOfItems": 0, "status": "ID_INVALID" }]),
+ true,
+ ),
+ (json!([{ "drivewsid": root, "status": "OK" }]), false),
+ (json!([]), false),
+ ] {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/retrieveItemDetailsInFolders$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(answer))
+ .mount(&server)
+ .await;
+ let result = sync_folder(
+ &reqwest::Client::new(),
+ &server.uri(),
+ vec![],
+ &mut library,
+ root,
+ )
+ .await;
+ assert_eq!(matches!(result, Err(Error::NotFound)), gone, "{result:?}");
+ assert!(result.is_err());
+ assert_eq!(
+ library.drive_children(root).unwrap().len(),
+ 3,
+ "the stored folder is kept"
+ );
+ }
+
+ // A folder that really is empty still reads as empty.
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/retrieveItemDetailsInFolders$"))
+ .respond_with(
+ ResponseTemplate::new(200)
+ .set_body_json(json!([{ "drivewsid": root, "status": "OK", "items": [] }])),
+ )
+ .mount(&server)
+ .await;
+ assert_eq!(
+ sync_folder(
+ &reqwest::Client::new(),
+ &server.uri(),
+ vec![],
+ &mut library,
+ root
+ )
+ .await
+ .unwrap(),
+ 0
+ );
+ }
}
#[cfg(test)]
@@ -1865,8 +1962,27 @@ async fn upload_once(
},
}),
)
- .await?;
- let stored = filed_document(&filed, &local, size, &name)?;
+ .await;
+ let stored = match filed {
+ Ok(filed) => filed_document(&filed, &local, size, &name)?,
+ // No answer is not "not filed": Apple may have filed it and the reply been lost. Asked
+ // again from the start, the file would go up a second time beside the first — "photo
+ // 2.jpg". The slot's document id is this file's alone, so the folder says which.
+ Err(e @ (Error::Network(_) | Error::Refused(_))) => {
+ let id = format!("FILE::{DOCUMENTS_ZONE}::{document_id}");
+ match folder_listing(http, drivews_root, &params, folder_id).await {
+ Ok(items)
+ if items
+ .iter()
+ .any(|i| i.drivewsid == id && i.size == Some(size as i64)) =>
+ {
+ id
+ }
+ _ => return Err(e),
+ }
+ }
+ Err(e) => return Err(e),
+ };
// Apple answers 200 to writes it did not make. The folder is the only witness that counts.
let after = folder_listing(http, drivews_root, &params, folder_id).await?;
@@ -2141,6 +2257,71 @@ mod app_library_tests {
std::fs::remove_file(&file).ok();
}
+ /// Filing answered busy every time, though Apple did file it: the folder shows the slot's
+ /// document, so that is the upload — not a second copy sent from a fresh slot.
+ #[tokio::test]
+ async fn a_file_filed_without_an_answer_is_not_sent_twice() {
+ use wiremock::matchers::{method, path_regex};
+ use wiremock::{Mock, MockServer, ResponseTemplate};
+ let server = MockServer::start().await;
+ let mut sum = AppleChecksum::default();
+ sum.update(b"hello");
+ let sum = sum.finish();
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/upload/web$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([
+ { "url": format!("{}/content", server.uri()), "document_id": "D1" }
+ ])))
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/content$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
+ "singleFile": { "fileChecksum": sum, "size": 5, "receipt": "r" }
+ })))
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/update/documents$"))
+ .respond_with(
+ ResponseTemplate::new(503).set_body_json(serde_json::json!({ "retryAfter": 1 })),
+ )
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/retrieveItemDetailsInFolders$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([{
+ "drivewsid": "FOLDER::com.apple.CloudDocs::root", "status": "OK",
+ "items": [{ "drivewsid": "FILE::com.apple.CloudDocs::D1", "name": "quince-lost-reply",
+ "extension": "txt", "type": "FILE", "size": 5 }]
+ }])))
+ .mount(&server)
+ .await;
+ let file = std::env::temp_dir().join("quince-lost-reply.txt");
+ std::fs::write(&file, b"hello").unwrap();
+ let item = upload(
+ &reqwest::Client::new(),
+ &server.uri(),
+ &server.uri(),
+ vec![],
+ "FOLDER::com.apple.CloudDocs::root",
+ &file,
+ std::sync::Arc::new(|_, _| {}),
+ )
+ .await
+ .expect("the folder holds it, so it was uploaded");
+ assert_eq!(item.drivewsid, "FILE::com.apple.CloudDocs::D1");
+ let slots = server
+ .received_requests()
+ .await
+ .unwrap()
+ .iter()
+ .filter(|r| r.url.path().ends_with("/upload/web"))
+ .count();
+ assert_eq!(slots, 1, "sent once, never again beside itself");
+ std::fs::remove_file(&file).ok();
+ }
+
/// The documents zone is everything else, and must still take a file.
#[tokio::test]
async fn the_ordinary_drive_is_not_refused() {
diff --git a/probe/examples/drive_move_clash.rs b/probe/examples/drive_move_clash.rs
new file mode 100644
index 0000000..b69b1aa
--- /dev/null
+++ b/probe/examples/drive_move_clash.rs
@@ -0,0 +1,56 @@
+//! Throwaway: what Apple does when something is moved into a folder that already holds an item
+//! of the same name — refuses, renames, or replaces. Everything made here is deleted after.
+use anyhow::{anyhow, Result};
+use quince_icloud::drive;
+const ROOT: &str = "FOLDER::com.apple.CloudDocs::root";
+#[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 docws = client
+ .session()
+ .service_url("docws")
+ .ok_or_else(|| anyhow!("no docws"))?;
+ let dws = client
+ .session()
+ .service_url("drivews")
+ .ok_or_else(|| anyhow!("no drivews"))?;
+ let (http, p) = (client.http().clone(), client.service_params());
+ let none = || std::sync::Arc::new(|_: u64, _: u64| {}) as drive::Progress;
+ let top = drive::create_folder(&http, &dws, p.clone(), ROOT, "Quince move clash test").await?;
+ let dest = drive::create_folder(&http, &dws, p.clone(), &top.drivewsid, "dest").await?;
+ let tmp = std::env::temp_dir().join("quince-clash");
+ std::fs::create_dir_all(&tmp)?;
+ let f = tmp.join("same.txt");
+ std::fs::write(&f, b"one")?;
+ let there = drive::upload(&http, &docws, &dws, p.clone(), &dest.drivewsid, &f, none()).await?;
+ std::fs::write(&f, b"two!")?;
+ let moving = drive::upload(&http, &docws, &dws, p.clone(), &top.drivewsid, &f, none()).await?;
+ println!(
+ "in dest: {} ({:?}), moving: {} ({:?})",
+ there.name, there.size, moving.name, moving.size
+ );
+ let r = drive::move_to(&http, &dws, p.clone(), &moving, &dest.drivewsid).await;
+ println!("move file onto same name: {r:?}");
+ let sub = drive::create_folder(&http, &dws, p.clone(), &top.drivewsid, "dest2").await?;
+ let sub_in = drive::create_folder(&http, &dws, p.clone(), &dest.drivewsid, "dest2").await?;
+ let r2 = drive::move_to(&http, &dws, p.clone(), &sub, &dest.drivewsid).await;
+ println!(
+ "move folder onto same-named folder: {r2:?} (inner {})",
+ sub_in.name
+ );
+ let mut lib = quince_core::Library::in_memory()?;
+ drive::sync_folder(&http, &dws, p.clone(), &mut lib, &dest.drivewsid).await?;
+ for i in lib.drive_children(&dest.drivewsid)? {
+ println!(
+ " dest holds {} {:?} {:?} {}",
+ i.name, i.extension, i.size, i.drivewsid
+ );
+ }
+ drive::delete(&http, &dws, p.clone(), &top).await?;
+ println!("cleaned up");
+ Ok(())
+}
diff --git a/probe/examples/drive_raw_folder.rs b/probe/examples/drive_raw_folder.rs
new file mode 100644
index 0000000..06be441
--- /dev/null
+++ b/probe/examples/drive_raw_folder.rs
@@ -0,0 +1,57 @@
+//! Read-only: Apple's raw answer for a folder, a folder with nothing in it, and one that
+//! does not exist — what a fetch must tell apart before it replaces a stored folder.
+use anyhow::{anyhow, Result};
+#[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 root = client
+ .session()
+ .service_url("drivews")
+ .ok_or_else(|| anyhow!("no drivews"))?;
+ let http = client.http().clone();
+ let mut q = client.service_params();
+ q.push(("clientVersion".into(), "2.1".into()));
+ let ask = |id: String| {
+ let (http, q, root) = (http.clone(), q.clone(), root.clone());
+ async move {
+ let r = http
+ .post(format!(
+ "{}/retrieveItemDetailsInFolders",
+ root.trim_end_matches('/')
+ ))
+ .query(&q)
+ .json(&serde_json::json!([{ "drivewsid": id, "partialData": false }]))
+ .send()
+ .await?;
+ let s = r.status();
+ let v: serde_json::Value = r.json().await?;
+ anyhow::Ok((s, v))
+ }
+ };
+ let (s, v) = ask("FOLDER::com.apple.CloudDocs::root".into()).await?;
+ let items = v[0]["items"].as_array().cloned().unwrap_or_default();
+ println!("root {s}: {} items, status {}", items.len(), v[0]["status"]);
+ if let Some(empty) = items
+ .iter()
+ .find(|i| i["type"] == "FOLDER" && i["numberOfItems"] == 0)
+ {
+ let (s, v) = ask(empty["drivewsid"].as_str().unwrap().into()).await?;
+ println!(
+ "empty {s}: {}",
+ v.to_string().chars().take(400).collect::<String>()
+ );
+ } else {
+ println!("no empty folder at the top level");
+ }
+ let (s, v) =
+ ask("FOLDER::com.apple.CloudDocs::00000000-0000-0000-0000-000000000000".into()).await?;
+ println!(
+ "missing {s}: {}",
+ v.to_string().chars().take(400).collect::<String>()
+ );
+ Ok(())
+}
diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsx
index e6dedad..e880ecc 100644
--- a/src/screens/DriveView.tsx
+++ b/src/screens/DriveView.tsx
@@ -482,10 +482,14 @@ export function DriveView() {
// fifteen "Downloads" deep, all pointing at the one actual folder, turned out to be.
const openToken = useRef(0);
+ // Why the last folder was left, kept on screen in the one stepped back to.
+ const leftBecause = useRef<string | null>(null);
+
const open = useCallback(async (folderId: string) => {
const token = ++openToken.current;
setLoading(true);
- setError(null);
+ setError(leftBecause.current);
+ leftBecause.current = null;
try {
// Show what is stored first, then fetch — a folder visited before opens instantly.
const cached = await api.driveChildren(folderId);
@@ -506,7 +510,18 @@ export function DriveView() {
() => {},
);
} catch (e) {
- if (token === openToken.current) setError(messageOf(e));
+ if (token !== openToken.current) return;
+ setError(messageOf(e));
+ // Deleted or moved on another device: the backend has forgotten it, so step back out
+ // to the nearest folder still here rather than sit in one that is not.
+ if (folderId !== DRIVE_ROOT && (await api.driveItem(folderId).catch(() => undefined)) === null) {
+ if (token !== openToken.current) return;
+ leftBecause.current = messageOf(e);
+ setTrail((t) => {
+ const at = t.findIndex((c) => c.id === folderId);
+ return at > 0 ? t.slice(0, at) : t;
+ });
+ }
} finally {
if (token === openToken.current) {
setLoading(false);