authoravatarclover caruso<git@paperclover.net>2026-10-19 11:26:32-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 22:58:56-07:00
log292d28fbcc025e5fdbdf777b2783867d9b9346ce
tree2fcfbca7e3586d85d51a13f406fe4650bd1e1892
parent29745d45b154e4fdf378bd73d297e5b657c23c5b
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(notebook): a section moves or copies into another notebook whole

Notebook::copy_section and move_section do what OneNote 2010's Move or Copy does with a section and another notebook, as watched in the lab: the section is written anew under new file and object space identities, named as it was or with the first free number after it ("Beta 2"), keeping its colour and every page, and listed into a folder last or after a section. Edits waiting in its replica come along; a section whose file can't be reached is refused, as its replica may be behind it. The copy is written under a name discovery passes over and takes its own only once it reads back whole, so a copy cut short is never listed. A move then sends the original to its notebook's recycle bin, where OneNote deletes it, through Storage::rename_unchanged: under the writers' coordination on a share, and only while the original's stamp is still the one copied, so another client's commit keeps it in place beside the copy and the move says so. Once it has left, a TOC that fails to follow is left as OneNote leaves its own, and the original's replica, whose edits the copy holds, is deleted. A copy that cannot be listed is deleted again. A local file created or renamed is flushed to disk with its folder entry before the call returns, and a local rename refuses a name a file, or an iCloud Drive placeholder, holds. Refs #105 Assisted-by: claude-opus-5.5

6 files changed, 722 insertions(+), 23 deletions(-)

