authoravatarclover caruso<git@paperclover.net>2026-10-19 17:01:25-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:39:58-07:00
log00efb7f2a201741ccea64df3120cf80bb4d0f6cd
tree327d51a3e2863f5172b34cda40dd861c7c0b22e1
parent0be69d99f016301e65f1c6b8431e432c5f19f9cc
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix: keep each section's uploads beside its replica, once

Review fixes for the lost-upload stack (yvvmzxws, rpmtnlzk, toxstspw, pswvkryr, ssnownqr). They share one change of design, so they land as one commit rather than folded into each. The images a section's publications keep now live beside its replica ("<replica>.published"), as the session chose it, instead of in a folder guessed from whether a copy's folder had been labelled: the remotes the session and the background make are handed the replica's path. A copy found after the notebook opened kept its images under the original's identity and replaced the original's; now each keeps its own. They move with the replica (location::moved, adopt) and go with it (remove_replica), so a password set elsewhere, which supersedes the replica, takes the plaintext kept with it; closing a notebook, as the app does before renaming it, keeps an offline copy whose images are not yet merged, as a waiting edit keeps it, and only removing the notebook deletes them, and copies' folders are labelled as they are first named, so a rename carries them. Android's provider keeps its images the same way. An upload iCloud Drive keeps as a conflict version is no longer also merged from the store, which kept it beside the file twice: the store skips an image a conflict version is, lists none while a conflict version cannot be read yet, and drops it when that version is retired. A confirmation, which rewrites only the header, carries the kept image along rather than letting it list as lost and go. The store reads the file's stamp only where it keeps something. The Mac names its versions by SCDynamicStoreCopyComputerName instead of the deprecated NSHost, and its iCloud functions take no cache, which pswvkryr had added only on macOS. A share copy's replica is named as the session names a copy's (session::replica_location) instead of by a second rule. Tests: the store's own (a lost image lists once, after LAG, not beside a conflict version; a confirmation; nothing read when nothing is kept), and on the phone an upload undone then merged back after LAG, a phone-only notebook keeping nothing, a password set elsewhere, a copy found while open, and the launch test now asserting nothing kept beside the file and no stale image. The test coordinator holds files per test, so parallel tests no longer take each other's hold. Assisted-by: claude-opus-5.5

17 files changed, 836 insertions(+), 410 deletions(-)

