diff --git a/crates/notebook/README.md b/crates/notebook/README.md index 3a02a464795ec7791184a2720b877826b0d9a395..94afba057499f4e6613a07de864fc7149526b61c 100644 --- a/crates/notebook/README.md +++ b/crates/notebook/README.md @@ -366,6 +366,19 @@ re-identified and listed anew. They run over `Storage`: a mounted directory header placement under native writer coordination); `tools/test_smb_structure.py VM OUTPUT` drives them against a disposable Samba lab VM. +`copy_section(path, replica, target, folder, after)` and `move_section(path, section, target, +folder, after)` are OneNote 2010's Move or Copy into another notebook (or this one opened +again): the section is written anew under new identities (`onestore::protected::rekey`), named +as it is or with the first free number after ("Beta 2"), with its colour, pages, versions and +conflict pages and the edits its replica holds, and listed in `target`'s folder after `after` +or last once it reads back whole, written until then under a name discovery passes over. A +copy that cannot be listed is deleted again. A move stops `section` first, then sends the +original to its notebook's recycle bin with `Storage::rename_unchanged`, under the writers' +coordination provided its stamp is still the copy's, and deletes its replica; where the copy +landed but the original changed since or cannot reach the bin, it stays and `Moved::kept` says +why. Queued edits waiting to rebase onto a file another client changed, or a file that can't +be reached, refuse both. + `Notebook::find_page(url)` resolves a stored internal link to a section path and page space by identity: the linked section first, then every readable section, so a link follows its page when the section is renamed or moved and @@ -410,7 +423,8 @@ another client removed does. A session open meanwhile stops as `SyncState::Prote section, to the end of this one as one `SectionOp::Import` queued like the user's own edits, under fresh identities (`Page::copy`); payloads travel with the model, so the copy publishes offline later like any edit. Content outside -the model refuses to copy. A move is an import here followed by +the model refuses to copy. `session::copied(page, author)` is that op, for a replica held +elsewhere. A move is an import here followed by `Section::delete_pages` there. `Notebook::refresh` rereads the directory on the caller's schedule and reports diff --git a/crates/notebook/src/session.rs b/crates/notebook/src/session.rs index 4cbf57ba5810b73cc9c7edc126ef8d5308bad2e6..6ad99987eee3a124377d81a2a20c69b1735736df 100644 --- a/crates/notebook/src/session.rs +++ b/crates/notebook/src/session.rs @@ -113,6 +113,30 @@ pub trait Storage: Send + Sync { /// Puts the file `with` in the place of the section or TOC at `path` under the coordination /// its writers take, provided `path` still has `base`'s stamp. fn supersede(&self, path: &str, base: &Stamp, with: &str) -> Result<()>; + /// Renames the section file `from` to `to` provided it still has `base`'s stamp, under the + /// coordination its writers take where the storage has one, else just after checking. + fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> { + if self.stamp(from)? != *base { + return Err(changed()); + } + self.rename(from, to) + } +} + +/// Makes the entry naming `path` in its folder durable, where the system lets a folder sync. +fn synced_folder(path: &Path) -> Result<()> { + #[cfg(unix)] + if let Some(folder) = path.parent() { + fs::File::open(folder)?.sync_all()?; + } + #[cfg(not(unix))] + let _ = path; + Ok(()) +} + +/// A section that changed while it moved, which stays where it was. +fn changed() -> Error { + io::Error::other("The section changed while it moved").into() } struct Publication { @@ -201,8 +225,10 @@ impl Storage for Directory { { return Err(io::Error::from(io::ErrorKind::AlreadyExists).into()); } - fs::File::create_new(path)?.write_all(bytes)?; - Ok(()) + let mut file = fs::File::create_new(&path)?; + file.write_all(bytes)?; + file.sync_all()?; + synced_folder(&path) } fn create_directory(&self, path: &str) -> Result<()> { @@ -251,8 +277,17 @@ impl Storage for Directory { Ok(()) } + /// Refuses a name taken by a file, or by one iCloud Drive keeps elsewhere as a placeholder. fn rename(&self, from: &str, to: &str) -> Result<()> { - Ok(fs::rename(self.path(from), self.path(to))?) + let to = self.path(to); + if fs::symlink_metadata(&to).is_ok() + || discover::placeholder(&to) + .is_some_and(|placeholder| fs::metadata(placeholder).is_ok()) + { + return Err(io::Error::from(io::ErrorKind::AlreadyExists).into()); + } + fs::rename(self.path(from), &to)?; + synced_folder(&to) } /// A local file system shows no other writer's hold; one that refuses to rename a folder @@ -425,6 +460,12 @@ impl Storage for Share { .client .supersede(&self.path(path), base, &self.path(with))?) } + + fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> { + Ok(self + .client + .rename_unchanged(&self.path(from), &self.path(to), base)?) + } } pub(crate) const LIMITS: discover::Limits = discover::Limits { @@ -1127,7 +1168,19 @@ impl Notebook { /// Moves the section file at `path`, its folder's `entry`, into the recycle bin, under a /// name no binned section has, and lists it there; its own folder's TOC is the caller's. fn bin_section(&self, path: &str, entry: &Entry) -> Result<()> { - let filename = &entry.filename; + let binned = self.send_to_bin(path, &entry.filename, None)?; + self.list_binned(binned, entry) + } + + /// Moves the section file at `path`, named `filename`, into the recycle bin under a name no + /// binned section has, provided it still has `base`'s stamp where one is given: the bin's + /// TOC and identity, and the name it took there. + fn send_to_bin( + &self, + path: &str, + filename: &str, + base: Option<&Stamp>, + ) -> Result<(String, [u8; 16], String)> { let (bin_toc, bin_identity) = self.bin()?; let mut target = filename.to_owned(); let mut attempt = 1; @@ -1137,11 +1190,24 @@ impl Notebook { target = format!("{stem} ({attempt}).{extension}"); } let binned = catalog_path(RECYCLE_BIN, &target); - self.storage.rename(path, &binned)?; + match base { + Some(base) => self.storage.rename_unchanged(path, &binned, base)?, + None => self.storage.rename(path, &binned)?, + } + Ok((bin_toc, bin_identity, target)) + } + + /// Places and lists the section `entry`, which `send_to_bin` moved, in the recycle bin. + fn list_binned( + &self, + (bin_toc, bin_identity, target): (String, [u8; 16], String), + entry: &Entry, + ) -> Result<()> { let (false, Some(identity)) = (entry.copy, entry.identity) else { return Ok(()); }; - self.storage.place(&binned, bin_identity, &target)?; + self.storage + .place(&catalog_path(RECYCLE_BIN, &target), bin_identity, &target)?; self.commit_toc( &bin_toc, &[onestore::TocEdit::Add { @@ -1434,6 +1500,197 @@ impl Notebook { Ok(target) } + /// Copies the section at catalog `path`, whose replica is `replica`, into the folder + /// `folder` of `target`, after its section `after` or else last, as OneNote 2010's Move or + /// Copy puts a section in another notebook: a file of its own under new identities + /// (`protected::rekey`), named as the section is or with the first free number after it + /// ("Beta 2"), with its colour, pages, versions and conflict pages, and the edits the + /// replica holds that the file does not yet. Refused, `WouldBlock`, while those wait to be + /// rebased onto a file another client changed, and while the file can't be reached, as the + /// replica may be behind it. `target` may be this notebook opened again. The copy is listed + /// once it reads back whole, and taken away again where it cannot be. Returns its catalog + /// path in `target`. + pub fn copy_section( + &self, + path: &str, + replica: &Replica, + target: &mut Notebook, + folder: &str, + after: Option<&str>, + ) -> Result { + let (image, _) = self.copied(path, replica)?; + target.place_copy(image, split(path).1, folder, after) + } + + /// Moves the section at catalog `path`, open as `section`, as `copy_section` copies it, + /// then sends it to this notebook's recycle bin as `delete` does, and deletes its replica, + /// whose edits the copy holds. `section` stops first, so nothing publishes to or rebases it + /// meanwhile. The section stays where the copy could not be made, and also, as + /// `Moved::kept` then says, where it changed after it was copied or could not reach the bin. + pub fn move_section( + &mut self, + path: &str, + section: Section, + target: &mut Notebook, + folder: &str, + after: Option<&str>, + ) -> Result { + let replica = Arc::clone(section.replica()); + section.close()?; + let (image, stamp) = self.copied(path, &replica)?; + let newest = replica.pending()?.last().map(|edit| edit.id); + let copy = target.place_copy(image, split(path).1, folder, after)?; + let (from, name) = split(path); + let entry = self.entry(from, name)?; + let file = self.replica_path(path); + let left = (|| { + if replica.pending()?.last().map(|edit| edit.id) != newest { + return Err(changed()); + } + self.send_to_bin(path, &entry.filename, Some(&stamp)) + })(); + let binned = match left { + Ok(binned) => binned, + Err(error) => { + return Ok(Moved { + path: copy, + kept: Some(error), + }); + } + }; + // The section has left. What lists it is tidied as far as it can be: OneNote 2010 + // leaves a moved section's TOC entry, and a catalog read failing now is the next one's. + let _ = self + .list_binned(binned, &entry) + .and_then(|()| match entry.identity { + Some(identity) => self.edit_toc(from, &[onestore::TocEdit::Remove { identity }]), + None => Ok(()), + }); + let _ = self.refresh(); + drop(replica); + if let Ok(file) = file { + let _ = remove_replica(&file); + } + Ok(Moved { + path: copy, + kept: None, + }) + } + + /// The section at catalog `path`, whose replica is `replica`, as a copy of it carries it, + /// under new identities, and the stamp of the file it was taken from. + fn copied(&self, path: &str, replica: &Replica) -> Result<(Vec, Stamp)> { + replica.written()?; + let stamp = self.storage.stamp(path)?; + let (image, stamp) = if replica.pending()?.is_empty() { + let image = self.storage.read(path)?; + let stamp = Stamp::of(&image)?; + (image, stamp) + } else if stamp == crate::base::base_stamp(&*replica.lock()?)? { + (replica.snapshot()?, stamp) + } else { + return Err(io::Error::new( + io::ErrorKind::WouldBlock, + "Edits to the section are still syncing", + ) + .into()); + }; + let key = replica.section.key.as_ref(); + Ok((protected::rekey(&image, key, key)?, stamp)) + } + + /// Lists the section file `image` holds in `folder`, after its section `after` or last, + /// under `filename` or the first free number after its name; its catalog path. It is + /// written under a name discovery passes over, and takes its own once it reads back whole, + /// so that a copy cut short is never taken for a section. + fn place_copy( + &mut self, + mut image: Vec, + filename: &str, + folder: &str, + after: Option<&str>, + ) -> Result { + let parent = self.folder(folder)?; + let order: Vec<[u8; 16]> = match after { + Some(after) => { + let at = (parent.sections.iter()) + .position(|section| section.path == after) + .ok_or_else(|| io::Error::from(io::ErrorKind::NotFound))?; + (parent.sections[..=at].iter()) + .filter_map(|section| Entry::of(parent, section).identity) + .collect() + } + None => Vec::new(), + }; + let stem = match filename.len().checked_sub(4) { + Some(at) + if filename + .get(at..) + .is_some_and(|end| end.eq_ignore_ascii_case(".one")) => + { + &filename[..at] + } + _ => filename, + }; + let name = |n| match n { + 1 => format!("{stem}.one"), + n => format!("{stem} {n}.one"), + }; + let (_, ancestor) = self.toc(folder)?; + onestore::place_image(&mut image, Some((ancestor, &name(1))))?; + let identity = onestore::Store::parse(&image)?.header.file_id; + let written = catalog_path(folder, &format!(".{stem}.one.snowbound")); + if self.storage.exists(&written) { + self.storage.delete(&written)?; + } + self.storage.create(&written, &image)?; + let named = (|| { + if self.storage.read(&written)? != image { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "The copy didn't read back as it was written", + ) + .into()); + } + for n in 1.. { + if n > 1 { + self.storage.place(&written, ancestor, &name(n))?; + } + match self + .storage + .rename(&written, &catalog_path(folder, &name(n))) + { + Err(Error::Io(error)) if error.kind() == io::ErrorKind::AlreadyExists => {} + renamed => return renamed.map(|()| name(n)), + } + } + unreachable!("a free name") + })(); + let filename = match named { + Ok(filename) => filename, + Err(error) => { + let _ = self.storage.delete(&written); + return Err(error); + } + }; + let path = catalog_path(folder, &filename); + let mut edits = vec![onestore::TocEdit::Add { + filename, + identity, + group: false, + }]; + if !order.is_empty() { + edits.push(onestore::TocEdit::Order([order, vec![identity]].concat())); + } + if let Err(error) = self.edit_toc(folder, &edits) { + let _ = self.storage.delete(&path); + return Err(error); + } + // The copy stands listed; a catalog read failing now is the next one's to repair. + let _ = self.refresh(); + Ok(path) + } + /// The stored filename and TOC identity of a folder's section or group. fn entry(&self, folder: &str, name: &str) -> Result { let parent = self.folder(folder)?; @@ -1875,6 +2132,14 @@ impl Notebook { } } +/// A section `Notebook::move_section` moved: its catalog path in the notebook it went to, +/// and why it also stayed where it was, where it could not leave once copied. +#[derive(Debug)] +pub struct Moved { + pub path: String, + pub kept: Option, +} + /// A section or group as its folder holds it, for the structure operations. struct Entry { filename: String, @@ -2014,6 +2279,15 @@ pub fn moved(page: &Page, author: &str) -> Result { })) } +/// The op putting a copy of `page` last in another section, as OneNote copies a page: under +/// fresh identities, its page identity too, and titled as it is. +pub fn copied(page: &Page, author: &str) -> Result { + Ok(Op::Section(SectionOp::Import { + creation: PageCreation::new(None, Some(&page.title), author)?, + page: page.copy()?, + })) +} + /// The edits putting the pages `moved` where `order` lists them, with their levels, in a /// section now listing `listed`: each goes before the next page of `order` that stays put /// and is still listed. Pages gone from the section are left out. @@ -2677,23 +2951,18 @@ impl Section { ) -> impl Future> + Send + 'static + use<> { let replica = Arc::clone(&self.replica); let author = author.to_owned(); - let creation = PageCreation::new(None, Some(&page.title), &author); - let page = page.copy(); + let op = copied(page, &author); async move { - let creation = creation?; + let op = op?; + let Op::Section(SectionOp::Import { creation, .. }) = &op else { + unreachable!("a copy is an import") + }; let space = creation.space(); - replica - .apply_async( - &author, - Edit { - at: crate::now(), - ops: vec![Op::Section(SectionOp::Import { - creation, - page: page?, - })], - }, - ) - .await?; + let edit = Edit { + at: crate::now(), + ops: vec![op], + }; + replica.apply_async(&author, edit).await?; Ok(space) } } diff --git a/crates/notebook/src/session/tests.rs b/crates/notebook/src/session/tests.rs index e922fb84a50214a546c27ed10dfe8f16d47df4a7..c8d38b9a52b2aa23387bc60e2f8812f375cf1375 100644 --- a/crates/notebook/src/session/tests.rs +++ b/crates/notebook/src/session/tests.rs @@ -194,6 +194,8 @@ struct FaultStorage { inner: Directory, revision: Mutex>, receipt: Option>>, + /// Placing a section file in its folder fails. + misplace: bool, } #[derive(Default)] @@ -261,6 +263,9 @@ impl Storage for FaultStorage { self.inner.delete(path) } fn place(&self, path: &str, ancestor: [u8; 16], name: &str) -> Result<()> { + if self.misplace && path.ends_with(".one") { + return Err(io::Error::from(io::ErrorKind::PermissionDenied).into()); + } self.inner.place(path, ancestor, name) } fn commit(&self, path: &str, transaction: &Transaction) -> Result<()> { @@ -293,6 +298,98 @@ impl Storage for FaultStorage { self.race(path); self.inner.supersede(path, base, with) } + fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> { + self.race(from); + self.inner.rename_unchanged(from, to, base) + } +} + +/// A notebook holding "Mine" in its first section and a notebook to move it to, the first on +/// `storage` made from its folder; the section's catalog path. +fn moving( + temporary: &Path, + storage: impl FnOnce(Directory, Option<(String, Transaction)>) -> FaultStorage, + theirs: bool, +) -> (Notebook, Notebook, String) { + let (folder, cache) = (temporary.join("notebook"), temporary.join("cache")); + let creation = PageCreation::new(None, Some("Mine"), "Fixture").unwrap(); + let notebook = Notebook::create(&folder, &cache, 0x00f0_a0c0, &creation).unwrap(); + let path = notebook.catalog().sections[0].path.clone(); + // Another writer's revision, which the storage commits as the move checks the file. + let revision = theirs.then(|| { + let arena = onestore::Arena::default(); + let image = notebook.read_section(&path).unwrap(); + let mut section = onestore::Section::open(&arena, image).unwrap(); + let theirs = PageCreation::new(None, Some("Theirs"), "OneNote").unwrap(); + let edit = Edit { + at: crate::now(), + ops: vec![Op::Section(SectionOp::Create(theirs))], + }; + section.apply("OneNote", &edit).unwrap(); + (path.clone(), section.seal().unwrap().unwrap()) + }); + drop(notebook); + let root = folder.canonicalize().unwrap(); + let storage = storage(Directory(root.clone()), revision); + let source = Notebook::with(Box::new(storage), Some(root), &cache).unwrap(); + let target = Notebook::create(temporary.join("target"), &cache, 0x91baae, &creation).unwrap(); + (source, target, path) +} + +fn titles(image: &[u8]) -> Vec { + (stored_pages(image).unwrap().into_iter()) + .map(|stored| stored.page.title) + .collect() +} + +/// A revision another writer commits after the copy is made keeps the section where it was, +/// with the revision, beside the copy. +#[test] +fn a_section_changed_while_it_moves_stays_where_it_was() { + let temporary = tempfile::tempdir().unwrap(); + let (mut source, mut target, path) = moving( + temporary.path(), + |inner, revision| FaultStorage { + inner, + revision: Mutex::new(revision), + receipt: None, + misplace: false, + }, + true, + ); + let section = source.section(&path, false, || {}).unwrap(); + let moved = (source.move_section(&path, section, &mut target, "", None)).unwrap(); + assert!(moved.kept.is_some()); + assert_eq!( + titles(&source.read_section(&path).unwrap()), + ["Mine", "Theirs"] + ); + assert_eq!(titles(&target.read_section(&moved.path).unwrap()), ["Mine"]); +} + +/// Once the section has left for the bin, a listing that fails to follow it leaves a move, not +/// a section said to have stayed. +#[test] +fn a_move_whose_bin_listing_fails_has_still_moved() { + let temporary = tempfile::tempdir().unwrap(); + let (mut source, mut target, path) = moving( + temporary.path(), + |inner, _| FaultStorage { + inner, + revision: Mutex::new(None), + receipt: None, + misplace: true, + }, + false, + ); + let section = source.section(&path, false, || {}).unwrap(); + let replica = source.replica_path(&path).unwrap(); + let moved = (source.move_section(&path, section, &mut target, "", None)).unwrap(); + assert!(moved.kept.is_none(), "{:?}", moved.kept); + assert!(!source.storage.exists(&path)); + assert!(source.storage.exists(&catalog_path(RECYCLE_BIN, &path))); + assert_eq!(titles(&target.read_section(&moved.path).unwrap()), ["Mine"]); + assert!(!replica.exists(), "the original's replica goes with it"); } /// A revision another writer commits while a password is being set is never overwritten: @@ -330,6 +427,7 @@ fn a_password_never_overwrites_a_concurrent_revision() { inner: Directory(root.clone()), revision: Mutex::new(Some((path.clone(), revision))), receipt: None, + misplace: false, }; let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap(); let set = notebook.set_password(&path, None, Some("secret")); @@ -368,6 +466,7 @@ fn recycling_confirms_uncertain_toc_and_page_appends_and_undo() { inner: Directory(root.clone()), revision: Mutex::new(None), receipt: Some(receipt.clone()), + misplace: false, }; let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap(); notebook @@ -439,6 +538,7 @@ fn recycling_rejects_unconfirmed_appends_and_can_retry_each_stage() { inner: Directory(root.clone()), revision: Mutex::new(None), receipt: Some(receipt.clone()), + misplace: false, }; let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap(); assert!( diff --git a/crates/notebook/src/smb/mod.rs b/crates/notebook/src/smb/mod.rs index c7376e942a4eba856ea34b0ce1c5e62836eca439..80b0dd647e613eb6210ca2d228e62f5689ae4b63 100644 --- a/crates/notebook/src/smb/mod.rs +++ b/crates/notebook/src/smb/mod.rs @@ -524,6 +524,22 @@ impl Client { self.delete(&aside).map_err(failed(CommitState::Committed)) } + /// Renames the revision store at `path` to `to`, an existing `to` an error, provided `path` + /// still has `base`'s stamp, under writer coordination. + pub(crate) fn rename_unchanged( + &self, + path: &str, + to: &str, + base: &onestore::Stamp, + ) -> Result<(), CommitError> { + self.commit(path, &checked(base), |file| { + (base.check(file).and_then(|()| self.rename(path, to))).map_err(|error| CommitError { + state: CommitState::NotCommitted, + error, + }) + }) + } + /// Opens `path` for writing under OneNote's coordination, making `reads` with the locks. fn commit( &self, diff --git a/crates/notebook/tests/move_section.rs b/crates/notebook/tests/move_section.rs new file mode 100644 index 0000000000000000000000000000000000000000..b5967d88d4eb3170b1477dd6b95dbcc576ee6d6e --- /dev/null +++ b/crates/notebook/tests/move_section.rs @@ -0,0 +1,299 @@ +//! Moving and copying a section into another notebook, as OneNote 2010's Move or Copy does. + +use notebook::session::{Notebook, stored_pages}; +use onestore::{ + PageCreation, + op::{Edit, Op, SectionOp}, +}; +use std::path::{Path, PathBuf}; + +struct Notebooks { + _temporary: tempfile::TempDir, + cache: PathBuf, + source: PathBuf, + target: PathBuf, +} + +/// A notebook holding Alpha and Beta beside "New Section 1", and another holding Gamma. +fn notebooks() -> Notebooks { + let temporary = tempfile::tempdir().unwrap(); + let cache = temporary.path().join("cache"); + let page = PageCreation::new(None, Some("First page"), "Author").unwrap(); + let [source, target] = ["Source", "Target"].map(|name| temporary.path().join(name)); + let mut notebook = Notebook::create(&source, &cache, 0x00d7ff, &page).unwrap(); + for name in ["Alpha", "Beta"] { + notebook.create_section("", name, &page).unwrap(); + } + let mut notebook = Notebook::create(&target, &cache, 0x91baae, &page).unwrap(); + notebook.create_section("", "Gamma", &page).unwrap(); + Notebooks { + _temporary: temporary, + cache, + source, + target, + } +} + +fn sections(notebook: &Notebook) -> Vec { + (notebook.catalog().sections.iter()) + .map(|section| section.path.clone()) + .collect() +} + +fn titles(file: &Path) -> Vec { + stored_pages(&std::fs::read(file).unwrap()) + .unwrap() + .into_iter() + .map(|stored| stored.page.title) + .collect() +} + +/// The file identity, root object space and colour a section file holds. +fn identities(file: &Path) -> ([u8; 16], onestore::ExGuid, Option) { + let image = std::fs::read(file).unwrap(); + let store = onestore::Store::parse(&image).unwrap(); + let index = onestore::RevisionIndex::parse(&store).unwrap(); + let document = onestore::document::Document::parse(&index).unwrap(); + let revision = document.active(document.root).unwrap(); + let onestore::document::Kind::SectionMetadata { color, .. } = + &revision.nodes[&revision.roots[&2]].kind + else { + panic!("no section metadata") + }; + (store.header.file_id, index.root, *color) +} + +fn queue_page(section: ¬ebook::session::Section, title: &str) { + let creation = PageCreation::new(None, Some(title), "Author").unwrap(); + section + .apply( + "Author", + Edit { + at: 133_000_000_000_000_000, + ops: vec![Op::Section(SectionOp::Create(creation))], + }, + ) + .unwrap(); + section.written().unwrap(); +} + +#[test] +fn a_moved_section_carries_its_queued_edits_and_its_original_goes_to_the_bin() { + let place = notebooks(); + let mut source = Notebook::open(&place.source, &place.cache).unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + let before = identities(&place.source.join("Alpha.one")); + // Offline, so the edit waits in the replica, published nowhere. + let section = source.section("Alpha.one", true, || {}).unwrap(); + queue_page(§ion, "Queued page"); + assert!(!section.pending().unwrap().is_empty()); + let moved = source + .move_section( + "Alpha.one", + section, + &mut target, + "", + Some("New Section 1.one"), + ) + .unwrap(); + assert_eq!(moved.path, "Alpha.one"); + assert!(moved.kept.is_none(), "{:?}", moved.kept); + + assert_eq!( + sections(&target), + ["New Section 1.one", "Alpha.one", "Gamma.one"] + ); + let copy = place.target.join("Alpha.one"); + let after = identities(©); + assert_ne!(after.0, before.0, "a file identity of its own"); + assert_ne!(after.1, before.1, "object spaces of its own"); + assert_eq!(after.2, before.2, "its colour"); + assert_eq!(titles(©), ["First page", "Queued page"]); + let header = onestore::Store::parse(&std::fs::read(©).unwrap()) + .unwrap() + .header; + let toc = target.catalog().toc.as_ref().unwrap().file_id; + assert_eq!(header.ancestor, toc, "placed under its TOC"); + + assert_eq!(sections(&source), ["New Section 1.one", "Beta.one"]); + assert!(source.catalog().toc.as_ref().unwrap().unresolved.is_empty()); + let bin = &source.catalog().groups[0]; + assert_eq!(bin.path, "OneNote_RecycleBin"); + assert_eq!(bin.sections[0].path, "OneNote_RecycleBin/Alpha.one"); + assert_eq!( + identities(&place.source.join("OneNote_RecycleBin/Alpha.one")), + before + ); + + // Each notebook read cold lists the section where it went. + let source = Notebook::open(&place.source, &place.cache).unwrap(); + let target = Notebook::open(&place.target, &place.cache).unwrap(); + assert_eq!(sections(&source), ["New Section 1.one", "Beta.one"]); + assert_eq!( + sections(&target), + ["New Section 1.one", "Alpha.one", "Gamma.one"] + ); +} + +#[test] +fn a_copy_takes_the_next_free_name_and_leaves_the_section_where_it_is() { + let place = notebooks(); + let source = Notebook::open(&place.source, &place.cache).unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + let beta = source.section("Beta.one", false, || {}).unwrap(); + for expected in ["Beta.one", "Beta 2.one"] { + let copy = source + .copy_section("Beta.one", beta.replica(), &mut target, "", None) + .unwrap(); + assert_eq!(copy, expected); + } + assert_eq!( + sections(&target), + ["New Section 1.one", "Gamma.one", "Beta.one", "Beta 2.one"] + ); + // Into its own notebook, beside itself, with an identity of its own. + let mut again = Notebook::open(&place.source, &place.cache).unwrap(); + let copy = source + .copy_section("Beta.one", beta.replica(), &mut again, "", Some("Beta.one")) + .unwrap(); + assert_eq!(copy, "Beta 2.one"); + assert_eq!( + sections(&again), + ["New Section 1.one", "Alpha.one", "Beta.one", "Beta 2.one"] + ); + let [original, copied] = + ["Beta.one", "Beta 2.one"].map(|name| identities(&place.source.join(name))); + assert_ne!(original.1, copied.1); + assert_ne!( + again.replica_path("Beta.one").unwrap(), + again.replica_path("Beta 2.one").unwrap(), + "a replica of its own" + ); + assert!(place.source.join("Beta.one").exists()); +} + +#[test] +fn a_section_without_waiting_edits_moves_and_its_replica_goes() { + let place = notebooks(); + let mut source = Notebook::open(&place.source, &place.cache).unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + let section = source.section("Beta.one", false, || {}).unwrap(); + let replica = source.replica_path("Beta.one").unwrap(); + assert!(replica.exists()); + let moved = source + .move_section("Beta.one", section, &mut target, "", None) + .unwrap(); + assert!(moved.kept.is_none(), "{:?}", moved.kept); + assert_eq!(titles(&place.target.join(&moved.path)), ["First page"]); + assert!(place.source.join("OneNote_RecycleBin/Beta.one").exists()); + assert!(!replica.exists()); +} + +/// A name iCloud Drive holds for a section it keeps elsewhere is taken, and a copy cut short +/// leaves nothing a notebook lists. +#[test] +fn a_copy_passes_over_a_name_icloud_holds() { + let place = notebooks(); + let source = Notebook::open(&place.source, &place.cache).unwrap(); + std::fs::write(place.target.join(".Beta.one.icloud"), b"").unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + let beta = source.section("Beta.one", false, || {}).unwrap(); + let copy = source + .copy_section("Beta.one", beta.replica(), &mut target, "", None) + .unwrap(); + assert_eq!(copy, "Beta 2.one"); + let hidden: Vec<_> = (std::fs::read_dir(&place.target).unwrap()) + .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()) + .filter(|name| name.ends_with(".snowbound")) + .collect(); + assert!(hidden.is_empty(), "{hidden:?}"); +} + +/// A section whose file can't be reached is not copied from its replica, which may be behind. +#[test] +fn a_copy_of_a_section_out_of_reach_is_refused() { + let place = notebooks(); + let source = Notebook::open(&place.source, &place.cache).unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + let beta = source.section("Beta.one", true, || {}).unwrap(); + std::fs::rename(place.source.join("Beta.one"), place.source.join("Away.bak")).unwrap(); + assert!( + source + .copy_section("Beta.one", beta.replica(), &mut target, "", None) + .is_err() + ); + assert!(!place.target.join("Beta.one").exists()); +} + +#[test] +fn a_move_that_cannot_reach_the_bin_keeps_the_section_in_both_notebooks() { + let place = notebooks(); + let mut source = Notebook::open(&place.source, &place.cache).unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + // The bin can't be made: a file stands where its folder would. + std::fs::write(place.source.join("OneNote_RecycleBin"), b"").unwrap(); + let original = std::fs::read(place.source.join("Alpha.one")).unwrap(); + let section = source.section("Alpha.one", true, || {}).unwrap(); + queue_page(§ion, "Queued page"); + let replica = source.replica_path("Alpha.one").unwrap(); + let moved = source + .move_section("Alpha.one", section, &mut target, "", None) + .unwrap(); + assert!(moved.kept.is_some()); + assert_eq!( + titles(&place.target.join(&moved.path)), + ["First page", "Queued page"] + ); + assert_eq!( + std::fs::read(place.source.join("Alpha.one")).unwrap(), + original + ); + assert_eq!( + sections(&Notebook::open(&place.source, &place.cache).unwrap()), + ["New Section 1.one", "Alpha.one", "Beta.one"] + ); + // The edit still waits in the original's replica, to publish there too. + let replica = notebook::Replica::open(replica).unwrap(); + assert_eq!(replica.pending().unwrap().len(), 1); +} + +#[test] +fn a_copy_the_notebook_cannot_list_leaves_nothing_behind() { + let place = notebooks(); + let source = Notebook::open(&place.source, &place.cache).unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + let toc = place.target.join("Open Notebook.onetoc2"); + let mut permissions = std::fs::metadata(&toc).unwrap().permissions(); + permissions.set_readonly(true); + std::fs::set_permissions(&toc, permissions).unwrap(); + let alpha = source.section("Alpha.one", false, || {}).unwrap(); + assert!( + source + .copy_section("Alpha.one", alpha.replica(), &mut target, "", None) + .is_err() + ); + assert!(!place.target.join("Alpha.one").exists()); + assert!(place.source.join("Alpha.one").exists()); +} + +#[test] +fn a_move_waits_while_queued_edits_have_a_changed_file_to_rebase_onto() { + let place = notebooks(); + let mut source = Notebook::open(&place.source, &place.cache).unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + let section = source.section("Alpha.one", true, || {}).unwrap(); + queue_page(§ion, "Queued page"); + // Another client's change, which the replica has not read. + source + .set_section_color("Alpha.one", Some(0x5ed7ff)) + .unwrap(); + let error = source + .move_section("Alpha.one", section, &mut target, "", None) + .unwrap_err(); + assert!( + matches!(&error, notebook::Error::Io(error) if error.kind() == std::io::ErrorKind::WouldBlock), + "{error}" + ); + assert!(!place.target.join("Alpha.one").exists()); + assert!(place.source.join("Alpha.one").exists()); +} diff --git a/crates/onestore/src/protected/mod.rs b/crates/onestore/src/protected/mod.rs index 6896fcd68dbd57b9588ee8eca9964ad7ecd9bcac..b12c129c34b1204b5646360fe25f50e0752e6ec2 100644 --- a/crates/onestore/src/protected/mod.rs +++ b/crates/onestore/src/protected/mod.rs @@ -332,7 +332,8 @@ impl Key { } /// The section `image` written anew, under `new` or in the clear, as OneNote 2010 rewrites -/// a section whose password it sets, changes or removes: the file and its object spaces +/// a section whose password it sets, changes or removes, or that it moves or copies into +/// another notebook: the file and its object spaces /// take new identities, each space keeps its labelled revisions, every one a checkpoint, /// and the payloads come along. A protected `image` opens under `key`. pub fn rekey(image: &[u8], key: Option<&Key>, new: Option<&Key>) -> Result> {