authoravatarclover caruso<git@paperclover.net>2026-10-19 12:08:16-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:39:58-07:00
log531f68e7ffcf2451cd10a5668d8d64f5547a363c
tree2ea693c90743390f9a0233133e8773c619afa9f9
parent02876d691c955ff0594f528e889ff7e7a057fdd9
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(ios): keep lost-upload protection across a notebook rename

The images an iCloud Drive notebook's publications keep were filed under each section file's path, so renaming the notebook left them behind and an upload iCloud lost afterwards stayed lost. They now live beside the section's replica, as its replica folder is chosen (the notebook's, or a copy's or a lone section's own), named by the file's identity, and location::moved carries them with the replicas, so neither a notebook rename nor a section rename strands them. A test renames the notebook between the phone's upload and iCloud losing it; it fails when the images stay behind. Assisted-by: claude-opus-5.5

4 files changed, 120 insertions(+), 13 deletions(-)

crates/mobile/src/library.rs+19-8
...@@ -131,15 +131,22 @@ struct Coordinated {...@@ -131,15 +131,22 @@ struct Coordinated {
131}131}
132132
133impl Coordinated {133impl Coordinated {
134 fn new(file: &Path, cache: &Path, elsewhere: bool) -> Self {134 /// The file `file` of the notebook folder, or lone section, at `notebook`. Its images are
135 /// kept beside its replica, so that they move with the notebook (`location::moved`).
136 fn new(file: &Path, notebook: &Path, cache: &Path, elsewhere: bool) -> Self {
137 use notebook::location::{PUBLISHED, local, section_folder};
135 let published = elsewhere138 let published = elsewhere
136 .then(|| {139 .then(|| {
137 notebook::location::claimed(cache, &notebook::location::local(file).ok()?).ok()140 Some(section_folder(
141 cache,
142 &local(notebook).ok()?,
143 &local(file).ok()?,
144 ))
138 })145 })
139 .flatten()146 .flatten()
140 .map(|folder| {147 .map(|folder| {
141 let device = VERSIONS.get().map(|(.., device)| device.clone());148 let device = VERSIONS.get().map(|(.., device)| device.clone());
142 Published::new(folder.join("published"), device)149 Published::new(folder.join(PUBLISHED), device)
143 });150 });
144 Self {151 Self {
145 file: file.to_owned(),152 file: file.to_owned(),
...@@ -473,11 +480,11 @@ impl Library {...@@ -473,11 +480,11 @@ impl Library {
473 (None, Place::File(path.to_owned()), None)480 (None, Place::File(path.to_owned()), None)
474 } else {481 } else {
475 let mut notebook = Notebook::open(path, cache)?;482 let mut notebook = Notebook::open(path, cache)?;
476 let kept = cache.to_owned();483 let (root, kept) = (path.to_owned(), cache.to_owned());
477 let background = notebook.background_with(484 let background = notebook.background_with(
478 local,485 local,
479 !local,486 !local,
480 move |file| Coordinated::new(file, &kept, !local),487 move |file| Coordinated::new(file, &root, &kept, !local),
481 sync_woken,488 sync_woken,
482 )?;489 )?;
483 background.watch(notebook.replicas());490 background.watch(notebook.replicas());
...@@ -875,16 +882,20 @@ impl Library {...@@ -875,16 +882,20 @@ impl Library {
875 ) -> Result<session::Section> {882 ) -> Result<session::Section> {
876 let offline = self.offline.load(Ordering::Relaxed);883 let offline = self.offline.load(Ordering::Relaxed);
877 let (cache, elsewhere) = (self.cache.clone(), self.elsewhere);884 let (cache, elsewhere) = (self.cache.clone(), self.elsewhere);
878 let connect = move |file: &Path| Ok(Coordinated::new(file, &cache, elsewhere));885 let connect = |notebook: &Path| {
886 let notebook = notebook.to_owned();
887 move |file: &Path| Ok(Coordinated::new(file, &notebook, &cache, elsewhere))
888 };
879 match &self.place {889 match &self.place {
880 Place::File(file) => Ok(session::Section::open_with(890 Place::File(file) => Ok(session::Section::open_with(
881 file,891 file,
882 &self.cache,892 &self.cache,
883 connect,893 connect(file),
884 offline,894 offline,
885 notify,895 notify,
886 )?),896 )?),
887 Place::Folder(_) => {897 Place::Folder(root) => {
898 let connect = connect(root);
888 let notebook = self.notebook();899 let notebook = self.notebook();
889 let notebook = notebook.as_ref().ok_or("A lone section has no notebook")?;900 let notebook = notebook.as_ref().ok_or("A lone section has no notebook")?;
890 Ok(match self.unlocked().get(path) {901 Ok(match self.unlocked().get(path) {
crates/mobile/src/tests.rs+70
...@@ -4829,6 +4829,76 @@ fn an_icloud_notebook_merges_the_phones_upload_back_at_launch() {...@@ -4829,6 +4829,76 @@ fn an_icloud_notebook_merges_the_phones_upload_back_at_launch() {
4829 }4829 }
4830}4830}
48314831
4832/// The phone's upload to an iCloud Drive notebook is lost after the phone renamed the
4833/// notebook: the images its publications keep move with it, and the next launch merges it back.
4834#[test]
4835fn an_upload_lost_after_the_notebook_is_renamed_merges_back() {
4836 unsafe {
4837 library::sb_set_versions(cloud_versions, cloud_retire, c"Clover’s iPhone".as_ptr())
4838 };
4839 let directory = tempfile::tempdir().unwrap();
4840 let folder = |name: &str| directory.path().join(name);
4841 let (phone, mac, cache) = (folder("Notes"), folder("mac"), folder("phone-cache"));
4842 let creation = onestore::PageCreation::new(None, Some("Shared page"), "Clover").unwrap();
4843 let notebook =
4844 notebook::session::Notebook::create(&phone, &cache, 0x00f0_a0c0, &creation).unwrap();
4845 let path = notebook.catalog().sections[0].path.clone();
4846 drop(notebook);
4847 let library = cloud_library(&phone, &cache);
4848 let section = cloud_section(&library, &path);
4849 let page = listed_ids(&section)[0];
4850 type_on(&section, page, "Typed on both ");
4851 close_section(section);
4852 drop(library);
4853 let copied = std::process::Command::new("cp")
4854 .arg("-R")
4855 .arg(&phone)
4856 .arg(&mac)
4857 .status();
4858 assert!(copied.unwrap().success());
4859 for (root, cache, typed) in [
4860 (&phone, &cache, "Phone words "),
4861 (&mac, &folder("mac-cache"), "Mac words "),
4862 ] {
4863 let library = cloud_library(root, cache);
4864 let section = cloud_section(&library, &path);
4865 type_on(&section, page, typed);
4866 close_section(section);
4867 library.flush(Duration::from_secs(20)).unwrap();
4868 }
4869
4870 // Renamed in the app, as Arranging.swift renames a notebook's folder.
4871 let renamed = folder("Renamed Notes");
4872 std::fs::rename(&phone, &renamed).unwrap();
4873 let text = |path: &Path| std::ffi::CString::new(path.as_os_str().as_encoded_bytes()).unwrap();
4874 let mut error = std::ptr::null_mut();
4875 let moved = unsafe {
4876 library::sb_notebook_moved(
4877 text(&cache).as_ptr(),
4878 text(&phone).as_ptr(),
4879 text(&renamed).as_ptr(),
4880 &mut error,
4881 )
4882 };
4883 assert!(moved && error.is_null());
4884 // iCloud Drive: the Mac's upload lands over the phone's, and no version is kept.
4885 std::fs::copy(mac.join(&path), renamed.join(&path)).unwrap();
4886
4887 let library = cloud_library(&renamed, &cache);
4888 let section = cloud_section(&library, &path);
4889 sb_library_sync_now(&library);
4890 assert!(
4891 eventually(|| {
4892 section.shared.section.wake();
4893 stored(&renamed).contains("Phone words")
4894 }),
4895 "the phone's upload never merged back:\n{}",
4896 stored(&renamed)
4897 );
4898 assert!(stored(&renamed).contains("Mac words"));
4899 close_section(section);
4900}
4901
4832/// What the iOS host calls on the main thread as a launch restores a page returns while the4902/// What the iOS host calls on the main thread as a launch restores a page returns while the
4833/// background waits on a coordinated read of the section, as iCloud Drive holds one until the4903/// background waits on a coordinated read of the section, as iCloud Drive holds one until the
4834/// file downloads: a main thread that waited would have the system end the app.4904/// file downloads: a main thread that waited would have the system end the app.
crates/notebook/src/location.rs+30-4
...@@ -41,9 +41,9 @@ pub fn smb(address: &str, share: &str, root: &str) -> String {...@@ -41,9 +41,9 @@ pub fn smb(address: &str, share: &str, root: &str) -> String {
41 format!("smb://{address}/{share}/{}", root.trim_matches('/'))41 format!("smb://{address}/{share}/{}", root.trim_matches('/'))
42}42}
4343
44/// Moves the replicas of the sections at `from` to `to`, as when the app renames or moves a44/// Moves the replicas of the sections at `from` to `to`, and the images their publications
45/// notebook; each replica `to` already holds for the same section stays. Every replica moved45/// keep (`published`), as when the app renames or moves a notebook; each replica `to` already
46/// must be closed.46/// holds for the same section stays. Every replica moved must be closed.
47pub fn moved(cache: &Path, from: &str, to: &str) -> io::Result<()> {47pub fn moved(cache: &Path, from: &str, to: &str) -> io::Result<()> {
48 let old = folder(cache, from);48 let old = folder(cache, from);
49 if fs::metadata(&old).is_err() {49 if fs::metadata(&old).is_err() {
...@@ -58,10 +58,36 @@ pub fn moved(cache: &Path, from: &str, to: &str) -> io::Result<()> {...@@ -58,10 +58,36 @@ pub fn moved(cache: &Path, from: &str, to: &str) -> io::Result<()> {
58 adopt(&old.join(&name), &new.join(&name))?;58 adopt(&old.join(&name), &new.join(&name))?;
59 }59 }
60 }60 }
61 if let Ok(images) = fs::read_dir(old.join(PUBLISHED)) {
62 fs::create_dir_all(new.join(PUBLISHED))?;
63 for entry in images {
64 let name = entry?.file_name();
65 let image = new.join(PUBLISHED).join(&name);
66 if fs::metadata(&image).is_err() {
67 fs::rename(old.join(PUBLISHED).join(&name), image)?;
68 }
69 }
70 let _ = fs::remove_dir(old.join(PUBLISHED));
71 }
61 tidy(&old);72 tidy(&old);
62 Ok(())73 Ok(())
63}74}
6475
76/// The folder of a replica folder holding the images its sections' publications keep.
77pub const PUBLISHED: &str = "published";
78
79/// The replica folder of the section file at location `file` in the notebook at location
80/// `notebook`: the file's own where one was claimed, as a copy's and a lone section's are,
81/// else the notebook's.
82pub fn section_folder(cache: &Path, notebook: &str, file: &str) -> PathBuf {
83 let own = folder(cache, file);
84 if fs::metadata(own.join(LOCATION)).is_ok() {
85 own
86 } else {
87 folder(cache, notebook)
88 }
89}
90
65/// Deletes what the cache keeps of the notebook at `location`, as when the app deletes the91/// Deletes what the cache keeps of the notebook at `location`, as when the app deletes the
66/// notebook: its replicas, edits waiting in them included, its sections' copies' and its92/// notebook: its replicas, edits waiting in them included, its sections' copies' and its
67/// listing. Every replica must be closed.93/// listing. Every replica must be closed.
...@@ -84,7 +110,7 @@ pub fn forget(cache: &Path, location: &str) -> io::Result<()> {...@@ -84,7 +110,7 @@ pub fn forget(cache: &Path, location: &str) -> io::Result<()> {
84}110}
85111
86/// The folder for `location`, made and labelled with it where it was not.112/// The folder for `location`, made and labelled with it where it was not.
87pub fn claimed(cache: &Path, location: &str) -> io::Result<PathBuf> {113fn claimed(cache: &Path, location: &str) -> io::Result<PathBuf> {
88 let folder = folder(cache, location);114 let folder = folder(cache, location);
89 let label = folder.join(LOCATION);115 let label = folder.join(LOCATION);
90 if fs::metadata(&label).is_err() {116 if fs::metadata(&label).is_err() {
crates/notebook/src/provider.rs+1-1
...@@ -81,7 +81,7 @@ impl Documents {...@@ -81,7 +81,7 @@ impl Documents {
81 /// Where this device keeps the images it published until the documents are seen holding81 /// Where this device keeps the images it published until the documents are seen holding
82 /// them: a provider without compare-and-swap may let another device's upload replace one.82 /// them: a provider without compare-and-swap may let another device's upload replace one.
83 fn published(&self) -> PathBuf {83 fn published(&self) -> PathBuf {
84 crate::location::folder(&self.cache, &self.location()).join("published")84 crate::location::folder(&self.cache, &self.location()).join(crate::location::PUBLISHED)
85 }85 }
8686
87 fn id(&self, path: &str) -> io::Result<String> {87 fn id(&self, path: &str) -> io::Result<String> {