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: compare names composed, so an accented name read back decomposed is the same name

Yashwanth Kamireddi23507d244c5d
diff --git a/crates/quince-core/src/fold.rs b/crates/quince-core/src/fold.rs
index 0033d39..b791bb4 100644
--- a/crates/quince-core/src/fold.rs
+++ b/crates/quince-core/src/fold.rs
@@ -30,6 +30,21 @@ pub fn fold(text: &str) -> String {
.collect()
}
+/// `text` in Unicode's composed form (NFC): "é" one character however it was typed, so a
+/// name a Mac wrote decomposed (NFD) and the same name typed here are the same name.
+#[must_use]
+pub fn composed(text: &str) -> String {
+ icu_normalizer::ComposingNormalizerBorrowed::new_nfc()
+ .normalize(text)
+ .into_owned()
+}
+
+/// A file name as iCloud Drive tells two apart: composed, and without regard to case.
+#[must_use]
+pub fn name_key(name: &str) -> String {
+ composed(name).to_lowercase()
+}
+
/// The words of a query, folded; empty when there is nothing to look for.
#[must_use]
pub fn words(query: &str) -> Vec<String> {
diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rs
index 8032478..edea7cb 100644
--- a/crates/quince-icloud/src/drive.rs
+++ b/crates/quince-icloud/src/drive.rs
@@ -593,11 +593,16 @@ pub async fn rename(
}
let after = folder_listing(http, root, &params, parent).await?;
+ // Compared composed: iCloud can hand a name back decomposed (NFD), "é" as "e" and an
+ // accent (rclone/rclone#9314), and read as bytes every rename to an accented name — any
+ // name typed in French, Vietnamese, Hindi with its marks — read as refused.
+ let wanted = quince_core::fold::composed(name);
+ let is_wanted = |n: &str| quince_core::fold::composed(n) == wanted;
match after.iter().find(|i| i.drivewsid == item.drivewsid) {
// Apple keeps a file's extension apart from its name: "final.pdf" comes back as
// "final" and "pdf". Compared whole, every rename of a file with an extension read as
// refused, and the old name was put back on screen.
- Some(found) if found.name == name || found.display_name() == name => Ok(()),
+ Some(found) if is_wanted(&found.name) || is_wanted(&found.display_name()) => Ok(()),
Some(found) => Err(Error::Protocol(format!(
"Apple answered success but the name is still {:?}, not {name:?}",
found.name
@@ -1289,8 +1294,9 @@ pub fn copy_name(stem: &str, extension: Option<&str>, taken: &[String]) -> Strin
.filter(|e| !e.is_empty())
.map(|e| format!(".{e}"))
.unwrap_or_default();
- let taken: std::collections::HashSet<String> = taken.iter().map(|t| t.to_lowercase()).collect();
- let free = |name: &String| !taken.contains(&name.to_lowercase());
+ let key = quince_core::fold::name_key;
+ let taken: std::collections::HashSet<String> = taken.iter().map(|t| key(t)).collect();
+ let free = |name: &String| !taken.contains(&key(name));
let whole = format!("{stem}{ext}");
if free(&whole) {
return whole;
@@ -1460,8 +1466,8 @@ async fn copy_file_to(
);
let want = copy_name(&file.name, ext, &dest_names);
let free_in = |names: &[String], name: &str| {
- let name = name.to_lowercase();
- !names.iter().any(|n| n.to_lowercase() == name)
+ let name = quince_core::fold::name_key(name);
+ !names.iter().any(|n| quince_core::fold::name_key(n) == name)
};
// The name it travels under: free in the folder it leaves and in the one it goes to.
let travel = if free_in(&source_names, &want) {
@@ -2034,6 +2040,55 @@ mod write_tests {
assert!(err.to_string().contains("still"), "{err}");
}
+ /// A name iCloud hands back decomposed (NFD) is the name asked for, composed (NFC): the
+ /// rename is done, not "Apple answered success but the name is still …" (rclone#9314).
+ #[tokio::test]
+ async fn a_rename_read_back_decomposed_is_done() {
+ let server = MockServer::start().await;
+ let mut renamed = file();
+ renamed.name = "Re\u{301}sume\u{301}".into();
+ renamed.extension = Some("pdf".into());
+ Mock::given(method("POST"))
+ .and(path("/retrieveItemDetailsInFolders"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(listing_of(
+ "FOLDER::com.apple.CloudDocs::root",
+ &renamed,
+ "e2",
+ )))
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/renameItems"))
+ .respond_with(
+ ResponseTemplate::new(200).set_body_json(serde_json::json!({"items": []})),
+ )
+ .mount(&server)
+ .await;
+ rename(
+ &reqwest::Client::new(),
+ &server.uri(),
+ vec![],
+ &file(),
+ "R\u{e9}sum\u{e9}.pdf",
+ )
+ .await
+ .unwrap();
+ }
+
+ /// A copy's name is free only if no name there is the same once composed and cased alike:
+ /// "Résumé.pdf" written decomposed by a Mac is taken for one typed here.
+ #[test]
+ fn a_decomposed_name_is_the_same_name() {
+ assert_eq!(
+ copy_name(
+ "R\u{e9}sum\u{e9}",
+ Some("pdf"),
+ &["RE\u{301}SUME\u{301}.PDF".into()]
+ ),
+ "R\u{e9}sum\u{e9} copy.pdf"
+ );
+ }
+
/// Apple keeps the extension apart from the name: a rename to "final.pdf" reads back as
/// "final" + "pdf", which is the rename done — not "the name is still final".
#[tokio::test]