lb_rs/model/
server_ops.rs

1use super::errors::{DiffError, LbErrKind, LbResult};
2use super::meta::Meta;
3use super::server_meta::{IntoServerMeta, ServerMeta};
4use super::signed_meta::SignedMeta;
5use crate::model::clock::get_time;
6use crate::model::file_like::FileLike;
7use crate::model::file_metadata::FileDiff;
8use crate::model::lazy::{LazyStaged1, LazyTree};
9use crate::model::server_tree::ServerTree;
10use crate::model::signed_file::SignedFile;
11use crate::model::tree_like::TreeLike;
12
13type LazyServerStaged1<'a> = LazyStaged1<ServerTree<'a>, Vec<ServerMeta>>;
14
15impl<'a> LazyTree<ServerTree<'a>> {
16    /// Validates a diff prior to staging it. Performs individual validations, then validations that
17    /// require a tree
18    pub fn stage_diff(self, changes: Vec<FileDiff<SignedFile>>) -> LbResult<LazyServerStaged1<'a>> {
19        let mut changes_meta: Vec<FileDiff<SignedMeta>> = vec![];
20        for change in changes {
21            let mut new_meta: SignedMeta = change.new.into();
22            let mut old_meta = change.old.map(SignedMeta::from);
23            if let Some(old) = &mut old_meta {
24                let current_size = *self
25                    .maybe_find(old.id())
26                    .ok_or(LbErrKind::Diff(DiffError::OldFileNotFound))?
27                    .file
28                    .timestamped_value
29                    .value
30                    .doc_size();
31
32                match &mut old.timestamped_value.value {
33                    Meta::V1 { doc_size, .. } => {
34                        *doc_size = current_size;
35                    }
36                };
37
38                match &mut new_meta.timestamped_value.value {
39                    Meta::V1 { doc_size, .. } => {
40                        *doc_size = current_size;
41                    }
42                };
43            }
44
45            changes_meta.push(FileDiff { old: old_meta, new: new_meta });
46        }
47        let mut changes = changes_meta;
48
49        // Check new.id == old.id
50        for change in &changes {
51            if let Some(old) = &change.old {
52                if old.id() != change.new.id() {
53                    return Err(LbErrKind::Diff(DiffError::DiffMalformed))?;
54                }
55            }
56        }
57
58        // Check for changes to digest
59        for change in &changes {
60            match &change.old {
61                Some(old) => {
62                    if old.timestamped_value.value.document_hmac()
63                        != change.new.timestamped_value.value.document_hmac()
64                    {
65                        return Err(LbErrKind::Diff(DiffError::HmacModificationInvalid))?;
66                    }
67                }
68                None => {
69                    if change.new.timestamped_value.value.doc_size().is_some() {
70                        return Err(LbErrKind::Diff(DiffError::HmacModificationInvalid))?;
71                    }
72                    if change.new.timestamped_value.value.document_hmac().is_some() {
73                        return Err(LbErrKind::Diff(DiffError::HmacModificationInvalid))?;
74                    }
75                }
76            }
77        }
78
79        // Check for race conditions and populate prior size
80        for change in &mut changes {
81            match &change.old {
82                Some(old) => {
83                    let current = &self
84                        .maybe_find(old.id())
85                        .ok_or(LbErrKind::Diff(DiffError::OldFileNotFound))?
86                        .file;
87
88                    if current != old {
89                        return Err(LbErrKind::Diff(DiffError::OldVersionIncorrect))?;
90                    }
91                }
92                None => {
93                    // if you're claiming this file is new, it must be globally unique
94                    if self.tree.files.maybe_find(change.new.id()).is_some() {
95                        return Err(LbErrKind::Diff(DiffError::OldVersionRequired))?;
96                    }
97                }
98            }
99        }
100
101        let now = get_time().0 as u64;
102        let changes = changes
103            .into_iter()
104            .map(|change| change.new.add_time(now))
105            .collect();
106
107        Ok(self.stage(changes))
108    }
109}