authoravatarclover caruso<git@paperclover.net>2026-10-19 12:44:16-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 22:58:56-07:00
log548ff67a10b8310d3ef2841ea5c508df8ab4df0f
treef8a38c6d7924d6d115e317944bffb5ccb86ecbfe
parent292d28fbcc025e5fdbdf777b2783867d9b9346ce
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix: a section written anew names its pages' metadata for their new identities

A section given, changed or relieved of a password, or moved or copied into another notebook, is written anew under new object space identities. OneNote 2010 names a page's metadata in the section's page list after the page's object space; the rewrite renamed the spaces but kept each metadata object's old name, so OneNote, finding no metadata under the name it expected, appended a revision holding a second copy of it on first open. The metadata now takes the name of its page's new space, and a cold open leaves a moved or copied file as it was. OneNote still adds properties to pages Snowbound made in a protected section when it first shows them, which is separate. - OneNote no longer adds a second copy of each page's details to a section Snowbound moved, copied or changed the password of Refs #105 Assisted-by: claude-opus-5.5

2 files changed, 140 insertions(+), 0 deletions(-)

crates/onestore/src/protected/mod.rs+8
...@@ -357,6 +357,14 @@ pub fn rekey(image: &[u8], key: Option<&Key>, new: Option<&Key>) -> Result<Vec<u...@@ -357,6 +357,14 @@ pub fn rekey(image: &[u8], key: Option<&Key>, new: Option<&Key>) -> Result<Vec<u
357 entry.insert(crate::write::fresh_guid()?);357 entry.insert(crate::write::fresh_guid()?);
358 }358 }
359 }359 }
360 // A page's metadata is named for its object space, so it takes the new space's name.
361 let derived: Vec<([u8; 16], [u8; 16])> = (fresh.iter())
362 .map(|(old, new)| {
363 let named = |guid: &[u8; 16]| crate::pages::metadata_id(ExGuid { guid: *guid, n: 1 });
364 (named(old).guid, named(new).guid)
365 })
366 .collect();
367 fresh.extend(derived);
360 // OneNote gives the payloads new identities too.368 // OneNote gives the payloads new identities too.
361 let mut files = BTreeMap::new();369 let mut files = BTreeMap::new();
362 for guid in crate::write::declared_payloads(&store) {370 for guid in crate::write::declared_payloads(&store) {
crates/onestore/tests/rewrite.rs created+132
...@@ -0,0 +1,132 @@
1//! A section written anew under new identities (`protected::rekey`), as a password, a move or
2//! a copy into another notebook writes one: every page, version and conflict page comes along,
3//! each under the identities OneNote 2010 derives from its own.
4
5use onestore::{
6 Arena, ExGuid, RevisionIndex, Section, Store,
7 document::{Document, Kind},
8 page::Page,
9 protected::rekey,
10};
11
12fn read(path: &str) -> Vec<u8> {
13 std::fs::read(format!(
14 "{}/../../corpus/{path}",
15 env!("CARGO_MANIFEST_DIR")
16 ))
17 .unwrap()
18}
19
20/// A page as a reader sees it: its title, its versions' authors and its conflict pages'
21/// titles and users.
22type Seen = (String, Vec<Option<String>>, Vec<(String, String)>);
23
24/// What a reader sees of a section's pages, in order.
25fn seen(image: &[u8]) -> Vec<Seen> {
26 let arena = Arena::default();
27 let mut section = Section::open(&arena, image.to_vec()).unwrap();
28 let versions = section.versions().unwrap();
29 let conflicts = section.conflicts().unwrap();
30 let mut seen = Vec::new();
31 for (space, title, _) in section.pages().unwrap() {
32 let page: Page = section.page(space).unwrap();
33 assert_eq!(page.title, title);
34 let versions: Vec<Option<String>> = (versions.iter())
35 .filter(|(listed, _)| *listed == space)
36 .flat_map(|(_, versions)| versions)
37 .map(|version| {
38 section.version(space, version.context).unwrap();
39 version.author.clone()
40 })
41 .collect();
42 let conflicts = (conflicts.iter())
43 .filter(|(listed, _)| *listed == space)
44 .flat_map(|(_, conflicts)| conflicts)
45 .map(|conflict| {
46 section.page(conflict.space).unwrap();
47 (conflict.title.clone(), conflict.user.clone())
48 })
49 .collect();
50 seen.push((title, versions, conflicts));
51 }
52 seen
53}
54
55/// The identity OneNote 2010 gives the metadata of the page in object space `space`.
56fn metadata(space: ExGuid) -> ExGuid {
57 let salt = [
58 0x31, 0xc0, 0xa8, 0x22, 0, 0x36, 0xee, 0x42, 0xb7, 0x14, 0xd7, 0xac, 0xda, 0x24, 0x35, 0xe8,
59 ];
60 let mut guid = space.guid;
61 guid.iter_mut()
62 .zip(salt)
63 .for_each(|(byte, salt)| *byte ^= salt);
64 ExGuid { guid, n: 1 }
65}
66
67/// The page metadata the section's page list holds, each with the page space it names.
68fn listed(image: &[u8]) -> Vec<(ExGuid, ExGuid)> {
69 let store = Store::parse(image).unwrap();
70 let index = RevisionIndex::parse(&store).unwrap();
71 let document = Document::parse(&index).unwrap();
72 let revision = document.active(document.root).unwrap();
73 let spaces: Vec<ExGuid> = index.spaces.keys().copied().collect();
74 (revision.nodes.iter())
75 .filter(|(_, node)| matches!(node.kind, Kind::Metadata { .. }))
76 .map(|(id, _)| {
77 let space = spaces.iter().find(|space| metadata(**space) == *id);
78 (*id, *space.unwrap_or(&ExGuid::default()))
79 })
80 .collect()
81}
82
83fn rewritten_whole(path: &str) {
84 rewritten(path, read(path));
85}
86
87fn rewritten(path: &str, image: Vec<u8>) {
88 let rewritten = rekey(&image, None, None).unwrap();
89 let before = seen(&image);
90 assert!(!before.is_empty());
91 assert_eq!(seen(&rewritten), before, "{path}");
92 let store = |image: &[u8]| Store::parse(image).unwrap().header.file_id;
93 assert_ne!(store(&rewritten), store(&image));
94 for image in [&image, &rewritten] {
95 let listed = listed(image);
96 assert!(!listed.is_empty(), "{path}");
97 for (id, space) in listed {
98 assert_ne!(
99 space,
100 ExGuid::default(),
101 "{path}: metadata {id} names no page"
102 );
103 }
104 }
105}
106
107#[test]
108fn versions_come_along() {
109 rewritten_whole("page-versions/native/step-02/notebook/History.one");
110}
111
112#[test]
113fn conflict_pages_come_along() {
114 rewritten_whole("conflict-page/native-three/notebook/synthetic.one");
115}
116
117#[test]
118fn a_section_snowbound_made_comes_along() {
119 let mut image = onestore::create_empty_section("Made.one", None).unwrap();
120 let page = onestore::PageCreation::new(None, Some("Made"), "Author").unwrap();
121 let edit = onestore::op::Edit {
122 at: 134_040_000_000_000_000,
123 ops: vec![onestore::op::Op::Section(onestore::op::SectionOp::Create(
124 page,
125 ))],
126 };
127 let arena = Arena::default();
128 let mut section = Section::open(&arena, image.clone()).unwrap();
129 section.apply("Author", &edit).unwrap();
130 section.seal().unwrap().unwrap().apply(&mut image).unwrap();
131 rewritten("Made.one", image);
132}