| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::cell_id::CellId; |
| 3 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 4 |
use crate::fsshttpb::data_element::storage_index::StorageIndex; |
| 5 |
use crate::fsshttpb::packaging::OneStorePackaging; |
| 6 |
use crate::onestore::object::Object; |
| 7 |
use crate::onestore::object_space::GroupData; |
| 8 |
use crate::onestore::revision_role::RevisionRole; |
| 9 |
use std::collections::HashMap; |
| 10 |
|
| 11 |
/// A OneNote file revision. |
| 12 |
/// |
| 13 |
/// See [\[MS-ONESTOR\] 2.1.8] |
| 14 |
/// |
| 15 |
/// [\[MS-ONESTOR\] 2.1.8]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-onestore/a8ca2a90-d92a-4cf7-bf68-ed18ae476a11 |
| 16 |
#[derive(Debug, Clone)] |
| 17 |
pub(crate) struct Revision<'a> { |
| 18 |
objects: HashMap<ExGuid, Object<'a>>, |
| 19 |
roots: HashMap<RevisionRole, ExGuid>, |
| 20 |
} |
| 21 |
|
| 22 |
impl<'a, 'b> Revision<'a> { |
| 23 |
#[allow(clippy::too_many_arguments)] |
| 24 |
pub(crate) fn parse( |
| 25 |
revision_manifest_id: ExGuid, |
| 26 |
context_id: ExGuid, |
| 27 |
object_space_id: ExGuid, |
| 28 |
storage_index: &'a StorageIndex, |
| 29 |
packaging: &'a OneStorePackaging, |
| 30 |
revision_cache: &'b mut HashMap<CellId, Revision<'a>>, |
| 31 |
objects: &'b mut HashMap<ExGuid, Object<'a>>, |
| 32 |
roots: &'b mut HashMap<RevisionRole, ExGuid>, |
| 33 |
) -> Result<Option<ExGuid>> { |
| 34 |
let revision_manifest = packaging |
| 35 |
.data_element_package |
| 36 |
.find_revision_manifest(revision_manifest_id) |
| 37 |
.ok_or_else(|| { |
| 38 |
ErrorKind::MalformedOneStoreData("revision manifest not found".into()) |
| 39 |
})?; |
| 40 |
|
| 41 |
let base_rev = revision_manifest |
| 42 |
.base_rev_id |
| 43 |
.as_option() |
| 44 |
.map(|mapping_id| { |
| 45 |
storage_index |
| 46 |
.find_revision_mapping_id(mapping_id) |
| 47 |
.ok_or_else(|| { |
| 48 |
ErrorKind::MalformedOneStoreData("revision mapping not found".into()) |
| 49 |
}) |
| 50 |
}) |
| 51 |
.transpose()?; |
| 52 |
|
| 53 |
if let Some(rev) = revision_cache.get(&CellId(context_id, revision_manifest.rev_id)) { |
| 54 |
roots.extend(rev.roots.iter()); |
| 55 |
objects.extend(rev.objects.clone().into_iter()); |
| 56 |
|
| 57 |
return Ok(base_rev); |
| 58 |
} |
| 59 |
|
| 60 |
roots.extend( |
| 61 |
revision_manifest |
| 62 |
.root_declare |
| 63 |
.iter() |
| 64 |
.map(|root| Ok((RevisionRole::parse(root.root_id)?, root.object_id))) |
| 65 |
.collect::<Result<Vec<_>>>()? |
| 66 |
.into_iter(), |
| 67 |
); |
| 68 |
|
| 69 |
for group_id in revision_manifest.group_references.iter() { |
| 70 |
Self::parse_group(context_id, *group_id, object_space_id, packaging, objects)? |
| 71 |
} |
| 72 |
|
| 73 |
Ok(base_rev) |
| 74 |
} |
| 75 |
|
| 76 |
fn parse_group( |
| 77 |
context_id: ExGuid, |
| 78 |
group_id: ExGuid, |
| 79 |
object_space_id: ExGuid, |
| 80 |
packaging: &'a OneStorePackaging, |
| 81 |
objects: &'b mut HashMap<ExGuid, Object<'a>>, |
| 82 |
) -> Result<()> { |
| 83 |
let group = packaging |
| 84 |
.data_element_package |
| 85 |
.find_object_group(group_id) |
| 86 |
.ok_or_else(|| ErrorKind::MalformedOneStoreData("object group not found".into()))?; |
| 87 |
|
| 88 |
let object_ids: Vec<_> = group.declarations.iter().map(|o| o.object_id()).collect(); |
| 89 |
|
| 90 |
let group_objects: GroupData = group |
| 91 |
.declarations |
| 92 |
.iter() |
| 93 |
.zip(group.objects.iter()) |
| 94 |
.map(|(decl, data)| ((decl.object_id(), decl.partition_id()), data)) |
| 95 |
.collect(); |
| 96 |
|
| 97 |
for object_id in object_ids { |
| 98 |
if objects.contains_key(&object_id) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if group.declarations.len() != group.objects.len() { |
| 103 |
return Err(ErrorKind::MalformedOneStoreData( |
| 104 |
"object declaration/data counts do not match".into(), |
| 105 |
) |
| 106 |
.into()); |
| 107 |
} |
| 108 |
|
| 109 |
let object = Object::parse( |
| 110 |
object_id, |
| 111 |
context_id, |
| 112 |
object_space_id, |
| 113 |
&group_objects, |
| 114 |
packaging, |
| 115 |
)?; |
| 116 |
|
| 117 |
objects.insert(object_id, object); |
| 118 |
} |
| 119 |
|
| 120 |
Ok(()) |
| 121 |
} |
| 122 |
} |