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.

CloudKit: a zone walk whose position does not move stops, and a page without one keeps the place

Yashwanth Kamireddid915a78aa283
diff --git a/crates/quince-icloud/src/cloudkit.rs b/crates/quince-icloud/src/cloudkit.rs
index e822c17..5f9cbda 100644
--- a/crates/quince-icloud/src/cloudkit.rs
+++ b/crates/quince-icloud/src/cloudkit.rs
@@ -313,13 +313,16 @@ impl<'a> ZoneClient<'a> {
return Err(Error::StaleCursor);
}
records.extend(page.records);
- token = page.sync_token;
+ let (next, advanced) = advance(token.take(), page.sync_token);
+ token = next;
if !page.more_coming {
complete = true;
break;
}
- // A page that says "more coming" but hands back no cursor would loop forever.
- if token.is_none() {
+ // "More coming" with no new position — none, or the one just sent — would ask
+ // the same thing again, up to the page limit (coddingtonbear/icloud-md#48): it
+ // stops there, unfinished, and the next pass asks again from where it is.
+ if !advanced {
break;
}
}
@@ -332,6 +335,17 @@ impl<'a> ZoneClient<'a> {
}
}
+/// Where a walk carries on from after a page: the page's position, or — given none, or an empty
+/// one — the one it was asked from, so the last page leaving it out never loses the place
+/// (and the next pass never reads the whole zone again for it). Answers whether it moved.
+pub(crate) fn advance(asked: Option<String>, given: Option<String>) -> (Option<String>, bool) {
+ match given.filter(|t| !t.is_empty()) {
+ Some(t) if asked.as_ref() != Some(&t) => (Some(t), true),
+ Some(t) => (Some(t), false),
+ None => (asked, false),
+ }
+}
+
/// What one walk of a zone returned.
///
/// `complete` is the part that matters. A walk stops at a page cap, and one that stopped
@@ -702,6 +716,52 @@ mod tests {
);
}
+ /// A server that says more-coming but hands back the position it was asked from would be
+ /// asked the same thing two hundred times (coddingtonbear/icloud-md#48): the walk stops,
+ /// unfinished, at the position it reached.
+ #[tokio::test]
+ async fn paging_stops_when_the_cursor_does_not_move() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/changes/zone$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "zones": [{ "records": [record("a", "Reminder")], "syncToken": "STUCK", "moreComing": true }]
+ })))
+ .mount(&server)
+ .await;
+ let http = reqwest::Client::new();
+ let client = ZoneClient::new(&http, &server.uri(), "com.apple.reminders", vec![]);
+ let walk = client.all_records("Reminders", None).await.unwrap();
+ assert!(!walk.complete, "not the whole zone");
+ assert_eq!(walk.token.as_deref(), Some("STUCK"));
+ assert_eq!(
+ server.received_requests().await.unwrap().len(),
+ 2,
+ "asked once more, then stopped"
+ );
+ }
+
+ /// A last page that leaves its position out, or leaves it empty, keeps the one the walk
+ /// was at: dropped, the next pass read the whole zone again.
+ #[test]
+ fn a_page_without_a_position_keeps_the_one_before() {
+ assert_eq!(advance(Some("A".into()), None), (Some("A".into()), false));
+ assert_eq!(
+ advance(Some("A".into()), Some(String::new())),
+ (Some("A".into()), false)
+ );
+ assert_eq!(
+ advance(Some("A".into()), Some("A".into())),
+ (Some("A".into()), false)
+ );
+ assert_eq!(
+ advance(Some("A".into()), Some("B".into())),
+ (Some("B".into()), true)
+ );
+ assert_eq!(advance(None, Some("B".into())), (Some("B".into()), true));
+ assert_eq!(advance(None, None), (None, false));
+ }
+
/// A server that always says more-coming without a cursor must not spin.
#[tokio::test]
async fn paging_stops_when_there_is_no_cursor() {
diff --git a/crates/quince-icloud/src/photos.rs b/crates/quince-icloud/src/photos.rs
index c0bd826..9712290 100644
--- a/crates/quince-icloud/src/photos.rs
+++ b/crates/quince-icloud/src/photos.rs
@@ -113,12 +113,13 @@ async fn walk(
return Err(crate::Error::StaleCursor);
}
records.extend(page.records);
- token = page.sync_token;
+ let (next, advanced) = crate::cloudkit::advance(token.take(), page.sync_token);
+ token = next;
if !page.more_coming {
return Ok((records, token, true));
}
- // "More coming" with no position to carry on from would loop forever.
- if token.is_none() {
+ // "More coming" with no new position to carry on from would ask the same again.
+ if !advanced {
break;
}
}
@@ -2086,22 +2087,30 @@ mod tests {
use wiremock::matchers::{method, path_regex};
use wiremock::{Mock, MockServer};
let server = MockServer::start().await;
+ // Each page a position further on, as Apple's are.
+ let pages = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let counter = pages.clone();
Mock::given(method("POST"))
.and(path_regex(r".*/changes/zone"))
- .respond_with(zone_reply(json!({
- "records": [master_named("M1")], "syncToken": "further", "moreComing": true
- })))
+ .respond_with(move |_: &wiremock::Request| {
+ let n = counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1;
+ zone_reply(json!({
+ "records": [master_named("M1")], "syncToken": format!("further-{n}"),
+ "moreComing": true
+ }))
+ })
.mount(&server)
.await;
let http = reqwest::Client::new();
let fetched = fetch(&http, &server.uri(), vec![], None).await.unwrap();
assert!(!fetched.complete);
- assert_eq!(fetched.token.as_deref(), Some("further"));
+ let reached = format!("further-{PASS_PAGES}");
+ assert_eq!(fetched.token.as_deref(), Some(reached.as_str()));
assert_eq!(fetched.records.len(), PASS_PAGES);
- let again = fetch(&http, &server.uri(), vec![], Some("further"))
+ let again = fetch(&http, &server.uri(), vec![], Some(&reached))
.await
.unwrap();
- assert!(again.incremental && again.from.as_deref() == Some("further"));
+ assert!(again.incremental && again.from.as_deref() == Some(reached.as_str()));
}
/// A whole walk read the way a refresh reads it — a few pages at a time, each stored