| ... | @@ -6,6 +6,7 @@ | ... | @@ -6,6 +6,7 @@ |
| 6 | use block2::{RcBlock, StackBlock}; | 6 | use block2::{RcBlock, StackBlock}; |
| 7 | use notebook::{ | 7 | use notebook::{ |
| 8 | Remote, Version, | 8 | Remote, Version, |
| | 9 | published::{Published, Written}, |
| 9 | session::{Background, Notebook, Section}, | 10 | session::{Background, Notebook, Section}, |
| 10 | }; | 11 | }; |
| 11 | use objc2::{ | 12 | use 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)] |
| | 18 | use objc2_foundation::NSHost; |
| 16 | use objc2_foundation::{ | 19 | use 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 | }; |
| 32 | | 35 | |
| ... | @@ -238,8 +241,13 @@ pub fn download(root: &Path) -> usize { | ... | @@ -238,8 +241,13 @@ pub fn download(root: &Path) -> usize { |
| 238 | const SF_DATALESS: u32 = 0x4000_0000; | 241 | const SF_DATALESS: u32 = 0x4000_0000; |
| 239 | | 242 | |
| 240 | /// A section file in iCloud Drive, read and published under file coordination, with the | 243 | /// 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 |
| 242 | pub struct Coordinated(pub PathBuf); | 245 | /// until the file holds them: an upload another device's replaced merges back |
| | 246 | /// (`notebook::published`). |
| | 247 | pub struct Coordinated { |
| | 248 | file: PathBuf, |
| | 249 | published: Option<Published>, |
| | 250 | } |
| 243 | | 251 | |
| 244 | fn uncommitted(error: io::Error) -> CommitError { | 252 | fn 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 | } |
| 250 | | 258 | |
| 251 | impl Coordinated { | 259 | impl 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> { |
| 272 | | 296 | |
| 273 | impl Remote for Coordinated { | 297 | impl 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 | } |
| 278 | | 302 | |
| 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 | } |
| 292 | | 316 | |
| 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 | } |
| 297 | | 330 | |
| 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 | } |
| 301 | | 337 | |
| | 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(self | 340 | 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 | } |
| 315 | | 359 | |
| 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 | } |
| 326 | | 377 | |
| | 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 { |
| 363 | pub fn section( | 419 | pub 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. |
| 415 | pub fn background( | 476 | pub 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 | } |
| 421 | | 486 | |
| 422 | struct Ivars { | 487 | struct 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 = section | 660 | 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 | } |