From 0be69d99f016301e65f1c6b8431e432c5f19f9cc Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 12:58:34 -0700 Subject: [PATCH] fix(mobile): carry a section copy's offline copy across a notebook rename A copy of a section beside it keeps its replica under a location of its own, within the notebook's. The desktop's rename moved those one by one from its catalog, but the app's (sb_notebook_moved) moved only the notebook's own, so a renamed notebook's copy reopened from its file and the edits waiting in it were stranded under the old name. location::moved now moves every location within the one it moves, which serves both and lets the desktop's rename drop its own loop. A test edits a copy offline, renames the notebook through sb_notebook_moved and finds the edit still waiting; it fails before. - Renaming a notebook on iPhone keeps changes still waiting in a copy of a section. Assisted-by: claude-opus-5.5 --- crates/mobile/src/tests.rs | 52 +++++++++++++++++++++++++++++++++ crates/notebook/src/location.rs | 24 +++++++++++++-- crates/notebook/src/session.rs | 15 +--------- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/crates/mobile/src/tests.rs b/crates/mobile/src/tests.rs index c920623964a8f3215149b7169287d723fb7c7c6d..e912bb725ea7727b7a487c3efcb6917487b9ce23 100644 --- a/crates/mobile/src/tests.rs +++ b/crates/mobile/src/tests.rs @@ -4951,6 +4951,58 @@ fn an_upload_lost_after_the_notebook_is_renamed_merges_back() { close_section(section); } +/// A copy of a section beside it keeps a replica of its own; renaming the notebook in the app +/// carries the copy's along with the rest, its edits still waiting in it. +#[test] +fn renaming_a_notebook_keeps_a_copys_waiting_edits() { + let directory = tempfile::tempdir().unwrap(); + let (root, cache) = ( + directory.path().join("Notes"), + directory.path().join("cache"), + ); + std::fs::create_dir(&root).unwrap(); + let bytes = onestore::create_section("Notes.one", "Original text", "Fixture").unwrap(); + for name in ["Notes.one", "Notes - Copy.one"] { + std::fs::write(root.join(name), &bytes).unwrap(); + } + let open_copy = |root: &Path| { + let library = Arc::new(Library::open(root, &cache, true).unwrap()); + sb_library_set_offline(&library, true); + open(&library, "Notes - Copy.one") + }; + let copy = open_copy(&root); + let space = copy.shared.section.pages().unwrap()[0].0; + let op = Op::Page { + space, + op: PageOp::Color(Some(0x00ef_cdab)), + }; + let at = library::filetime(); + copy.shared.apply(Edit { at, ops: vec![op] }).unwrap(); + copy.shared.section.written().unwrap(); + let pending = copy.shared.section.pending().unwrap(); + assert_eq!(pending.len(), 1); + let shared = Arc::clone(©.shared); + drop(copy); + Arc::into_inner(shared).unwrap().section.close().unwrap(); + + let renamed = directory.path().join("Renamed Notes"); + std::fs::rename(&root, &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(&root).as_ptr(), + text(&renamed).as_ptr(), + &mut error, + ) + }; + assert!(moved && error.is_null()); + let copy = open_copy(&renamed); + assert_eq!(copy.shared.section.pending().unwrap(), pending); + assert_eq!(copy.shared.page(space).unwrap().0.color, Some(0x00ef_cdab)); +} + /// 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 3ce4ed60de99630f8fae5371e4ad5fd6da844f9b..f7eba5ec7ee008d1aed9d935beb21d42c28da9a4 100644 --- a/crates/notebook/src/location.rs +++ b/crates/notebook/src/location.rs @@ -42,9 +42,29 @@ pub fn smb(address: &str, share: &str, root: &str) -> String { } /// 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. +/// keep (`published`), as when the app renames or moves a notebook; with them those of every +/// location within it, as a copy of a section keeps its own. 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 within = format!("{from}/"); + let inner: Vec = fs::read_dir(cache.join("replicas")) + .into_iter() + .flatten() + .filter_map(|entry| fs::read_to_string(entry.ok()?.path().join(LOCATION)).ok()) + .filter(|location| location.starts_with(&within)) + .collect(); + move_folder(cache, from, to)?; + for location in inner { + move_folder( + cache, + &location, + &format!("{to}/{}", &location[within.len()..]), + )?; + } + Ok(()) +} + +fn move_folder(cache: &Path, from: &str, to: &str) -> io::Result<()> { let old = folder(cache, from); if fs::metadata(&old).is_err() { return Ok(()); diff --git a/crates/notebook/src/session.rs b/crates/notebook/src/session.rs index eee84a17bdf0981a5cb4141ae84c230c75567385..5d48608ab5689b369d0c13ceb6eda102d334c222 100644 --- a/crates/notebook/src/session.rs +++ b/crates/notebook/src/session.rs @@ -983,20 +983,7 @@ impl Notebook { }) .collect(); let to = self.storage.rename_root(name, &files)?; - let mut moves = vec![(from.clone(), to.clone())]; - moves.extend( - (self.catalog.sections()) - .filter(|section| section.copy) - .map(|section| { - ( - replica_location(&from, section), - replica_location(&to, section), - ) - }), - ); - for (from, to) in moves { - crate::location::moved(&self.cache, &from, &to)?; - } + crate::location::moved(&self.cache, &from, &to)?; let _ = fs::remove_file(&self.listing); self.listing = listing(&self.cache, &to); self.keep_listing(); -- 2.54.1