authoravatarclover caruso<git@paperclover.net>2026-10-19 12:58:34-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:39:58-07:00
log0be69d99f016301e65f1c6b8431e432c5f19f9cc
treec24f5ed7092aac09956b7691fca68dff4af17e66
parenta4c43834fe0b1f842728249f263c9dc06ebd702c
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

3 files changed, 75 insertions(+), 16 deletions(-)

crates/mobile/src/tests.rs+52
...@@ -4951,6 +4951,58 @@ fn an_upload_lost_after_the_notebook_is_renamed_merges_back() {...@@ -4951,6 +4951,58 @@ fn an_upload_lost_after_the_notebook_is_renamed_merges_back() {
4951 close_section(section);4951 close_section(section);
4952}4952}
49534953
4954/// A copy of a section beside it keeps a replica of its own; renaming the notebook in the app
4955/// carries the copy's along with the rest, its edits still waiting in it.
4956#[test]
4957fn renaming_a_notebook_keeps_a_copys_waiting_edits() {
4958 let directory = tempfile::tempdir().unwrap();
4959 let (root, cache) = (
4960 directory.path().join("Notes"),
4961 directory.path().join("cache"),
4962 );
4963 std::fs::create_dir(&root).unwrap();
4964 let bytes = onestore::create_section("Notes.one", "Original text", "Fixture").unwrap();
4965 for name in ["Notes.one", "Notes - Copy.one"] {
4966 std::fs::write(root.join(name), &bytes).unwrap();
4967 }
4968 let open_copy = |root: &Path| {
4969 let library = Arc::new(Library::open(root, &cache, true).unwrap());
4970 sb_library_set_offline(&library, true);
4971 open(&library, "Notes - Copy.one")
4972 };
4973 let copy = open_copy(&root);
4974 let space = copy.shared.section.pages().unwrap()[0].0;
4975 let op = Op::Page {
4976 space,
4977 op: PageOp::Color(Some(0x00ef_cdab)),
4978 };
4979 let at = library::filetime();
4980 copy.shared.apply(Edit { at, ops: vec![op] }).unwrap();
4981 copy.shared.section.written().unwrap();
4982 let pending = copy.shared.section.pending().unwrap();
4983 assert_eq!(pending.len(), 1);
4984 let shared = Arc::clone(&copy.shared);
4985 drop(copy);
4986 Arc::into_inner(shared).unwrap().section.close().unwrap();
4987
4988 let renamed = directory.path().join("Renamed Notes");
4989 std::fs::rename(&root, &renamed).unwrap();
4990 let text = |path: &Path| std::ffi::CString::new(path.as_os_str().as_encoded_bytes()).unwrap();
4991 let mut error = std::ptr::null_mut();
4992 let moved = unsafe {
4993 library::sb_notebook_moved(
4994 text(&cache).as_ptr(),
4995 text(&root).as_ptr(),
4996 text(&renamed).as_ptr(),
4997 &mut error,
4998 )
4999 };
5000 assert!(moved && error.is_null());
5001 let copy = open_copy(&renamed);
5002 assert_eq!(copy.shared.section.pending().unwrap(), pending);
5003 assert_eq!(copy.shared.page(space).unwrap().0.color, Some(0x00ef_cdab));
5004}
5005
4954/// What the iOS host calls on the main thread as a launch restores a page returns while the5006/// What the iOS host calls on the main thread as a launch restores a page returns while the
4955/// background waits on a coordinated read of the section, as iCloud Drive holds one until the5007/// background waits on a coordinated read of the section, as iCloud Drive holds one until the
4956/// file downloads: a main thread that waited would have the system end the app.5008/// file downloads: a main thread that waited would have the system end the app.
crates/notebook/src/location.rs+22-2
...@@ -42,9 +42,29 @@ pub fn smb(address: &str, share: &str, root: &str) -> String {...@@ -42,9 +42,29 @@ pub fn smb(address: &str, share: &str, root: &str) -> String {
42}42}
4343
44/// Moves the replicas of the sections at `from` to `to`, and the images their publications44/// Moves the replicas of the sections at `from` to `to`, and the images their publications
45/// keep (`published`), as when the app renames or moves a notebook; each replica `to` already45/// keep (`published`), as when the app renames or moves a notebook; with them those of every
46/// holds for the same section stays. Every replica moved must be closed.46/// location within it, as a copy of a section keeps its own. Each replica `to` already holds
47/// for the same section stays. Every replica moved must be closed.
47pub fn moved(cache: &Path, from: &str, to: &str) -> io::Result<()> {48pub fn moved(cache: &Path, from: &str, to: &str) -> io::Result<()> {
49 let within = format!("{from}/");
50 let inner: Vec<String> = fs::read_dir(cache.join("replicas"))
51 .into_iter()
52 .flatten()
53 .filter_map(|entry| fs::read_to_string(entry.ok()?.path().join(LOCATION)).ok())
54 .filter(|location| location.starts_with(&within))
55 .collect();
56 move_folder(cache, from, to)?;
57 for location in inner {
58 move_folder(
59 cache,
60 &location,
61 &format!("{to}/{}", &location[within.len()..]),
62 )?;
63 }
64 Ok(())
65}
66
67fn move_folder(cache: &Path, from: &str, to: &str) -> io::Result<()> {
48 let old = folder(cache, from);68 let old = folder(cache, from);
49 if fs::metadata(&old).is_err() {69 if fs::metadata(&old).is_err() {
50 return Ok(());70 return Ok(());
crates/notebook/src/session.rs+1-14
...@@ -983,20 +983,7 @@ impl Notebook {...@@ -983,20 +983,7 @@ impl Notebook {
983 })983 })
984 .collect();984 .collect();
985 let to = self.storage.rename_root(name, &files)?;985 let to = self.storage.rename_root(name, &files)?;
986 let mut moves = vec![(from.clone(), to.clone())];986 crate::location::moved(&self.cache, &from, &to)?;
987 moves.extend(
988 (self.catalog.sections())
989 .filter(|section| section.copy)
990 .map(|section| {
991 (
992 replica_location(&from, section),
993 replica_location(&to, section),
994 )
995 }),
996 );
997 for (from, to) in moves {
998 crate::location::moved(&self.cache, &from, &to)?;
999 }
1000 let _ = fs::remove_file(&self.listing);987 let _ = fs::remove_file(&self.listing);
1001 self.listing = listing(&self.cache, &to);988 self.listing = listing(&self.cache, &to);
1002 self.keep_listing();989 self.keep_listing();