crates/notebook/README.md+15-1
...@@ -366,6 +366,19 @@ re-identified and listed anew. They run over `Storage`: a mounted directory...@@ -366,6 +366,19 @@ re-identified and listed anew. They run over `Storage`: a mounted directory
366header placement under native writer coordination); `tools/test_smb_structure.py366header placement under native writer coordination); `tools/test_smb_structure.py
367VM OUTPUT` drives them against a disposable Samba lab VM.367VM OUTPUT` drives them against a disposable Samba lab VM.
368368
369`copy_section(path, replica, target, folder, after)` and `move_section(path, section, target,
370folder, after)` are OneNote 2010's Move or Copy into another notebook (or this one opened
371again): the section is written anew under new identities (`onestore::protected::rekey`), named
372as it is or with the first free number after ("Beta 2"), with its colour, pages, versions and
373conflict pages and the edits its replica holds, and listed in `target`'s folder after `after`
374or last once it reads back whole, written until then under a name discovery passes over. A
375copy that cannot be listed is deleted again. A move stops `section` first, then sends the
376original to its notebook's recycle bin with `Storage::rename_unchanged`, under the writers'
377coordination provided its stamp is still the copy's, and deletes its replica; where the copy
378landed but the original changed since or cannot reach the bin, it stays and `Moved::kept` says
379why. Queued edits waiting to rebase onto a file another client changed, or a file that can't
380be reached, refuse both.
381
369`Notebook::find_page(url)` resolves a stored internal link to a section path382`Notebook::find_page(url)` resolves a stored internal link to a section path
370and page space by identity: the linked section first, then every readable383and page space by identity: the linked section first, then every readable
371section, so a link follows its page when the section is renamed or moved and384section, 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...@@ -410,7 +423,8 @@ another client removed does. A session open meanwhile stops as `SyncState::Prote
410section, to the end of this one as one `SectionOp::Import` queued like the423section, to the end of this one as one `SectionOp::Import` queued like the
411user's own edits, under fresh identities (`Page::copy`); payloads travel with424user's own edits, under fresh identities (`Page::copy`); payloads travel with
412the model, so the copy publishes offline later like any edit. Content outside425the model, so the copy publishes offline later like any edit. Content outside
413the model refuses to copy. A move is an import here followed by426the model refuses to copy. `session::copied(page, author)` is that op, for a replica held
427elsewhere. A move is an import here followed by
414`Section::delete_pages` there.428`Section::delete_pages` there.
415429
416`Notebook::refresh` rereads the directory on the caller's schedule and reports430`Notebook::refresh` rereads the directory on the caller's schedule and reports
crates/notebook/src/session.rs+290-21
...@@ -113,6 +113,30 @@ pub trait Storage: Send + Sync {...@@ -113,6 +113,30 @@ pub trait Storage: Send + Sync {
113 /// Puts the file `with` in the place of the section or TOC at `path` under the coordination113 /// Puts the file `with` in the place of the section or TOC at `path` under the coordination
114 /// its writers take, provided `path` still has `base`'s stamp.114 /// its writers take, provided `path` still has `base`'s stamp.
115 fn supersede(&self, path: &str, base: &Stamp, with: &str) -> Result<()>;115 fn supersede(&self, path: &str, base: &Stamp, with: &str) -> Result<()>;
116 /// Renames the section file `from` to `to` provided it still has `base`'s stamp, under the
117 /// coordination its writers take where the storage has one, else just after checking.
118 fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> {
119 if self.stamp(from)? != *base {
120 return Err(changed());
121 }
122 self.rename(from, to)
123 }
124}
125
126/// Makes the entry naming `path` in its folder durable, where the system lets a folder sync.
127fn synced_folder(path: &Path) -> Result<()> {
128 #[cfg(unix)]
129 if let Some(folder) = path.parent() {
130 fs::File::open(folder)?.sync_all()?;
131 }
132 #[cfg(not(unix))]
133 let _ = path;
134 Ok(())
135}
136
137/// A section that changed while it moved, which stays where it was.
138fn changed() -> Error {
139 io::Error::other("The section changed while it moved").into()
116}140}
117141
118struct Publication {142struct Publication {
...@@ -201,8 +225,10 @@ impl Storage for Directory {...@@ -201,8 +225,10 @@ impl Storage for Directory {
201 {225 {
202 return Err(io::Error::from(io::ErrorKind::AlreadyExists).into());226 return Err(io::Error::from(io::ErrorKind::AlreadyExists).into());
203 }227 }
204 fs::File::create_new(path)?.write_all(bytes)?;228 let mut file = fs::File::create_new(&path)?;
205 Ok(())229 file.write_all(bytes)?;
230 file.sync_all()?;
231 synced_folder(&path)
206 }232 }
207233
208 fn create_directory(&self, path: &str) -> Result<()> {234 fn create_directory(&self, path: &str) -> Result<()> {
...@@ -251,8 +277,17 @@ impl Storage for Directory {...@@ -251,8 +277,17 @@ impl Storage for Directory {
251 Ok(())277 Ok(())
252 }278 }
253279
280 /// Refuses a name taken by a file, or by one iCloud Drive keeps elsewhere as a placeholder.
254 fn rename(&self, from: &str, to: &str) -> Result<()> {281 fn rename(&self, from: &str, to: &str) -> Result<()> {
255 Ok(fs::rename(self.path(from), self.path(to))?)282 let to = self.path(to);
283 if fs::symlink_metadata(&to).is_ok()
284 || discover::placeholder(&to)
285 .is_some_and(|placeholder| fs::metadata(placeholder).is_ok())
286 {
287 return Err(io::Error::from(io::ErrorKind::AlreadyExists).into());
288 }
289 fs::rename(self.path(from), &to)?;
290 synced_folder(&to)
256 }291 }
257292
258 /// A local file system shows no other writer's hold; one that refuses to rename a folder293 /// 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 {...@@ -425,6 +460,12 @@ impl Storage for Share {
425 .client460 .client
426 .supersede(&self.path(path), base, &self.path(with))?)461 .supersede(&self.path(path), base, &self.path(with))?)
427 }462 }
463
464 fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> {
465 Ok(self
466 .client
467 .rename_unchanged(&self.path(from), &self.path(to), base)?)
468 }
428}469}
429470
430pub(crate) const LIMITS: discover::Limits = discover::Limits {471pub(crate) const LIMITS: discover::Limits = discover::Limits {
...@@ -1127,7 +1168,19 @@ impl Notebook {...@@ -1127,7 +1168,19 @@ impl Notebook {
1127 /// Moves the section file at `path`, its folder's `entry`, into the recycle bin, under a1168 /// Moves the section file at `path`, its folder's `entry`, into the recycle bin, under a
1128 /// name no binned section has, and lists it there; its own folder's TOC is the caller's.1169 /// name no binned section has, and lists it there; its own folder's TOC is the caller's.
1129 fn bin_section(&self, path: &str, entry: &Entry) -> Result<()> {1170 fn bin_section(&self, path: &str, entry: &Entry) -> Result<()> {
1130 let filename = &entry.filename;1171 let binned = self.send_to_bin(path, &entry.filename, None)?;
1172 self.list_binned(binned, entry)
1173 }
1174
1175 /// Moves the section file at `path`, named `filename`, into the recycle bin under a name no
1176 /// binned section has, provided it still has `base`'s stamp where one is given: the bin's
1177 /// TOC and identity, and the name it took there.
1178 fn send_to_bin(
1179 &self,
1180 path: &str,
1181 filename: &str,
1182 base: Option<&Stamp>,
1183 ) -> Result<(String, [u8; 16], String)> {
1131 let (bin_toc, bin_identity) = self.bin()?;1184 let (bin_toc, bin_identity) = self.bin()?;
1132 let mut target = filename.to_owned();1185 let mut target = filename.to_owned();
1133 let mut attempt = 1;1186 let mut attempt = 1;
...@@ -1137,11 +1190,24 @@ impl Notebook {...@@ -1137,11 +1190,24 @@ impl Notebook {
1137 target = format!("{stem} ({attempt}).{extension}");1190 target = format!("{stem} ({attempt}).{extension}");
1138 }1191 }
1139 let binned = catalog_path(RECYCLE_BIN, &target);1192 let binned = catalog_path(RECYCLE_BIN, &target);
1140 self.storage.rename(path, &binned)?;1193 match base {
1194 Some(base) => self.storage.rename_unchanged(path, &binned, base)?,
1195 None => self.storage.rename(path, &binned)?,
1196 }
1197 Ok((bin_toc, bin_identity, target))
1198 }
1199
1200 /// Places and lists the section `entry`, which `send_to_bin` moved, in the recycle bin.
1201 fn list_binned(
1202 &self,
1203 (bin_toc, bin_identity, target): (String, [u8; 16], String),
1204 entry: &Entry,
1205 ) -> Result<()> {
1141 let (false, Some(identity)) = (entry.copy, entry.identity) else {1206 let (false, Some(identity)) = (entry.copy, entry.identity) else {
1142 return Ok(());1207 return Ok(());
1143 };1208 };
1144 self.storage.place(&binned, bin_identity, &target)?;1209 self.storage
1210 .place(&catalog_path(RECYCLE_BIN, &target), bin_identity, &target)?;
1145 self.commit_toc(1211 self.commit_toc(
1146 &bin_toc,1212 &bin_toc,
1147 &[onestore::TocEdit::Add {1213 &[onestore::TocEdit::Add {
...@@ -1434,6 +1500,197 @@ impl Notebook {...@@ -1434,6 +1500,197 @@ impl Notebook {
1434 Ok(target)1500 Ok(target)
1435 }1501 }
14361502
1503 /// Copies the section at catalog `path`, whose replica is `replica`, into the folder
1504 /// `folder` of `target`, after its section `after` or else last, as OneNote 2010's Move or
1505 /// Copy puts a section in another notebook: a file of its own under new identities
1506 /// (`protected::rekey`), named as the section is or with the first free number after it
1507 /// ("Beta 2"), with its colour, pages, versions and conflict pages, and the edits the
1508 /// replica holds that the file does not yet. Refused, `WouldBlock`, while those wait to be
1509 /// rebased onto a file another client changed, and while the file can't be reached, as the
1510 /// replica may be behind it. `target` may be this notebook opened again. The copy is listed
1511 /// once it reads back whole, and taken away again where it cannot be. Returns its catalog
1512 /// path in `target`.
1513 pub fn copy_section(
1514 &self,
1515 path: &str,
1516 replica: &Replica,
1517 target: &mut Notebook,
1518 folder: &str,
1519 after: Option<&str>,
1520 ) -> Result<String> {
1521 let (image, _) = self.copied(path, replica)?;
1522 target.place_copy(image, split(path).1, folder, after)
1523 }
1524
1525 /// Moves the section at catalog `path`, open as `section`, as `copy_section` copies it,
1526 /// then sends it to this notebook's recycle bin as `delete` does, and deletes its replica,
1527 /// whose edits the copy holds. `section` stops first, so nothing publishes to or rebases it
1528 /// meanwhile. The section stays where the copy could not be made, and also, as
1529 /// `Moved::kept` then says, where it changed after it was copied or could not reach the bin.
1530 pub fn move_section(
1531 &mut self,
1532 path: &str,
1533 section: Section,
1534 target: &mut Notebook,
1535 folder: &str,
1536 after: Option<&str>,
1537 ) -> Result<Moved> {
1538 let replica = Arc::clone(section.replica());
1539 section.close()?;
1540 let (image, stamp) = self.copied(path, &replica)?;
1541 let newest = replica.pending()?.last().map(|edit| edit.id);
1542 let copy = target.place_copy(image, split(path).1, folder, after)?;
1543 let (from, name) = split(path);
1544 let entry = self.entry(from, name)?;
1545 let file = self.replica_path(path);
1546 let left = (|| {
1547 if replica.pending()?.last().map(|edit| edit.id) != newest {
1548 return Err(changed());
1549 }
1550 self.send_to_bin(path, &entry.filename, Some(&stamp))
1551 })();
1552 let binned = match left {
1553 Ok(binned) => binned,
1554 Err(error) => {
1555 return Ok(Moved {
1556 path: copy,
1557 kept: Some(error),
1558 });
1559 }
1560 };
1561 // The section has left. What lists it is tidied as far as it can be: OneNote 2010
1562 // leaves a moved section's TOC entry, and a catalog read failing now is the next one's.
1563 let _ = self
1564 .list_binned(binned, &entry)
1565 .and_then(|()| match entry.identity {
1566 Some(identity) => self.edit_toc(from, &[onestore::TocEdit::Remove { identity }]),
1567 None => Ok(()),
1568 });
1569 let _ = self.refresh();
1570 drop(replica);
1571 if let Ok(file) = file {
1572 let _ = remove_replica(&file);
1573 }
1574 Ok(Moved {
1575 path: copy,
1576 kept: None,
1577 })
1578 }
1579
1580 /// The section at catalog `path`, whose replica is `replica`, as a copy of it carries it,
1581 /// under new identities, and the stamp of the file it was taken from.
1582 fn copied(&self, path: &str, replica: &Replica) -> Result<(Vec<u8>, Stamp)> {
1583 replica.written()?;
1584 let stamp = self.storage.stamp(path)?;
1585 let (image, stamp) = if replica.pending()?.is_empty() {
1586 let image = self.storage.read(path)?;
1587 let stamp = Stamp::of(&image)?;
1588 (image, stamp)
1589 } else if stamp == crate::base::base_stamp(&*replica.lock()?)? {
1590 (replica.snapshot()?, stamp)
1591 } else {
1592 return Err(io::Error::new(
1593 io::ErrorKind::WouldBlock,
1594 "Edits to the section are still syncing",
1595 )
1596 .into());
1597 };
1598 let key = replica.section.key.as_ref();
1599 Ok((protected::rekey(&image, key, key)?, stamp))
1600 }
1601
1602 /// Lists the section file `image` holds in `folder`, after its section `after` or last,
1603 /// under `filename` or the first free number after its name; its catalog path. It is
1604 /// written under a name discovery passes over, and takes its own once it reads back whole,
1605 /// so that a copy cut short is never taken for a section.
1606 fn place_copy(
1607 &mut self,
1608 mut image: Vec<u8>,
1609 filename: &str,
1610 folder: &str,
1611 after: Option<&str>,
1612 ) -> Result<String> {
1613 let parent = self.folder(folder)?;
1614 let order: Vec<[u8; 16]> = match after {
1615 Some(after) => {
1616 let at = (parent.sections.iter())
1617 .position(|section| section.path == after)
1618 .ok_or_else(|| io::Error::from(io::ErrorKind::NotFound))?;
1619 (parent.sections[..=at].iter())
1620 .filter_map(|section| Entry::of(parent, section).identity)
1621 .collect()
1622 }
1623 None => Vec::new(),
1624 };
1625 let stem = match filename.len().checked_sub(4) {
1626 Some(at)
1627 if filename
1628 .get(at..)
1629 .is_some_and(|end| end.eq_ignore_ascii_case(".one")) =>
1630 {
1631 &filename[..at]
1632 }
1633 _ => filename,
1634 };
1635 let name = |n| match n {
1636 1 => format!("{stem}.one"),
1637 n => format!("{stem} {n}.one"),
1638 };
1639 let (_, ancestor) = self.toc(folder)?;
1640 onestore::place_image(&mut image, Some((ancestor, &name(1))))?;
1641 let identity = onestore::Store::parse(&image)?.header.file_id;
1642 let written = catalog_path(folder, &format!(".{stem}.one.snowbound"));
1643 if self.storage.exists(&written) {
1644 self.storage.delete(&written)?;
1645 }
1646 self.storage.create(&written, &image)?;
1647 let named = (|| {
1648 if self.storage.read(&written)? != image {
1649 return Err(io::Error::new(
1650 io::ErrorKind::InvalidData,
1651 "The copy didn't read back as it was written",
1652 )
1653 .into());
1654 }
1655 for n in 1.. {
1656 if n > 1 {
1657 self.storage.place(&written, ancestor, &name(n))?;
1658 }
1659 match self
1660 .storage
1661 .rename(&written, &catalog_path(folder, &name(n)))
1662 {
1663 Err(Error::Io(error)) if error.kind() == io::ErrorKind::AlreadyExists => {}
1664 renamed => return renamed.map(|()| name(n)),
1665 }
1666 }
1667 unreachable!("a free name")
1668 })();
1669 let filename = match named {
1670 Ok(filename) => filename,
1671 Err(error) => {
1672 let _ = self.storage.delete(&written);
1673 return Err(error);
1674 }
1675 };
1676 let path = catalog_path(folder, &filename);
1677 let mut edits = vec![onestore::TocEdit::Add {
1678 filename,
1679 identity,
1680 group: false,
1681 }];
1682 if !order.is_empty() {
1683 edits.push(onestore::TocEdit::Order([order, vec![identity]].concat()));
1684 }
1685 if let Err(error) = self.edit_toc(folder, &edits) {
1686 let _ = self.storage.delete(&path);
1687 return Err(error);
1688 }
1689 // The copy stands listed; a catalog read failing now is the next one's to repair.
1690 let _ = self.refresh();
1691 Ok(path)
1692 }
1693
1437 /// The stored filename and TOC identity of a folder's section or group.1694 /// The stored filename and TOC identity of a folder's section or group.
1438 fn entry(&self, folder: &str, name: &str) -> Result<Entry> {1695 fn entry(&self, folder: &str, name: &str) -> Result<Entry> {
1439 let parent = self.folder(folder)?;1696 let parent = self.folder(folder)?;
...@@ -1875,6 +2132,14 @@ impl Notebook {...@@ -1875,6 +2132,14 @@ impl Notebook {
1875 }2132 }
1876}2133}
18772134
2135/// A section `Notebook::move_section` moved: its catalog path in the notebook it went to,
2136/// and why it also stayed where it was, where it could not leave once copied.
2137#[derive(Debug)]
2138pub struct Moved {
2139 pub path: String,
2140 pub kept: Option<Error>,
2141}
2142
1878/// A section or group as its folder holds it, for the structure operations.2143/// A section or group as its folder holds it, for the structure operations.
1879struct Entry {2144struct Entry {
1880 filename: String,2145 filename: String,
...@@ -2014,6 +2279,15 @@ pub fn moved(page: &Page, author: &str) -> Result<Op> {...@@ -2014,6 +2279,15 @@ pub fn moved(page: &Page, author: &str) -> Result<Op> {
2014 }))2279 }))
2015}2280}
20162281
2282/// The op putting a copy of `page` last in another section, as OneNote copies a page: under
2283/// fresh identities, its page identity too, and titled as it is.
2284pub fn copied(page: &Page, author: &str) -> Result<Op> {
2285 Ok(Op::Section(SectionOp::Import {
2286 creation: PageCreation::new(None, Some(&page.title), author)?,
2287 page: page.copy()?,
2288 }))
2289}
2290
2017/// The edits putting the pages `moved` where `order` lists them, with their levels, in a2291/// The edits putting the pages `moved` where `order` lists them, with their levels, in a
2018/// section now listing `listed`: each goes before the next page of `order` that stays put2292/// section now listing `listed`: each goes before the next page of `order` that stays put
2019/// and is still listed. Pages gone from the section are left out.2293/// and is still listed. Pages gone from the section are left out.
...@@ -2677,23 +2951,18 @@ impl Section {...@@ -2677,23 +2951,18 @@ impl Section {
2677 ) -> impl Future<Output = Result<ExGuid>> + Send + 'static + use<> {2951 ) -> impl Future<Output = Result<ExGuid>> + Send + 'static + use<> {
2678 let replica = Arc::clone(&self.replica);2952 let replica = Arc::clone(&self.replica);
2679 let author = author.to_owned();2953 let author = author.to_owned();
2680 let creation = PageCreation::new(None, Some(&page.title), &author);2954 let op = copied(page, &author);
2681 let page = page.copy();
2682 async move {2955 async move {
2683 let creation = creation?;2956 let op = op?;
2957 let Op::Section(SectionOp::Import { creation, .. }) = &op else {
2958 unreachable!("a copy is an import")
2959 };
2684 let space = creation.space();2960 let space = creation.space();
2685 replica2961 let edit = Edit {
2686 .apply_async(2962 at: crate::now(),
2687 &author,2963 ops: vec![op],
2688 Edit {2964 };
2689 at: crate::now(),2965 replica.apply_async(&author, edit).await?;
2690 ops: vec![Op::Section(SectionOp::Import {
2691 creation,
2692 page: page?,
2693 })],
2694 },
2695 )
2696 .await?;
2697 Ok(space)2966 Ok(space)
2698 }2967 }
2699 }2968 }
crates/notebook/src/session/tests.rs+100
...@@ -194,6 +194,8 @@ struct FaultStorage {...@@ -194,6 +194,8 @@ struct FaultStorage {
194 inner: Directory,194 inner: Directory,
195 revision: Mutex<Option<(String, Transaction)>>,195 revision: Mutex<Option<(String, Transaction)>>,
196 receipt: Option<Arc<Mutex<Receipt>>>,196 receipt: Option<Arc<Mutex<Receipt>>>,
197 /// Placing a section file in its folder fails.
198 misplace: bool,
197}199}
198200
199#[derive(Default)]201#[derive(Default)]
...@@ -261,6 +263,9 @@ impl Storage for FaultStorage {...@@ -261,6 +263,9 @@ impl Storage for FaultStorage {
261 self.inner.delete(path)263 self.inner.delete(path)
262 }264 }
263 fn place(&self, path: &str, ancestor: [u8; 16], name: &str) -> Result<()> {265 fn place(&self, path: &str, ancestor: [u8; 16], name: &str) -> Result<()> {
266 if self.misplace && path.ends_with(".one") {
267 return Err(io::Error::from(io::ErrorKind::PermissionDenied).into());
268 }
264 self.inner.place(path, ancestor, name)269 self.inner.place(path, ancestor, name)
265 }270 }
266 fn commit(&self, path: &str, transaction: &Transaction) -> Result<()> {271 fn commit(&self, path: &str, transaction: &Transaction) -> Result<()> {
...@@ -293,6 +298,98 @@ impl Storage for FaultStorage {...@@ -293,6 +298,98 @@ impl Storage for FaultStorage {
293 self.race(path);298 self.race(path);
294 self.inner.supersede(path, base, with)299 self.inner.supersede(path, base, with)
295 }300 }
301 fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> {
302 self.race(from);
303 self.inner.rename_unchanged(from, to, base)
304 }
305}
306
307/// A notebook holding "Mine" in its first section and a notebook to move it to, the first on
308/// `storage` made from its folder; the section's catalog path.
309fn moving(
310 temporary: &Path,
311 storage: impl FnOnce(Directory, Option<(String, Transaction)>) -> FaultStorage,
312 theirs: bool,
313) -> (Notebook, Notebook, String) {
314 let (folder, cache) = (temporary.join("notebook"), temporary.join("cache"));
315 let creation = PageCreation::new(None, Some("Mine"), "Fixture").unwrap();
316 let notebook = Notebook::create(&folder, &cache, 0x00f0_a0c0, &creation).unwrap();
317 let path = notebook.catalog().sections[0].path.clone();
318 // Another writer's revision, which the storage commits as the move checks the file.
319 let revision = theirs.then(|| {
320 let arena = onestore::Arena::default();
321 let image = notebook.read_section(&path).unwrap();
322 let mut section = onestore::Section::open(&arena, image).unwrap();
323 let theirs = PageCreation::new(None, Some("Theirs"), "OneNote").unwrap();
324 let edit = Edit {
325 at: crate::now(),
326 ops: vec![Op::Section(SectionOp::Create(theirs))],
327 };
328 section.apply("OneNote", &edit).unwrap();
329 (path.clone(), section.seal().unwrap().unwrap())
330 });
331 drop(notebook);
332 let root = folder.canonicalize().unwrap();
333 let storage = storage(Directory(root.clone()), revision);
334 let source = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
335 let target = Notebook::create(temporary.join("target"), &cache, 0x91baae, &creation).unwrap();
336 (source, target, path)
337}
338
339fn titles(image: &[u8]) -> Vec<String> {
340 (stored_pages(image).unwrap().into_iter())
341 .map(|stored| stored.page.title)
342 .collect()
343}
344
345/// A revision another writer commits after the copy is made keeps the section where it was,
346/// with the revision, beside the copy.
347#[test]
348fn a_section_changed_while_it_moves_stays_where_it_was() {
349 let temporary = tempfile::tempdir().unwrap();
350 let (mut source, mut target, path) = moving(
351 temporary.path(),
352 |inner, revision| FaultStorage {
353 inner,
354 revision: Mutex::new(revision),
355 receipt: None,
356 misplace: false,
357 },
358 true,
359 );
360 let section = source.section(&path, false, || {}).unwrap();
361 let moved = (source.move_section(&path, section, &mut target, "", None)).unwrap();
362 assert!(moved.kept.is_some());
363 assert_eq!(
364 titles(&source.read_section(&path).unwrap()),
365 ["Mine", "Theirs"]
366 );
367 assert_eq!(titles(&target.read_section(&moved.path).unwrap()), ["Mine"]);
368}
369
370/// Once the section has left for the bin, a listing that fails to follow it leaves a move, not
371/// a section said to have stayed.
372#[test]
373fn a_move_whose_bin_listing_fails_has_still_moved() {
374 let temporary = tempfile::tempdir().unwrap();
375 let (mut source, mut target, path) = moving(
376 temporary.path(),
377 |inner, _| FaultStorage {
378 inner,
379 revision: Mutex::new(None),
380 receipt: None,
381 misplace: true,
382 },
383 false,
384 );
385 let section = source.section(&path, false, || {}).unwrap();
386 let replica = source.replica_path(&path).unwrap();
387 let moved = (source.move_section(&path, section, &mut target, "", None)).unwrap();
388 assert!(moved.kept.is_none(), "{:?}", moved.kept);
389 assert!(!source.storage.exists(&path));
390 assert!(source.storage.exists(&catalog_path(RECYCLE_BIN, &path)));
391 assert_eq!(titles(&target.read_section(&moved.path).unwrap()), ["Mine"]);
392 assert!(!replica.exists(), "the original's replica goes with it");
296}393}
297394
298/// A revision another writer commits while a password is being set is never overwritten:395/// 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() {...@@ -330,6 +427,7 @@ fn a_password_never_overwrites_a_concurrent_revision() {
330 inner: Directory(root.clone()),427 inner: Directory(root.clone()),
331 revision: Mutex::new(Some((path.clone(), revision))),428 revision: Mutex::new(Some((path.clone(), revision))),
332 receipt: None,429 receipt: None,
430 misplace: false,
333 };431 };
334 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();432 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
335 let set = notebook.set_password(&path, None, Some("secret"));433 let set = notebook.set_password(&path, None, Some("secret"));
...@@ -368,6 +466,7 @@ fn recycling_confirms_uncertain_toc_and_page_appends_and_undo() {...@@ -368,6 +466,7 @@ fn recycling_confirms_uncertain_toc_and_page_appends_and_undo() {
368 inner: Directory(root.clone()),466 inner: Directory(root.clone()),
369 revision: Mutex::new(None),467 revision: Mutex::new(None),
370 receipt: Some(receipt.clone()),468 receipt: Some(receipt.clone()),
469 misplace: false,
371 };470 };
372 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();471 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
373 notebook472 notebook
...@@ -439,6 +538,7 @@ fn recycling_rejects_unconfirmed_appends_and_can_retry_each_stage() {...@@ -439,6 +538,7 @@ fn recycling_rejects_unconfirmed_appends_and_can_retry_each_stage() {
439 inner: Directory(root.clone()),538 inner: Directory(root.clone()),
440 revision: Mutex::new(None),539 revision: Mutex::new(None),
441 receipt: Some(receipt.clone()),540 receipt: Some(receipt.clone()),
541 misplace: false,
442 };542 };
443 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();543 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
444 assert!(544 assert!(
crates/notebook/src/smb/mod.rs+16
...@@ -524,6 +524,22 @@ impl Client {...@@ -524,6 +524,22 @@ impl Client {
524 self.delete(&aside).map_err(failed(CommitState::Committed))524 self.delete(&aside).map_err(failed(CommitState::Committed))
525 }525 }
526526
527 /// Renames the revision store at `path` to `to`, an existing `to` an error, provided `path`
528 /// still has `base`'s stamp, under writer coordination.
529 pub(crate) fn rename_unchanged(
530 &self,
531 path: &str,
532 to: &str,
533 base: &onestore::Stamp,
534 ) -> Result<(), CommitError> {
535 self.commit(path, &checked(base), |file| {
536 (base.check(file).and_then(|()| self.rename(path, to))).map_err(|error| CommitError {
537 state: CommitState::NotCommitted,
538 error,
539 })
540 })
541 }
542
527 /// Opens `path` for writing under OneNote's coordination, making `reads` with the locks.543 /// Opens `path` for writing under OneNote's coordination, making `reads` with the locks.
528 fn commit(544 fn commit(
529 &self,545 &self,
crates/notebook/tests/move_section.rs created+299
...@@ -0,0 +1,299 @@
1//! Moving and copying a section into another notebook, as OneNote 2010's Move or Copy does.
2
3use notebook::session::{Notebook, stored_pages};
4use onestore::{
5 PageCreation,
6 op::{Edit, Op, SectionOp},
7};
8use std::path::{Path, PathBuf};
9
10struct Notebooks {
11 _temporary: tempfile::TempDir,
12 cache: PathBuf,
13 source: PathBuf,
14 target: PathBuf,
15}
16
17/// A notebook holding Alpha and Beta beside "New Section 1", and another holding Gamma.
18fn notebooks() -> Notebooks {
19 let temporary = tempfile::tempdir().unwrap();
20 let cache = temporary.path().join("cache");
21 let page = PageCreation::new(None, Some("First page"), "Author").unwrap();
22 let [source, target] = ["Source", "Target"].map(|name| temporary.path().join(name));
23 let mut notebook = Notebook::create(&source, &cache, 0x00d7ff, &page).unwrap();
24 for name in ["Alpha", "Beta"] {
25 notebook.create_section("", name, &page).unwrap();
26 }
27 let mut notebook = Notebook::create(&target, &cache, 0x91baae, &page).unwrap();
28 notebook.create_section("", "Gamma", &page).unwrap();
29 Notebooks {
30 _temporary: temporary,
31 cache,
32 source,
33 target,
34 }
35}
36
37fn sections(notebook: &Notebook) -> Vec<String> {
38 (notebook.catalog().sections.iter())
39 .map(|section| section.path.clone())
40 .collect()
41}
42
43fn titles(file: &Path) -> Vec<String> {
44 stored_pages(&std::fs::read(file).unwrap())
45 .unwrap()
46 .into_iter()
47 .map(|stored| stored.page.title)
48 .collect()
49}
50
51/// The file identity, root object space and colour a section file holds.
52fn identities(file: &Path) -> ([u8; 16], onestore::ExGuid, Option<u32>) {
53 let image = std::fs::read(file).unwrap();
54 let store = onestore::Store::parse(&image).unwrap();
55 let index = onestore::RevisionIndex::parse(&store).unwrap();
56 let document = onestore::document::Document::parse(&index).unwrap();
57 let revision = document.active(document.root).unwrap();
58 let onestore::document::Kind::SectionMetadata { color, .. } =
59 &revision.nodes[&revision.roots[&2]].kind
60 else {
61 panic!("no section metadata")
62 };
63 (store.header.file_id, index.root, *color)
64}
65
66fn queue_page(section: &notebook::session::Section, title: &str) {
67 let creation = PageCreation::new(None, Some(title), "Author").unwrap();
68 section
69 .apply(
70 "Author",
71 Edit {
72 at: 133_000_000_000_000_000,
73 ops: vec![Op::Section(SectionOp::Create(creation))],
74 },
75 )
76 .unwrap();
77 section.written().unwrap();
78}
79
80#[test]
81fn a_moved_section_carries_its_queued_edits_and_its_original_goes_to_the_bin() {
82 let place = notebooks();
83 let mut source = Notebook::open(&place.source, &place.cache).unwrap();
84 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
85 let before = identities(&place.source.join("Alpha.one"));
86 // Offline, so the edit waits in the replica, published nowhere.
87 let section = source.section("Alpha.one", true, || {}).unwrap();
88 queue_page(&section, "Queued page");
89 assert!(!section.pending().unwrap().is_empty());
90 let moved = source
91 .move_section(
92 "Alpha.one",
93 section,
94 &mut target,
95 "",
96 Some("New Section 1.one"),
97 )
98 .unwrap();
99 assert_eq!(moved.path, "Alpha.one");
100 assert!(moved.kept.is_none(), "{:?}", moved.kept);
101
102 assert_eq!(
103 sections(&target),
104 ["New Section 1.one", "Alpha.one", "Gamma.one"]
105 );
106 let copy = place.target.join("Alpha.one");
107 let after = identities(&copy);
108 assert_ne!(after.0, before.0, "a file identity of its own");
109 assert_ne!(after.1, before.1, "object spaces of its own");
110 assert_eq!(after.2, before.2, "its colour");
111 assert_eq!(titles(&copy), ["First page", "Queued page"]);
112 let header = onestore::Store::parse(&std::fs::read(&copy).unwrap())
113 .unwrap()
114 .header;
115 let toc = target.catalog().toc.as_ref().unwrap().file_id;
116 assert_eq!(header.ancestor, toc, "placed under its TOC");
117
118 assert_eq!(sections(&source), ["New Section 1.one", "Beta.one"]);
119 assert!(source.catalog().toc.as_ref().unwrap().unresolved.is_empty());
120 let bin = &source.catalog().groups[0];
121 assert_eq!(bin.path, "OneNote_RecycleBin");
122 assert_eq!(bin.sections[0].path, "OneNote_RecycleBin/Alpha.one");
123 assert_eq!(
124 identities(&place.source.join("OneNote_RecycleBin/Alpha.one")),
125 before
126 );
127
128 // Each notebook read cold lists the section where it went.
129 let source = Notebook::open(&place.source, &place.cache).unwrap();
130 let target = Notebook::open(&place.target, &place.cache).unwrap();
131 assert_eq!(sections(&source), ["New Section 1.one", "Beta.one"]);
132 assert_eq!(
133 sections(&target),
134 ["New Section 1.one", "Alpha.one", "Gamma.one"]
135 );
136}
137
138#[test]
139fn a_copy_takes_the_next_free_name_and_leaves_the_section_where_it_is() {
140 let place = notebooks();
141 let source = Notebook::open(&place.source, &place.cache).unwrap();
142 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
143 let beta = source.section("Beta.one", false, || {}).unwrap();
144 for expected in ["Beta.one", "Beta 2.one"] {
145 let copy = source
146 .copy_section("Beta.one", beta.replica(), &mut target, "", None)
147 .unwrap();
148 assert_eq!(copy, expected);
149 }
150 assert_eq!(
151 sections(&target),
152 ["New Section 1.one", "Gamma.one", "Beta.one", "Beta 2.one"]
153 );
154 // Into its own notebook, beside itself, with an identity of its own.
155 let mut again = Notebook::open(&place.source, &place.cache).unwrap();
156 let copy = source
157 .copy_section("Beta.one", beta.replica(), &mut again, "", Some("Beta.one"))
158 .unwrap();
159 assert_eq!(copy, "Beta 2.one");
160 assert_eq!(
161 sections(&again),
162 ["New Section 1.one", "Alpha.one", "Beta.one", "Beta 2.one"]
163 );
164 let [original, copied] =
165 ["Beta.one", "Beta 2.one"].map(|name| identities(&place.source.join(name)));
166 assert_ne!(original.1, copied.1);
167 assert_ne!(
168 again.replica_path("Beta.one").unwrap(),
169 again.replica_path("Beta 2.one").unwrap(),
170 "a replica of its own"
171 );
172 assert!(place.source.join("Beta.one").exists());
173}
174
175#[test]
176fn a_section_without_waiting_edits_moves_and_its_replica_goes() {
177 let place = notebooks();
178 let mut source = Notebook::open(&place.source, &place.cache).unwrap();
179 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
180 let section = source.section("Beta.one", false, || {}).unwrap();
181 let replica = source.replica_path("Beta.one").unwrap();
182 assert!(replica.exists());
183 let moved = source
184 .move_section("Beta.one", section, &mut target, "", None)
185 .unwrap();
186 assert!(moved.kept.is_none(), "{:?}", moved.kept);
187 assert_eq!(titles(&place.target.join(&moved.path)), ["First page"]);
188 assert!(place.source.join("OneNote_RecycleBin/Beta.one").exists());
189 assert!(!replica.exists());
190}
191
192/// A name iCloud Drive holds for a section it keeps elsewhere is taken, and a copy cut short
193/// leaves nothing a notebook lists.
194#[test]
195fn a_copy_passes_over_a_name_icloud_holds() {
196 let place = notebooks();
197 let source = Notebook::open(&place.source, &place.cache).unwrap();
198 std::fs::write(place.target.join(".Beta.one.icloud"), b"").unwrap();
199 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
200 let beta = source.section("Beta.one", false, || {}).unwrap();
201 let copy = source
202 .copy_section("Beta.one", beta.replica(), &mut target, "", None)
203 .unwrap();
204 assert_eq!(copy, "Beta 2.one");
205 let hidden: Vec<_> = (std::fs::read_dir(&place.target).unwrap())
206 .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
207 .filter(|name| name.ends_with(".snowbound"))
208 .collect();
209 assert!(hidden.is_empty(), "{hidden:?}");
210}
211
212/// A section whose file can't be reached is not copied from its replica, which may be behind.
213#[test]
214fn a_copy_of_a_section_out_of_reach_is_refused() {
215 let place = notebooks();
216 let source = Notebook::open(&place.source, &place.cache).unwrap();
217 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
218 let beta = source.section("Beta.one", true, || {}).unwrap();
219 std::fs::rename(place.source.join("Beta.one"), place.source.join("Away.bak")).unwrap();
220 assert!(
221 source
222 .copy_section("Beta.one", beta.replica(), &mut target, "", None)
223 .is_err()
224 );
225 assert!(!place.target.join("Beta.one").exists());
226}
227
228#[test]
229fn a_move_that_cannot_reach_the_bin_keeps_the_section_in_both_notebooks() {
230 let place = notebooks();
231 let mut source = Notebook::open(&place.source, &place.cache).unwrap();
232 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
233 // The bin can't be made: a file stands where its folder would.
234 std::fs::write(place.source.join("OneNote_RecycleBin"), b"").unwrap();
235 let original = std::fs::read(place.source.join("Alpha.one")).unwrap();
236 let section = source.section("Alpha.one", true, || {}).unwrap();
237 queue_page(&section, "Queued page");
238 let replica = source.replica_path("Alpha.one").unwrap();
239 let moved = source
240 .move_section("Alpha.one", section, &mut target, "", None)
241 .unwrap();
242 assert!(moved.kept.is_some());
243 assert_eq!(
244 titles(&place.target.join(&moved.path)),
245 ["First page", "Queued page"]
246 );
247 assert_eq!(
248 std::fs::read(place.source.join("Alpha.one")).unwrap(),
249 original
250 );
251 assert_eq!(
252 sections(&Notebook::open(&place.source, &place.cache).unwrap()),
253 ["New Section 1.one", "Alpha.one", "Beta.one"]
254 );
255 // The edit still waits in the original's replica, to publish there too.
256 let replica = notebook::Replica::open(replica).unwrap();
257 assert_eq!(replica.pending().unwrap().len(), 1);
258}
259
260#[test]
261fn a_copy_the_notebook_cannot_list_leaves_nothing_behind() {
262 let place = notebooks();
263 let source = Notebook::open(&place.source, &place.cache).unwrap();
264 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
265 let toc = place.target.join("Open Notebook.onetoc2");
266 let mut permissions = std::fs::metadata(&toc).unwrap().permissions();
267 permissions.set_readonly(true);
268 std::fs::set_permissions(&toc, permissions).unwrap();
269 let alpha = source.section("Alpha.one", false, || {}).unwrap();
270 assert!(
271 source
272 .copy_section("Alpha.one", alpha.replica(), &mut target, "", None)
273 .is_err()
274 );
275 assert!(!place.target.join("Alpha.one").exists());
276 assert!(place.source.join("Alpha.one").exists());
277}
278
279#[test]
280fn a_move_waits_while_queued_edits_have_a_changed_file_to_rebase_onto() {
281 let place = notebooks();
282 let mut source = Notebook::open(&place.source, &place.cache).unwrap();
283 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
284 let section = source.section("Alpha.one", true, || {}).unwrap();
285 queue_page(&section, "Queued page");
286 // Another client's change, which the replica has not read.
287 source
288 .set_section_color("Alpha.one", Some(0x5ed7ff))
289 .unwrap();
290 let error = source
291 .move_section("Alpha.one", section, &mut target, "", None)
292 .unwrap_err();
293 assert!(
294 matches!(&error, notebook::Error::Io(error) if error.kind() == std::io::ErrorKind::WouldBlock),
295 "{error}"
296 );
297 assert!(!place.target.join("Alpha.one").exists());
298 assert!(place.source.join("Alpha.one").exists());
299}
crates/onestore/src/protected/mod.rs+2-1
...@@ -332,7 +332,8 @@ impl Key {...@@ -332,7 +332,8 @@ impl Key {
332}332}
333333
334/// The section `image` written anew, under `new` or in the clear, as OneNote 2010 rewrites334/// The section `image` written anew, under `new` or in the clear, as OneNote 2010 rewrites
335/// a section whose password it sets, changes or removes: the file and its object spaces335/// a section whose password it sets, changes or removes, or that it moves or copies into
336/// another notebook: the file and its object spaces
336/// take new identities, each space keeps its labelled revisions, every one a checkpoint,337/// take new identities, each space keeps its labelled revisions, every one a checkpoint,
337/// and the payloads come along. A protected `image` opens under `key`.338/// and the payloads come along. A protected `image` opens under `key`.
338pub fn rekey(image: &[u8], key: Option<&Key>, new: Option<&Key>) -> Result<Vec<u8>> {339pub fn rekey(image: &[u8], key: Option<&Key>, new: Option<&Key>) -> Result<Vec<u8>> {