diff --git a/crates/mobile/src/library.rs b/crates/mobile/src/library.rs index 673cce47075af3e8810ff8802767c9155c7d86a6..067d19b71d08e4c81ac7560fe727ceac5cc87ed2 100644 --- a/crates/mobile/src/library.rs +++ b/crates/mobile/src/library.rs @@ -326,7 +326,7 @@ struct Joined { #[derive(serde::Serialize, serde::Deserialize)] struct Listed { tabs: Vec, - /// Each readable section's file identity in hex, which names its replica, by catalog path. + /// The name of each readable section's replica (`files`), by catalog path. files: BTreeMap, } @@ -334,13 +334,25 @@ fn hex(bytes: &[u8]) -> String { bytes.iter().map(|byte| format!("{byte:02x}")).collect() } -/// Every readable section's file identity in `folder` and its groups, by catalog path, as -/// `Notebook::replicas` lists a local notebook's. `Client::read` answers a protected section -/// as it does one mid-write (WouldBlock), so syncing one would show it in use for good. +/// The name of the replica of every readable section in `folder` and its groups, by catalog +/// path, as `Notebook::replicas` lists a local notebook's: its file identity in hex, and for a +/// copy of another section, which holds the same identity, its path besides, so that each +/// keeps its own edits. `Client::read` answers a protected section as it does one mid-write +/// (WouldBlock), so syncing one would show it in use for good. pub(crate) fn files(folder: &Folder, files: &mut BTreeMap) { for section in &folder.sections { if matches!(section.state, SectionState::Readable { .. }) { - files.insert(section.path.clone(), hex(§ion.file_id)); + let mut replica = hex(§ion.file_id); + if section.copy { + replica.push('-'); + replica.extend( + section + .path + .chars() + .map(|char| if char.is_alphanumeric() { char } else { '_' }), + ); + } + files.insert(section.path.clone(), replica); } } for group in &folder.groups { @@ -634,9 +646,9 @@ impl Library { background.watch( files .iter() - .map(|(path, identity)| Known { + .map(|(path, replica)| Known { path: path.clone(), - replica: Some(replicas.join(format!("{identity}.sqlite"))), + replica: Some(replicas.join(format!("{replica}.sqlite"))), found: None, image: None, }) @@ -916,12 +928,12 @@ impl Library { .ok_or("The notebook hasn’t been read")? .replica_path(path)?, None => { - let identity = self + let replica = self .listed() .and_then(|mut listed| listed.files.remove(path)) .ok_or("The notebook hasn’t listed this section")?; let replicas = self.share_replicas().ok_or("Not a share")?; - replicas.join(format!("{identity}.sqlite")) + replicas.join(format!("{replica}.sqlite")) } }; std::fs::create_dir_all(cache.parent().ok_or("No cache")?)?; @@ -2832,4 +2844,81 @@ mod tests { assert_eq!(section.shared.section.pending().unwrap(), pending); assert_eq!(std::fs::read(root.join("Read.one")).unwrap(), bytes); } + + /// A copy of a section beside it on a share holds the same file identity. Offline, an + /// edit made in the copy stays in the copy's replica and waits to publish to the copy, + /// never reaching the original's. + #[test] + fn a_share_sections_copy_keeps_its_own_replica() { + let directory = tempfile::tempdir().unwrap(); + let root = directory.path().join("notebook"); + let cache = 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 notebook = Notebook::open(&root, &cache).unwrap(); + let mut listed = Listed { + tabs: Vec::new(), + files: BTreeMap::new(), + }; + tabs(notebook.catalog(), &HashMap::new(), &mut listed.tabs); + files(notebook.catalog(), &mut listed.files); + drop(notebook); + let address = "127.0.0.1:0"; + let replicas = notebook::location::folder( + &cache, + ¬ebook::location::smb(address, "agent", "Notebook"), + ); + std::fs::create_dir_all(&replicas).unwrap(); + for replica in listed.files.values() { + let _ = Replica::create(replicas.join(format!("{replica}.sqlite")), &bytes); + } + std::fs::create_dir_all(cache.join("smb")).unwrap(); + std::fs::write( + cache.join("smb").join("127_0_0_1_0_agent_Notebook.json"), + serde_json::to_vec(&listed).unwrap(), + ) + .unwrap(); + let server = Server { + address: address.into(), + share: "agent".into(), + user: String::new(), + password: String::new(), + domain: String::new(), + }; + let library = Arc::new(Library::server(server, "Notebook", &cache).unwrap()); + sb_library_set_offline(&library, true); + let open = + |path| Section::open(Arc::clone(&library), path, "Fixture".into(), || {}).unwrap(); + + let copy = open("Notes - Copy.one"); + let space = copy.shared.section.pages().unwrap()[0].0; + let colored = Edit { + at: filetime(), + ops: vec![Op::Page { + space, + op: onestore::op::PageOp::Color(Some(0x00ef_cdab)), + }], + }; + copy.shared.apply(colored).unwrap(); + copy.shared.section.written().unwrap(); + let shared = Arc::clone(©.shared); + drop(copy); + Arc::into_inner(shared).unwrap().section.close().unwrap(); + + let original = open("Notes.one"); + assert_eq!(original.shared.section.page(space).unwrap().color, None); + assert!(original.shared.section.pending().unwrap().is_empty()); + let shared = Arc::clone(&original.shared); + drop(original); + Arc::into_inner(shared).unwrap().section.close().unwrap(); + let copy = open("Notes - Copy.one"); + assert_eq!( + copy.shared.section.page(space).unwrap().color, + Some(0x00ef_cdab) + ); + assert_eq!(copy.shared.section.pending().unwrap().len(), 1); + } } diff --git a/crates/mobile/src/tests.rs b/crates/mobile/src/tests.rs index 6a77780111499cf34ce911d8304f50bfb012bdb0..c920623964a8f3215149b7169287d723fb7c7c6d 100644 --- a/crates/mobile/src/tests.rs +++ b/crates/mobile/src/tests.rs @@ -1211,6 +1211,58 @@ fn a_notebook_on_a_share_saves_and_opens_offline() { client.delete(&root).unwrap(); } +/// Against a disposable share (as above): a section and a copy of it beside it, which holds +/// the same file identity, each edited offline, then synced: each file takes its own edit. +#[test] +#[ignore = "requires ONESTORE_SMB_LAB pointing to a disposable share"] +fn a_share_sections_copy_syncs_its_own_edits() { + let address = std::env::var("ONESTORE_SMB_LAB").unwrap(); + let server = || library::Server { + address: address.clone(), + share: "agent".into(), + user: std::env::var("SNOWBOUND_TEST_SMB_USER").unwrap_or_default(), + password: std::env::var("SNOWBOUND_TEST_SMB_PASSWORD").unwrap_or_default(), + domain: String::new(), + }; + let client = server().connect().unwrap(); + let root = format!("snowbound-test-copy-{}", std::process::id()); + client.create_directory(&root).unwrap(); + let bytes = onestore::create_section("Notes.one", "Original text", "Fixture").unwrap(); + let names = ["Notes.one", "Notes - Copy.one"]; + for name in names { + client.create(&format!("{root}/{name}"), &bytes).unwrap(); + } + let cache = tempfile::tempdir().unwrap(); + let library = Arc::new(Library::server(server(), &root, cache.path()).unwrap()); + library.tabs().unwrap(); + sb_library_set_offline(&library, true); + for (name, color) in names.into_iter().zip([0x0011_2233, 0x00ef_cdab]).rev() { + let section = open(&library, name); + let space = section.shared.section.pages().unwrap()[0].0; + let op = Op::Page { + space, + op: PageOp::Color(Some(color)), + }; + let at = library::filetime(); + section.shared.apply(Edit { at, ops: vec![op] }).unwrap(); + section.shared.section.written().unwrap(); + let shared = Arc::clone(§ion.shared); + drop(section); + Arc::into_inner(shared).unwrap().section.close().unwrap(); + } + sb_library_set_offline(&library, false); + library.flush(Duration::from_secs(30)).unwrap(); + for (name, color) in names.into_iter().zip([0x0011_2233, 0x00ef_cdab]) { + let stored = client.read(&format!("{root}/{name}"), 1 << 26).unwrap(); + let arena = onestore::Arena::default(); + let mut section = onestore::Section::open(&arena, stored).unwrap(); + let space = section.pages().unwrap()[0].0; + assert_eq!(section.page(space).unwrap().color, Some(color), "{name}"); + client.delete(&format!("{root}/{name}")).unwrap(); + } + client.delete(&root).unwrap(); +} + #[test] fn a_share_notebook_syncs_only_its_readable_sections() { let (directory, root) = copy("native-encrypted/cold-encrypted-02/notebook");