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: stays where you are, fills in as files land, retries a busy server

Yashwanth Kamireddi385f0843b2c0
diff --git a/.uiverify/drive-nav.mjs b/.uiverify/drive-nav.mjs
index ef29a30..d545125 100644
--- a/.uiverify/drive-nav.mjs
+++ b/.uiverify/drive-nav.mjs
@@ -54,5 +54,15 @@ await page.waitForTimeout(200);
const making = await page.locator(".drive-row.is-busy").allInnerTexts();
ok(making.some((t) => t.includes("Trips") && t.includes("Creating")), "a new folder shows at once, being made",
`straight after Return: ${JSON.stringify(making)}`);
+// Gone elsewhere before it finished: the folder on screen stays the one on screen. A refresh
+// bound to where the work began re-read that folder at the end, and its rows replaced these.
+await page.locator(".drive-row-hit", { hasText: "Downloads" }).first().click();
+await page.waitForTimeout(2200);
+const inDownloads = await page.locator(".drive-name").allInnerTexts();
+await page.waitForTimeout(500);
+const stayed = await page.locator(".drive-name").allInnerTexts();
+const crumb = await page.locator('.crumb[aria-current="true"]').innerText();
+ok(crumb === "Downloads" && stayed.some((t) => t.startsWith("Ticket")) && !stayed.includes("Keynote"),
+ "a folder made elsewhere finishing does not pull the screen back", `in ${crumb}: ${JSON.stringify(stayed)} (was ${JSON.stringify(inDownloads)})`);
await b.close();
process.exitCode = failures === 0 ? 0 : 1;
diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rs
index 8b7a2de..a0c9117 100644
--- a/crates/quince-icloud/src/drive.rs
+++ b/crates/quince-icloud/src/drive.rs
@@ -1383,28 +1383,111 @@ async fn post_json(
params: &[(String, String)],
body: &Value,
) -> Result<Value> {
- let response = http
- .post(url)
- .query(params)
- .header("Referer", "https://www.icloud.com/")
- .json(body)
- .send()
- .await?;
- let status = response.status();
- let text = response.text().await.unwrap_or_default();
+ // A busy Drive (503, 429) asks to be asked again shortly. Ninety files going up three at
+ // a time met exactly that, and every one that met it was reported as not uploaded. It is
+ // waited out, as the server asks, up to three times; only one still busy is reported.
+ let mut tries: u64 = 0;
+ let (status, text) = loop {
+ tries += 1;
+ let response = http
+ .post(url)
+ .query(params)
+ .header("Referer", "https://www.icloud.com/")
+ .json(body)
+ .send()
+ .await?;
+ let status = response.status();
+ let wait = response
+ .headers()
+ .get("retry-after")
+ .and_then(|v| v.to_str().ok())
+ .and_then(|v| v.trim().parse::<u64>().ok());
+ let text = response.text().await.unwrap_or_default();
+ if !matches!(status.as_u16(), 429 | 500 | 502 | 503 | 504) || tries >= 3 {
+ break (status, text);
+ }
+ let asked = serde_json::from_str::<Value>(&text)
+ .ok()
+ .and_then(|v| v.get("retryAfter").and_then(Value::as_u64))
+ .or(wait)
+ .unwrap_or(2 * tries);
+ tokio::time::sleep(std::time::Duration::from_secs(asked.clamp(1, 30))).await;
+ };
if status.as_u16() == 450 {
return Err(Error::ReauthRequired);
}
if !status.is_success() {
- return Err(Error::Protocol(format!(
- "Drive returned {status}: {}",
- text.chars().take(160).collect::<String>()
- )));
+ eprintln!(
+ "drive: {url} answered {status}: {}",
+ text.chars().take(300).collect::<String>()
+ );
+ return Err(Error::Refused(match status.as_u16() {
+ 429 | 500..=599 => {
+ "iCloud Drive is busy right now — try this one again in a minute.".into()
+ }
+ 409 => "Something with that name is already there.".into(),
+ 403 => "iCloud Drive won't let this be changed.".into(),
+ code => format!("iCloud Drive turned this down (status {code})."),
+ }));
}
serde_json::from_str(&text)
.map_err(|e| Error::Protocol(format!("Drive returned invalid JSON: {e}")))
}
+#[cfg(test)]
+mod busy_tests {
+ use super::*;
+ use wiremock::matchers::method;
+ use wiremock::{Mock, MockServer, ResponseTemplate};
+
+ /// Ninety files three at a time met a busy Drive, and each was reported as not uploaded.
+ #[tokio::test]
+ async fn a_busy_drive_is_waited_out() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .respond_with(ResponseTemplate::new(503).insert_header("retry-after", "1"))
+ .up_to_n_times(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .respond_with(
+ ResponseTemplate::new(200).set_body_json(serde_json::json!({ "ok": true })),
+ )
+ .mount(&server)
+ .await;
+ let v = post_json(
+ &reqwest::Client::new(),
+ &server.uri(),
+ &[],
+ &serde_json::json!({}),
+ )
+ .await
+ .unwrap();
+ assert_eq!(v["ok"], true);
+ }
+
+ #[tokio::test]
+ async fn a_drive_that_stays_busy_says_so_plainly() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .respond_with(ResponseTemplate::new(503).insert_header("retry-after", "1"))
+ .mount(&server)
+ .await;
+ let e = post_json(
+ &reqwest::Client::new(),
+ &server.uri(),
+ &[],
+ &serde_json::json!({}),
+ )
+ .await
+ .unwrap_err();
+ assert_eq!(
+ e.to_string(),
+ "iCloud Drive is busy right now — try this one again in a minute."
+ );
+ }
+}
+
#[cfg(test)]
mod upload_tests {
use super::*;
diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsx
index 6c6223a..a412a7d 100644
--- a/src/screens/DriveView.tsx
+++ b/src/screens/DriveView.tsx
@@ -61,6 +61,14 @@ export function DriveView() {
const [dropping, setDropping] = useState(false);
const here = trail[trail.length - 1];
+ // The folder on screen now, for work that finishes later. A refresh bound to the folder
+ // open when an upload or a new folder began re-read that folder when it ended — after the
+ // person had gone elsewhere — and its rows replaced the folder they were in: Drive "went
+ // back to the previous location".
+ const hereRef = useRef(here.id);
+ hereRef.current = here.id;
+ /** Items deleted here that Apple's listing may still carry for a moment after. */
+ const [gone, setGone] = useState<Set<string>>(new Set());
// A folder load is asynchronous and the id it was for is not always the one still on
// screen by the time it resolves — a fast double click used to let a second, third,
@@ -109,7 +117,7 @@ export function DriveView() {
setTrail((t) => [...t, { id: item.drivewsid, name: driveName(item) }]);
}
- const refresh = useCallback(() => open(here.id), [open, here.id]);
+ const refresh = useCallback(() => open(hereRef.current), [open]);
const download = useCallback(async (item: DriveItem) => {
setBusyGuid(item.drivewsid);
@@ -165,6 +173,9 @@ export function DriveView() {
setError(null);
try {
await api.driveDelete(item.drivewsid);
+ // Gone from the screen now, and kept gone: Apple's listing can still carry it for a
+ // moment, and it flickered back before vanishing.
+ setGone((g) => new Set(g).add(item.drivewsid));
await refresh();
} catch (e) {
setError(messageOf(e));
@@ -212,6 +223,21 @@ export function DriveView() {
const [pending, setPending] = useState<{ key: string; folder: string; name: string; doing: string }[]>([]);
const settle = (key: string) => setPending((p) => p.filter((x) => !x.key.startsWith(key)));
+ // At most one re-read of the open folder every couple of seconds while files land.
+ const landedTimer = useRef<number | null>(null);
+ const refreshRef = useRef(refresh);
+ refreshRef.current = refresh;
+ const landed = () => {
+ if (landedTimer.current !== null) return;
+ landedTimer.current = window.setTimeout(() => {
+ landedTimer.current = null;
+ void refreshRef.current();
+ }, 2000);
+ };
+ useEffect(() => () => {
+ if (landedTimer.current !== null) window.clearTimeout(landedTimer.current);
+ }, []);
+
useEffect(() => {
let stop: (() => void) | undefined;
let cancelled = false;
@@ -225,6 +251,8 @@ export function DriveView() {
next.set(u.id, u);
return next;
});
+ // Each file that lands shows as it lands, not all at once when the batch ends.
+ if (u.state === "done") landed();
})
.then((unlisten) => {
// Stopping may throw where there is no event system behind the page — a plain
@@ -266,7 +294,7 @@ export function DriveView() {
try {
await api.driveUpload(batch, into, paths);
// What failed is named in the tray, file by file; the folder is re-read either way.
- if (into === here.id) await refresh();
+ if (into === hereRef.current) await refresh();
} catch (e) {
setError(messageOf(e));
} finally {
@@ -478,7 +506,7 @@ export function DriveView() {
</li>
)}
- {items.map((item) => {
+ {items.filter((item) => !gone.has(item.drivewsid)).map((item) => {
const container = isContainer(item);
const busy = busyGuid === item.drivewsid;
return (