arc/sync.md+2-1
......@@ -265,7 +265,8 @@ mount reports none of another client's changes to inotify, so there each section
265265read every 15 seconds. On iOS, a folder on the device (Snowbound's own, or On My iPhone) has no
266266copies and learns of other apps' writes through file coordination (`NSFilePresenter`); one a
267267file provider keeps elsewhere, as iCloud Drive does, keeps copies and is checked every 15
268seconds. Closing a notebook deletes its copies, except any with edits still waiting.
268seconds. Closing a notebook deletes its copies, except any with edits still waiting or images
269of uploads it keeps (`notebook::published`).
269270
270271## iCloud Drive
271272
crates/mobile/src/library.rs+102-103
......@@ -20,7 +20,7 @@ use onestore::{
2020 protected::Key,
2121};
2222use std::{
23 collections::{BTreeMap, HashMap},
23 collections::{BTreeMap, BTreeSet, HashMap},
2424 ffi::{CString, c_char, c_void},
2525 io,
2626 path::{Path, PathBuf},
......@@ -123,44 +123,51 @@ pub unsafe extern "C" fn sb_set_versions(
123123
124124/// A local section file read and published under the host's file coordination. One a file
125125/// provider keeps elsewhere, which syncs it whole with no lock, keeps each image this device
126/// publishes until the file holds it, and merges one another device's upload replaced back
127/// (`notebook::published`).
126/// publishes beside its replica until the file holds it, and merges one another device's
127/// upload replaced back (`notebook::published`).
128128struct Coordinated {
129129 file: PathBuf,
130130 published: Option<Published>,
131131}
132132
133133impl Coordinated {
134 /// The file `file` of the notebook folder, or lone section, at `notebook`.
135 fn new(file: &Path, notebook: &Path, cache: &Path, elsewhere: bool) -> Self {
136 let device = VERSIONS.get().map(|(.., device)| device.clone());
137 let published = elsewhere
138 .then(|| Published::of(cache, notebook, file, device))
139 .flatten();
134 /// The section file `file`, its replica at `replica` where it has one.
135 fn new(file: &Path, replica: Option<&Path>, elsewhere: bool) -> Self {
136 let device = VERSIONS
137 .get()
138 .filter(|_| elsewhere)
139 .map(|(.., device)| device);
140140 Self {
141141 file: file.to_owned(),
142 published,
142 published: replica
143 .zip(device)
144 .map(|(replica, device)| Published::of(replica, device)),
143145 }
144146 }
145147}
146148
149/// The header and length of `file`, as the file remote's uncoordinated change detector reads
150/// them.
151fn stamp(file: &Path) -> io::Result<Stamp> {
152 coordinated(file, false, || {
153 use std::io::Read;
154 let mut opened = std::fs::File::open(file)?;
155 let mut header = [0; 1024];
156 opened.read_exact(&mut header)?;
157 Ok(Stamp {
158 header,
159 length: opened.metadata()?.len(),
160 })
161 })?
162}
163
147164impl Remote for Coordinated {
148165 fn read(&mut self) -> io::Result<Vec<u8>> {
149166 coordinated(&self.file, false, || onestore::read_file(&self.file))?
150167 }
151168
152 /// The header and length, as the file remote's uncoordinated change detector reads them.
153169 fn stamp(&mut self) -> io::Result<Stamp> {
154 coordinated(&self.file, false, || {
155 use std::io::Read;
156 let mut file = std::fs::File::open(&self.file)?;
157 let mut header = [0; 1024];
158 file.read_exact(&mut header)?;
159 Ok(Stamp {
160 header,
161 length: file.metadata()?.len(),
162 })
163 })?
170 stamp(&self.file)
164171 }
165172
166173 fn publish(&mut self, transaction: &Transaction) -> std::result::Result<(), CommitError> {
......@@ -178,7 +185,12 @@ impl Remote for Coordinated {
178185
179186 fn confirm(&mut self, base: &Stamp) -> std::result::Result<(), CommitError> {
180187 coordinated(&self.file, true, || {
181 onestore::confirm_file(&self.file, base)
188 onestore::confirm_file(&self.file, base)?;
189 if let Some(published) = &self.published {
190 let _ = std::fs::File::open(&self.file)
191 .and_then(|mut file| published.confirmed(Written::InPlace(&mut file), base));
192 }
193 Ok(())
182194 })
183195 .map_err(uncommitted)?
184196 }
......@@ -198,13 +210,9 @@ impl Remote for Coordinated {
198210 let path = CString::new(self.file.as_os_str().as_encoded_bytes())?;
199211 versions(path.as_ptr(), found, (&raw mut listed).cast());
200212 }
201 if self.published.is_some() {
202 let stamp = self.stamp()?;
203 listed.extend(
204 self.published
205 .iter()
206 .flat_map(|published| published.versions(&stamp)),
207 );
213 if let Some(published) = &self.published {
214 let lost = published.versions(|| stamp(&self.file), &listed)?;
215 listed.extend(lost);
208216 }
209217 Ok(listed)
210218 }
......@@ -221,12 +229,14 @@ impl Remote for Coordinated {
221229 && published.holds(id)
222230 {
223231 if keep {
224 let device = VERSIONS.get().map_or("this device", |(.., device)| device);
225 let beside = notebook::published::beside(&self.file, device);
232 let beside = notebook::published::beside(&self.file, published.device());
226233 coordinated(&beside, true, || std::fs::copy(id, &beside))??;
227234 }
228235 return std::fs::remove_file(id);
229236 }
237 if let Some(published) = &self.published {
238 let _ = published.retired(Path::new(id));
239 }
230240 let Some((_, retire, _)) = VERSIONS.get() else {
231241 return Ok(());
232242 };
......@@ -304,40 +314,38 @@ struct Joined {
304314
305315/// A share notebook's sections as last listed, so they open from their replicas while the
306316/// server cannot be reached.
307#[derive(serde::Serialize, serde::Deserialize)]
308struct Listed {
317#[derive(serde::Serialize, serde::Deserialize, Default)]
318pub(crate) struct Listed {
309319 tabs: Vec<Tab>,
310 /// The name of each readable section's replica (`files`), by catalog path.
311 files: BTreeMap<String, String>,
320 /// Each readable section's file identity in hex, which names its replica, by catalog path.
321 pub(crate) files: BTreeMap<String, String>,
322 /// The sections that copy another, holding its identity, whose replicas are their own
323 /// (`session::replica_location`).
324 #[serde(default)]
325 pub(crate) copies: BTreeSet<String>,
312326}
313327
314328fn hex(bytes: &[u8]) -> String {
315329 bytes.iter().map(|byte| format!("{byte:02x}")).collect()
316330}
317331
318/// The name of the replica of every readable section in `folder` and its groups, by catalog
319/// path, as `Notebook::replicas` lists a local notebook's: its file identity in hex, and for a
320/// copy of another section, which holds the same identity, its path besides, so that each
321/// keeps its own edits. `Client::read` answers a protected section as it does one mid-write
322/// (WouldBlock), so syncing one would show it in use for good.
323pub(crate) fn files(folder: &Folder, files: &mut BTreeMap<String, String>) {
332/// Every readable section's file identity in `folder` and its groups, by catalog path, and
333/// the copies among them, as `Notebook::replicas` lists a local notebook's. `Client::read`
334/// answers a protected section as it does one mid-write (WouldBlock), so syncing one would
335/// show it in use for good.
336pub(crate) fn files(folder: &Folder, listed: &mut Listed) {
324337 for section in &folder.sections {
325338 if matches!(section.state, SectionState::Readable { .. }) {
326 let mut replica = hex(&section.file_id);
339 listed
340 .files
341 .insert(section.path.clone(), hex(&section.file_id));
327342 if section.copy {
328 replica.push('-');
329 replica.extend(
330 section
331 .path
332 .chars()
333 .map(|char| if char.is_alphanumeric() { char } else { '_' }),
334 );
343 listed.copies.insert(section.path.clone());
335344 }
336 files.insert(section.path.clone(), replica);
337345 }
338346 }
339347 for group in &folder.groups {
340 self::files(group, files);
348 self::files(group, listed);
341349 }
342350}
343351
......@@ -473,11 +481,10 @@ impl Library {
473481 (None, Place::File(path.to_owned()), None)
474482 } else {
475483 let mut notebook = Notebook::open(path, cache)?;
476 let (root, kept) = (path.to_owned(), cache.to_owned());
477484 let background = notebook.background_with(
478485 local,
479486 !local,
480 move |file| Coordinated::new(file, &root, &kept, !local),
487 move |file, replica| Coordinated::new(file, replica, !local),
481488 sync_woken,
482489 )?;
483490 background.watch(notebook.replicas());
......@@ -523,7 +530,7 @@ impl Library {
523530 };
524531 let reached = library.with_notebook(false, |_| Ok(()));
525532 match library.listed() {
526 Some(listed) => library.watch(&listed.files),
533 Some(listed) => library.watch(&listed),
527534 None => reached?,
528535 }
529536 Ok(library)
......@@ -619,17 +626,18 @@ impl Library {
619626 })
620627 }
621628
622 /// Has the background sync a share notebook's sections, `files` as `Listed` keeps them.
623 fn watch(&self, files: &BTreeMap<String, String>) {
624 let (Some(background), Some(replicas)) = (&self.background, self.share_replicas()) else {
629 /// Has the background sync a share notebook's sections, as `listed` names them.
630 fn watch(&self, listed: &Listed) {
631 let Some(background) = &self.background else {
625632 return;
626633 };
627634 background.watch(
628 files
629 .iter()
630 .map(|(path, replica)| Known {
635 listed
636 .files
637 .keys()
638 .map(|path| Known {
631639 path: path.clone(),
632 replica: Some(replicas.join(format!("{replica}.sqlite"))),
640 replica: self.share_replica(listed, path),
633641 found: None,
634642 image: None,
635643 })
......@@ -637,13 +645,16 @@ impl Library {
637645 );
638646 }
639647
640 /// The cache folder of a share notebook's replicas.
641 fn share_replicas(&self) -> Option<PathBuf> {
648 /// The replica of the share section at catalog `path`, as `listed` names it: by its file
649 /// identity in the notebook's folder, a copy's in a folder of its own.
650 fn share_replica(&self, listed: &Listed, path: &str) -> Option<PathBuf> {
642651 let Place::Share { server, root, .. } = &self.place else {
643652 return None;
644653 };
645 let location = notebook::location::smb(&server.address, &server.share, root);
646 Some(notebook::location::folder(&self.cache, &location))
654 let notebook = notebook::location::smb(&server.address, &server.share, root);
655 let location = session::replica_location(&notebook, path, listed.copies.contains(path));
656 let folder = notebook::location::claimed(&self.cache, &location).ok()?;
657 Some(folder.join(format!("{}.sqlite", listed.files.get(path)?)))
647658 }
648659
649660 /// Where a share notebook's last listing is kept.
......@@ -757,13 +768,10 @@ impl Library {
757768 }
758769 let listed = self.with_notebook(false, |notebook| {
759770 notebook.refresh()?;
760 let mut listed = Listed {
761 tabs: Vec::new(),
762 files: BTreeMap::new(),
763 };
771 let mut listed = Listed::default();
764772 tabs(notebook.catalog(), &self.unlocked(), &mut listed.tabs);
765773 if matches!(self.place, Place::Share { .. }) {
766 files(notebook.catalog(), &mut listed.files);
774 files(notebook.catalog(), &mut listed);
767775 }
768776 if let (Place::Folder(_) | Place::Hosted(_) | Place::Provider(_), Some(background)) =
769777 (&self.place, &self.background)
......@@ -779,7 +787,7 @@ impl Library {
779787 Ok(listed) => {
780788 std::fs::create_dir_all(listing.parent().ok_or("No cache")?)?;
781789 std::fs::write(&listing, serde_json::to_vec(&listed)?)?;
782 self.watch(&listed.files);
790 self.watch(&listed);
783791 Ok(listed.tabs)
784792 }
785793 Err(error) => Ok(self.listed().ok_or(error)?.tabs),
......@@ -874,21 +882,19 @@ impl Library {
874882 notify: impl Fn() + Send + Sync + 'static,
875883 ) -> Result<session::Section> {
876884 let offline = self.offline.load(Ordering::Relaxed);
877 let (cache, elsewhere) = (self.cache.clone(), self.elsewhere);
878 let connect = |notebook: &Path| {
879 let notebook = notebook.to_owned();
880 move |file: &Path| Ok(Coordinated::new(file, &notebook, &cache, elsewhere))
885 let elsewhere = self.elsewhere;
886 let connect = move |file: &Path, replica: Option<&Path>| {
887 Ok(Coordinated::new(file, replica, elsewhere))
881888 };
882889 match &self.place {
883890 Place::File(file) => Ok(session::Section::open_with(
884891 file,
885892 &self.cache,
886 connect(file),
893 connect,
887894 offline,
888895 notify,
889896 )?),
890 Place::Folder(root) => {
891 let connect = connect(root);
897 Place::Folder(_) => {
892898 let notebook = self.notebook();
893899 let notebook = notebook.as_ref().ok_or("A lone section has no notebook")?;
894900 Ok(match self.unlocked().get(path) {
......@@ -908,14 +914,10 @@ impl Library {
908914 .as_ref()
909915 .ok_or("The notebook hasn’t been read")?
910916 .replica_path(path)?,
911 None => {
912 let replica = self
913 .listed()
914 .and_then(|mut listed| listed.files.remove(path))
915 .ok_or("The notebook hasn’t listed this section")?;
916 let replicas = self.share_replicas().ok_or("Not a share")?;
917 replicas.join(format!("{replica}.sqlite"))
918 }
917 None => self
918 .listed()
919 .and_then(|listed| self.share_replica(&listed, path))
920 .ok_or("The notebook hasn’t listed this section")?,
919921 };
920922 std::fs::create_dir_all(cache.parent().ok_or("No cache")?)?;
921923 let replica = Replica::open_or_create(&cache, key.as_ref(), || {
......@@ -2753,12 +2755,9 @@ mod tests {
27532755 onestore::create_section("Read.one", "Offline source content", "Fixture").unwrap();
27542756 std::fs::write(root.join("Read.one"), &bytes).unwrap();
27552757 let notebook = Notebook::open(&root, &cache).unwrap();
2756 let mut listed = Listed {
2757 tabs: Vec::new(),
2758 files: BTreeMap::new(),
2759 };
2758 let mut listed = Listed::default();
27602759 tabs(notebook.catalog(), &HashMap::new(), &mut listed.tabs);
2761 files(notebook.catalog(), &mut listed.files);
2760 files(notebook.catalog(), &mut listed);
27622761 let identity = listed.files["Read.one"].clone();
27632762 drop(notebook);
27642763
......@@ -2840,21 +2839,21 @@ mod tests {
28402839 std::fs::write(root.join(name), &bytes).unwrap();
28412840 }
28422841 let notebook = Notebook::open(&root, &cache).unwrap();
2843 let mut listed = Listed {
2844 tabs: Vec::new(),
2845 files: BTreeMap::new(),
2846 };
2842 let mut listed = Listed::default();
28472843 tabs(notebook.catalog(), &HashMap::new(), &mut listed.tabs);
2848 files(notebook.catalog(), &mut listed.files);
2844 files(notebook.catalog(), &mut listed);
28492845 drop(notebook);
2850 let address = "127.0.0.1:0";
2851 let replicas = notebook::location::folder(
2852 &cache,
2853 &notebook::location::smb(address, "agent", "Notebook"),
2846 assert_eq!(
2847 listed.copies.iter().collect::<Vec<_>>(),
2848 ["Notes - Copy.one"]
28542849 );
2855 std::fs::create_dir_all(&replicas).unwrap();
2856 for replica in listed.files.values() {
2857 let _ = Replica::create(replicas.join(format!("{replica}.sqlite")), &bytes);
2850 let address = "127.0.0.1:0";
2851 let notebook = notebook::location::smb(address, "agent", "Notebook");
2852 for (path, identity) in &listed.files {
2853 let copy = listed.copies.contains(path);
2854 let location = session::replica_location(&notebook, path, copy);
2855 let replicas = notebook::location::claimed(&cache, &location).unwrap();
2856 Replica::create(replicas.join(format!("{identity}.sqlite")), &bytes).unwrap();
28582857 }
28592858 std::fs::create_dir_all(cache.join("smb")).unwrap();
28602859 std::fs::write(
crates/mobile/src/tests.rs+213-22
......@@ -1273,22 +1273,22 @@ fn a_share_notebook_syncs_only_its_readable_sections() {
12731273 .unwrap();
12741274 let notebook =
12751275 notebook::session::Notebook::open(&root, directory.path().join("cache")).unwrap();
1276 let mut files = std::collections::BTreeMap::new();
1277 library::files(notebook.catalog(), &mut files);
1278 assert_eq!(files.keys().collect::<Vec<_>>(), ["Empty.one"]);
1276 let mut listed = library::Listed::default();
1277 library::files(notebook.catalog(), &mut listed);
1278 assert_eq!(listed.files.keys().collect::<Vec<_>>(), ["Empty.one"]);
12791279}
12801280
12811281/// Paths the test coordinator was asked for, with whether to write.
12821282static COORDINATED: Mutex<Vec<(String, bool)>> = Mutex::new(Vec::new());
12831283
1284/// A file whose next coordinated access the test coordinator holds, as another app may: who
1285/// hears that it is held, and what lets it go.
1284/// Files whose next coordinated access the test coordinator holds, as another app may: who
1285/// hears that each is held, and what lets it go. Tests running at once hold files of their own.
12861286type Held = (
12871287 String,
12881288 std::sync::mpsc::Sender<()>,
12891289 std::sync::mpsc::Receiver<()>,
12901290);
1291static HELD: Mutex<Option<Held>> = Mutex::new(None);
1291static HELD: Mutex<Vec<Held>> = Mutex::new(Vec::new());
12921292
12931293extern "C" fn coordinator(
12941294 path: *const c_char,
......@@ -1300,10 +1300,8 @@ extern "C" fn coordinator(
13001300 COORDINATED.lock().unwrap().push((path.clone(), write));
13011301 let held = {
13021302 let mut held = HELD.lock().unwrap();
1303 match &*held {
1304 Some((file, ..)) if *file == path => held.take(),
1305 _ => None,
1306 }
1303 let at = held.iter().position(|(file, ..)| *file == path);
1304 at.map(|at| held.remove(at))
13071305 };
13081306 if let Some((_, entered, released)) = held {
13091307 let _ = entered.send(());
......@@ -2043,7 +2041,9 @@ fn a_page_edited_while_it_moves_stays_with_its_edit() {
20432041 let (entered, holding) = std::sync::mpsc::channel();
20442042 let (release, released) = std::sync::mpsc::channel();
20452043 let empty = root.canonicalize().unwrap().join("Empty.one");
2046 *HELD.lock().unwrap() = Some((empty.to_string_lossy().into_owned(), entered, released));
2044 HELD.lock()
2045 .unwrap()
2046 .push((empty.to_string_lossy().into_owned(), entered, released));
20472047 let moving = Arc::clone(&library);
20482048 let date = ["Thursday, October 1, 2026", "9:00 AM"];
20492049 let move_page = std::thread::spawn(move || {
......@@ -4707,6 +4707,35 @@ fn every_page(root: &Path, cache: &Path) -> String {
47074707 shown
47084708}
47094709
4710/// The images the cache keeps of this device's publications.
4711fn kept(cache: &Path) -> Vec<Vec<u8>> {
4712 let folders = std::fs::read_dir(cache.join("replicas"))
4713 .into_iter()
4714 .flatten();
4715 folders
4716 .filter_map(|folder| std::fs::read_dir(folder.ok()?.path()).ok())
4717 .flatten()
4718 .filter_map(|entry| {
4719 let path = entry.ok()?.path();
4720 (path.extension()? == "published").then_some(path)
4721 })
4722 .filter_map(|store| std::fs::read_dir(store).ok())
4723 .flatten()
4724 .map(|image| std::fs::read(image.unwrap().path()).unwrap())
4725 .collect()
4726}
4727
4728/// How many versions of a section are kept beside it in `root`, as "Name (iPhone).one".
4729fn beside(root: &Path) -> usize {
4730 std::fs::read_dir(root)
4731 .unwrap()
4732 .filter(|entry| {
4733 let name = entry.as_ref().unwrap().file_name();
4734 name.to_string_lossy().contains("(iPhone")
4735 })
4736 .count()
4737}
4738
47104739/// The titles and text of every page and conflict page each section file in `root` stores.
47114740fn stored(root: &Path) -> String {
47124741 let mut text = String::new();
......@@ -4741,13 +4770,11 @@ fn stored(root: &Path) -> String {
47414770/// types on it, and a section the phone adds. The Mac's upload wins: iCloud Drive keeps the
47424771/// phone's file as a conflict version, or loses it. The phone's next launch opens, lists and
47434772/// merges it back through the C surface as iOS does, its pages open and typed on in the
4744/// Reading View all the while. Nothing either device typed goes missing, merged or set aside
4745/// whole.
4773/// Reading View all the while. Nothing either device typed goes missing, and nothing is kept
4774/// beside the file twice.
47464775#[test]
47474776fn an_icloud_notebook_merges_the_phones_upload_back_at_launch() {
4748 unsafe {
4749 library::sb_set_versions(cloud_versions, cloud_retire, c"Clover’s iPhone".as_ptr())
4750 };
4777 unsafe { library::sb_set_versions(cloud_versions, cloud_retire, c"iPhone".as_ptr()) };
47514778 // Whether the devices' copies already share an edit of the page both type on, and
47524779 // whether iCloud Drive lost the phone's upload rather than keep it as a version.
47534780 for (shared, lost) in [(false, false), (true, false), (false, true), (true, true)] {
......@@ -4825,7 +4852,7 @@ fn an_icloud_notebook_merges_the_phones_upload_back_at_launch() {
48254852 std::fs::rename(&file, &version).unwrap();
48264853 std::fs::copy(mac.join(&path), &file).unwrap();
48274854 if !lost {
4828 let device = "Clover’s iPhone".to_owned();
4855 let device = "iPhone".to_owned();
48294856 let file = std::fs::canonicalize(&file).unwrap();
48304857 CLOUD_VERSIONS.lock().unwrap().push((file, version, device));
48314858 }
......@@ -4864,9 +4891,25 @@ fn an_icloud_notebook_merges_the_phones_upload_back_at_launch() {
48644891 unsafe { sb_insert(&mut view, c"After ".as_ptr()) };
48654892 sb_view_keep(&view);
48664893 }
4894 // What the phone kept of its uploads goes once its file moves on, keeping them; only
4895 // what a file stands as stays kept.
4896 assert!(
4897 eventually(|| {
4898 section.shared.section.wake();
4899 let files: Vec<Vec<u8>> = (std::fs::read_dir(&phone).unwrap())
4900 .map(|entry| std::fs::read(entry.unwrap().path()).unwrap_or_default())
4901 .collect();
4902 kept(&folder("phone-cache"))
4903 .iter()
4904 .all(|image| files.contains(image))
4905 }),
4906 "the phone's uploads stay kept (shared {shared}, lost {lost})"
4907 );
48674908 close_section(section);
48684909 library.flush(Duration::from_secs(20)).unwrap();
48694910 drop(library);
4911 // Every upload merged: none is kept beside the file, nor twice.
4912 assert_eq!(beside(&phone), 0);
48704913
48714914 let shown = every_page(&phone, &folder("phone-cache"));
48724915 for typed in [
......@@ -4881,13 +4924,158 @@ fn an_icloud_notebook_merges_the_phones_upload_back_at_launch() {
48814924 }
48824925}
48834926
4927/// A notebook Snowbound made in a scratch folder standing in for iCloud Drive's, opened as
4928/// the phone opens one there, and its one section.
4929fn cloud_notebook(directory: &Path) -> (PathBuf, PathBuf, String) {
4930 let (root, cache) = (directory.join("Notes"), directory.join("cache"));
4931 let creation = onestore::PageCreation::new(None, Some("Shared page"), "Clover").unwrap();
4932 let notebook =
4933 notebook::session::Notebook::create(&root, &cache, 0x00f0_a0c0, &creation).unwrap();
4934 let path = notebook.catalog().sections[0].path.clone();
4935 (root, cache, path)
4936}
4937
4938/// Ages the images the cache keeps past the time reads get to catch up with an upload.
4939fn aged(cache: &Path) {
4940 let folders = std::fs::read_dir(cache.join("replicas")).unwrap();
4941 for folder in folders {
4942 for entry in std::fs::read_dir(folder.unwrap().path()).unwrap() {
4943 let store = entry.unwrap().path();
4944 if store
4945 .extension()
4946 .is_none_or(|extension| extension != "published")
4947 {
4948 continue;
4949 }
4950 for image in std::fs::read_dir(store).unwrap() {
4951 let file = std::fs::File::options()
4952 .write(true)
4953 .open(image.unwrap().path())
4954 .unwrap();
4955 file.set_modified(std::time::SystemTime::now() - Duration::from_secs(61))
4956 .unwrap();
4957 }
4958 }
4959 }
4960}
4961
4962/// iCloud Drive puts back the file as it stood before the phone's upload, as when another
4963/// device's upload made on that state wins: the phone's edits merge back once reads have had a
4964/// minute to catch up with its upload, not before.
4965#[test]
4966fn an_upload_icloud_undid_merges_back_once_reads_have_caught_up() {
4967 unsafe { library::sb_set_versions(cloud_versions, cloud_retire, c"iPhone".as_ptr()) };
4968 let directory = tempfile::tempdir().unwrap();
4969 let (root, cache, path) = cloud_notebook(directory.path());
4970 let library = cloud_library(&root, &cache);
4971 let section = cloud_section(&library, &path);
4972 let page = listed_ids(&section)[0];
4973 type_on(&section, page, "Before ");
4974 assert!(section.flush(Duration::from_secs(20)));
4975 let before = std::fs::read(root.join(&path)).unwrap();
4976 type_on(&section, page, "Phone words ");
4977 assert!(section.flush(Duration::from_secs(20)));
4978 std::fs::write(root.join(&path), before).unwrap();
4979 for _ in 0..20 {
4980 section.shared.section.wake();
4981 std::thread::sleep(Duration::from_millis(50));
4982 }
4983 assert!(!stored(&root).contains("Phone words"));
4984 aged(&cache);
4985 assert!(eventually(|| {
4986 section.shared.section.wake();
4987 stored(&root).contains("Phone words")
4988 }));
4989 close_section(section);
4990}
4991
4992/// A notebook on this device alone loses no upload, and keeps nothing of its publications.
4993#[test]
4994fn a_notebook_on_the_phone_alone_keeps_no_uploads() {
4995 unsafe { library::sb_set_versions(cloud_versions, cloud_retire, c"iPhone".as_ptr()) };
4996 let directory = tempfile::tempdir().unwrap();
4997 let (root, cache, path) = cloud_notebook(directory.path());
4998 let library = Arc::new(Library::open(&root, &cache, true).unwrap());
4999 let section = cloud_section(&library, &path);
5000 type_on(&section, listed_ids(&section)[0], "Phone words ");
5001 assert!(section.flush(Duration::from_secs(20)));
5002 assert!(stored(&root).contains("Phone words"));
5003 assert!(kept(&cache).is_empty());
5004 close_section(section);
5005}
5006
5007/// Another device sets a password on the section: the file is written anew under a new
5008/// identity, and the plaintext the phone kept of its uploads goes with the replica it backed.
5009#[test]
5010fn a_password_set_elsewhere_takes_the_plaintext_kept() {
5011 unsafe { library::sb_set_versions(cloud_versions, cloud_retire, c"iPhone".as_ptr()) };
5012 let directory = tempfile::tempdir().unwrap();
5013 let (root, cache, path) = cloud_notebook(directory.path());
5014 let library = cloud_library(&root, &cache);
5015 let section = cloud_section(&library, &path);
5016 type_on(&section, listed_ids(&section)[0], "Secret words ");
5017 close_section(section);
5018 assert!(!kept(&cache).is_empty());
5019
5020 let other = directory.path().join("other");
5021 let copied = std::process::Command::new("cp")
5022 .arg("-R")
5023 .arg(&root)
5024 .arg(&other)
5025 .status();
5026 assert!(copied.unwrap().success());
5027 let mut notebook =
5028 notebook::session::Notebook::open(&other, directory.path().join("other-cache")).unwrap();
5029 notebook
5030 .set_password(&path, None, Some("password"))
5031 .unwrap();
5032 drop(notebook);
5033 for entry in std::fs::read_dir(&other).unwrap() {
5034 let entry = entry.unwrap();
5035 if entry.path().is_file() {
5036 std::fs::copy(entry.path(), root.join(entry.file_name())).unwrap();
5037 }
5038 }
5039 let tabs = library.tabs().unwrap();
5040 assert!(tabs.iter().any(|tab| tab.path == path && tab.locked));
5041 assert!(kept(&cache).is_empty());
5042}
5043
5044/// A copy of a section made while the notebook is open keeps what it publishes of its own,
5045/// beside its own replica, never in place of the original's.
5046#[test]
5047fn a_copy_found_while_open_keeps_its_own_uploads() {
5048 unsafe { library::sb_set_versions(cloud_versions, cloud_retire, c"iPhone".as_ptr()) };
5049 let directory = tempfile::tempdir().unwrap();
5050 let (root, cache, path) = cloud_notebook(directory.path());
5051 let library = cloud_library(&root, &cache);
5052 let section = cloud_section(&library, &path);
5053 type_on(&section, listed_ids(&section)[0], "Original words ");
5054 close_section(section);
5055 let original = kept(&cache);
5056 assert_eq!(original.len(), 1);
5057 std::fs::copy(root.join(&path), root.join("Copy.one")).unwrap();
5058 assert!(
5059 library
5060 .tabs()
5061 .unwrap()
5062 .iter()
5063 .any(|tab| tab.path == "Copy.one")
5064 );
5065 let copy = cloud_section(&library, "Copy.one");
5066 type_on(&copy, listed_ids(&copy)[0], "Copy words ");
5067 close_section(copy);
5068 let images = kept(&cache);
5069 assert_eq!(images.len(), 2);
5070 assert!(images.contains(&original[0]));
5071 assert!(images.contains(&std::fs::read(root.join("Copy.one")).unwrap()));
5072}
5073
48845074/// The phone's upload to an iCloud Drive notebook is lost after the phone renamed the
48855075/// notebook: the images its publications keep move with it, and the next launch merges it back.
48865076#[test]
48875077fn an_upload_lost_after_the_notebook_is_renamed_merges_back() {
4888 unsafe {
4889 library::sb_set_versions(cloud_versions, cloud_retire, c"Clover’s iPhone".as_ptr())
4890 };
5078 unsafe { library::sb_set_versions(cloud_versions, cloud_retire, c"iPhone".as_ptr()) };
48915079 let directory = tempfile::tempdir().unwrap();
48925080 let folder = |name: &str| directory.path().join(name);
48935081 let (phone, mac, cache) = (folder("Notes"), folder("mac"), folder("phone-cache"));
......@@ -4916,7 +5104,8 @@ fn an_upload_lost_after_the_notebook_is_renamed_merges_back() {
49165104 let section = cloud_section(&library, &path);
49175105 type_on(&section, page, typed);
49185106 close_section(section);
4919 library.flush(Duration::from_secs(20)).unwrap();
5107 // As the app closes a notebook before renaming it (Lists.swift).
5108 library.close(Duration::from_secs(20)).unwrap();
49205109 }
49215110
49225111 // Renamed in the app, as Arranging.swift renames a notebook's folder.
......@@ -5025,7 +5214,9 @@ fn main_thread_calls_return_while_icloud_holds_the_section_file() {
50255214 let file = root.canonicalize().unwrap().join(&path);
50265215 let (entered, held) = std::sync::mpsc::channel();
50275216 let (release, released) = std::sync::mpsc::channel();
5028 *HELD.lock().unwrap() = Some((file.to_string_lossy().into_owned(), entered, released));
5217 HELD.lock()
5218 .unwrap()
5219 .push((file.to_string_lossy().into_owned(), entered, released));
50295220 type_on(&section, space, "Typed while iCloud downloads ");
50305221 sb_library_sync_now(&library);
50315222 held.recv_timeout(Duration::from_secs(10)).unwrap();
crates/notebook/src/background.rs+23-19
......@@ -144,7 +144,7 @@ impl Background {
144144 ) -> Result<Self>
145145 where
146146 R: Remote,
147 B: FnMut(&str) -> R,
147 B: FnMut(&str, Option<&Path>) -> R,
148148 L: FnMut(String) -> LF,
149149 LF: Future<Output = io::Result<Vec<Entry>>>,
150150 {
......@@ -262,7 +262,7 @@ impl Background {
262262 continue;
263263 };
264264 let (queued, outcome) = step_async(
265 &mut bind(&path),
265 &mut bind(&path, replica.as_deref()),
266266 replica.as_deref(),
267267 seen.as_deref(),
268268 current,
......@@ -360,7 +360,7 @@ impl Background {
360360 Err(error) => return Err(error),
361361 };
362362 let (bound, folder) = (Arc::clone(&client), root.clone());
363 let bind = move |path: &str| {
363 let bind = move |path: &str, _: Option<&Path>| {
364364 let file = match folder.as_str() {
365365 "" => path.to_owned(),
366366 root => format!("{root}/{path}"),
......@@ -394,7 +394,9 @@ impl Background {
394394 move |reports| {
395395 guest.watch(reports)?;
396396 let (bound, listing) = (Arc::clone(&guest), Arc::clone(&guest));
397 let bind = move |path: &str| crate::live::share::HostedRemote::new(&bound, path);
397 let bind = move |path: &str, _: Option<&Path>| {
398 crate::live::share::HostedRemote::new(&bound, path)
399 };
398400 let list = move |folder: String| {
399401 let listing = listing.clone();
400402 async move { listing.entries_async(&folder).await }
......@@ -418,7 +420,9 @@ impl Background {
418420 true,
419421 move |_| {
420422 let (bound, listing) = (Arc::clone(&documents), Arc::clone(&documents));
421 let bind = move |path: &str| crate::provider::ProviderRemote::new(&bound, path);
423 let bind = move |path: &str, replica: Option<&Path>| {
424 crate::provider::ProviderRemote::new(&bound, path, replica)
425 };
422426 let list = move |folder: String| std::future::ready(listing.entries(&folder));
423427 Ok(((bind, list), false))
424428 },
......@@ -909,17 +913,17 @@ async fn list<F: Future<Output = io::Result<Vec<Entry>>>>(
909913 listings
910914}
911915
912/// Deletes the replica at `replica` if it holds nothing unpublished and no one holds it.
916/// Deletes the replica at `replica` if it holds nothing unpublished and no one holds it. One
917/// whose section keeps images of its publications stays as an edit waiting keeps it: an image
918/// may be an upload the file lost, which merges back once the notebook opens again.
913919fn discard(replica: &Path) {
914 if matches!(
915 crate::closed(replica).and_then(|held| crate::peek(&held)),
916 Ok((_, 0))
917 ) {
918 for suffix in ["-wal", "-shm", ""] {
919 let mut file = replica.as_os_str().to_owned();
920 file.push(suffix);
921 let _ = fs::remove_file(file);
922 }
920 if !crate::published::keeps(replica)
921 && matches!(
922 crate::closed(replica).and_then(|held| crate::peek(&held)),
923 Ok((_, 0))
924 )
925 {
926 let _ = crate::remove_replica(replica);
923927 }
924928}
925929
......@@ -1448,7 +1452,7 @@ mod tests {
14481452 move |reports| {
14491453 armed.send(reports).unwrap();
14501454 let (files, listed) = (remote.clone(), remote.clone());
1451 let bind = move |path: &str| File(files.clone(), path.to_owned());
1455 let bind = move |path: &str, _: Option<&Path>| File(files.clone(), path.to_owned());
14521456 let list = move |folder: String| std::future::ready(Ok(listed.list(&folder)));
14531457 Ok(((bind, list), true))
14541458 },
......@@ -1524,7 +1528,7 @@ mod tests {
15241528 move |reports| {
15251529 armed.send(reports).unwrap();
15261530 let (files, listed) = (remote.clone(), remote.clone());
1527 let bind = move |path: &str| File(files.clone(), path.to_owned());
1531 let bind = move |path: &str, _: Option<&Path>| File(files.clone(), path.to_owned());
15281532 let list = move |folder: String| std::future::ready(Ok(listed.list(&folder)));
15291533 Ok(((bind, list), true))
15301534 },
......@@ -1575,7 +1579,7 @@ mod tests {
15751579 false,
15761580 move |_| {
15771581 let (files, listed) = (remote.clone(), remote.clone());
1578 let bind = move |path: &str| File(files.clone(), path.to_owned());
1582 let bind = move |path: &str, _: Option<&Path>| File(files.clone(), path.to_owned());
15791583 let list = move |folder: String| std::future::ready(Ok(listed.list(&folder)));
15801584 Ok(((bind, list), true))
15811585 },
......@@ -1674,7 +1678,7 @@ mod tests {
16741678 move |reports| {
16751679 armed.send(reports).unwrap();
16761680 let (files, listed) = (remote.clone(), remote.clone());
1677 let bind = move |path: &str| File(files.clone(), path.to_owned());
1681 let bind = move |path: &str, _: Option<&Path>| File(files.clone(), path.to_owned());
16781682 let list = move |folder: String| std::future::ready(Ok(listed.list(&folder)));
16791683 Ok(((bind, list), true))
16801684 },
crates/notebook/src/lib.rs+22
......@@ -153,6 +153,23 @@ impl Drop for Released {
153153 }
154154}
155155
156/// Deletes the closed replica at `replica`, with the files SQLite keeps beside it and the
157/// images its section's publications keep (`published`).
158pub(crate) fn remove_replica(replica: &Path) -> io::Result<()> {
159 for suffix in ["", "-wal", "-shm"] {
160 let mut file = replica.as_os_str().to_owned();
161 file.push(suffix);
162 match fs::remove_file(file) {
163 Err(error) if error.kind() != io::ErrorKind::NotFound => return Err(error),
164 _ => {}
165 }
166 }
167 match fs::remove_dir_all(published::folder(replica)) {
168 Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error),
169 _ => Ok(()),
170 }
171}
172
156173impl Replica {
157174 /// Seeds a new cache from a validated section image, refusing any existing path.
158175 /// An initialization error preserves the created file for inspection.
......@@ -286,6 +303,11 @@ impl Replica {
286303 lock(&self.section.cache)
287304 }
288305
306 /// Where the cache is.
307 pub fn path(&self) -> Option<PathBuf> {
308 self.lock().ok()?.path().map(PathBuf::from)
309 }
310
289311 /// Hands `request` to the section thread.
290312 fn send(&self, request: working::Request) -> Result<()> {
291313 self.section.send(request)
crates/notebook/src/location.rs+33-49
......@@ -41,29 +41,37 @@ pub fn smb(address: &str, share: &str, root: &str) -> String {
4141 format!("smb://{address}/{share}/{}", root.trim_matches('/'))
4242}
4343
44/// Moves the replicas of the sections at `from` to `to`, and the images their publications
44/// Moves the replicas of the sections at `from` to `to`, with the images their publications
4545/// keep (`published`), as when the app renames or moves a notebook; with them those of every
4646/// location within it, as a copy of a section keeps its own. Each replica `to` already holds
4747/// for the same section stays. Every replica moved must be closed.
4848pub 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();
49 let inner = within(cache, from);
5650 move_folder(cache, from, to)?;
57 for location in inner {
51 for (_, location) in inner {
5852 move_folder(
5953 cache,
6054 &location,
61 &format!("{to}/{}", &location[within.len()..]),
55 &format!("{to}{}", &location[from.len()..]),
6256 )?;
6357 }
6458 Ok(())
6559}
6660
61/// The folders of the locations within `location`, labelled, with their locations.
62fn within(cache: &Path, location: &str) -> Vec<(PathBuf, String)> {
63 let within = format!("{location}/");
64 fs::read_dir(cache.join("replicas"))
65 .into_iter()
66 .flatten()
67 .filter_map(|entry| {
68 let folder = entry.ok()?.path();
69 let label = fs::read_to_string(folder.join(LOCATION)).ok()?;
70 label.starts_with(&within).then_some((folder, label))
71 })
72 .collect()
73}
74
6775fn move_folder(cache: &Path, from: &str, to: &str) -> io::Result<()> {
6876 let old = folder(cache, from);
6977 if fs::metadata(&old).is_err() {
......@@ -78,50 +86,22 @@ fn move_folder(cache: &Path, from: &str, to: &str) -> io::Result<()> {
7886 adopt(&old.join(&name), &new.join(&name))?;
7987 }
8088 }
81 if let Ok(images) = fs::read_dir(old.join(PUBLISHED)) {
82 fs::create_dir_all(new.join(PUBLISHED))?;
83 for entry in images {
84 let name = entry?.file_name();
85 let image = new.join(PUBLISHED).join(&name);
86 if fs::metadata(&image).is_err() {
87 fs::rename(old.join(PUBLISHED).join(&name), image)?;
88 }
89 }
90 let _ = fs::remove_dir(old.join(PUBLISHED));
91 }
9289 tidy(&old);
9390 Ok(())
9491}
9592
96/// The folder of a replica folder holding the images its sections' publications keep.
97pub const PUBLISHED: &str = "published";
98
99/// The replica folder of the section file at location `file` in the notebook at location
100/// `notebook`: the file's own where one was claimed, as a copy's and a lone section's are,
101/// else the notebook's.
102pub fn section_folder(cache: &Path, notebook: &str, file: &str) -> PathBuf {
103 let own = folder(cache, file);
104 if fs::metadata(own.join(LOCATION)).is_ok() {
105 own
106 } else {
107 folder(cache, notebook)
108 }
109}
110
11193/// Deletes what the cache keeps of the notebook at `location`, as when the app deletes the
11294/// notebook: its replicas, edits waiting in them included, its sections' copies' and its
11395/// listing. Every replica must be closed.
11496pub fn forget(cache: &Path, location: &str) -> io::Result<()> {
115 let within = format!("{location}/");
116 if let Ok(entries) = fs::read_dir(cache.join("replicas")) {
117 for entry in entries {
118 let folder = entry?.path();
119 let ours = fs::read_to_string(folder.join(LOCATION))
120 .is_ok_and(|label| label == location || label.starts_with(&within));
121 if ours {
122 fs::remove_dir_all(&folder)?;
123 }
124 }
97 let own = folder(cache, location);
98 let ours = fs::read_to_string(own.join(LOCATION)).is_ok_and(|label| label == location);
99 for folder in ours.then_some(own).into_iter().chain(
100 within(cache, location)
101 .into_iter()
102 .map(|(folder, _)| folder),
103 ) {
104 fs::remove_dir_all(&folder)?;
125105 }
126106 match fs::remove_file(crate::session::listing(cache, location)) {
127107 Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error),
......@@ -130,7 +110,7 @@ pub fn forget(cache: &Path, location: &str) -> io::Result<()> {
130110}
131111
132112/// The folder for `location`, made and labelled with it where it was not.
133fn claimed(cache: &Path, location: &str) -> io::Result<PathBuf> {
113pub fn claimed(cache: &Path, location: &str) -> io::Result<PathBuf> {
134114 let folder = folder(cache, location);
135115 let label = folder.join(LOCATION);
136116 if fs::metadata(&label).is_err() {
......@@ -223,7 +203,8 @@ fn follows(file: &Stamp, base: &Stamp) -> bool {
223203 )
224204}
225205
226/// Moves the replica at `from`, with the write-ahead log a crash may have left, to `to`.
206/// Moves the replica at `from`, with the write-ahead log a crash may have left and the images
207/// its publications keep, to `to`.
227208fn adopt(from: &Path, to: &Path) -> io::Result<()> {
228209 for suffix in ["-wal", "-shm", ""] {
229210 let (mut source, mut target) = (from.as_os_str().to_owned(), to.as_os_str().to_owned());
......@@ -234,7 +215,10 @@ fn adopt(from: &Path, to: &Path) -> io::Result<()> {
234215 moved => moved?,
235216 }
236217 }
237 Ok(())
218 match fs::rename(crate::published::folder(from), crate::published::folder(to)) {
219 Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error),
220 _ => Ok(()),
221 }
238222}
239223
240224/// Removes a replica folder that holds no replica, with the owner lock a crash may have left
crates/notebook/src/provider.rs+25-29
......@@ -6,7 +6,7 @@
66
77use crate::{
88 Error, Result, discover,
9 published::{Published, Written},
9 published::{Published, Written, whole},
1010 session::Storage,
1111};
1212use onestore::{CommitError, CommitIo, CommitState, Stamp, Store, Transaction};
......@@ -14,7 +14,7 @@ use std::{
1414 collections::BTreeMap,
1515 fs::File,
1616 io::{self, Read, Seek, SeekFrom, Write},
17 path::PathBuf,
17 path::{Path, PathBuf},
1818 sync::{Arc, Mutex, MutexGuard, PoisonError},
1919};
2020
......@@ -78,12 +78,6 @@ impl Documents {
7878 format!("provider:{}", self.root)
7979 }
8080
81 /// Where this device keeps the images it published until the documents are seen holding
82 /// them: a provider without compare-and-swap may let another device's upload replace one.
83 fn published(&self) -> PathBuf {
84 crate::location::folder(&self.cache, &self.location()).join(crate::location::PUBLISHED)
85 }
86
8781 fn id(&self, path: &str) -> io::Result<String> {
8882 if path.is_empty() {
8983 return Ok(self.root.clone());
......@@ -288,15 +282,6 @@ fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
288282 mutex.lock().unwrap_or_else(PoisonError::into_inner)
289283}
290284
291fn whole(file: &mut File, limit: usize) -> io::Result<Vec<u8>> {
292 let mut bytes = Vec::new();
293 file.take(limit as u64 + 1).read_to_end(&mut bytes)?;
294 if bytes.len() > limit {
295 return Err(io::ErrorKind::FileTooLarge.into());
296 }
297 Ok(bytes)
298}
299
300285/// A document as a commit writes it: its seekable descriptor, or its image to write whole.
301286enum Io {
302287 File(File),
......@@ -349,21 +334,22 @@ impl CommitIo for Io {
349334}
350335
351336/// A section a document provider keeps, as its replica publishes to it, each image it
352/// publishes kept until the document holds it (`published.rs`).
337/// publishes kept beside the replica until the document holds it (`published.rs`).
353338pub(crate) struct ProviderRemote {
354339 documents: Arc<Documents>,
355340 path: String,
356 published: Published,
341 /// None for a section with no replica yet, which publishes nothing.
342 published: Option<Published>,
357343 /// The image the step read whole for a stamp, which its read takes.
358344 read: Option<Vec<u8>>,
359345}
360346
361347impl ProviderRemote {
362 pub(crate) fn new(documents: &Arc<Documents>, path: &str) -> Self {
348 pub(crate) fn new(documents: &Arc<Documents>, path: &str, replica: Option<&Path>) -> Self {
363349 Self {
364350 documents: Arc::clone(documents),
365351 path: path.to_owned(),
366 published: Published::new(documents.published(), Some(documents.device.clone())),
352 published: replica.map(|replica| Published::of(replica, &documents.device)),
367353 read: None,
368354 }
369355 }
......@@ -372,10 +358,8 @@ impl ProviderRemote {
372358 /// put back and read again: the newest image kept that the torn bytes begin. Any other
373359 /// document that does not parse is left as it is.
374360 fn repair(&self, torn: &[u8]) -> io::Result<Vec<u8>> {
375 let image = self
376 .published
377 .torn(torn)?
378 .ok_or(io::ErrorKind::WouldBlock)?;
361 let published = self.published.as_ref().ok_or(io::ErrorKind::WouldBlock)?;
362 let image = published.torn(torn)?.ok_or(io::ErrorKind::WouldBlock)?;
379363 let id = self.documents.id(&self.path)?;
380364 self.documents.writable(&id)?;
381365 self.documents
......@@ -420,14 +404,23 @@ impl crate::Remote for ProviderRemote {
420404 self.documents.write(
421405 &self.path,
422406 |io| transaction.commit(io),
423 |written| self.published.keep(written, transaction),
407 |written| match &self.published {
408 Some(published) => published.keep(written, transaction),
409 None => Ok(()),
410 },
424411 )
425412 }
426413
427414 fn confirm(&mut self, base: &Stamp) -> std::result::Result<(), CommitError> {
428415 self.read = None;
429 self.documents
430 .write(&self.path, |io| onestore::confirm(io, base), |_| Ok(()))
416 self.documents.write(
417 &self.path,
418 |io| onestore::confirm(io, base),
419 |written| match &self.published {
420 Some(published) => published.confirmed(written, base),
421 None => Ok(()),
422 },
423 )
431424 }
432425
433426 /// The images kept of this document other than the one it stands as
......@@ -441,7 +434,10 @@ impl crate::Remote for ProviderRemote {
441434 (stamp, image) = (repaired_stamp, Some(repaired));
442435 }
443436 self.read = image;
444 Ok(self.published.versions(&stamp))
437 match &self.published {
438 Some(published) => published.versions(|| Ok(stamp), &[]),
439 None => Ok(Vec::new()),
440 }
445441 }
446442
447443 fn version(&mut self, id: &str) -> io::Result<Vec<u8>> {
crates/notebook/src/provider/tests.rs+19-13
......@@ -899,8 +899,8 @@ fn a_provider_that_lists_but_cannot_open_opens_sections_from_their_replicas() {
899899 section.close().unwrap();
900900}
901901
902/// The images a device keeps of what it published, for every notebook in its cache.
903fn kept_images(fake: &Fake) -> Vec<String> {
902/// Where a device keeps the images it published, beside each replica in its cache.
903fn stores(fake: &Fake) -> Vec<std::path::PathBuf> {
904904 let cache = fake
905905 .store
906906 .folder
......@@ -908,7 +908,21 @@ fn kept_images(fake: &Fake) -> Vec<String> {
908908 .join(format!("cache-{}", fake.device));
909909 let folders = fs::read_dir(cache.join("replicas")).into_iter().flatten();
910910 folders
911 .filter_map(|folder| fs::read_dir(folder.ok()?.path().join("published")).ok())
911 .filter_map(|folder| fs::read_dir(folder.ok()?.path()).ok())
912 .flatten()
913 .map(|entry| entry.unwrap().path())
914 .filter(|path| {
915 path.extension()
916 .is_some_and(|extension| extension == "published")
917 })
918 .collect()
919}
920
921/// The images a device keeps of what it published, for every notebook in its cache.
922fn kept_images(fake: &Fake) -> Vec<String> {
923 stores(fake)
924 .into_iter()
925 .filter_map(|store| fs::read_dir(store).ok())
912926 .flatten()
913927 .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
914928 .filter(|name| name.ends_with(".one"))
......@@ -962,16 +976,8 @@ fn a_publication_another_devices_upload_replaced_merges_back() {
962976
963977/// Ages the images a device keeps past `LAG`, as if reads had long had time to catch up.
964978fn aged(fake: &Fake) {
965 let cache = fake
966 .store
967 .folder
968 .path()
969 .join(format!("cache-{}", fake.device));
970 for folder in fs::read_dir(cache.join("replicas")).unwrap() {
971 for image in fs::read_dir(folder.unwrap().path().join("published"))
972 .into_iter()
973 .flatten()
974 {
979 for store in stores(fake) {
980 for image in fs::read_dir(store).into_iter().flatten() {
975981 let file = File::options()
976982 .write(true)
977983 .open(image.unwrap().path())
crates/notebook/src/published.rs+253-52
......@@ -1,8 +1,9 @@
11//! The images this device publishes to section files a provider syncs whole, with no lock or
22//! compare-and-swap, as iCloud Drive and Android's document providers do: another device's
3//! upload can replace this one's. Each image is kept until the file is seen holding it, and
4//! one the file lost is listed as a `Remote::versions` version, so that it merges back as a
5//! conflict version does (`resolve.rs`).
3//! upload can replace this one's. Each image is kept beside the section's replica until the
4//! file is seen holding it, and one the file lost is listed as a `Remote::versions` version,
5//! so that it merges back as a conflict version does (`resolve.rs`). They move and go with
6//! the replica (`location::moved`, `remove_replica`).
67
78use crate::Version;
89use onestore::{Stamp, Transaction};
......@@ -23,13 +24,13 @@ pub enum Written<'a> {
2324 Image(&'a [u8]),
2425}
2526
26/// The images this device published to the files of one place, kept in `folder` and named
27/// `<document>-<image>-<base>.one`: the file's identity, then the digests of the image's stamp
28/// and of the stamp of the image it was published on. Version ids are their paths.
27/// The images this device published to one section file, named `<document>-<image>-<base>.one`:
28/// the file's identity, then the digests of the image's stamp and of the stamp of the image it
29/// was published on. Version ids are their paths.
2930pub struct Published {
3031 folder: PathBuf,
3132 /// This device, as the conflict pages a lost publication makes name it.
32 device: Option<String>,
33 device: String,
3334}
3435
3536struct Kept {
......@@ -40,21 +41,22 @@ struct Kept {
4041 at: SystemTime,
4142}
4243
44/// The folder beside the replica at `replica` holding its section's images.
45pub(crate) fn folder(replica: &Path) -> PathBuf {
46 replica.with_extension("published")
47}
48
4349impl Published {
44 pub fn new(folder: impl Into<PathBuf>, device: Option<String>) -> Self {
50 /// The images kept of the section whose replica is at `replica`.
51 pub fn of(replica: &Path, device: impl Into<String>) -> Self {
4552 Self {
46 folder: folder.into(),
47 device,
53 folder: folder(replica),
54 device: device.into(),
4855 }
4956 }
5057
51 /// The images kept of the section file `file` of the notebook folder, or lone section, at
52 /// `notebook`, which `cache` holds the replicas of: beside the section's replica, so that
53 /// they move with the notebook (`location::moved`).
54 pub fn of(cache: &Path, notebook: &Path, file: &Path, device: Option<String>) -> Option<Self> {
55 use crate::location::{PUBLISHED, local, section_folder};
56 let folder = section_folder(cache, &local(notebook).ok()?, &local(file).ok()?);
57 Some(Self::new(folder.join(PUBLISHED), device))
58 pub fn device(&self) -> &str {
59 &self.device
5860 }
5961
6062 fn kept(&self) -> Vec<Kept> {
......@@ -75,6 +77,19 @@ impl Published {
7577 .collect()
7678 }
7779
80 fn put(&self, image: &[u8], name: String, replacing: Option<Kept>) -> io::Result<()> {
81 crate::fs::create_dir_all(&self.folder)?;
82 let mut kept = tempfile::NamedTempFile::new_in(&self.folder)?;
83 kept.write_all(image)?;
84 kept.as_file().sync_all()?;
85 kept.persist(self.folder.join(name))
86 .map_err(|error| error.error)?;
87 if let Some(kept) = replacing {
88 crate::fs::remove_file(kept.path)?;
89 }
90 Ok(())
91 }
92
7893 /// Keeps the image `transaction` published, in place of the one it was published on:
7994 /// that kept image with the transaction applied where this device kept it, else read back.
8095 pub fn keep(&self, written: Written<'_>, transaction: &Transaction) -> io::Result<()> {
......@@ -93,31 +108,46 @@ impl Published {
93108 }
94109 (Written::InPlace(file), None) => {
95110 file.seek(SeekFrom::Start(0))?;
96 let mut image = Vec::new();
97 file.take(crate::MAX_FILE_BYTES as u64 + 1)
98 .read_to_end(&mut image)?;
99 if image.len() > crate::MAX_FILE_BYTES {
100 return Err(io::ErrorKind::FileTooLarge.into());
101 }
102 image
111 whole(file, crate::MAX_FILE_BYTES)?
103112 }
104113 };
105114 let name = format!("{document}-{}-{base}.one", digest(&transaction.stamp()));
106 crate::fs::create_dir_all(&self.folder)?;
107 let mut kept = tempfile::NamedTempFile::new_in(&self.folder)?;
108 kept.write_all(&image)?;
109 kept.as_file().sync_all()?;
110 kept.persist(self.folder.join(name))
111 .map_err(|error| error.error)?;
112 if let Some(kept) = on {
113 crate::fs::remove_file(kept.path)?;
115 self.put(&image, name, on)
116 }
117
118 /// Follows the image kept as `base` through a confirmation (`onestore::confirm`), which
119 /// rewrites only the header: the image `written` now begins with.
120 pub fn confirmed(&self, written: Written<'_>, base: &Stamp) -> io::Result<()> {
121 let image = digest(base);
122 let Some(kept) = (self.kept().into_iter()).find(|kept| kept.image == image) else {
123 return Ok(());
124 };
125 let mut header = [0; 1024];
126 match written {
127 Written::InPlace(file) => {
128 file.seek(SeekFrom::Start(0))?;
129 file.read_exact(&mut header)?;
130 }
131 Written::Image(image) => {
132 header.copy_from_slice(image.get(..1024).ok_or(io::ErrorKind::UnexpectedEof)?)
133 }
114134 }
115 Ok(())
135 let mut confirmed = crate::fs::read(&kept.path)?;
136 confirmed
137 .get_mut(..1024)
138 .ok_or(io::ErrorKind::InvalidData)?
139 .copy_from_slice(&header);
140 let stamp = Stamp {
141 header,
142 length: confirmed.len() as u64,
143 };
144 let name = format!("{}-{}-{}.one", kept.document, digest(&stamp), kept.base);
145 self.put(&confirmed, name, Some(kept))
116146 }
117147
118148 /// The newest image kept that `torn` begins, as a provider writing in place leaves this
119149 /// device's upload cut off.
120 pub fn torn(&self, torn: &[u8]) -> io::Result<Option<Vec<u8>>> {
150 pub(crate) fn torn(&self, torn: &[u8]) -> io::Result<Option<Vec<u8>>> {
121151 let mut newest = None;
122152 for kept in self.kept() {
123153 let image = crate::fs::read(&kept.path)?;
......@@ -128,28 +158,60 @@ impl Published {
128158 Ok(newest.map(|(_, image)| image))
129159 }
130160
131 /// The images kept of the file `stamp` stamps, other than the one it stands as; one whose
132 /// upload the file still stands before is listed only once reads have had `LAG` to catch up.
133 pub fn versions(&self, stamp: &Stamp) -> Vec<Version> {
161 /// The images kept other than the one the file stands as, by `stamp`, which is read only
162 /// where any is kept; one whose upload the file still stands before is listed only once
163 /// reads have had `LAG` to catch up. One the provider keeps as a conflict version too, in
164 /// `conflicts`, is left to that version, so that it is not merged, or kept beside, twice;
165 /// none is listed while a conflict version cannot be read.
166 pub fn versions(
167 &self,
168 stamp: impl FnOnce() -> io::Result<Stamp>,
169 conflicts: &[Version],
170 ) -> io::Result<Vec<Version>> {
171 let kept = self.kept();
172 if kept.is_empty() {
173 return Ok(Vec::new());
174 }
175 let stamp = stamp()?;
134176 // A header mid-write names no file.
135 let Some(document) = document(stamp) else {
136 return Vec::new();
177 let Some(document) = document(&stamp) else {
178 return Ok(Vec::new());
137179 };
138 let current = digest(stamp);
180 let current = digest(&stamp);
139181 let lagging = |kept: &Kept| {
140182 kept.base == current && kept.at.elapsed().is_ok_and(|elapsed| elapsed < LAG)
141183 };
142 (self.kept().into_iter())
143 .filter(|kept| kept.document == document && kept.image != current && !lagging(kept))
184 // A conflict version not read yet, as one iCloud Drive has not brought down, may be
185 // one of these: they wait for it, rather than be merged or kept beside the file twice.
186 let Ok(conflicts) = (conflicts.iter())
187 .map(|version| Ok(digest(&file_stamp(Path::new(&version.id))?)))
188 .collect::<io::Result<Vec<String>>>()
189 else {
190 return Ok(Vec::new());
191 };
192 Ok(kept
193 .into_iter()
194 .filter(|kept| {
195 kept.document == document
196 && kept.image != current
197 && !lagging(kept)
198 && !conflicts.contains(&kept.image)
199 })
144200 .map(|kept| Version {
145201 id: kept.path.to_string_lossy().into_owned(),
146 device: self.device.clone(),
202 device: Some(self.device.clone()),
147203 })
148 .collect()
204 .collect())
149205 }
150206
151 pub fn device(&self) -> Option<&str> {
152 self.device.as_deref()
207 /// The provider's conflict version at `version` is retired, merged or kept beside the
208 /// file: an image kept here that it is goes with it, so that it is not merged again.
209 pub fn retired(&self, version: &Path) -> io::Result<()> {
210 let image = digest(&file_stamp(version)?);
211 for kept in self.kept().into_iter().filter(|kept| kept.image == image) {
212 crate::fs::remove_file(kept.path)?;
213 }
214 Ok(())
153215 }
154216
155217 /// Whether version `id` is an image kept here.
......@@ -158,18 +220,48 @@ impl Published {
158220 }
159221}
160222
223/// The stamp of the file at `path`: its header and length.
224fn file_stamp(path: &Path) -> io::Result<Stamp> {
225 let mut file = File::open(path)?;
226 let mut header = [0; 1024];
227 file.read_exact(&mut header)?;
228 Ok(Stamp {
229 header,
230 length: file.metadata()?.len(),
231 })
232}
233
234/// Whether the section whose replica is at `replica` keeps images of its publications.
235pub(crate) fn keeps(replica: &Path) -> bool {
236 crate::fs::read_dir(folder(replica)).is_ok_and(|mut images| images.next().is_some())
237}
238
161239/// Where a version of `file` that cannot merge is kept beside it, as iCloud Drive keeps a
162240/// conflict version: "Name (Device).one", numbered past any name taken.
163241pub fn beside(file: &Path, device: &str) -> PathBuf {
164242 let stem = file.file_stem().unwrap_or_default().to_string_lossy();
165 (1..)
166 .map(|n| match n {
243 let mut n = 1;
244 loop {
245 let name = match n {
167246 1 => format!("{stem} ({device}).one"),
168247 n => format!("{stem} ({device} {n}).one"),
169 })
170 .map(|name| file.with_file_name(name))
171 .find(|beside| std::fs::symlink_metadata(beside).is_err())
172 .unwrap_or_default()
248 };
249 let beside = file.with_file_name(name);
250 if std::fs::symlink_metadata(&beside).is_err() {
251 return beside;
252 }
253 n += 1;
254 }
255}
256
257/// All of `file`, at most `limit` bytes.
258pub(crate) fn whole(file: &mut File, limit: usize) -> io::Result<Vec<u8>> {
259 let mut bytes = Vec::new();
260 file.take(limit as u64 + 1).read_to_end(&mut bytes)?;
261 if bytes.len() > limit {
262 return Err(io::ErrorKind::FileTooLarge.into());
263 }
264 Ok(bytes)
173265}
174266
175267/// The hex of a stamp's digest, which names an image this device keeps.
......@@ -189,3 +281,112 @@ fn document(stamp: &Stamp) -> Option<String> {
189281fn hex(bytes: &[u8]) -> String {
190282 bytes.iter().map(|byte| format!("{byte:02x}")).collect()
191283}
284
285#[cfg(test)]
286mod tests {
287 use super::*;
288 use onestore::op::{Edit, Op, PageOp};
289
290 /// A section file in a scratch folder, its image, and the transaction one edit of it
291 /// publishes.
292 fn edited(folder: &Path) -> (PathBuf, Vec<u8>, Transaction) {
293 let file = folder.join("Notes.one");
294 let image = onestore::create_section("Notes.one", "Text", "Author").unwrap();
295 std::fs::write(&file, &image).unwrap();
296 let arena = onestore::Arena::default();
297 let mut section = onestore::Section::open(&arena, image.clone()).unwrap();
298 let space = section.pages().unwrap()[0].0;
299 let edit = Edit {
300 at: crate::now(),
301 ops: vec![Op::Page {
302 space,
303 op: PageOp::Color(Some(0x00ab_cdef)),
304 }],
305 };
306 section.apply("Author", &edit).unwrap();
307 (file, image, section.seal().unwrap().unwrap())
308 }
309
310 fn published(folder: &Path, file: &Path, transaction: &Transaction) -> Published {
311 let published = Published::of(&folder.join("replica.sqlite"), "iPhone");
312 transaction.commit_file(file).unwrap();
313 let mut opened = File::open(file).unwrap();
314 published
315 .keep(Written::InPlace(&mut opened), transaction)
316 .unwrap();
317 published
318 }
319
320 #[test]
321 fn nothing_kept_reads_no_stamp() {
322 let folder = tempfile::tempdir().unwrap();
323 let published = Published::of(&folder.path().join("replica.sqlite"), "iPhone");
324 let listed = published.versions(|| unreachable!("no stamp is read"), &[]);
325 assert!(listed.unwrap().is_empty());
326 }
327
328 /// The image this device published lists once the file stands otherwise: once reads have
329 /// had `LAG` to catch up where the file stands as the image it was published on, and not
330 /// where the provider keeps it as a conflict version itself.
331 #[test]
332 fn a_lost_image_lists_once() {
333 let folder = tempfile::tempdir().unwrap();
334 let (file, base, transaction) = edited(folder.path());
335 let published = published(folder.path(), &file, &transaction);
336 let listed =
337 |conflicts: &[Version]| published.versions(|| file_stamp(&file), conflicts).unwrap();
338 assert!(listed(&[]).is_empty());
339 let ours = std::fs::read(&file).unwrap();
340 std::fs::write(&file, base).unwrap();
341 assert!(listed(&[]).is_empty());
342 let aged = File::options()
343 .write(true)
344 .open(&published.kept()[0].path)
345 .unwrap();
346 aged.set_modified(SystemTime::now() - LAG - Duration::from_secs(1))
347 .unwrap();
348 assert_eq!(listed(&[]).len(), 1);
349 // A conflict version not here yet could be this image: it waits for it.
350 let missing = Version {
351 id: file
352 .with_file_name("Not here.one")
353 .to_string_lossy()
354 .into_owned(),
355 device: None,
356 };
357 assert!(listed(&[missing]).is_empty());
358 let version = file.with_file_name("Notes (iPhone).one");
359 std::fs::write(&version, ours).unwrap();
360 let conflict = Version {
361 id: version.to_string_lossy().into_owned(),
362 device: Some("iPhone".into()),
363 };
364 assert!(listed(std::slice::from_ref(&conflict)).is_empty());
365 published.retired(&version).unwrap();
366 assert!(published.kept().is_empty());
367 }
368
369 /// A confirmation rewrites the header of the image last published: the kept image follows
370 /// it, rather than listing as lost and going, which would leave it unprotected.
371 #[test]
372 fn a_confirmation_keeps_the_image_kept() {
373 let folder = tempfile::tempdir().unwrap();
374 let (file, _, transaction) = edited(folder.path());
375 let published = published(folder.path(), &file, &transaction);
376 let base = file_stamp(&file).unwrap();
377 onestore::confirm_file(&file, &base).unwrap();
378 assert_ne!(file_stamp(&file).unwrap(), base);
379 let mut opened = File::open(&file).unwrap();
380 published
381 .confirmed(Written::InPlace(&mut opened), &base)
382 .unwrap();
383 let kept = published.kept();
384 assert_eq!(kept.len(), 1);
385 assert_eq!(
386 std::fs::read(&kept[0].path).unwrap(),
387 std::fs::read(&file).unwrap()
388 );
389 let listed = published.versions(|| file_stamp(&file), &[]).unwrap();
390 assert!(listed.is_empty());
391 }
392}
crates/notebook/src/session.rs+69-53
......@@ -626,7 +626,7 @@ impl Notebook {
626626 _ => continue,
627627 };
628628 claims
629 .entry(replica_location(&location, section))
629 .entry(replica_location(&location, &section.path, section.copy))
630630 .or_default()
631631 .push((identity, &section.path));
632632 }
......@@ -1017,7 +1017,7 @@ impl Notebook {
10171017 self.catalog
10181018 .sections()
10191019 .filter(|section| section.copy)
1020 .map(|section| replica_location(&from, section)),
1020 .map(|section| replica_location(&from, &section.path, section.copy)),
10211021 );
10221022 for location in locations {
10231023 let entries = match fs::read_dir(crate::location::folder(&self.cache, &location)) {
......@@ -1592,11 +1592,13 @@ impl Notebook {
15921592 });
15931593 let _ = self.refresh();
15941594 drop(replica);
1595 // Unless something else opened it meanwhile.
1595 // Unless something else opened it meanwhile, or it keeps images of uploads the old
1596 // file may have lost, which name that file and stay with its replica.
15961597 if let Ok(file) = file
15971598 && crate::closed(&file).is_ok()
1599 && !crate::published::keeps(&file)
15981600 {
1599 let _ = remove_replica(&file);
1601 let _ = crate::remove_replica(&file);
16001602 }
16011603 Ok(Moved {
16021604 path: copy,
......@@ -1817,7 +1819,7 @@ impl Notebook {
18171819 replica.apply_async(&author, edit).await?;
18181820 }
18191821 drop(old);
1820 remove_replica(&path)?;
1822 crate::remove_replica(&path)?;
18211823 }
18221824 }
18231825 Ok(key)
......@@ -1893,7 +1895,7 @@ impl Notebook {
18931895 for (replica, held, queued) in self.superseded(&locked) {
18941896 drop(held);
18951897 if queued == 0 {
1896 let _ = remove_replica(&replica);
1898 let _ = crate::remove_replica(&replica);
18971899 }
18981900 }
18991901 }
......@@ -1944,7 +1946,7 @@ impl Notebook {
19441946 return Err(error);
19451947 }
19461948 drop(held);
1947 remove_replica(&replica)?;
1949 crate::remove_replica(&replica)?;
19481950 let parent = self.folder(folder)?;
19491951 if let Some(identity) = Entry::of(parent, section).identity {
19501952 self.edit_toc(
......@@ -1980,9 +1982,15 @@ impl Notebook {
19801982 }
19811983
19821984 /// The `location::folder` holding the replica of `section`.
1985 /// A copy's is labelled with its location as it is first named, as one found after the
1986 /// notebook opened is, so that it moves and goes with the notebook (`location::moved`).
19831987 fn replica_folder(&self, section: &discover::Section) -> PathBuf {
1984 let location = replica_location(&self.storage.location(), section);
1985 crate::location::folder(&self.cache, &location)
1988 let location = replica_location(&self.storage.location(), &section.path, section.copy);
1989 match section.copy {
1990 true => crate::location::claimed(&self.cache, &location)
1991 .unwrap_or_else(|_| crate::location::folder(&self.cache, &location)),
1992 false => crate::location::folder(&self.cache, &location),
1993 }
19861994 }
19871995
19881996 /// Every readable section, for `Background::watch`, handing on the files the last
......@@ -2012,16 +2020,21 @@ impl Notebook {
20122020 copies: bool,
20132021 notify: impl Fn() + Send + 'static,
20142022 ) -> Result<Background> {
2015 self.background_with(watched, copies, |file| FileRemote(file.to_owned()), notify)
2023 self.background_with(
2024 watched,
2025 copies,
2026 |file, _| FileRemote(file.to_owned()),
2027 notify,
2028 )
20162029 }
20172030
2018 /// `background`, reaching each section file through the remote `remote` makes for it
2019 /// (`section_with`).
2031 /// `background`, reaching each section file through the remote `remote` makes for it and
2032 /// its replica, where it has one (`section_with`).
20202033 pub fn background_with<R: Remote + 'static>(
20212034 &self,
20222035 watched: bool,
20232036 copies: bool,
2024 remote: impl Fn(&Path) -> R + Clone + Send + 'static,
2037 remote: impl Fn(&Path, Option<&Path>) -> R + Clone + Send + 'static,
20252038 notify: impl Fn() + Send + 'static,
20262039 ) -> Result<Background> {
20272040 let Some(root) = self.root.clone() else {
......@@ -2035,7 +2048,8 @@ impl Notebook {
20352048 copies,
20362049 move |_| {
20372050 let (folder, remote) = (root.clone(), remote.clone());
2038 let bind = move |path: &str| remote(&folder.join(path));
2051 let bind =
2052 move |path: &str, replica: Option<&Path>| remote(&folder.join(path), replica);
20392053 let mut local = discover::Local::open(&root)?;
20402054 let list = move |folder: String| {
20412055 use discover::Source;
......@@ -2056,18 +2070,18 @@ impl Notebook {
20562070 ) -> Result<Section> {
20572071 self.section_with(
20582072 path,
2059 |file| Ok(FileRemote(file.to_owned())),
2073 |file, _| Ok(FileRemote(file.to_owned())),
20602074 offline,
20612075 notify,
20622076 )
20632077 }
20642078
2065 /// `section`, reading and publishing through the remote `connect` makes for its file
2066 /// (`Section::open_with`).
2079 /// `section`, reading and publishing through the remote `connect` makes for its file and
2080 /// its replica, which it reads through before there is one (`Section::open_with`).
20672081 pub fn section_with<R: Remote + 'static>(
20682082 &self,
20692083 path: &str,
2070 connect: impl FnMut(&Path) -> io::Result<R> + Send + 'static,
2084 connect: impl FnMut(&Path, Option<&Path>) -> io::Result<R> + Send + 'static,
20712085 offline: bool,
20722086 notify: impl Fn() + Send + 'static,
20732087 ) -> Result<Section> {
......@@ -2085,7 +2099,7 @@ impl Notebook {
20852099 self.section_unlocked_with(
20862100 path,
20872101 key,
2088 |file| Ok(FileRemote(file.to_owned())),
2102 |file, _| Ok(FileRemote(file.to_owned())),
20892103 offline,
20902104 notify,
20912105 )
......@@ -2096,7 +2110,7 @@ impl Notebook {
20962110 &self,
20972111 path: &str,
20982112 key: &protected::Key,
2099 connect: impl FnMut(&Path) -> io::Result<R> + Send + 'static,
2113 connect: impl FnMut(&Path, Option<&Path>) -> io::Result<R> + Send + 'static,
21002114 offline: bool,
21012115 notify: impl Fn() + Send + 'static,
21022116 ) -> Result<Section> {
......@@ -2107,7 +2121,7 @@ impl Notebook {
21072121 &self,
21082122 path: &str,
21092123 key: Option<&protected::Key>,
2110 connect: impl FnMut(&Path) -> io::Result<R> + Send + 'static,
2124 connect: impl FnMut(&Path, Option<&Path>) -> io::Result<R> + Send + 'static,
21112125 offline: bool,
21122126 notify: impl Fn() + Send + 'static,
21132127 ) -> Result<Section> {
......@@ -2146,7 +2160,7 @@ impl Notebook {
21462160 return Section::start(
21472161 file,
21482162 replica,
2149 move || connect(&remote),
2163 move || connect(&remote, Some(&cache)),
21502164 CHECK,
21512165 offline,
21522166 notify,
......@@ -2412,27 +2426,14 @@ fn catalog_path(folder: &str, name: &str) -> String {
24122426/// The location keying the replica of `section` in the notebook at `notebook`: the notebook's,
24132427/// so that a section renamed or moved within it keeps its replica, or for a copy of another
24142428/// section of the notebook its own file's.
2415fn replica_location(notebook: &str, section: &discover::Section) -> String {
2416 if section.copy {
2417 format!("{notebook}/{}", section.path)
2429pub fn replica_location(notebook: &str, path: &str, copy: bool) -> String {
2430 if copy {
2431 format!("{notebook}/{path}")
24182432 } else {
24192433 notebook.to_owned()
24202434 }
24212435}
24222436
2423/// Deletes the closed replica at `replica`, with the files SQLite keeps beside it.
2424fn remove_replica(replica: &Path) -> io::Result<()> {
2425 for suffix in ["", "-wal", "-shm"] {
2426 let mut file = replica.as_os_str().to_owned();
2427 file.push(suffix);
2428 match fs::remove_file(file) {
2429 Err(error) if error.kind() != io::ErrorKind::NotFound => return Err(error),
2430 _ => {}
2431 }
2432 }
2433 Ok(())
2434}
2435
24362437/// How often an open section's file is checked where nothing watches it.
24372438const CHECK: Duration = Duration::from_secs(2);
24382439
......@@ -2598,7 +2599,7 @@ impl Section {
25982599 Self::open_with(
25992600 file,
26002601 cache,
2601 |file| Ok(FileRemote(file.to_owned())),
2602 |file, _| Ok(FileRemote(file.to_owned())),
26022603 offline,
26032604 notify,
26042605 )
......@@ -2610,7 +2611,7 @@ impl Section {
26102611 pub fn open_with<R: Remote + 'static>(
26112612 file: impl AsRef<Path>,
26122613 cache: impl AsRef<Path>,
2613 connect: impl FnMut(&Path) -> io::Result<R> + Send + 'static,
2614 connect: impl FnMut(&Path, Option<&Path>) -> io::Result<R> + Send + 'static,
26142615 offline: bool,
26152616 notify: impl Fn() + Send + 'static,
26162617 ) -> Result<Self> {
......@@ -2626,12 +2627,12 @@ impl Section {
26262627 file: PathBuf,
26272628 key: Option<&protected::Key>,
26282629 replica: impl FnOnce(&[u8; 16], &[u8]) -> Result<PathBuf>,
2629 mut connect: impl FnMut(&Path) -> io::Result<R> + Send + 'static,
2630 mut connect: impl FnMut(&Path, Option<&Path>) -> io::Result<R> + Send + 'static,
26302631 interval: Duration,
26312632 offline: bool,
26322633 notify: impl Fn() + Send + 'static,
26332634 ) -> Result<Self> {
2634 let source = connect(&file)?.read()?;
2635 let source = connect(&file, None)?.read()?;
26352636 let store = Store::parse(&source)?;
26362637 let identity = RevisionIndex::parse(&store)?.root;
26372638 let cache = replica(&identity.guid, &source)?;
......@@ -2640,7 +2641,7 @@ impl Section {
26402641 Self::start(
26412642 file,
26422643 replica,
2643 move || connect(&remote),
2644 move || connect(&remote, Some(&cache)),
26442645 interval,
26452646 offline,
26462647 notify,
......@@ -2717,10 +2718,15 @@ impl Section {
27172718 offline: bool,
27182719 notify: impl Fn() + Send + 'static,
27192720 ) -> Result<Self> {
2721 let cache = replica.path();
27202722 Self::start(
27212723 PathBuf::from(&path),
27222724 replica,
2723 move || Ok(crate::provider::ProviderRemote::new(&documents, &path)),
2725 move || {
2726 let remote =
2727 crate::provider::ProviderRemote::new(&documents, &path, cache.as_deref());
2728 Ok(remote)
2729 },
27242730 Background::UNWATCHED,
27252731 offline,
27262732 notify,
......@@ -2738,7 +2744,11 @@ impl Section {
27382744 let (cache, location) = (documents.cache.clone(), documents.location());
27392745 let cache = cache.as_path();
27402746 let file = PathBuf::from(&documents.root);
2741 let remote = move || Ok(crate::provider::ProviderRemote::new(&documents, ""));
2747 let remote = move |replica: Option<&Path>| {
2748 Ok(crate::provider::ProviderRemote::new(
2749 &documents, "", replica,
2750 ))
2751 };
27422752 let notify = Arc::new(notify);
27432753 let (connect, woken) = (remote.clone(), Arc::clone(&notify));
27442754 let replicas = claimed(cache, &location);
......@@ -2746,7 +2756,7 @@ impl Section {
27462756 file.clone(),
27472757 None,
27482758 replicas,
2749 move |_| connect(),
2759 move |_, replica| connect(replica),
27502760 Background::UNWATCHED,
27512761 offline,
27522762 move || woken(),
......@@ -2761,8 +2771,8 @@ impl Section {
27612771 match (replicas.next(), replicas.next()) {
27622772 (Some(only), None) => Self::start(
27632773 file,
2764 Replica::open(only)?,
2765 remote,
2774 Replica::open(&only)?,
2775 move || remote(Some(&only)),
27662776 Background::UNWATCHED,
27672777 offline,
27682778 move || notify(),
......@@ -2872,7 +2882,9 @@ impl Section {
28722882
28732883 /// Follows a section file moved within its notebook, keeping its replica and queued edits.
28742884 pub fn relocate(&mut self, file: impl AsRef<Path>) -> Result<()> {
2875 self.relocate_with(fs::absolute(file)?, |file| Ok(FileRemote(file.to_owned())))
2885 self.relocate_with(fs::absolute(file)?, |file, _| {
2886 Ok(FileRemote(file.to_owned()))
2887 })
28762888 }
28772889
28782890 /// `relocate`, with a host's publication target. Cancellation leaves any in-flight step
......@@ -2880,17 +2892,21 @@ impl Section {
28802892 pub fn relocate_with<R: Remote + 'static>(
28812893 &mut self,
28822894 file: impl AsRef<Path>,
2883 mut connect: impl FnMut(&Path) -> io::Result<R> + Send + 'static,
2895 mut connect: impl FnMut(&Path, Option<&Path>) -> io::Result<R> + Send + 'static,
28842896 ) -> Result<()> {
28852897 let file = file.as_ref().to_owned();
2886 let target = file.clone();
2898 let (target, replica) = (file.clone(), self.replica.path());
28872899 let previous = self.worker.take();
28882900 if let Some(worker) = &previous {
28892901 worker.cancel();
28902902 }
28912903 self.observed = Arc::new(Mutex::new(None));
2892 self.worker =
2893 Some(self.start_worker(move || connect(&target), CHECK, false, previous.as_ref())?);
2904 self.worker = Some(self.start_worker(
2905 move || connect(&target, replica.as_deref()),
2906 CHECK,
2907 false,
2908 previous.as_ref(),
2909 )?);
28942910 self.file = file;
28952911 Ok(())
28962912 }
crates/notebook/src/session/tests.rs+1-1
......@@ -691,7 +691,7 @@ fn initially_offline_workers_keep_the_original_queue_until_explicitly_woken() {
691691 fs::rename(&file, &target).unwrap();
692692 let counter = Arc::clone(&calls);
693693 section
694 .relocate_with(&target, move |file| {
694 .relocate_with(&target, move |file, _| {
695695 counter[0].fetch_add(1, Ordering::SeqCst);
696696 Ok(Counted {
697697 file: FileRemote(file.to_owned()),
crates/notebook/tests/session.rs+3-3
......@@ -1360,7 +1360,7 @@ fn a_moved_section_keeps_its_replica_while_the_old_remote_is_stalled() {
13601360 let mut section = Section::open_with(
13611361 &file,
13621362 directory.path().join("cache"),
1363 move |file| {
1363 move |file, _| {
13641364 let finishing = (!std::mem::replace(&mut opening, false)).then(|| finished.clone());
13651365 if finishing.is_some() {
13661366 connected.send(()).unwrap();
......@@ -1460,7 +1460,7 @@ fn an_open_section_the_background_holds_checks_its_file_when_the_watch_reports_i
14601460 let section = notebook
14611461 .section_with(
14621462 "Second.one",
1463 move |file| Ok(Counted(file.to_owned(), Arc::clone(&counter))),
1463 move |file, _| Ok(Counted(file.to_owned(), Arc::clone(&counter))),
14641464 false,
14651465 || {},
14661466 )
......@@ -1544,7 +1544,7 @@ fn the_background_takes_each_file_discovery_read_rather_than_reading_it_again()
15441544 let mut notebook = two_sections(directory.path());
15451545 let reads = Arc::new(AtomicUsize::new(0));
15461546 let counter = Arc::clone(&reads);
1547 let remote = move |file: &Path| Reads(file.to_owned(), Arc::clone(&counter));
1547 let remote = move |file: &Path, _: Option<&Path>| Reads(file.to_owned(), Arc::clone(&counter));
15481548 let synced = |background: &notebook::session::Background| {
15491549 let status = background.status();
15501550 status.len() == 2 && status.iter().all(|(_, status)| status.synced.is_some())
crates/snowbound/Cargo.toml+1-1
......@@ -65,7 +65,7 @@ web-sys = { version = "0.3", features = ["console", "Document", "Element", "Html
6565libc = "0.2"
6666objc2 = "0.5.2"
6767objc2-app-kit = { version = "0.2.2", default-features = false, features = ["NSAlert", "NSButton", "NSCell", "NSControl", "NSView", "NSResponder", "NSWindow", "NSApplication", "NSEvent", "NSMenu", "NSMenuItem", "NSColor", "NSColorSpace", "NSDatePicker", "NSDatePickerCell", "NSOpenPanel", "NSPanel", "NSSavePanel", "NSWorkspace"] }
68objc2-foundation = { version = "0.2.2", default-features = false, features = ["NSString", "NSThread", "NSAttributedString", "NSDate", "NSCalendar", "NSDateFormatter", "NSGeometry", "NSLocale", "NSPathUtilities", "NSTimeZone", "NSURL", "NSArray", "NSRange", "NSError", "NSFileCoordinator", "NSFilePresenter", "NSFileVersion", "NSFileManager", "NSHost", "NSNotification", "NSOperation", "block2"] }
68objc2-foundation = { version = "0.2.2", default-features = false, features = ["NSString", "NSThread", "NSAttributedString", "NSDate", "NSCalendar", "NSDateFormatter", "NSGeometry", "NSLocale", "NSPathUtilities", "NSTimeZone", "NSURL", "NSArray", "NSRange", "NSError", "NSFileCoordinator", "NSFilePresenter", "NSFileVersion", "NSFileManager", "NSNotification", "NSOperation", "block2"] }
6969block2 = "0.5.1"
7070
7171[target.'cfg(not(any(target_os = "macos", target_arch = "wasm32")))'.dependencies]
crates/snowbound/src/icloud_linux.rs-3
......@@ -24,7 +24,6 @@ pub fn drive() -> Option<PathBuf> {
2424pub fn section(
2525 notebook: &Notebook,
2626 _: &Path,
27 _: &Path,
2827 path: &str,
2928 key: Option<&onestore::protected::Key>,
3029 offline: bool,
......@@ -47,8 +46,6 @@ pub fn lone_section(
4746
4847pub fn background(
4948 notebook: &Notebook,
50 _: &Path,
51 _: &Path,
5249 notify: impl Fn() + Send + 'static,
5350) -> Result<Background, notebook::Error> {
5451 notebook.background(false, true, notify)
crates/snowbound/src/icloud_macos.rs+61-49
......@@ -14,8 +14,6 @@ use objc2::{
1414 rc::Retained,
1515 runtime::{AnyObject, NSObject, NSObjectProtocol, ProtocolObject},
1616};
17#[allow(deprecated)]
18use objc2_foundation::NSHost;
1917use objc2_foundation::{
2018 NSFileCoordinator, NSFileCoordinatorReadingOptions, NSFileCoordinatorWritingOptions,
2119 NSFileManager, NSFilePresenter, NSFileVersion, NSNotification, NSNotificationCenter,
......@@ -249,6 +247,44 @@ pub struct Coordinated {
249247 published: Option<Published>,
250248}
251249
250/// This Mac's name as Sharing shows it, as iCloud Drive names its conflict versions.
251fn computer() -> &'static str {
252 static NAME: OnceLock<String> = OnceLock::new();
253 NAME.get_or_init(|| {
254 #[link(name = "SystemConfiguration", kind = "framework")]
255 unsafe extern "C" {
256 fn SCDynamicStoreCopyComputerName(
257 store: *const std::ffi::c_void,
258 encoding: *mut u32,
259 ) -> *mut NSString;
260 }
261 // SAFETY: a null store asks the system's; the name, a CFString the caller owns, is
262 // toll-free bridged to NSString.
263 let name = unsafe {
264 Retained::from_raw(SCDynamicStoreCopyComputerName(
265 std::ptr::null(),
266 std::ptr::null_mut(),
267 ))
268 };
269 name.map_or_else(|| "Mac".to_owned(), |name| name.to_string())
270 })
271}
272
273/// The header and length of `file`, once iCloud Drive has it here.
274fn stamp(file: &Path) -> io::Result<Stamp> {
275 present(file)?;
276 coordinated(file, false, || {
277 use std::io::Read;
278 let mut opened = std::fs::File::open(file)?;
279 let mut header = [0; 1024];
280 opened.read_exact(&mut header)?;
281 Ok(Stamp {
282 header,
283 length: opened.metadata()?.len(),
284 })
285 })?
286}
287
252288fn uncommitted(error: io::Error) -> CommitError {
253289 CommitError {
254290 state: CommitState::NotCommitted,
......@@ -257,19 +293,11 @@ fn uncommitted(error: io::Error) -> CommitError {
257293}
258294
259295impl Coordinated {
260 /// The file `file` of the notebook folder, or lone section, at `notebook`, whose replicas
261 /// `cache` holds.
262 pub fn new(file: &Path, notebook: &Path, cache: &Path) -> Self {
263 static DEVICE: OnceLock<Option<String>> = OnceLock::new();
264 // The computer's name as Sharing shows it, as iCloud names this Mac's versions; the
265 // Network framework that deprecates NSHost has no such name.
266 #[allow(deprecated)]
267 let device = DEVICE.get_or_init(|| {
268 unsafe { NSHost::currentHost().localizedName() }.map(|name| name.to_string())
269 });
296 /// The section file `file`, its replica at `replica` where it has one.
297 pub fn new(file: &Path, replica: Option<&Path>) -> Self {
270298 Self {
271299 file: file.to_owned(),
272 published: Published::of(cache, notebook, file, device.clone()),
300 published: replica.map(|replica| Published::of(replica, computer())),
273301 }
274302 }
275303
......@@ -301,17 +329,7 @@ impl Remote for Coordinated {
301329 }
302330
303331 fn stamp(&mut self) -> io::Result<Stamp> {
304 present(&self.file)?;
305 coordinated(&self.file, false, || {
306 use std::io::Read;
307 let mut file = std::fs::File::open(&self.file)?;
308 let mut header = [0; 1024];
309 file.read_exact(&mut header)?;
310 Ok(Stamp {
311 header,
312 length: file.metadata()?.len(),
313 })
314 })?
332 stamp(&self.file)
315333 }
316334
317335 fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> {
......@@ -330,7 +348,12 @@ impl Remote for Coordinated {
330348
331349 fn confirm(&mut self, base: &Stamp) -> Result<(), CommitError> {
332350 coordinated(&self.file, true, || {
333 onestore::confirm_file(&self.file, base)
351 onestore::confirm_file(&self.file, base)?;
352 if let Some(published) = &self.published {
353 let _ = std::fs::File::open(&self.file)
354 .and_then(|mut file| published.confirmed(Written::InPlace(&mut file), base));
355 }
356 Ok(())
334357 })
335358 .map_err(uncommitted)?
336359 }
......@@ -346,13 +369,9 @@ impl Remote for Coordinated {
346369 })
347370 })
348371 .collect();
349 if self.published.is_some() {
350 let stamp = self.stamp()?;
351 listed.extend(
352 self.published
353 .iter()
354 .flat_map(|published| published.versions(&stamp)),
355 );
372 if let Some(published) = &self.published {
373 let lost = published.versions(|| stamp(&self.file), &listed)?;
374 listed.extend(lost);
356375 }
357376 Ok(listed)
358377 }
......@@ -382,12 +401,14 @@ impl Remote for Coordinated {
382401 && published.holds(id)
383402 {
384403 if keep {
385 let device = published.device().unwrap_or("this Mac");
386 let beside = notebook::published::beside(&self.file, device);
404 let beside = notebook::published::beside(&self.file, published.device());
387405 coordinated(&beside, true, || std::fs::copy(id, &beside))??;
388406 }
389407 return std::fs::remove_file(id);
390408 }
409 if let Some(published) = &self.published {
410 let _ = published.retired(Path::new(id));
411 }
391412 let version = match self.conflict(id) {
392413 Ok(version) => version,
393414 // Another device resolved it first.
......@@ -419,7 +440,6 @@ impl Remote for Coordinated {
419440pub fn section(
420441 notebook: &Notebook,
421442 root: &Path,
422 cache: &Path,
423443 path: &str,
424444 key: Option<&onestore::protected::Key>,
425445 offline: bool,
......@@ -428,8 +448,7 @@ pub fn section(
428448 if !notebook.replica_path(path)?.exists() {
429449 fetch(&root.join(path))?;
430450 }
431 let (root, cache) = (root.to_owned(), cache.to_owned());
432 let connect = move |file: &Path| Ok(Coordinated::new(file, &root, &cache));
451 let connect = |file: &Path, replica: Option<&Path>| Ok(Coordinated::new(file, replica));
433452 let section = match key {
434453 Some(key) => notebook.section_unlocked_with(path, key, connect, offline, notify)?,
435454 None => notebook.section_with(path, connect, offline, notify)?,
......@@ -458,10 +477,7 @@ pub fn lone_section(
458477 let section = Section::open_with(
459478 file,
460479 cache,
461 {
462 let cache = cache.to_owned();
463 move |file: &Path| Ok(Coordinated::new(file, file, &cache))
464 },
480 |file, replica| Ok(Coordinated::new(file, replica)),
465481 offline,
466482 notify,
467483 )?;
......@@ -475,13 +491,9 @@ pub fn lone_section(
475491/// presenter or the folder's watch reports it.
476492pub fn background(
477493 notebook: &Notebook,
478 root: &Path,
479 cache: &Path,
480494 notify: impl Fn() + Send + 'static,
481495) -> Result<Background, notebook::Error> {
482 let (root, cache) = (root.to_owned(), cache.to_owned());
483 let remote = move |file: &Path| Coordinated::new(file, &root, &cache);
484 notebook.background_with(false, true, remote, notify)
496 notebook.background_with(false, true, Coordinated::new, notify)
485497}
486498
487499struct Ivars {
......@@ -655,7 +667,7 @@ mod tests {
655667 let notebook = Notebook::create(&root, &cache, Notebook::NEW_COLOR, &page).unwrap();
656668 assert!(ubiquitous(&root));
657669 let path = notebook.catalog().sections[0].path.clone();
658 let section = section(&notebook, &root, &cache, &path, None, false, || {}).unwrap();
670 let section = section(&notebook, &root, &path, None, false, || {}).unwrap();
659671 let (space, ..) = section.pages().unwrap()[0].clone();
660672 let title = section
661673 .page(space)
......@@ -687,7 +699,7 @@ mod tests {
687699 .unwrap();
688700 // Wakes skip the pause in typing, as leaving the window does.
689701 let file = root.join(&path);
690 let mut remote = Coordinated::new(&file, &root, &cache);
702 let mut remote = Coordinated::new(&file, None);
691703 let deadline = std::time::Instant::now() + Duration::from_secs(30);
692704 loop {
693705 section.wake();
......@@ -725,7 +737,7 @@ mod tests {
725737 let notebook = Notebook::create(&root, &cache, Notebook::NEW_COLOR, &page).unwrap();
726738 let path = notebook.catalog().sections[0].path.clone();
727739 let file = root.join(&path);
728 let section = section(&notebook, &root, &cache, &path, None, false, || {}).unwrap();
740 let section = section(&notebook, &root, &path, None, false, || {}).unwrap();
729741 let space = section.pages().unwrap()[0].0;
730742 let stored = || {
731743 let arena = onestore::Arena::default();
crates/snowbound/src/library.rs+8-11
......@@ -413,7 +413,7 @@ impl Library {
413413 }
414414 let mut notebook = Notebook::open(location, cache);
415415 let (background, watch) = match &mut notebook {
416 Ok(notebook) => local_background(notebook, location, cache),
416 Ok(notebook) => local_background(notebook, location),
417417 Err(_) => (None, None),
418418 };
419419 let name = display_name(cache, location).unwrap_or_else(|| file_name(Path::new(location)));
......@@ -819,7 +819,7 @@ impl Library {
819819
820820 /// A notebook `Notebook::create` just made in the folder at `location`.
821821 pub fn created(location: &str, mut notebook: Notebook, cache: &Path) -> Self {
822 let (background, watch) = local_background(&mut notebook, location, cache);
822 let (background, watch) = local_background(&mut notebook, location);
823823 Self {
824824 background,
825825 watch,
......@@ -902,7 +902,7 @@ impl Library {
902902 #[cfg(feature = "live")]
903903 if let Some(joined) = &self.joined {
904904 let guest = Arc::clone(&joined.guest);
905 section.relocate_with(path, move |file| {
905 section.relocate_with(path, move |file, _| {
906906 Ok(notebook::live::share::HostedRemote::new(
907907 &guest,
908908 &file.to_string_lossy(),
......@@ -921,7 +921,7 @@ impl Library {
921921 root => format!("{root}/{path}"),
922922 };
923923 let server = Arc::clone(server);
924 section.relocate_with(file, move |file| {
924 section.relocate_with(file, move |file, _| {
925925 Ok(notebook::SmbRemote::new(
926926 server.connect()?,
927927 file.to_string_lossy(),
......@@ -935,9 +935,8 @@ impl Library {
935935 let file = Path::new(&self.location).join(path);
936936 #[cfg(target_os = "macos")]
937937 if self.in_icloud() {
938 let (root, cache) = (PathBuf::from(&self.location), self.cache.clone());
939 section.relocate_with(file, move |file| {
940 Ok(crate::icloud::Coordinated::new(file, &root, &cache))
938 section.relocate_with(file, |file, replica| {
939 Ok(crate::icloud::Coordinated::new(file, replica))
941940 })?;
942941 } else {
943942 section.relocate(file)?;
......@@ -1025,7 +1024,6 @@ impl Library {
10251024 crate::icloud::section(
10261025 notebook,
10271026 root,
1028 &self.cache,
10291027 path,
10301028 key.as_ref(),
10311029 offline(),
......@@ -1535,13 +1533,12 @@ fn read_tag_art(notebook: &Notebook) -> TagArt {
15351533fn local_background(
15361534 notebook: &mut Notebook,
15371535 location: &str,
1538 cache: &Path,
15391536) -> (Option<Arc<Background>>, Option<Arc<crate::watch::Watch>>) {
15401537 let reports = Arc::new(OnceLock::new());
15411538 let watch = watcher(location, Arc::clone(&reports));
15421539 let copies = !crate::watch::on_this_computer(Path::new(location));
15431540 let background = match crate::icloud::ubiquitous(Path::new(location)) {
1544 true => crate::icloud::background(notebook, Path::new(location), cache, notify_background),
1541 true => crate::icloud::background(notebook, notify_background),
15451542 false => notebook.background(watch.is_some(), copies, notify_background),
15461543 };
15471544 let Some(background) = background
......@@ -1772,7 +1769,7 @@ mod tests {
17721769 let section = Section::open_with(
17731770 &file,
17741771 root.join("cache"),
1775 move |file| {
1772 move |file, _| {
17761773 Ok(Stalled {
17771774 file: file.to_owned(),
17781775 entered: Arc::clone(&entered),
crates/snowbound/src/undo.rs+1-1
......@@ -1872,7 +1872,7 @@ mod tests {
18721872 let arrived = Arc::clone(b.replica());
18731873 let (entered, holding) = std::sync::mpsc::channel();
18741874 let (release, released) = std::sync::mpsc::channel::<()>();
1875 b.relocate_with(fixture.folder.join("Notebook/B.one"), move |_| {
1875 b.relocate_with(fixture.folder.join("Notebook/B.one"), move |_, _| {
18761876 let _ = entered.send(());
18771877 let _ = released.recv();
18781878 Err::<Unreached, _>(std::io::ErrorKind::TimedOut.into())