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
Drive: downloads and uploads that used to fail or go wrong
Yashwanth Kamireddi7343dbf52b38
diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rsindex 3041e9f..9dd858b 100644--- a/crates/quince-icloud/src/drive.rs+++ b/crates/quince-icloud/src/drive.rs@@ -259,16 +259,39 @@ pub async fn download_to(// Into a file of its own beside the destination, moved into place only once it is all// there and only if nothing has taken the name meanwhile. Written straight to the name,// a download that broke off left a truncated file under the real one.- let partial = destination.with_extension(match destination.extension() {+ let base = destination.with_extension(match destination.extension() {Some(ext) => format!("{}.part", ext.to_string_lossy()),None => "part".into(),});- let written: Result<()> = async {- let mut file = tokio::fs::OpenOptions::new()+ // A partial file left by a download cut off (a crash, a power cut) keeps its name: this one+ // takes the next free one beside it, rather than failing on "File exists" for good.+ let mut partial = base.clone();+ let mut opened = None;+ for n in 1..=100 {+ match tokio::fs::OpenOptions::new().write(true).create_new(true).open(&partial)- .await?;+ .await+ {+ Ok(file) => {+ opened = Some(file);+ break;+ }+ Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {+ let mut name = base.as_os_str().to_owned();+ name.push(format!(".{n}"));+ partial = std::path::PathBuf::from(name);+ }+ Err(e) => return Err(e.into()),+ }+ }+ let Some(mut file) = opened else {+ return Err(Error::Protocol(+ "too many unfinished downloads of this file are in the way".into(),+ ));+ };+ let written: Result<()> = async {let mut stream = response.bytes_stream();while let Some(chunk) = stream.next().await {file.write_all(&chunk?).await?;@@ -357,13 +380,21 @@ pub async fn create_folders() -> Result<Vec<(String, std::result::Result<String, String>)>> {let mut out = Vec::with_capacity(names.len());for chunk in names.chunks(100) {- let asked: Vec<(String, String)> = chunk+ // (as asked, as sent, client id): answered under the name asked, which is how the caller+ // looks each one up — "Notes " is made as "Notes".+ let asked: Vec<(String, String, String)> = chunk.iter()- .map(|n| (n.trim().to_string(), uuid::Uuid::new_v4().to_string()))+ .map(|n| {+ (+ n.clone(),+ n.trim().to_string(),+ uuid::Uuid::new_v4().to_string(),+ )+ }).collect();let body = json!({"destinationDrivewsId": parent_id,- "folders": asked.iter().map(|(name, id)| json!({ "name": name, "clientId": id })).collect::<Vec<_>>(),+ "folders": asked.iter().map(|(_, name, id)| json!({ "name": name, "clientId": id })).collect::<Vec<_>>(),});let url = format!("{}/createFolders", root.trim_end_matches('/'));let reply = post_json(http, &url, ¶ms, &body).await?;@@ -372,7 +403,7 @@ pub async fn create_folders(.and_then(Value::as_array).cloned().unwrap_or_default();- for (name, client_id) in asked {+ for (key, name, client_id) in asked {let folder = made.iter().find(|f| f.get("clientId").and_then(Value::as_str) == Some(client_id.as_str()))@@ -398,7 +429,7 @@ pub async fn create_folders("iCloud Drive said nothing about \u{201c}{name}\u{201d}.")),};- out.push((name, result));+ out.push((key, result));}}Ok(out)@@ -1541,6 +1572,44 @@ mod write_tests {assert_eq!(body["folders"][0]["name"], "Scans");}+ /// A folder made with its siblings in one request answers under the name it was asked by:+ /// "Notes " (a space at the end, as folders copied from a Mac often have) was made as+ /// "Notes" and answered as "Notes", so the upload never found it, and every file in it+ /// failed against the folder it had just made.+ #[tokio::test]+ async fn folders_made_together_answer_under_the_names_asked() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path("/createFolders"))+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({+ "folders": [{+ "drivewsid": "FOLDER::com.apple.CloudDocs::N1",+ "parentId": "FOLDER::com.apple.CloudDocs::root",+ "name": "Notes",+ "type": "FOLDER",+ "status": "OK"+ }]+ })))+ .mount(&server)+ .await;+ let made = create_folders(+ &reqwest::Client::new(),+ &server.uri(),+ vec![],+ "FOLDER::com.apple.CloudDocs::root",+ &["Notes ".to_string()],+ )+ .await+ .unwrap();+ assert_eq!(made.len(), 1);+ assert_eq!(made[0].0, "Notes ");+ assert_eq!(made[0].1.as_deref(), Ok("FOLDER::com.apple.CloudDocs::N1"));+ assert_eq!(+ sent(&server, "/createFolders").await["folders"][0]["name"],+ "Notes"+ );+ }+/// A response shaped like a real `retrieveItemDetailsInFolders` reply, holding `item`/// (possibly under a different, current etag) as the sole child of its folder.fn listing_of(folder_id: &str, item: &DriveItem, etag: &str) -> serde_json::Value {@@ -2033,6 +2102,45 @@ mod write_tests {std::fs::remove_dir_all(&dir).ok();}+ /// A download cut off by a crash or a power cut leaves its partial file behind; the next+ /// download of that file must not trip over it — it failed with "File exists" for good.+ #[tokio::test]+ async fn a_partial_file_left_by_a_crash_does_not_block_the_next_download() {+ let server = MockServer::start().await;+ Mock::given(method("GET"))+ .and(path("/ws/com.apple.CloudDocs/download/by_id"))+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({+ "data_token": { "url": format!("{}/bytes", server.uri()) }+ })))+ .mount(&server)+ .await;+ Mock::given(method("GET"))+ .and(path("/bytes"))+ .respond_with(ResponseTemplate::new(200).set_body_bytes(b"whole film".to_vec()))+ .mount(&server)+ .await;++ let dir = tempdir();+ std::fs::write(dir.join("Film.mov.part"), b"half a fi").unwrap();+ let destination = dir.join("Film.mov");+ download_to(+ &reqwest::Client::new(),+ &server.uri(),+ vec![],+ &file(),+ &destination,+ )+ .await+ .unwrap();+ assert_eq!(std::fs::read(&destination).unwrap(), b"whole film");+ let left: Vec<_> = std::fs::read_dir(&dir)+ .unwrap()+ .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())+ .collect();+ assert_eq!(left.len(), 2, "only the film and the old partial: {left:?}");+ std::fs::remove_dir_all(&dir).ok();+ }+#[tokio::test]async fn a_download_never_makes_folders_on_its_way() {let server = MockServer::start().await;diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rsindex a92cc6d..43a1b8d 100644--- a/src-tauri/src/main.rs+++ b/src-tauri/src/main.rs@@ -810,12 +810,23 @@ pub(crate) fn attachment_name(given: Option<&str>, id: &str) -> String {// Long enough for any real name, short enough for every file system — and, cut short, it// still ends in neither a dot nor a space, which Windows drops (turning `a.pdf.` into// another file than the one checked).- let mut name: String = trimmed- .chars()- .take(120)- .collect::<String>()- .trim_end_matches(['.', ' '])- .to_string();+ //+ // Counted in bytes, as Linux counts a name (255 at most, with room left for " 2" and+ // ".part"), and cut from the stem so the extension stays: 120 characters dropped ".pdf",+ // and 120 CJK characters or emoji were over the limit and could not be saved at all.+ const LONGEST: usize = 120;+ let cut: String = if trimmed.len() <= LONGEST {+ trimmed.to_string()+ } else {+ let (stem, ext) = name_parts(trimmed);+ let room = LONGEST.saturating_sub(ext.len()).max(1);+ let mut at = room.min(stem.len());+ while !stem.is_char_boundary(at) {+ at -= 1;+ }+ format!("{}{ext}", stem[..at].trim_end_matches(['.', ' ']))+ };+ let mut name = cut.trim_end_matches(['.', ' ']).to_string();// Windows reads a device in the stem whatever follows it (`CON.txt`, `CON .txt`), and// counts superscript digits as digits (`COM¹`).let stem = name@@ -3839,22 +3850,44 @@ async fn contact_delete(guid: String, data: State<'_, Data>) -> UiResult<()> {/// A path in `dir` for `name` that holds nothing yet: a number goes on the end rather than/// writing over a file already there — the rule a browser follows.fn free_path(dir: &std::path::Path, name: &str) -> std::path::PathBuf {- let (stem, ext) = name.rsplit_once('.').map_or((name, ""), |(s, e)| (s, e));+ let (stem, ext) = name_parts(name);let mut path = dir.join(name);let mut suffix = 1;// Anything at all under the name counts as taken — a link too, even one that leads// nowhere: written through, it would put the file wherever the link points.while path.symlink_metadata().is_ok() {- path = dir.join(if ext.is_empty() {- format!("{stem} {suffix}")- } else {- format!("{stem} {suffix}.{ext}")- });+ path = dir.join(format!("{stem} {suffix}{ext}"));suffix += 1;}path}+/// A file name as its stem and its extension (with the dot, or empty): the whole of a+/// compressed tar's (".tar.gz"), and only something that reads as one — short, with no space —+/// so "v1.2 notes" has none.+pub(crate) fn name_parts(name: &str) -> (&str, &str) {+ let lower = name.to_ascii_lowercase();+ for double in [".tar.gz", ".tar.bz2", ".tar.xz", ".tar.zst"] {+ if lower.len() > double.len() && lower.ends_with(double) {+ let at = name.len() - double.len();+ if name.is_char_boundary(at) {+ return (&name[..at], &name[at..]);+ }+ }+ }+ match name.rfind('.') {+ Some(at)+ if at > 0+ && name.len() - at - 1 <= 16+ && !name[at..].contains(' ')+ && at + 1 < name.len() =>+ {+ (&name[..at], &name[at..])+ }+ _ => (name, ""),+ }+}+/// These people as a vCard file in Downloads, written by Apple. Returns where it went.#[tauri::command]async fn contacts_export(@@ -4173,6 +4206,31 @@ fn cancelled(batch: &str) -> bool {cancelled_batches().lock().is_ok_and(|b| b.contains(batch))}+/// Files stopped one by one with their ✕, by id: one stopped while its folder upload was still+/// being walked had no stop registered yet, so it was sent anyway once the walk was done.+fn cancelled_uploads() -> &'static std::sync::Mutex<std::collections::HashSet<String>> {+ static CANCELLED: std::sync::OnceLock<std::sync::Mutex<std::collections::HashSet<String>>> =+ std::sync::OnceLock::new();+ CANCELLED.get_or_init(Default::default)+}++/// Whether this file was stopped before it began — on its own, or with its whole batch. Asked+/// as each file is about to send: Cancel pressed while a folder upload is still being planned+/// stops the planning, and must stop the files already found too.+fn upload_stopped(batch: &str, id: &str) -> bool {+ cancelled(batch) || cancelled_uploads().lock().is_ok_and(|c| c.contains(id))+}++/// A batch is over: what was remembered about stopping it is let go.+fn forget_cancelled(batch: &str) {+ cancelled_batches().lock().map(|mut b| b.remove(batch)).ok();+ let mine = format!("{batch}:");+ cancelled_uploads()+ .lock()+ .map(|mut c| c.retain(|id| !id.starts_with(&mine)))+ .ok();+}+/// Stop uploads: these files, or a whole batch. A file already being filed in iCloud is past/// stopping and finishes; anything waiting or still sending stops, with nothing of it kept.#[tauri::command]@@ -4183,6 +4241,10 @@ fn drive_upload_cancel(ids: Vec<String>, batch: Option<String>) {.map(|mut b| b.insert(batch.clone())).ok();}+ cancelled_uploads()+ .lock()+ .map(|mut c| c.extend(ids.iter().cloned()))+ .ok();if let Ok(running) = uploads_running().lock() {for (id, handle) in running.iter() {let in_batch = batch@@ -4195,6 +4257,36 @@ fn drive_upload_cancel(ids: Vec<String>, batch: Option<String>) {}}+/// Something in an upload that will not go up, said in the tray with its own row as well as in+/// the answer: found while the upload was being planned — a file that could not be read, a link,+/// a folder Apple would not make — it had no row, and the tray said "Uploaded" over it.+fn upload_failed(+ app: &tauri::AppHandle,+ batch: &str,+ name: &str,+ within: &str,+ folder: bool,+ error: String,+ failed: &mut Vec<String>,+) {+ app.emit(+ UPLOAD,+ &UploadEvent {+ id: format!("{batch}:x:{}", failed.len()),+ batch: batch.to_string(),+ name: name.to_string(),+ within: within.to_string(),+ sent: 0,+ total: 0,+ state: "failed",+ folder,+ error: Some(error.clone()),+ },+ )+ .ok();+ failed.push(format!("{name}: {error}"));+}+/// One file to send, and the Drive folder it goes into.struct UploadJob {event: UploadEvent,@@ -4271,10 +4363,17 @@ async fn plan_upload(|| path.display().to_string(),|n| n.to_string_lossy().into(),);- let meta = match tokio::fs::symlink_metadata(&path).await {+ // What was chosen is followed if it is a link (a Music folder linked from another+ // disk); only what is found inside is not.+ let chosen = within.is_empty() && resolved.is_none();+ let meta = match if chosen {+ tokio::fs::metadata(&path).await+ } else {+ tokio::fs::symlink_metadata(&path).await+ } {Ok(meta) => meta,Err(e) => {- failed.push(format!("{name}: {e}"));+ upload_failed(app, batch, &name, &within, false, e.to_string(), failed);continue;}};@@ -4326,6 +4425,9 @@ async fn plan_upload(let mut entries = match tokio::fs::read_dir(&path).await {Ok(entries) => entries,Err(e) => {+ shown.state = "failed";+ shown.error = Some(e.to_string());+ app.emit(UPLOAD, &shown).ok();failed.push(format!("{name}: {e}"));continue;}@@ -4387,7 +4489,7 @@ async fn plan_upload(Some(Ok(id)) => {pending.push((child, folder.clone(), below.clone(), Some((id, true))))}- Some(Err(e)) => failed.push(format!("{child_name}: {e}")),+ Some(Err(e)) => upload_failed(app, batch, &child_name, &below, true, e, failed),None => pending.push((child, folder.clone(), below.clone(), None)),}}@@ -4410,6 +4512,16 @@ async fn plan_upload(path,folder: into,});+ } else {+ upload_failed(+ app,+ batch,+ &name,+ &within,+ false,+ "a link or a special file, which isn\u{2019}t uploaded".into(),+ failed,+ );}}jobs@@ -4507,8 +4619,13 @@ async fn drive_upload(e};let app_for_cancel = app.clone();+ let stopped_event = cancelled_event.clone();let run = async move {let mut event = job.event.clone();+ if upload_stopped(&event.batch, &event.id) {+ app.emit(UPLOAD, &stopped_event).ok();+ return None;+ }event.state = "sending";app.emit(UPLOAD, &event).ok();@@ -4580,10 +4697,7 @@ async fn drive_upload(.collect().await;failed.extend(results.into_iter().flatten());- cancelled_batches()- .lock()- .map(|mut b| b.remove(&batch))- .ok();+ forget_cancelled(&batch);settle(resync_drive_folder(&http, &root, params, &data, &folder_id).await)?;Ok(failed)@@ -6658,6 +6772,73 @@ mod tests {assert_eq!(mode(&outside), 0o644);}+ /// A long name is cut to fit every disk, counted in bytes as Linux counts them, and keeps+ /// its extension: 120 characters of it dropped ".pdf", and 120 CJK characters or emoji,+ /// with " 2" and ".part" added on the way, were over ext4's 255 bytes and failed to save.+ #[test]+ fn a_long_name_keeps_its_extension_and_fits_the_disk() {+ use super::attachment_name;+ for given in [+ format!("{}.pdf", "a".repeat(200)),+ format!("{}.pdf", "\u{6f22}".repeat(100)),+ format!("{}.pdf", "\u{1f600}".repeat(64)),+ ] {+ let name = attachment_name(Some(&given), "A");+ assert!(name.ends_with(".pdf"), "{name}");+ assert!(name.len() <= 120, "{} bytes: {name}", name.len());+ assert!(format!("{name} 99.part").len() <= 255);+ }+ // Short names, and the parts of one that look like an extension and are not, as before.+ assert_eq!(+ attachment_name(Some("Report 2026.pdf"), "A"),+ "Report 2026.pdf"+ );+ let plain = attachment_name(Some(&format!("{}.v2 final draft", "b".repeat(150))), "A");+ assert!(plain.len() <= 120 && plain.starts_with('b'), "{plain}");+ }++ /// The number a second copy takes goes before the whole extension, as a browser puts it:+ /// "archive.tar.gz" was saved as "archive.tar 1.gz", and "v1.2 notes" as "v1 1.2 notes".+ #[test]+ fn a_second_copy_is_numbered_before_its_extension() {+ let dir = tempfile::tempdir().unwrap();+ for name in ["archive.tar.gz", "v1.2 notes", "Report.pdf", "Makefile"] {+ std::fs::write(dir.path().join(name), b"x").unwrap();+ }+ let next = |n: &str| {+ super::free_path(dir.path(), n)+ .file_name()+ .unwrap()+ .to_string_lossy()+ .into_owned()+ };+ assert_eq!(next("archive.tar.gz"), "archive 1.tar.gz");+ assert_eq!(next("v1.2 notes"), "v1.2 notes 1");+ assert_eq!(next("Report.pdf"), "Report 1.pdf");+ assert_eq!(next("Makefile"), "Makefile 1");+ assert_eq!(next("new.txt"), "new.txt");+ }++ /// Cancel pressed while a folder upload is still being walked — before any file has a stop+ /// of its own — reaches every file found by then, as does one file's ✕; the next batch+ /// starts clean.+ #[test]+ fn an_upload_cancelled_before_it_began_is_never_sent() {+ use super::{drive_upload_cancel, forget_cancelled, upload_stopped};+ drive_upload_cancel(Vec::new(), Some("planning".into()));+ assert!(upload_stopped("planning", "planning:0"));+ assert!(upload_stopped("planning", "planning:41"));+ assert!(!upload_stopped("other", "other:0"));+ forget_cancelled("planning");+ assert!(!upload_stopped("planning", "planning:0"));++ drive_upload_cancel(vec!["one:2".into()], None);+ assert!(upload_stopped("one", "one:2"));+ assert!(!upload_stopped("one", "one:3"));+ forget_cancelled("one");+ assert!(!upload_stopped("one", "one:2"));+ }+/// A plain http feed is tried over https first; a secure one is left as it is.#[test]fn a_plain_feed_is_tried_secure_first() {