authoravatarclover caruso<git@paperclover.net>2026-10-19 12:24:25-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:39:58-07:00
loga4c43834fe0b1f842728249f263c9dc06ebd702c
treec0b99e5327f2deda0c339fbabfe4f968e3fd96b1
parentb77c879d3ca88201a13162025edad2643f9a374f
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(desktop): merge back uploads iCloud Drive lost on the Mac

The Mac's iCloud Drive notebooks had the gap the phone's had: an upload another device's replaced with no conflict version kept was gone at the next sync. The macOS file remote now keeps each image it publishes beside the section's replica and lists the ones the file lost among its versions, as the iOS one does (notebook::published), naming its conflict pages for the computer as Sharing names it. The "Name (Device).one" naming and the choice of folder move into notebook::published, so both hosts, and the Mac's handling of iCloud's own conflict versions, share them. A test lands another device's upload over this Mac's in a scratch folder; it fails before. - Edits you make on the Mac to an iCloud Drive notebook come back if iCloud lets another device's upload replace them, instead of being lost. Assisted-by: claude-opus-5.5

7 files changed, 223 insertions(+), 67 deletions(-)

arc/sync.md+4-4
...@@ -296,12 +296,12 @@ device that wrote it. A version of another section, or one that cannot be read,...@@ -296,12 +296,12 @@ device that wrote it. A version of another section, or one that cannot be read,
296the file as `Name (Device).one` instead.296the file as `Name (Device).one` instead.
297297
298iCloud Drive can also lose an upload outright: another device's lands over it and no conflict298iCloud Drive can also lose an upload outright: another device's lands over it and no conflict
299version reaches this one. So on the iPhone and on Android each image a section publishes to a299version reaches this one. So on the Mac, the iPhone and Android each image a section publishes
300file a provider keeps stays in the cache (`notebook::published`) until the file is seen300to a file a provider keeps stays in the cache (`notebook::published`) until the file is seen
301otherwise, then lists as one more version: one the file holds merges as nothing and goes, one301otherwise, then lists as one more version: one the file holds merges as nothing and goes, one
302it lost merges back as a conflict version does, its conflict pages named for this device. One302it lost merges back as a conflict version does, its conflict pages named for this device. One
303that cannot merge is kept beside the file as `Name (Device).one` on the iPhone, and in the303that cannot merge is kept beside the file as `Name (Device).one` on the Mac and the iPhone, and
304cache on Android, where no provider takes a new document beside it.304in the cache on Android, where no provider takes a new document beside it.
305305
306On a cloud drive every read and write of a section file runs under `NSFileCoordinator`, so the306On a cloud drive every read and write of a section file runs under `NSFileCoordinator`, so the
307iCloud daemon never swaps a file between the stamp check and the append. A file iCloud evicted307iCloud daemon never swaps a file between the stamp check and the append. A file iCloud evicted
crates/mobile/src/library.rs+5-24
...@@ -131,23 +131,12 @@ struct Coordinated {...@@ -131,23 +131,12 @@ struct Coordinated {
131}131}
132132
133impl Coordinated {133impl Coordinated {
134 /// The file `file` of the notebook folder, or lone section, at `notebook`. Its images are134 /// The file `file` of the notebook folder, or lone section, at `notebook`.
135 /// kept beside its replica, so that they move with the notebook (`location::moved`).
136 fn new(file: &Path, notebook: &Path, cache: &Path, elsewhere: bool) -> Self {135 fn new(file: &Path, notebook: &Path, cache: &Path, elsewhere: bool) -> Self {
137 use notebook::location::{PUBLISHED, local, section_folder};136 let device = VERSIONS.get().map(|(.., device)| device.clone());
138 let published = elsewhere137 let published = elsewhere
139 .then(|| {138 .then(|| Published::of(cache, notebook, file, device))
140 Some(section_folder(139 .flatten();
141 cache,
142 &local(notebook).ok()?,
143 &local(file).ok()?,
144 ))
145 })
146 .flatten()
147 .map(|folder| {
148 let device = VERSIONS.get().map(|(.., device)| device.clone());
149 Published::new(folder.join(PUBLISHED), device)
150 });
151 Self {140 Self {
152 file: file.to_owned(),141 file: file.to_owned(),
153 published,142 published,
...@@ -232,16 +221,8 @@ impl Remote for Coordinated {...@@ -232,16 +221,8 @@ impl Remote for Coordinated {
232 && published.holds(id)221 && published.holds(id)
233 {222 {
234 if keep {223 if keep {
235 let stem = self.file.file_stem().unwrap_or_default().to_string_lossy();
236 let device = VERSIONS.get().map_or("this device", |(.., device)| device);224 let device = VERSIONS.get().map_or("this device", |(.., device)| device);
237 let beside = (1..)225 let beside = notebook::published::beside(&self.file, device);
238 .map(|n| match n {
239 1 => format!("{stem} ({device}).one"),
240 _ => format!("{stem} ({device} {n}).one"),
241 })
242 .map(|name| self.file.with_file_name(name))
243 .find(|beside| std::fs::symlink_metadata(beside).is_err())
244 .ok_or(io::ErrorKind::AlreadyExists)?;
245 coordinated(&beside, true, || std::fs::copy(id, &beside))??;226 coordinated(&beside, true, || std::fs::copy(id, &beside))??;
246 }227 }
247 return std::fs::remove_file(id);228 return std::fs::remove_file(id);
crates/notebook/src/published.rs+27
...@@ -48,6 +48,15 @@ impl Published {...@@ -48,6 +48,15 @@ impl Published {
48 }48 }
49 }49 }
5050
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 }
59
51 fn kept(&self) -> Vec<Kept> {60 fn kept(&self) -> Vec<Kept> {
52 let entries = crate::fs::read_dir(&self.folder).into_iter().flatten();61 let entries = crate::fs::read_dir(&self.folder).into_iter().flatten();
53 entries62 entries
...@@ -139,12 +148,30 @@ impl Published {...@@ -139,12 +148,30 @@ impl Published {
139 .collect()148 .collect()
140 }149 }
141150
151 pub fn device(&self) -> Option<&str> {
152 self.device.as_deref()
153 }
154
142 /// Whether version `id` is an image kept here.155 /// Whether version `id` is an image kept here.
143 pub fn holds(&self, id: &str) -> bool {156 pub fn holds(&self, id: &str) -> bool {
144 Path::new(id).parent() == Some(self.folder.as_path())157 Path::new(id).parent() == Some(self.folder.as_path())
145 }158 }
146}159}
147160
161/// Where a version of `file` that cannot merge is kept beside it, as iCloud Drive keeps a
162/// conflict version: "Name (Device).one", numbered past any name taken.
163pub fn beside(file: &Path, device: &str) -> PathBuf {
164 let stem = file.file_stem().unwrap_or_default().to_string_lossy();
165 (1..)
166 .map(|n| match n {
167 1 => format!("{stem} ({device}).one"),
168 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()
173}
174
148/// The hex of a stamp's digest, which names an image this device keeps.175/// The hex of a stamp's digest, which names an image this device keeps.
149pub(crate) fn digest(stamp: &Stamp) -> String {176pub(crate) fn digest(stamp: &Stamp) -> String {
150 use sha2::Digest;177 use sha2::Digest;
crates/snowbound/Cargo.toml+1-1
...@@ -65,7 +65,7 @@ web-sys = { version = "0.3", features = ["console", "Document", "Element", "Html...@@ -65,7 +65,7 @@ web-sys = { version = "0.3", features = ["console", "Document", "Element", "Html
65libc = "0.2"65libc = "0.2"
66objc2 = "0.5.2"66objc2 = "0.5.2"
67objc2-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"] }67objc2-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", "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", "NSHost", "NSNotification", "NSOperation", "block2"] }
69block2 = "0.5.1"69block2 = "0.5.1"
7070
71[target.'cfg(not(any(target_os = "macos", target_arch = "wasm32")))'.dependencies]71[target.'cfg(not(any(target_os = "macos", target_arch = "wasm32")))'.dependencies]
crates/snowbound/src/icloud_linux.rs+3
...@@ -24,6 +24,7 @@ pub fn drive() -> Option<PathBuf> {...@@ -24,6 +24,7 @@ pub fn drive() -> Option<PathBuf> {
24pub fn section(24pub fn section(
25 notebook: &Notebook,25 notebook: &Notebook,
26 _: &Path,26 _: &Path,
27 _: &Path,
27 path: &str,28 path: &str,
28 key: Option<&onestore::protected::Key>,29 key: Option<&onestore::protected::Key>,
29 offline: bool,30 offline: bool,
...@@ -46,6 +47,8 @@ pub fn lone_section(...@@ -46,6 +47,8 @@ pub fn lone_section(
4647
47pub fn background(48pub fn background(
48 notebook: &Notebook,49 notebook: &Notebook,
50 _: &Path,
51 _: &Path,
49 notify: impl Fn() + Send + 'static,52 notify: impl Fn() + Send + 'static,
50) -> Result<Background, notebook::Error> {53) -> Result<Background, notebook::Error> {
51 notebook.background(false, true, notify)54 notebook.background(false, true, notify)
crates/snowbound/src/icloud_macos.rs+175-33
...@@ -6,6 +6,7 @@...@@ -6,6 +6,7 @@
6use block2::{RcBlock, StackBlock};6use block2::{RcBlock, StackBlock};
7use notebook::{7use notebook::{
8 Remote, Version,8 Remote, Version,
9 published::{Published, Written},
9 session::{Background, Notebook, Section},10 session::{Background, Notebook, Section},
10};11};
11use objc2::{12use objc2::{
...@@ -13,6 +14,8 @@ use objc2::{...@@ -13,6 +14,8 @@ use objc2::{
13 rc::Retained,14 rc::Retained,
14 runtime::{AnyObject, NSObject, NSObjectProtocol, ProtocolObject},15 runtime::{AnyObject, NSObject, NSObjectProtocol, ProtocolObject},
15};16};
17#[allow(deprecated)]
18use objc2_foundation::NSHost;
16use objc2_foundation::{19use objc2_foundation::{
17 NSFileCoordinator, NSFileCoordinatorReadingOptions, NSFileCoordinatorWritingOptions,20 NSFileCoordinator, NSFileCoordinatorReadingOptions, NSFileCoordinatorWritingOptions,
18 NSFileManager, NSFilePresenter, NSFileVersion, NSNotification, NSNotificationCenter,21 NSFileManager, NSFilePresenter, NSFileVersion, NSNotification, NSNotificationCenter,
...@@ -26,7 +29,7 @@ use std::{...@@ -26,7 +29,7 @@ use std::{
26 io,29 io,
27 path::{Path, PathBuf},30 path::{Path, PathBuf},
28 ptr::NonNull,31 ptr::NonNull,
29 sync::Mutex,32 sync::{Mutex, OnceLock},
30 time::Duration,33 time::Duration,
31};34};
3235
...@@ -238,8 +241,13 @@ pub fn download(root: &Path) -> usize {...@@ -238,8 +241,13 @@ pub fn download(root: &Path) -> usize {
238const SF_DATALESS: u32 = 0x4000_0000;241const SF_DATALESS: u32 = 0x4000_0000;
239242
240/// A section file in iCloud Drive, read and published under file coordination, with the243/// A section file in iCloud Drive, read and published under file coordination, with the
241/// conflict versions iCloud keeps beside it.244/// conflict versions iCloud keeps beside it and the images this Mac published to it, kept
242pub struct Coordinated(pub PathBuf);245/// until the file holds them: an upload another device's replaced merges back
246/// (`notebook::published`).
247pub struct Coordinated {
248 file: PathBuf,
249 published: Option<Published>,
250}
243251
244fn uncommitted(error: io::Error) -> CommitError {252fn uncommitted(error: io::Error) -> CommitError {
245 CommitError {253 CommitError {
...@@ -249,8 +257,24 @@ fn uncommitted(error: io::Error) -> CommitError {...@@ -249,8 +257,24 @@ fn uncommitted(error: io::Error) -> CommitError {
249}257}
250258
251impl Coordinated {259impl 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 });
270 Self {
271 file: file.to_owned(),
272 published: Published::of(cache, notebook, file, device.clone()),
273 }
274 }
275
252 fn conflicts(&self) -> io::Result<Vec<Retained<NSFileVersion>>> {276 fn conflicts(&self) -> io::Result<Vec<Retained<NSFileVersion>>> {
253 let url = url(&self.0).ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput))?;277 let url = url(&self.file).ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput))?;
254 Ok(278 Ok(
255 unsafe { NSFileVersion::unresolvedConflictVersionsOfItemAtURL(&url) }279 unsafe { NSFileVersion::unresolvedConflictVersionsOfItemAtURL(&url) }
256 .map(|versions| versions.to_vec_retained())280 .map(|versions| versions.to_vec_retained())
...@@ -272,15 +296,15 @@ fn version_path(version: &NSFileVersion) -> Option<PathBuf> {...@@ -272,15 +296,15 @@ fn version_path(version: &NSFileVersion) -> Option<PathBuf> {
272296
273impl Remote for Coordinated {297impl Remote for Coordinated {
274 fn read(&mut self) -> io::Result<Vec<u8>> {298 fn read(&mut self) -> io::Result<Vec<u8>> {
275 present(&self.0)?;299 present(&self.file)?;
276 coordinated(&self.0, false, || onestore::read_file(&self.0))?300 coordinated(&self.file, false, || onestore::read_file(&self.file))?
277 }301 }
278302
279 fn stamp(&mut self) -> io::Result<Stamp> {303 fn stamp(&mut self) -> io::Result<Stamp> {
280 present(&self.0)?;304 present(&self.file)?;
281 coordinated(&self.0, false, || {305 coordinated(&self.file, false, || {
282 use std::io::Read;306 use std::io::Read;
283 let mut file = std::fs::File::open(&self.0)?;307 let mut file = std::fs::File::open(&self.file)?;
284 let mut header = [0; 1024];308 let mut header = [0; 1024];
285 file.read_exact(&mut header)?;309 file.read_exact(&mut header)?;
286 Ok(Stamp {310 Ok(Stamp {
...@@ -291,18 +315,29 @@ impl Remote for Coordinated {...@@ -291,18 +315,29 @@ impl Remote for Coordinated {
291 }315 }
292316
293 fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> {317 fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> {
294 present(&self.0).map_err(uncommitted)?;318 present(&self.file).map_err(uncommitted)?;
295 coordinated(&self.0, true, || transaction.commit_file(&self.0)).map_err(uncommitted)?319 coordinated(&self.file, true, || {
320 transaction.commit_file(&self.file)?;
321 if let Some(published) = &self.published {
322 // A copy it could not keep only leaves the publication unprotected.
323 let _ = std::fs::File::open(&self.file)
324 .and_then(|mut file| published.keep(Written::InPlace(&mut file), transaction));
325 }
326 Ok(())
327 })
328 .map_err(uncommitted)?
296 }329 }
297330
298 fn confirm(&mut self, base: &Stamp) -> Result<(), CommitError> {331 fn confirm(&mut self, base: &Stamp) -> Result<(), CommitError> {
299 coordinated(&self.0, true, || onestore::confirm_file(&self.0, base)).map_err(uncommitted)?332 coordinated(&self.file, true, || {
333 onestore::confirm_file(&self.file, base)
334 })
335 .map_err(uncommitted)?
300 }336 }
301337
338 /// iCloud's conflict versions, then this Mac's publications the file lost.
302 fn versions(&mut self) -> io::Result<Vec<Version>> {339 fn versions(&mut self) -> io::Result<Vec<Version>> {
303 Ok(self340 let mut listed: Vec<Version> = (self.conflicts()?.into_iter())
304 .conflicts()?
305 .into_iter()
306 .filter_map(|version| {341 .filter_map(|version| {
307 Some(Version {342 Some(Version {
308 id: version_path(&version)?.to_string_lossy().into_owned(),343 id: version_path(&version)?.to_string_lossy().into_owned(),
...@@ -310,10 +345,26 @@ impl Remote for Coordinated {...@@ -310,10 +345,26 @@ impl Remote for Coordinated {
310 .map(|name| name.to_string()),345 .map(|name| name.to_string()),
311 })346 })
312 })347 })
313 .collect())348 .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 );
356 }
357 Ok(listed)
314 }358 }
315359
316 fn version(&mut self, id: &str) -> io::Result<Vec<u8>> {360 fn version(&mut self, id: &str) -> io::Result<Vec<u8>> {
361 if self
362 .published
363 .as_ref()
364 .is_some_and(|published| published.holds(id))
365 {
366 return std::fs::read(id);
367 }
317 let version = self.conflict(id)?;368 let version = self.conflict(id)?;
318 if !unsafe { version.hasLocalContents() } {369 if !unsafe { version.hasLocalContents() } {
319 return Err(io::Error::new(370 return Err(io::Error::new(
...@@ -324,28 +375,33 @@ impl Remote for Coordinated {...@@ -324,28 +375,33 @@ impl Remote for Coordinated {
324 std::fs::read(id)375 std::fs::read(id)
325 }376 }
326377
378 /// A version that cannot merge is kept beside the file under a name of its own:
379 /// "Section (Clover's iPhone).one".
327 fn retire(&mut self, id: &str, keep: bool) -> io::Result<()> {380 fn retire(&mut self, id: &str, keep: bool) -> io::Result<()> {
381 if let Some(published) = &self.published
382 && published.holds(id)
383 {
384 if keep {
385 let device = published.device().unwrap_or("this Mac");
386 let beside = notebook::published::beside(&self.file, device);
387 coordinated(&beside, true, || std::fs::copy(id, &beside))??;
388 }
389 return std::fs::remove_file(id);
390 }
328 let version = match self.conflict(id) {391 let version = match self.conflict(id) {
329 Ok(version) => version,392 Ok(version) => version,
330 // Another device resolved it first.393 // Another device resolved it first.
331 Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),394 Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
332 Err(error) => return Err(error),395 Err(error) => return Err(error),
333 };396 };
334 coordinated(&self.0, true, || {397 coordinated(&self.file, true, || {
335 if keep {398 if keep {
336 // Beside the file, under a name of its own: "Section (Clover's iPhone).one".
337 let device = unsafe { version.localizedNameOfSavingComputer() }399 let device = unsafe { version.localizedNameOfSavingComputer() }
338 .map_or_else(|| "another device".to_owned(), |name| name.to_string());400 .map_or_else(|| "another device".to_owned(), |name| name.to_string());
339 let stem = self.0.file_stem().unwrap_or_default().to_string_lossy();401 std::fs::write(
340 let folder = self.0.parent().unwrap_or(Path::new("."));402 notebook::published::beside(&self.file, &device),
341 let kept = (1..)403 std::fs::read(id)?,
342 .map(|n| match n {404 )?;
343 1 => folder.join(format!("{stem} ({device}).one")),
344 n => folder.join(format!("{stem} ({device} {n}).one")),
345 })
346 .find(|kept| !kept.exists())
347 .unwrap_or_default();
348 std::fs::write(kept, std::fs::read(id)?)?;
349 }405 }
350 unsafe {406 unsafe {
351 version.setResolved(true);407 version.setResolved(true);
...@@ -363,6 +419,7 @@ impl Remote for Coordinated {...@@ -363,6 +419,7 @@ impl Remote for Coordinated {
363pub fn section(419pub fn section(
364 notebook: &Notebook,420 notebook: &Notebook,
365 root: &Path,421 root: &Path,
422 cache: &Path,
366 path: &str,423 path: &str,
367 key: Option<&onestore::protected::Key>,424 key: Option<&onestore::protected::Key>,
368 offline: bool,425 offline: bool,
...@@ -371,7 +428,8 @@ pub fn section(...@@ -371,7 +428,8 @@ pub fn section(
371 if !notebook.replica_path(path)?.exists() {428 if !notebook.replica_path(path)?.exists() {
372 fetch(&root.join(path))?;429 fetch(&root.join(path))?;
373 }430 }
374 let connect = |file: &Path| Ok(Coordinated(file.to_owned()));431 let (root, cache) = (root.to_owned(), cache.to_owned());
432 let connect = move |file: &Path| Ok(Coordinated::new(file, &root, &cache));
375 let section = match key {433 let section = match key {
376 Some(key) => notebook.section_unlocked_with(path, key, connect, offline, notify)?,434 Some(key) => notebook.section_unlocked_with(path, key, connect, offline, notify)?,
377 None => notebook.section_with(path, connect, offline, notify)?,435 None => notebook.section_with(path, connect, offline, notify)?,
...@@ -400,7 +458,10 @@ pub fn lone_section(...@@ -400,7 +458,10 @@ pub fn lone_section(
400 let section = Section::open_with(458 let section = Section::open_with(
401 file,459 file,
402 cache,460 cache,
403 |file| Ok(Coordinated(file.to_owned())),461 {
462 let cache = cache.to_owned();
463 move |file: &Path| Ok(Coordinated::new(file, file, &cache))
464 },
404 offline,465 offline,
405 notify,466 notify,
406 )?;467 )?;
...@@ -414,9 +475,13 @@ pub fn lone_section(...@@ -414,9 +475,13 @@ pub fn lone_section(
414/// presenter or the folder's watch reports it.475/// presenter or the folder's watch reports it.
415pub fn background(476pub fn background(
416 notebook: &Notebook,477 notebook: &Notebook,
478 root: &Path,
479 cache: &Path,
417 notify: impl Fn() + Send + 'static,480 notify: impl Fn() + Send + 'static,
418) -> Result<Background, notebook::Error> {481) -> Result<Background, notebook::Error> {
419 notebook.background_with(false, true, |file| Coordinated(file.to_owned()), notify)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)
420}485}
421486
422struct Ivars {487struct Ivars {
...@@ -590,7 +655,7 @@ mod tests {...@@ -590,7 +655,7 @@ mod tests {
590 let notebook = Notebook::create(&root, &cache, Notebook::NEW_COLOR, &page).unwrap();655 let notebook = Notebook::create(&root, &cache, Notebook::NEW_COLOR, &page).unwrap();
591 assert!(ubiquitous(&root));656 assert!(ubiquitous(&root));
592 let path = notebook.catalog().sections[0].path.clone();657 let path = notebook.catalog().sections[0].path.clone();
593 let section = section(&notebook, &root, &path, None, false, || {}).unwrap();658 let section = section(&notebook, &root, &cache, &path, None, false, || {}).unwrap();
594 let (space, ..) = section.pages().unwrap()[0].clone();659 let (space, ..) = section.pages().unwrap()[0].clone();
595 let title = section660 let title = section
596 .page(space)661 .page(space)
...@@ -622,7 +687,7 @@ mod tests {...@@ -622,7 +687,7 @@ mod tests {
622 .unwrap();687 .unwrap();
623 // Wakes skip the pause in typing, as leaving the window does.688 // Wakes skip the pause in typing, as leaving the window does.
624 let file = root.join(&path);689 let file = root.join(&path);
625 let mut remote = Coordinated(file.clone());690 let mut remote = Coordinated::new(&file, &root, &cache);
626 let deadline = std::time::Instant::now() + Duration::from_secs(30);691 let deadline = std::time::Instant::now() + Duration::from_secs(30);
627 loop {692 loop {
628 section.wake();693 section.wake();
...@@ -646,4 +711,81 @@ mod tests {...@@ -646,4 +711,81 @@ mod tests {
646 let _ = std::fs::remove_dir_all(&cache);711 let _ = std::fs::remove_dir_all(&cache);
647 result.unwrap();712 result.unwrap();
648 }713 }
714
715 /// This Mac's upload to a notebook in iCloud Drive is lost, another device's landing over
716 /// it with no conflict version kept: the next sync merges it back. Coordination works on
717 /// any folder, so a scratch one stands in for iCloud Drive's.
718 #[test]
719 fn an_upload_icloud_lost_merges_back() {
720 use onestore::op::{Edit, Op, PageOp};
721 let folder = std::env::temp_dir().join(format!("snowbound-lost-{}", std::process::id()));
722 let (root, cache) = (folder.join("Notes"), folder.join("cache"));
723 std::fs::create_dir_all(&folder).unwrap();
724 let page = onestore::PageCreation::new(None, Some("Shared"), "Test").unwrap();
725 let notebook = Notebook::create(&root, &cache, Notebook::NEW_COLOR, &page).unwrap();
726 let path = notebook.catalog().sections[0].path.clone();
727 let file = root.join(&path);
728 let section = section(&notebook, &root, &cache, &path, None, false, || {}).unwrap();
729 let space = section.pages().unwrap()[0].0;
730 let stored = || {
731 let arena = onestore::Arena::default();
732 let section = onestore::Section::open(&arena, std::fs::read(&file).unwrap()).unwrap();
733 section.page(space).unwrap()
734 };
735 let published = |what: &str, ready: &dyn Fn(&onestore::page::Page) -> bool| {
736 let deadline = std::time::Instant::now() + Duration::from_secs(20);
737 while !ready(&stored()) {
738 assert!(
739 std::time::Instant::now() < deadline,
740 "{what}: {:?}",
741 stored()
742 );
743 section.wake();
744 std::thread::sleep(Duration::from_millis(50));
745 }
746 };
747 let edit = |op| Edit {
748 at: 134_030_000_000_000_000,
749 ops: vec![Op::Page { space, op }],
750 };
751 section
752 .apply("Test", edit(PageOp::Color(Some(0x0011_2233))))
753 .unwrap();
754 published("the shared edit", &|page| page.color == Some(0x0011_2233));
755 // Another device's copy, sharing that edit.
756 let mut theirs = std::fs::read(&file).unwrap();
757 let title = (stored().objects.iter())
758 .find_map(|object| match object {
759 onestore::page::PageObject::Title(title) => {
760 title.outlines[0].paragraphs[0].text().map(|text| text.id)
761 }
762 _ => None,
763 })
764 .unwrap();
765 section
766 .apply(
767 "Test",
768 edit(PageOp::Text {
769 text: title,
770 range: 0..0,
771 with: "Mac ".into(),
772 }),
773 )
774 .unwrap();
775 published("this Mac's edit", &|page| page.title.starts_with("Mac "));
776 let arena = onestore::Arena::default();
777 let mut other = onestore::Section::open(&arena, theirs.clone()).unwrap();
778 other
779 .apply("Other", &edit(PageOp::Color(Some(0x00ab_cdef))))
780 .unwrap();
781 other.seal().unwrap().unwrap().apply(&mut theirs).unwrap();
782 // iCloud Drive: the other device's upload lands over this Mac's, with no version.
783 std::fs::write(&file, &theirs).unwrap();
784 published("the lost edit merged back", &|page| {
785 page.title.starts_with("Mac ") && page.color == Some(0x00ab_cdef)
786 });
787 section.close().unwrap();
788 drop(notebook);
789 let _ = std::fs::remove_dir_all(&folder);
790 }
649}791}
crates/snowbound/src/library.rs+8-5
...@@ -413,7 +413,7 @@ impl Library {...@@ -413,7 +413,7 @@ impl Library {
413 }413 }
414 let mut notebook = Notebook::open(location, cache);414 let mut notebook = Notebook::open(location, cache);
415 let (background, watch) = match &mut notebook {415 let (background, watch) = match &mut notebook {
416 Ok(notebook) => local_background(notebook, location),416 Ok(notebook) => local_background(notebook, location, cache),
417 Err(_) => (None, None),417 Err(_) => (None, None),
418 };418 };
419 let name = display_name(cache, location).unwrap_or_else(|| file_name(Path::new(location)));419 let name = display_name(cache, location).unwrap_or_else(|| file_name(Path::new(location)));
...@@ -819,7 +819,7 @@ impl Library {...@@ -819,7 +819,7 @@ impl Library {
819819
820 /// A notebook `Notebook::create` just made in the folder at `location`.820 /// A notebook `Notebook::create` just made in the folder at `location`.
821 pub fn created(location: &str, mut notebook: Notebook, cache: &Path) -> Self {821 pub fn created(location: &str, mut notebook: Notebook, cache: &Path) -> Self {
822 let (background, watch) = local_background(&mut notebook, location);822 let (background, watch) = local_background(&mut notebook, location, cache);
823 Self {823 Self {
824 background,824 background,
825 watch,825 watch,
...@@ -935,8 +935,9 @@ impl Library {...@@ -935,8 +935,9 @@ impl Library {
935 let file = Path::new(&self.location).join(path);935 let file = Path::new(&self.location).join(path);
936 #[cfg(target_os = "macos")]936 #[cfg(target_os = "macos")]
937 if self.in_icloud() {937 if self.in_icloud() {
938 section.relocate_with(file, |file| {938 let (root, cache) = (PathBuf::from(&self.location), self.cache.clone());
939 Ok(crate::icloud::Coordinated(file.to_owned()))939 section.relocate_with(file, move |file| {
940 Ok(crate::icloud::Coordinated::new(file, &root, &cache))
940 })?;941 })?;
941 } else {942 } else {
942 section.relocate(file)?;943 section.relocate(file)?;
...@@ -1024,6 +1025,7 @@ impl Library {...@@ -1024,6 +1025,7 @@ impl Library {
1024 crate::icloud::section(1025 crate::icloud::section(
1025 notebook,1026 notebook,
1026 root,1027 root,
1028 &self.cache,
1027 path,1029 path,
1028 key.as_ref(),1030 key.as_ref(),
1029 offline(),1031 offline(),
...@@ -1533,12 +1535,13 @@ fn read_tag_art(notebook: &Notebook) -> TagArt {...@@ -1533,12 +1535,13 @@ fn read_tag_art(notebook: &Notebook) -> TagArt {
1533fn local_background(1535fn local_background(
1534 notebook: &mut Notebook,1536 notebook: &mut Notebook,
1535 location: &str,1537 location: &str,
1538 cache: &Path,
1536) -> (Option<Arc<Background>>, Option<Arc<crate::watch::Watch>>) {1539) -> (Option<Arc<Background>>, Option<Arc<crate::watch::Watch>>) {
1537 let reports = Arc::new(OnceLock::new());1540 let reports = Arc::new(OnceLock::new());
1538 let watch = watcher(location, Arc::clone(&reports));1541 let watch = watcher(location, Arc::clone(&reports));
1539 let copies = !crate::watch::on_this_computer(Path::new(location));1542 let copies = !crate::watch::on_this_computer(Path::new(location));
1540 let background = match crate::icloud::ubiquitous(Path::new(location)) {1543 let background = match crate::icloud::ubiquitous(Path::new(location)) {
1541 true => crate::icloud::background(notebook, notify_background),1544 true => crate::icloud::background(notebook, Path::new(location), cache, notify_background),
1542 false => notebook.background(watch.is_some(), copies, notify_background),1545 false => notebook.background(watch.is_some(), copies, notify_background),
1543 };1546 };
1544 let Some(background) = background1547 let Some(background) = background