diff --git a/crates/mobile/src/library.rs b/crates/mobile/src/library.rs index ba9ca405b364cd4899df23125c514fda962dc116..673cce47075af3e8810ff8802767c9155c7d86a6 100644 --- a/crates/mobile/src/library.rs +++ b/crates/mobile/src/library.rs @@ -131,15 +131,22 @@ struct Coordinated { } impl Coordinated { - fn new(file: &Path, cache: &Path, elsewhere: bool) -> Self { + /// The file `file` of the notebook folder, or lone section, at `notebook`. Its images are + /// kept beside its replica, so that they move with the notebook (`location::moved`). + fn new(file: &Path, notebook: &Path, cache: &Path, elsewhere: bool) -> Self { + use notebook::location::{PUBLISHED, local, section_folder}; let published = elsewhere .then(|| { - notebook::location::claimed(cache, ¬ebook::location::local(file).ok()?).ok() + Some(section_folder( + cache, + &local(notebook).ok()?, + &local(file).ok()?, + )) }) .flatten() .map(|folder| { let device = VERSIONS.get().map(|(.., device)| device.clone()); - Published::new(folder.join("published"), device) + Published::new(folder.join(PUBLISHED), device) }); Self { file: file.to_owned(), @@ -473,11 +480,11 @@ impl Library { (None, Place::File(path.to_owned()), None) } else { let mut notebook = Notebook::open(path, cache)?; - let kept = cache.to_owned(); + let (root, kept) = (path.to_owned(), cache.to_owned()); let background = notebook.background_with( local, !local, - move |file| Coordinated::new(file, &kept, !local), + move |file| Coordinated::new(file, &root, &kept, !local), sync_woken, )?; background.watch(notebook.replicas()); @@ -875,16 +882,20 @@ impl Library { ) -> Result { let offline = self.offline.load(Ordering::Relaxed); let (cache, elsewhere) = (self.cache.clone(), self.elsewhere); - let connect = move |file: &Path| Ok(Coordinated::new(file, &cache, elsewhere)); + let connect = |notebook: &Path| { + let notebook = notebook.to_owned(); + move |file: &Path| Ok(Coordinated::new(file, ¬ebook, &cache, elsewhere)) + }; match &self.place { Place::File(file) => Ok(session::Section::open_with( file, &self.cache, - connect, + connect(file), offline, notify, )?), - Place::Folder(_) => { + Place::Folder(root) => { + let connect = connect(root); let notebook = self.notebook(); let notebook = notebook.as_ref().ok_or("A lone section has no notebook")?; Ok(match self.unlocked().get(path) { diff --git a/crates/mobile/src/tests.rs b/crates/mobile/src/tests.rs index 39f2189c6a11a33b65c90a8c7674451880c1e99e..6a77780111499cf34ce911d8304f50bfb012bdb0 100644 --- a/crates/mobile/src/tests.rs +++ b/crates/mobile/src/tests.rs @@ -4829,6 +4829,76 @@ fn an_icloud_notebook_merges_the_phones_upload_back_at_launch() { } } +/// The phone's upload to an iCloud Drive notebook is lost after the phone renamed the +/// notebook: the images its publications keep move with it, and the next launch merges it back. +#[test] +fn an_upload_lost_after_the_notebook_is_renamed_merges_back() { + unsafe { + library::sb_set_versions(cloud_versions, cloud_retire, c"Clover’s iPhone".as_ptr()) + }; + let directory = tempfile::tempdir().unwrap(); + let folder = |name: &str| directory.path().join(name); + let (phone, mac, cache) = (folder("Notes"), folder("mac"), folder("phone-cache")); + let creation = onestore::PageCreation::new(None, Some("Shared page"), "Clover").unwrap(); + let notebook = + notebook::session::Notebook::create(&phone, &cache, 0x00f0_a0c0, &creation).unwrap(); + let path = notebook.catalog().sections[0].path.clone(); + drop(notebook); + let library = cloud_library(&phone, &cache); + let section = cloud_section(&library, &path); + let page = listed_ids(§ion)[0]; + type_on(§ion, page, "Typed on both "); + close_section(section); + drop(library); + let copied = std::process::Command::new("cp") + .arg("-R") + .arg(&phone) + .arg(&mac) + .status(); + assert!(copied.unwrap().success()); + for (root, cache, typed) in [ + (&phone, &cache, "Phone words "), + (&mac, &folder("mac-cache"), "Mac words "), + ] { + let library = cloud_library(root, cache); + let section = cloud_section(&library, &path); + type_on(§ion, page, typed); + close_section(section); + library.flush(Duration::from_secs(20)).unwrap(); + } + + // Renamed in the app, as Arranging.swift renames a notebook's folder. + let renamed = folder("Renamed Notes"); + std::fs::rename(&phone, &renamed).unwrap(); + let text = |path: &Path| std::ffi::CString::new(path.as_os_str().as_encoded_bytes()).unwrap(); + let mut error = std::ptr::null_mut(); + let moved = unsafe { + library::sb_notebook_moved( + text(&cache).as_ptr(), + text(&phone).as_ptr(), + text(&renamed).as_ptr(), + &mut error, + ) + }; + assert!(moved && error.is_null()); + // iCloud Drive: the Mac's upload lands over the phone's, and no version is kept. + std::fs::copy(mac.join(&path), renamed.join(&path)).unwrap(); + + let library = cloud_library(&renamed, &cache); + let section = cloud_section(&library, &path); + sb_library_sync_now(&library); + assert!( + eventually(|| { + section.shared.section.wake(); + stored(&renamed).contains("Phone words") + }), + "the phone's upload never merged back:\n{}", + stored(&renamed) + ); + assert!(stored(&renamed).contains("Mac words")); + close_section(section); +} + /// What the iOS host calls on the main thread as a launch restores a page returns while the /// background waits on a coordinated read of the section, as iCloud Drive holds one until the /// file downloads: a main thread that waited would have the system end the app. diff --git a/crates/notebook/src/location.rs b/crates/notebook/src/location.rs index f56575f7afce81b9e4461fa11b414344607a3bf1..3ce4ed60de99630f8fae5371e4ad5fd6da844f9b 100644 --- a/crates/notebook/src/location.rs +++ b/crates/notebook/src/location.rs @@ -41,9 +41,9 @@ pub fn smb(address: &str, share: &str, root: &str) -> String { format!("smb://{address}/{share}/{}", root.trim_matches('/')) } -/// Moves the replicas of the sections at `from` to `to`, as when the app renames or moves a -/// notebook; each replica `to` already holds for the same section stays. Every replica moved -/// must be closed. +/// Moves the replicas of the sections at `from` to `to`, and the images their publications +/// keep (`published`), as when the app renames or moves a notebook; each replica `to` already +/// holds for the same section stays. Every replica moved must be closed. pub fn moved(cache: &Path, from: &str, to: &str) -> io::Result<()> { let old = folder(cache, from); if fs::metadata(&old).is_err() { @@ -58,10 +58,36 @@ pub fn moved(cache: &Path, from: &str, to: &str) -> io::Result<()> { adopt(&old.join(&name), &new.join(&name))?; } } + if let Ok(images) = fs::read_dir(old.join(PUBLISHED)) { + fs::create_dir_all(new.join(PUBLISHED))?; + for entry in images { + let name = entry?.file_name(); + let image = new.join(PUBLISHED).join(&name); + if fs::metadata(&image).is_err() { + fs::rename(old.join(PUBLISHED).join(&name), image)?; + } + } + let _ = fs::remove_dir(old.join(PUBLISHED)); + } tidy(&old); Ok(()) } +/// The folder of a replica folder holding the images its sections' publications keep. +pub const PUBLISHED: &str = "published"; + +/// The replica folder of the section file at location `file` in the notebook at location +/// `notebook`: the file's own where one was claimed, as a copy's and a lone section's are, +/// else the notebook's. +pub fn section_folder(cache: &Path, notebook: &str, file: &str) -> PathBuf { + let own = folder(cache, file); + if fs::metadata(own.join(LOCATION)).is_ok() { + own + } else { + folder(cache, notebook) + } +} + /// Deletes what the cache keeps of the notebook at `location`, as when the app deletes the /// notebook: its replicas, edits waiting in them included, its sections' copies' and its /// listing. Every replica must be closed. @@ -84,7 +110,7 @@ pub fn forget(cache: &Path, location: &str) -> io::Result<()> { } /// The folder for `location`, made and labelled with it where it was not. -pub fn claimed(cache: &Path, location: &str) -> io::Result { +fn claimed(cache: &Path, location: &str) -> io::Result { let folder = folder(cache, location); let label = folder.join(LOCATION); if fs::metadata(&label).is_err() { diff --git a/crates/notebook/src/provider.rs b/crates/notebook/src/provider.rs index 3f5525661a7c35c7b12dfd9a744768bb1da968c3..2a9691e17b92bec8f85cc98b2515bcd0166c2669 100644 --- a/crates/notebook/src/provider.rs +++ b/crates/notebook/src/provider.rs @@ -81,7 +81,7 @@ impl Documents { /// Where this device keeps the images it published until the documents are seen holding /// them: a provider without compare-and-swap may let another device's upload replace one. fn published(&self) -> PathBuf { - crate::location::folder(&self.cache, &self.location()).join("published") + crate::location::folder(&self.cache, &self.location()).join(crate::location::PUBLISHED) } fn id(&self, path: &str) -> io::Result {