From a4c43834fe0b1f842728249f263c9dc06ebd702c Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 12:24:25 -0700 Subject: [PATCH] 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 --- arc/sync.md | 8 +- crates/mobile/src/library.rs | 29 +--- crates/notebook/src/published.rs | 27 ++++ crates/snowbound/Cargo.toml | 2 +- crates/snowbound/src/icloud_linux.rs | 3 + crates/snowbound/src/icloud_macos.rs | 208 ++++++++++++++++++++++----- crates/snowbound/src/library.rs | 13 +- 7 files changed, 223 insertions(+), 67 deletions(-) diff --git a/arc/sync.md b/arc/sync.md index ebd3e500b0dd44b5951351f5a049b81ce0a31e28..06700958857d7bf65b897cb2b5b88fdff19430f5 100644 --- a/arc/sync.md +++ b/arc/sync.md @@ -296,12 +296,12 @@ device that wrote it. A version of another section, or one that cannot be read, the file as `Name (Device).one` instead. iCloud Drive can also lose an upload outright: another device's lands over it and no conflict -version reaches this one. So on the iPhone and on Android each image a section publishes to a -file a provider keeps stays in the cache (`notebook::published`) until the file is seen +version reaches this one. So on the Mac, the iPhone and Android each image a section publishes +to a file a provider keeps stays in the cache (`notebook::published`) until the file is seen otherwise, then lists as one more version: one the file holds merges as nothing and goes, one it lost merges back as a conflict version does, its conflict pages named for this device. One -that cannot merge is kept beside the file as `Name (Device).one` on the iPhone, and in the -cache on Android, where no provider takes a new document beside it. +that cannot merge is kept beside the file as `Name (Device).one` on the Mac and the iPhone, and +in the cache on Android, where no provider takes a new document beside it. On a cloud drive every read and write of a section file runs under `NSFileCoordinator`, so the iCloud daemon never swaps a file between the stamp check and the append. A file iCloud evicted diff --git a/crates/mobile/src/library.rs b/crates/mobile/src/library.rs index 067d19b71d08e4c81ac7560fe727ceac5cc87ed2..a611290597e0843e3bea19fc5ec2d73622b32464 100644 --- a/crates/mobile/src/library.rs +++ b/crates/mobile/src/library.rs @@ -131,23 +131,12 @@ struct Coordinated { } impl Coordinated { - /// The file `file` of the notebook folder, or lone section, at `notebook`. Its images are - /// kept beside its replica, so that they move with the notebook (`location::moved`). + /// The file `file` of the notebook folder, or lone section, at `notebook`. fn new(file: &Path, notebook: &Path, cache: &Path, elsewhere: bool) -> Self { - use notebook::location::{PUBLISHED, local, section_folder}; + let device = VERSIONS.get().map(|(.., device)| device.clone()); let published = elsewhere - .then(|| { - Some(section_folder( - cache, - &local(notebook).ok()?, - &local(file).ok()?, - )) - }) - .flatten() - .map(|folder| { - let device = VERSIONS.get().map(|(.., device)| device.clone()); - Published::new(folder.join(PUBLISHED), device) - }); + .then(|| Published::of(cache, notebook, file, device)) + .flatten(); Self { file: file.to_owned(), published, @@ -232,16 +221,8 @@ impl Remote for Coordinated { && published.holds(id) { if keep { - let stem = self.file.file_stem().unwrap_or_default().to_string_lossy(); let device = VERSIONS.get().map_or("this device", |(.., device)| device); - let beside = (1..) - .map(|n| match n { - 1 => format!("{stem} ({device}).one"), - _ => format!("{stem} ({device} {n}).one"), - }) - .map(|name| self.file.with_file_name(name)) - .find(|beside| std::fs::symlink_metadata(beside).is_err()) - .ok_or(io::ErrorKind::AlreadyExists)?; + let beside = notebook::published::beside(&self.file, device); coordinated(&beside, true, || std::fs::copy(id, &beside))??; } return std::fs::remove_file(id); diff --git a/crates/notebook/src/published.rs b/crates/notebook/src/published.rs index a508b289225911fadf6b44c88a919e894242b399..573852ab73a37358c42f41cdecd238020e819635 100644 --- a/crates/notebook/src/published.rs +++ b/crates/notebook/src/published.rs @@ -48,6 +48,15 @@ impl Published { } } + /// The images kept of the section file `file` of the notebook folder, or lone section, at + /// `notebook`, which `cache` holds the replicas of: beside the section's replica, so that + /// they move with the notebook (`location::moved`). + pub fn of(cache: &Path, notebook: &Path, file: &Path, device: Option) -> Option { + use crate::location::{PUBLISHED, local, section_folder}; + let folder = section_folder(cache, &local(notebook).ok()?, &local(file).ok()?); + Some(Self::new(folder.join(PUBLISHED), device)) + } + fn kept(&self) -> Vec { let entries = crate::fs::read_dir(&self.folder).into_iter().flatten(); entries @@ -139,12 +148,30 @@ impl Published { .collect() } + pub fn device(&self) -> Option<&str> { + self.device.as_deref() + } + /// Whether version `id` is an image kept here. pub fn holds(&self, id: &str) -> bool { Path::new(id).parent() == Some(self.folder.as_path()) } } +/// Where a version of `file` that cannot merge is kept beside it, as iCloud Drive keeps a +/// conflict version: "Name (Device).one", numbered past any name taken. +pub fn beside(file: &Path, device: &str) -> PathBuf { + let stem = file.file_stem().unwrap_or_default().to_string_lossy(); + (1..) + .map(|n| match n { + 1 => format!("{stem} ({device}).one"), + n => format!("{stem} ({device} {n}).one"), + }) + .map(|name| file.with_file_name(name)) + .find(|beside| std::fs::symlink_metadata(beside).is_err()) + .unwrap_or_default() +} + /// The hex of a stamp's digest, which names an image this device keeps. pub(crate) fn digest(stamp: &Stamp) -> String { use sha2::Digest; diff --git a/crates/snowbound/Cargo.toml b/crates/snowbound/Cargo.toml index 1989f28adf6464a4a6c0c55eb5611eb5b34b479f..bd761c74e67be62f27d2ccc52dac9f32fcd62e7f 100644 --- a/crates/snowbound/Cargo.toml +++ b/crates/snowbound/Cargo.toml @@ -65,7 +65,7 @@ web-sys = { version = "0.3", features = ["console", "Document", "Element", "Html libc = "0.2" objc2 = "0.5.2" objc2-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"] } -objc2-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"] } +objc2-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"] } block2 = "0.5.1" [target.'cfg(not(any(target_os = "macos", target_arch = "wasm32")))'.dependencies] diff --git a/crates/snowbound/src/icloud_linux.rs b/crates/snowbound/src/icloud_linux.rs index e302c40b808d7eb666669bdf6bc0cc6744123541..4001c4eb99f308b8884b4ce5b57c79712eabcc7d 100644 --- a/crates/snowbound/src/icloud_linux.rs +++ b/crates/snowbound/src/icloud_linux.rs @@ -24,6 +24,7 @@ pub fn drive() -> Option { pub fn section( notebook: &Notebook, _: &Path, + _: &Path, path: &str, key: Option<&onestore::protected::Key>, offline: bool, @@ -46,6 +47,8 @@ pub fn lone_section( pub fn background( notebook: &Notebook, + _: &Path, + _: &Path, notify: impl Fn() + Send + 'static, ) -> Result { notebook.background(false, true, notify) diff --git a/crates/snowbound/src/icloud_macos.rs b/crates/snowbound/src/icloud_macos.rs index 307445c7b57338f4423b86ce3c7b327281b3c4f5..066b632c32b797d47147231d73f17af012078bb0 100644 --- a/crates/snowbound/src/icloud_macos.rs +++ b/crates/snowbound/src/icloud_macos.rs @@ -6,6 +6,7 @@ use block2::{RcBlock, StackBlock}; use notebook::{ Remote, Version, + published::{Published, Written}, session::{Background, Notebook, Section}, }; use objc2::{ @@ -13,6 +14,8 @@ use objc2::{ rc::Retained, runtime::{AnyObject, NSObject, NSObjectProtocol, ProtocolObject}, }; +#[allow(deprecated)] +use objc2_foundation::NSHost; use objc2_foundation::{ NSFileCoordinator, NSFileCoordinatorReadingOptions, NSFileCoordinatorWritingOptions, NSFileManager, NSFilePresenter, NSFileVersion, NSNotification, NSNotificationCenter, @@ -26,7 +29,7 @@ use std::{ io, path::{Path, PathBuf}, ptr::NonNull, - sync::Mutex, + sync::{Mutex, OnceLock}, time::Duration, }; @@ -238,8 +241,13 @@ pub fn download(root: &Path) -> usize { const SF_DATALESS: u32 = 0x4000_0000; /// A section file in iCloud Drive, read and published under file coordination, with the -/// conflict versions iCloud keeps beside it. -pub struct Coordinated(pub PathBuf); +/// conflict versions iCloud keeps beside it and the images this Mac published to it, kept +/// until the file holds them: an upload another device's replaced merges back +/// (`notebook::published`). +pub struct Coordinated { + file: PathBuf, + published: Option, +} fn uncommitted(error: io::Error) -> CommitError { CommitError { @@ -249,8 +257,24 @@ fn uncommitted(error: io::Error) -> CommitError { } impl Coordinated { + /// The file `file` of the notebook folder, or lone section, at `notebook`, whose replicas + /// `cache` holds. + pub fn new(file: &Path, notebook: &Path, cache: &Path) -> Self { + static DEVICE: OnceLock> = OnceLock::new(); + // The computer's name as Sharing shows it, as iCloud names this Mac's versions; the + // Network framework that deprecates NSHost has no such name. + #[allow(deprecated)] + let device = DEVICE.get_or_init(|| { + unsafe { NSHost::currentHost().localizedName() }.map(|name| name.to_string()) + }); + Self { + file: file.to_owned(), + published: Published::of(cache, notebook, file, device.clone()), + } + } + fn conflicts(&self) -> io::Result>> { - let url = url(&self.0).ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput))?; + let url = url(&self.file).ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput))?; Ok( unsafe { NSFileVersion::unresolvedConflictVersionsOfItemAtURL(&url) } .map(|versions| versions.to_vec_retained()) @@ -272,15 +296,15 @@ fn version_path(version: &NSFileVersion) -> Option { impl Remote for Coordinated { fn read(&mut self) -> io::Result> { - present(&self.0)?; - coordinated(&self.0, false, || onestore::read_file(&self.0))? + present(&self.file)?; + coordinated(&self.file, false, || onestore::read_file(&self.file))? } fn stamp(&mut self) -> io::Result { - present(&self.0)?; - coordinated(&self.0, false, || { + present(&self.file)?; + coordinated(&self.file, false, || { use std::io::Read; - let mut file = std::fs::File::open(&self.0)?; + let mut file = std::fs::File::open(&self.file)?; let mut header = [0; 1024]; file.read_exact(&mut header)?; Ok(Stamp { @@ -291,18 +315,29 @@ impl Remote for Coordinated { } fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> { - present(&self.0).map_err(uncommitted)?; - coordinated(&self.0, true, || transaction.commit_file(&self.0)).map_err(uncommitted)? + present(&self.file).map_err(uncommitted)?; + coordinated(&self.file, true, || { + transaction.commit_file(&self.file)?; + if let Some(published) = &self.published { + // A copy it could not keep only leaves the publication unprotected. + let _ = std::fs::File::open(&self.file) + .and_then(|mut file| published.keep(Written::InPlace(&mut file), transaction)); + } + Ok(()) + }) + .map_err(uncommitted)? } fn confirm(&mut self, base: &Stamp) -> Result<(), CommitError> { - coordinated(&self.0, true, || onestore::confirm_file(&self.0, base)).map_err(uncommitted)? + coordinated(&self.file, true, || { + onestore::confirm_file(&self.file, base) + }) + .map_err(uncommitted)? } + /// iCloud's conflict versions, then this Mac's publications the file lost. fn versions(&mut self) -> io::Result> { - Ok(self - .conflicts()? - .into_iter() + let mut listed: Vec = (self.conflicts()?.into_iter()) .filter_map(|version| { Some(Version { id: version_path(&version)?.to_string_lossy().into_owned(), @@ -310,10 +345,26 @@ impl Remote for Coordinated { .map(|name| name.to_string()), }) }) - .collect()) + .collect(); + if self.published.is_some() { + let stamp = self.stamp()?; + listed.extend( + self.published + .iter() + .flat_map(|published| published.versions(&stamp)), + ); + } + Ok(listed) } fn version(&mut self, id: &str) -> io::Result> { + if self + .published + .as_ref() + .is_some_and(|published| published.holds(id)) + { + return std::fs::read(id); + } let version = self.conflict(id)?; if !unsafe { version.hasLocalContents() } { return Err(io::Error::new( @@ -324,28 +375,33 @@ impl Remote for Coordinated { std::fs::read(id) } + /// A version that cannot merge is kept beside the file under a name of its own: + /// "Section (Clover's iPhone).one". fn retire(&mut self, id: &str, keep: bool) -> io::Result<()> { + if let Some(published) = &self.published + && published.holds(id) + { + if keep { + let device = published.device().unwrap_or("this Mac"); + let beside = notebook::published::beside(&self.file, device); + coordinated(&beside, true, || std::fs::copy(id, &beside))??; + } + return std::fs::remove_file(id); + } let version = match self.conflict(id) { Ok(version) => version, // Another device resolved it first. Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()), Err(error) => return Err(error), }; - coordinated(&self.0, true, || { + coordinated(&self.file, true, || { if keep { - // Beside the file, under a name of its own: "Section (Clover's iPhone).one". let device = unsafe { version.localizedNameOfSavingComputer() } .map_or_else(|| "another device".to_owned(), |name| name.to_string()); - let stem = self.0.file_stem().unwrap_or_default().to_string_lossy(); - let folder = self.0.parent().unwrap_or(Path::new(".")); - let kept = (1..) - .map(|n| match n { - 1 => folder.join(format!("{stem} ({device}).one")), - n => folder.join(format!("{stem} ({device} {n}).one")), - }) - .find(|kept| !kept.exists()) - .unwrap_or_default(); - std::fs::write(kept, std::fs::read(id)?)?; + std::fs::write( + notebook::published::beside(&self.file, &device), + std::fs::read(id)?, + )?; } unsafe { version.setResolved(true); @@ -363,6 +419,7 @@ impl Remote for Coordinated { pub fn section( notebook: &Notebook, root: &Path, + cache: &Path, path: &str, key: Option<&onestore::protected::Key>, offline: bool, @@ -371,7 +428,8 @@ pub fn section( if !notebook.replica_path(path)?.exists() { fetch(&root.join(path))?; } - let connect = |file: &Path| Ok(Coordinated(file.to_owned())); + let (root, cache) = (root.to_owned(), cache.to_owned()); + let connect = move |file: &Path| Ok(Coordinated::new(file, &root, &cache)); let section = match key { Some(key) => notebook.section_unlocked_with(path, key, connect, offline, notify)?, None => notebook.section_with(path, connect, offline, notify)?, @@ -400,7 +458,10 @@ pub fn lone_section( let section = Section::open_with( file, cache, - |file| Ok(Coordinated(file.to_owned())), + { + let cache = cache.to_owned(); + move |file: &Path| Ok(Coordinated::new(file, file, &cache)) + }, offline, notify, )?; @@ -414,9 +475,13 @@ pub fn lone_section( /// presenter or the folder's watch reports it. pub fn background( notebook: &Notebook, + root: &Path, + cache: &Path, notify: impl Fn() + Send + 'static, ) -> Result { - notebook.background_with(false, true, |file| Coordinated(file.to_owned()), notify) + let (root, cache) = (root.to_owned(), cache.to_owned()); + let remote = move |file: &Path| Coordinated::new(file, &root, &cache); + notebook.background_with(false, true, remote, notify) } struct Ivars { @@ -590,7 +655,7 @@ mod tests { let notebook = Notebook::create(&root, &cache, Notebook::NEW_COLOR, &page).unwrap(); assert!(ubiquitous(&root)); let path = notebook.catalog().sections[0].path.clone(); - let section = section(¬ebook, &root, &path, None, false, || {}).unwrap(); + let section = section(¬ebook, &root, &cache, &path, None, false, || {}).unwrap(); let (space, ..) = section.pages().unwrap()[0].clone(); let title = section .page(space) @@ -622,7 +687,7 @@ mod tests { .unwrap(); // Wakes skip the pause in typing, as leaving the window does. let file = root.join(&path); - let mut remote = Coordinated(file.clone()); + let mut remote = Coordinated::new(&file, &root, &cache); let deadline = std::time::Instant::now() + Duration::from_secs(30); loop { section.wake(); @@ -646,4 +711,81 @@ mod tests { let _ = std::fs::remove_dir_all(&cache); result.unwrap(); } + + /// This Mac's upload to a notebook in iCloud Drive is lost, another device's landing over + /// it with no conflict version kept: the next sync merges it back. Coordination works on + /// any folder, so a scratch one stands in for iCloud Drive's. + #[test] + fn an_upload_icloud_lost_merges_back() { + use onestore::op::{Edit, Op, PageOp}; + let folder = std::env::temp_dir().join(format!("snowbound-lost-{}", std::process::id())); + let (root, cache) = (folder.join("Notes"), folder.join("cache")); + std::fs::create_dir_all(&folder).unwrap(); + let page = onestore::PageCreation::new(None, Some("Shared"), "Test").unwrap(); + let notebook = Notebook::create(&root, &cache, Notebook::NEW_COLOR, &page).unwrap(); + let path = notebook.catalog().sections[0].path.clone(); + let file = root.join(&path); + let section = section(¬ebook, &root, &cache, &path, None, false, || {}).unwrap(); + let space = section.pages().unwrap()[0].0; + let stored = || { + let arena = onestore::Arena::default(); + let section = onestore::Section::open(&arena, std::fs::read(&file).unwrap()).unwrap(); + section.page(space).unwrap() + }; + let published = |what: &str, ready: &dyn Fn(&onestore::page::Page) -> bool| { + let deadline = std::time::Instant::now() + Duration::from_secs(20); + while !ready(&stored()) { + assert!( + std::time::Instant::now() < deadline, + "{what}: {:?}", + stored() + ); + section.wake(); + std::thread::sleep(Duration::from_millis(50)); + } + }; + let edit = |op| Edit { + at: 134_030_000_000_000_000, + ops: vec![Op::Page { space, op }], + }; + section + .apply("Test", edit(PageOp::Color(Some(0x0011_2233)))) + .unwrap(); + published("the shared edit", &|page| page.color == Some(0x0011_2233)); + // Another device's copy, sharing that edit. + let mut theirs = std::fs::read(&file).unwrap(); + let title = (stored().objects.iter()) + .find_map(|object| match object { + onestore::page::PageObject::Title(title) => { + title.outlines[0].paragraphs[0].text().map(|text| text.id) + } + _ => None, + }) + .unwrap(); + section + .apply( + "Test", + edit(PageOp::Text { + text: title, + range: 0..0, + with: "Mac ".into(), + }), + ) + .unwrap(); + published("this Mac's edit", &|page| page.title.starts_with("Mac ")); + let arena = onestore::Arena::default(); + let mut other = onestore::Section::open(&arena, theirs.clone()).unwrap(); + other + .apply("Other", &edit(PageOp::Color(Some(0x00ab_cdef)))) + .unwrap(); + other.seal().unwrap().unwrap().apply(&mut theirs).unwrap(); + // iCloud Drive: the other device's upload lands over this Mac's, with no version. + std::fs::write(&file, &theirs).unwrap(); + published("the lost edit merged back", &|page| { + page.title.starts_with("Mac ") && page.color == Some(0x00ab_cdef) + }); + section.close().unwrap(); + drop(notebook); + let _ = std::fs::remove_dir_all(&folder); + } } diff --git a/crates/snowbound/src/library.rs b/crates/snowbound/src/library.rs index 0c460aae14cae3050b0b1b98fa476600df1119da..c600c4d50a8fe7786a3322030772f4e43d907b27 100644 --- a/crates/snowbound/src/library.rs +++ b/crates/snowbound/src/library.rs @@ -413,7 +413,7 @@ impl Library { } let mut notebook = Notebook::open(location, cache); let (background, watch) = match &mut notebook { - Ok(notebook) => local_background(notebook, location), + Ok(notebook) => local_background(notebook, location, cache), Err(_) => (None, None), }; let name = display_name(cache, location).unwrap_or_else(|| file_name(Path::new(location))); @@ -819,7 +819,7 @@ impl Library { /// A notebook `Notebook::create` just made in the folder at `location`. pub fn created(location: &str, mut notebook: Notebook, cache: &Path) -> Self { - let (background, watch) = local_background(&mut notebook, location); + let (background, watch) = local_background(&mut notebook, location, cache); Self { background, watch, @@ -935,8 +935,9 @@ impl Library { let file = Path::new(&self.location).join(path); #[cfg(target_os = "macos")] if self.in_icloud() { - section.relocate_with(file, |file| { - Ok(crate::icloud::Coordinated(file.to_owned())) + let (root, cache) = (PathBuf::from(&self.location), self.cache.clone()); + section.relocate_with(file, move |file| { + Ok(crate::icloud::Coordinated::new(file, &root, &cache)) })?; } else { section.relocate(file)?; @@ -1024,6 +1025,7 @@ impl Library { crate::icloud::section( notebook, root, + &self.cache, path, key.as_ref(), offline(), @@ -1533,12 +1535,13 @@ fn read_tag_art(notebook: &Notebook) -> TagArt { fn local_background( notebook: &mut Notebook, location: &str, + cache: &Path, ) -> (Option>, Option>) { let reports = Arc::new(OnceLock::new()); let watch = watcher(location, Arc::clone(&reports)); let copies = !crate::watch::on_this_computer(Path::new(location)); let background = match crate::icloud::ubiquitous(Path::new(location)) { - true => crate::icloud::background(notebook, notify_background), + true => crate::icloud::background(notebook, Path::new(location), cache, notify_background), false => notebook.background(watch.is_some(), copies, notify_background), }; let Some(background) = background -- 2.54.1