| ... | ... | @@ -326,7 +326,7 @@ struct Joined { |
| 326 | 326 | #[derive(serde::Serialize, serde::Deserialize)] |
| 327 | 327 | struct Listed { |
| 328 | 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 | 330 | files: BTreeMap<String, String>, |
| 331 | 331 | } |
| 332 | 332 | |
| ... | ... | @@ -334,13 +334,25 @@ fn hex(bytes: &[u8]) -> String { |
| 334 | 334 | bytes.iter().map(|byte| format!("{byte:02x}")).collect() |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | | /// Every readable section's file identity in `folder` and its groups, by catalog path, as |
| 338 | | /// `Notebook::replicas` lists a local notebook's. `Client::read` answers a protected section |
| 339 | | /// as it does one mid-write (WouldBlock), so syncing one would show it in use for good. |
| 337 | /// The name of the replica of every readable section in `folder` and its groups, by catalog |
| 338 | /// path, as `Notebook::replicas` lists a local notebook's: its file identity in hex, and for a |
| 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. |
| 340 | 342 | pub(crate) fn files(folder: &Folder, files: &mut BTreeMap<String, String>) { |
| 341 | 343 | for section in &folder.sections { |
| 342 | 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 | 358 | for group in &folder.groups { |
| ... | ... | @@ -634,9 +646,9 @@ impl Library { |
| 634 | 646 | background.watch( |
| 635 | 647 | files |
| 636 | 648 | .iter() |
| 637 | | .map(|(path, identity)| Known { |
| 649 | .map(|(path, replica)| Known { |
| 638 | 650 | path: path.clone(), |
| 639 | | replica: Some(replicas.join(format!("{identity}.sqlite"))), |
| 651 | replica: Some(replicas.join(format!("{replica}.sqlite"))), |
| 640 | 652 | found: None, |
| 641 | 653 | image: None, |
| 642 | 654 | }) |
| ... | ... | @@ -916,12 +928,12 @@ impl Library { |
| 916 | 928 | .ok_or("The notebook hasn’t been read")? |
| 917 | 929 | .replica_path(path)?, |
| 918 | 930 | None => { |
| 919 | | let identity = self |
| 931 | let replica = self |
| 920 | 932 | .listed() |
| 921 | 933 | .and_then(|mut listed| listed.files.remove(path)) |
| 922 | 934 | .ok_or("The notebook hasn’t listed this section")?; |
| 923 | 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 | 939 | std::fs::create_dir_all(cache.parent().ok_or("No cache")?)?; |
| ... | ... | @@ -2832,4 +2844,81 @@ mod tests { |
| 2832 | 2844 | assert_eq!(section.shared.section.pending().unwrap(), pending); |
| 2833 | 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 | } |