authoravatarclover caruso<git@paperclover.net>2026-10-19 12:14:31-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:39:58-07:00
logb77c879d3ca88201a13162025edad2643f9a374f
tree19c72290793e34c71d4993c7d3bfc3364a23419c
parent531f68e7ffcf2451cd10a5668d8d64f5547a363c
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(mobile): keep a share section's copy apart from the section it copies

A section copied beside another on an SMB share holds the same file identity, and the mobile app named each share section's replica by that identity alone, so the copy and its original shared one. An edit made in the copy offline showed in the original and, once online, published to the original's file while the copy's stayed as it was. A copy's replica is now named by its path too, as local and iCloud notebooks already keep a copy's queue its own. One test edits the copy offline and opens the original; a live one against the Samba lab edits both offline and syncs them. Both failed before. - A copy of a section in a notebook on a server no longer takes edits made in the section it copies, or gives its own to it. Assisted-by: claude-opus-5.5

2 files changed, 150 insertions(+), 9 deletions(-)

crates/mobile/src/library.rs+98-9
...@@ -326,7 +326,7 @@ struct Joined {...@@ -326,7 +326,7 @@ struct Joined {
326#[derive(serde::Serialize, serde::Deserialize)]326#[derive(serde::Serialize, serde::Deserialize)]
327struct Listed {327struct Listed {
328 tabs: Vec<Tab>,328 tabs: Vec<Tab>,
329 /// Each readable section's file identity in hex, which names its replica, by catalog path.329 /// The name of each readable section's replica (`files`), by catalog path.
330 files: BTreeMap<String, String>,330 files: BTreeMap<String, String>,
331}331}
332332
...@@ -334,13 +334,25 @@ fn hex(bytes: &[u8]) -> String {...@@ -334,13 +334,25 @@ fn hex(bytes: &[u8]) -> String {
334 bytes.iter().map(|byte| format!("{byte:02x}")).collect()334 bytes.iter().map(|byte| format!("{byte:02x}")).collect()
335}335}
336336
337/// Every readable section's file identity in `folder` and its groups, by catalog path, as337/// The name of the replica of every readable section in `folder` and its groups, by catalog
338/// `Notebook::replicas` lists a local notebook's. `Client::read` answers a protected section338/// path, as `Notebook::replicas` lists a local notebook's: its file identity in hex, and for a
339/// as it does one mid-write (WouldBlock), so syncing one would show it in use for good.339/// copy of another section, which holds the same identity, its path besides, so that each
340/// keeps its own edits. `Client::read` answers a protected section as it does one mid-write
341/// (WouldBlock), so syncing one would show it in use for good.
340pub(crate) fn files(folder: &Folder, files: &mut BTreeMap<String, String>) {342pub(crate) fn files(folder: &Folder, files: &mut BTreeMap<String, String>) {
341 for section in &folder.sections {343 for section in &folder.sections {
342 if matches!(section.state, SectionState::Readable { .. }) {344 if matches!(section.state, SectionState::Readable { .. }) {
343 files.insert(section.path.clone(), hex(&section.file_id));345 let mut replica = hex(&section.file_id);
346 if section.copy {
347 replica.push('-');
348 replica.extend(
349 section
350 .path
351 .chars()
352 .map(|char| if char.is_alphanumeric() { char } else { '_' }),
353 );
354 }
355 files.insert(section.path.clone(), replica);
344 }356 }
345 }357 }
346 for group in &folder.groups {358 for group in &folder.groups {
...@@ -634,9 +646,9 @@ impl Library {...@@ -634,9 +646,9 @@ impl Library {
634 background.watch(646 background.watch(
635 files647 files
636 .iter()648 .iter()
637 .map(|(path, identity)| Known {649 .map(|(path, replica)| Known {
638 path: path.clone(),650 path: path.clone(),
639 replica: Some(replicas.join(format!("{identity}.sqlite"))),651 replica: Some(replicas.join(format!("{replica}.sqlite"))),
640 found: None,652 found: None,
641 image: None,653 image: None,
642 })654 })
...@@ -916,12 +928,12 @@ impl Library {...@@ -916,12 +928,12 @@ impl Library {
916 .ok_or("The notebook hasn’t been read")?928 .ok_or("The notebook hasn’t been read")?
917 .replica_path(path)?,929 .replica_path(path)?,
918 None => {930 None => {
919 let identity = self931 let replica = self
920 .listed()932 .listed()
921 .and_then(|mut listed| listed.files.remove(path))933 .and_then(|mut listed| listed.files.remove(path))
922 .ok_or("The notebook hasn’t listed this section")?;934 .ok_or("The notebook hasn’t listed this section")?;
923 let replicas = self.share_replicas().ok_or("Not a share")?;935 let replicas = self.share_replicas().ok_or("Not a share")?;
924 replicas.join(format!("{identity}.sqlite"))936 replicas.join(format!("{replica}.sqlite"))
925 }937 }
926 };938 };
927 std::fs::create_dir_all(cache.parent().ok_or("No cache")?)?;939 std::fs::create_dir_all(cache.parent().ok_or("No cache")?)?;
...@@ -2832,4 +2844,81 @@ mod tests {...@@ -2832,4 +2844,81 @@ mod tests {
2832 assert_eq!(section.shared.section.pending().unwrap(), pending);2844 assert_eq!(section.shared.section.pending().unwrap(), pending);
2833 assert_eq!(std::fs::read(root.join("Read.one")).unwrap(), bytes);2845 assert_eq!(std::fs::read(root.join("Read.one")).unwrap(), bytes);
2834 }2846 }
2847
2848 /// A copy of a section beside it on a share holds the same file identity. Offline, an
2849 /// edit made in the copy stays in the copy's replica and waits to publish to the copy,
2850 /// never reaching the original's.
2851 #[test]
2852 fn a_share_sections_copy_keeps_its_own_replica() {
2853 let directory = tempfile::tempdir().unwrap();
2854 let root = directory.path().join("notebook");
2855 let cache = directory.path().join("cache");
2856 std::fs::create_dir(&root).unwrap();
2857 let bytes = onestore::create_section("Notes.one", "Original text", "Fixture").unwrap();
2858 for name in ["Notes.one", "Notes - Copy.one"] {
2859 std::fs::write(root.join(name), &bytes).unwrap();
2860 }
2861 let notebook = Notebook::open(&root, &cache).unwrap();
2862 let mut listed = Listed {
2863 tabs: Vec::new(),
2864 files: BTreeMap::new(),
2865 };
2866 tabs(notebook.catalog(), &HashMap::new(), &mut listed.tabs);
2867 files(notebook.catalog(), &mut listed.files);
2868 drop(notebook);
2869 let address = "127.0.0.1:0";
2870 let replicas = notebook::location::folder(
2871 &cache,
2872 &notebook::location::smb(address, "agent", "Notebook"),
2873 );
2874 std::fs::create_dir_all(&replicas).unwrap();
2875 for replica in listed.files.values() {
2876 let _ = Replica::create(replicas.join(format!("{replica}.sqlite")), &bytes);
2877 }
2878 std::fs::create_dir_all(cache.join("smb")).unwrap();
2879 std::fs::write(
2880 cache.join("smb").join("127_0_0_1_0_agent_Notebook.json"),
2881 serde_json::to_vec(&listed).unwrap(),
2882 )
2883 .unwrap();
2884 let server = Server {
2885 address: address.into(),
2886 share: "agent".into(),
2887 user: String::new(),
2888 password: String::new(),
2889 domain: String::new(),
2890 };
2891 let library = Arc::new(Library::server(server, "Notebook", &cache).unwrap());
2892 sb_library_set_offline(&library, true);
2893 let open =
2894 |path| Section::open(Arc::clone(&library), path, "Fixture".into(), || {}).unwrap();
2895
2896 let copy = open("Notes - Copy.one");
2897 let space = copy.shared.section.pages().unwrap()[0].0;
2898 let colored = Edit {
2899 at: filetime(),
2900 ops: vec![Op::Page {
2901 space,
2902 op: onestore::op::PageOp::Color(Some(0x00ef_cdab)),
2903 }],
2904 };
2905 copy.shared.apply(colored).unwrap();
2906 copy.shared.section.written().unwrap();
2907 let shared = Arc::clone(&copy.shared);
2908 drop(copy);
2909 Arc::into_inner(shared).unwrap().section.close().unwrap();
2910
2911 let original = open("Notes.one");
2912 assert_eq!(original.shared.section.page(space).unwrap().color, None);
2913 assert!(original.shared.section.pending().unwrap().is_empty());
2914 let shared = Arc::clone(&original.shared);
2915 drop(original);
2916 Arc::into_inner(shared).unwrap().section.close().unwrap();
2917 let copy = open("Notes - Copy.one");
2918 assert_eq!(
2919 copy.shared.section.page(space).unwrap().color,
2920 Some(0x00ef_cdab)
2921 );
2922 assert_eq!(copy.shared.section.pending().unwrap().len(), 1);
2923 }
2835}2924}
crates/mobile/src/tests.rs+52
...@@ -1211,6 +1211,58 @@ fn a_notebook_on_a_share_saves_and_opens_offline() {...@@ -1211,6 +1211,58 @@ fn a_notebook_on_a_share_saves_and_opens_offline() {
1211 client.delete(&root).unwrap();1211 client.delete(&root).unwrap();
1212}1212}
12131213
1214/// Against a disposable share (as above): a section and a copy of it beside it, which holds
1215/// the same file identity, each edited offline, then synced: each file takes its own edit.
1216#[test]
1217#[ignore = "requires ONESTORE_SMB_LAB pointing to a disposable share"]
1218fn a_share_sections_copy_syncs_its_own_edits() {
1219 let address = std::env::var("ONESTORE_SMB_LAB").unwrap();
1220 let server = || library::Server {
1221 address: address.clone(),
1222 share: "agent".into(),
1223 user: std::env::var("SNOWBOUND_TEST_SMB_USER").unwrap_or_default(),
1224 password: std::env::var("SNOWBOUND_TEST_SMB_PASSWORD").unwrap_or_default(),
1225 domain: String::new(),
1226 };
1227 let client = server().connect().unwrap();
1228 let root = format!("snowbound-test-copy-{}", std::process::id());
1229 client.create_directory(&root).unwrap();
1230 let bytes = onestore::create_section("Notes.one", "Original text", "Fixture").unwrap();
1231 let names = ["Notes.one", "Notes - Copy.one"];
1232 for name in names {
1233 client.create(&format!("{root}/{name}"), &bytes).unwrap();
1234 }
1235 let cache = tempfile::tempdir().unwrap();
1236 let library = Arc::new(Library::server(server(), &root, cache.path()).unwrap());
1237 library.tabs().unwrap();
1238 sb_library_set_offline(&library, true);
1239 for (name, color) in names.into_iter().zip([0x0011_2233, 0x00ef_cdab]).rev() {
1240 let section = open(&library, name);
1241 let space = section.shared.section.pages().unwrap()[0].0;
1242 let op = Op::Page {
1243 space,
1244 op: PageOp::Color(Some(color)),
1245 };
1246 let at = library::filetime();
1247 section.shared.apply(Edit { at, ops: vec![op] }).unwrap();
1248 section.shared.section.written().unwrap();
1249 let shared = Arc::clone(&section.shared);
1250 drop(section);
1251 Arc::into_inner(shared).unwrap().section.close().unwrap();
1252 }
1253 sb_library_set_offline(&library, false);
1254 library.flush(Duration::from_secs(30)).unwrap();
1255 for (name, color) in names.into_iter().zip([0x0011_2233, 0x00ef_cdab]) {
1256 let stored = client.read(&format!("{root}/{name}"), 1 << 26).unwrap();
1257 let arena = onestore::Arena::default();
1258 let mut section = onestore::Section::open(&arena, stored).unwrap();
1259 let space = section.pages().unwrap()[0].0;
1260 assert_eq!(section.page(space).unwrap().color, Some(color), "{name}");
1261 client.delete(&format!("{root}/{name}")).unwrap();
1262 }
1263 client.delete(&root).unwrap();
1264}
1265
1214#[test]1266#[test]
1215fn a_share_notebook_syncs_only_its_readable_sections() {1267fn a_share_notebook_syncs_only_its_readable_sections() {
1216 let (directory, root) = copy("native-encrypted/cold-encrypted-02/notebook");1268 let (directory, root) = copy("native-encrypted/cold-encrypted-02/notebook");