-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparent.rs
298 lines (282 loc) · 9.05 KB
/
parent.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use std::{
cmp::Ordering,
ffi::{OsStr, OsString},
};
use log::warn;
use crate::{
archiver::tree::TreeType,
backend::{decrypt::DecryptReadBackend, node::Node},
blob::tree::Tree,
error::{ArchiverErrorKind, RusticResult},
id::Id,
index::ReadGlobalIndex,
};
/// The `ItemWithParent` is a `TreeType` wrapping the result of a parent search and a type `O`.
///
/// # Type Parameters
///
/// * `O` - The type of the `TreeType`.
pub(crate) type ItemWithParent<O> = TreeType<(O, ParentResult<()>), ParentResult<Id>>;
/// The `Parent` is responsible for finding the parent tree of a given tree.
#[derive(Debug)]
pub struct Parent {
/// The tree id of the parent tree.
tree_id: Option<Id>,
/// The parent tree.
tree: Option<Tree>,
/// The current node index.
node_idx: usize,
/// The stack of parent trees.
stack: Vec<(Option<Tree>, usize)>,
/// Ignore ctime when comparing nodes.
ignore_ctime: bool,
/// Ignore inode number when comparing nodes.
ignore_inode: bool,
}
/// The result of a parent search.
///
/// # Type Parameters
///
/// * `T` - The type of the matched parent.
#[derive(Clone, Debug)]
pub(crate) enum ParentResult<T> {
/// The parent was found and matches.
Matched(T),
/// The parent was not found.
NotFound,
/// The parent was found but doesn't match.
NotMatched,
}
impl<T> ParentResult<T> {
/// Maps a `ParentResult<T>` to a `ParentResult<R>` by applying a function to a contained value.
///
/// # Type Parameters
///
/// * `R` - The type of the returned `ParentResult`.
///
/// # Arguments
///
/// * `f` - The function to apply.
///
/// # Returns
///
/// A `ParentResult<R>` with the result of the function for each `ParentResult<T>`.
fn map<R>(self, f: impl FnOnce(T) -> R) -> ParentResult<R> {
match self {
Self::Matched(t) => ParentResult::Matched(f(t)),
Self::NotFound => ParentResult::NotFound,
Self::NotMatched => ParentResult::NotMatched,
}
}
}
impl Parent {
/// Creates a new `Parent`.
///
/// # Type Parameters
///
/// * `BE` - The type of the backend.
///
/// # Arguments
///
/// * `be` - The backend to read from.
/// * `tree_id` - The tree id of the parent tree.
/// * `ignore_ctime` - Ignore ctime when comparing nodes.
/// * `ignore_inode` - Ignore inode number when comparing nodes.
pub(crate) fn new(
be: &impl DecryptReadBackend,
index: &impl ReadGlobalIndex,
tree_id: Option<Id>,
ignore_ctime: bool,
ignore_inode: bool,
) -> Self {
// if tree_id is given, try to load tree from backend.
let tree = tree_id.and_then(|tree_id| match Tree::from_backend(be, index, tree_id) {
Ok(tree) => Some(tree),
Err(err) => {
warn!("ignoring error when loading parent tree {tree_id}: {err}");
None
}
});
Self {
tree_id,
tree,
node_idx: 0,
stack: Vec::new(),
ignore_ctime,
ignore_inode,
}
}
/// Returns the parent node with the given name.
///
/// # Arguments
///
/// * `name` - The name of the parent node.
///
/// # Returns
///
/// The parent node with the given name, or `None` if the parent node is not found.
fn p_node(&mut self, name: &OsStr) -> Option<&Node> {
match &self.tree {
None => None,
Some(tree) => {
let p_nodes = &tree.nodes;
loop {
match p_nodes.get(self.node_idx) {
None => break None,
Some(p_node) => match p_node.name().as_os_str().cmp(name) {
Ordering::Less => self.node_idx += 1,
Ordering::Equal => {
break Some(p_node);
}
Ordering::Greater => {
break None;
}
},
}
}
}
}
}
/// Returns whether the given node is the parent of the given tree.
///
/// # Arguments
///
/// * `node` - The node to check.
/// * `name` - The name of the tree.
///
/// # Returns
///
/// Whether the given node is the parent of the given tree.
///
/// # Note
///
/// TODO: This function does not check whether the given node is a directory.
fn is_parent(&mut self, node: &Node, name: &OsStr) -> ParentResult<&Node> {
// use new variables as the mutable borrow is used later
let ignore_ctime = self.ignore_ctime;
let ignore_inode = self.ignore_inode;
self.p_node(name).map_or(ParentResult::NotFound, |p_node| {
if p_node.node_type == node.node_type
&& p_node.meta.size == node.meta.size
&& p_node.meta.mtime == node.meta.mtime
&& (ignore_ctime || p_node.meta.ctime == node.meta.ctime)
&& (ignore_inode || p_node.meta.inode == 0 || p_node.meta.inode == node.meta.inode)
{
ParentResult::Matched(p_node)
} else {
ParentResult::NotMatched
}
})
}
// TODO: add documentation!
///
/// # Type Parameters
///
/// * `BE` - The type of the backend.
///
/// # Arguments
///
/// * `be` - The backend to read from.
/// * `name` - The name of the parent node.
fn set_dir(
&mut self,
be: &impl DecryptReadBackend,
index: &impl ReadGlobalIndex,
name: &OsStr,
) {
let tree = self.p_node(name).and_then(|p_node| {
p_node.subtree.map_or_else(
|| {
warn!("ignoring parent node {}: is no tree!", p_node.name);
None
},
|tree_id| match Tree::from_backend(be, index, tree_id) {
Ok(tree) => Some(tree),
Err(err) => {
warn!("ignoring error when loading parent tree {tree_id}: {err}");
None
}
},
)
});
self.stack.push((self.tree.take(), self.node_idx));
self.tree = tree;
self.node_idx = 0;
}
// TODO: add documentation!
///
/// # Errors
///
/// * [`ArchiverErrorKind::TreeStackEmpty`] - If the tree stack is empty.
///
/// [`ArchiverErrorKind::TreeStackEmpty`]: crate::error::ArchiverErrorKind::TreeStackEmpty
fn finish_dir(&mut self) -> RusticResult<()> {
let (tree, node_idx) = self
.stack
.pop()
.ok_or_else(|| ArchiverErrorKind::TreeStackEmpty)?;
self.tree = tree;
self.node_idx = node_idx;
Ok(())
}
// TODO: add documentation!
pub(crate) fn tree_id(&self) -> Option<Id> {
self.tree_id
}
// TODO: add documentation!
///
/// # Type Parameters
///
/// * `BE` - The type of the backend.
/// * `O` - The type of the tree item.
///
/// # Arguments
///
/// * `be` - The backend to read from.
/// * `item` - The item to process.
///
/// # Errors
///
/// * [`ArchiverErrorKind::TreeStackEmpty`] - If the tree stack is empty.
///
/// [`ArchiverErrorKind::TreeStackEmpty`]: crate::error::ArchiverErrorKind::TreeStackEmpty
pub(crate) fn process<O>(
&mut self,
be: &impl DecryptReadBackend,
index: &impl ReadGlobalIndex,
item: TreeType<O, OsString>,
) -> RusticResult<ItemWithParent<O>> {
let result = match item {
TreeType::NewTree((path, node, tree)) => {
let parent_result = self
.is_parent(&node, &tree)
.map(|node| node.subtree.unwrap());
self.set_dir(be, index, &tree);
TreeType::NewTree((path, node, parent_result))
}
TreeType::EndTree => {
self.finish_dir()?;
TreeType::EndTree
}
TreeType::Other((path, mut node, open)) => {
let parent = self.is_parent(&node, &node.name());
let parent = match parent {
ParentResult::Matched(p_node) => {
if p_node.content.iter().flatten().all(|id| index.has_data(id)) {
node.content = p_node.content.clone();
ParentResult::Matched(())
} else {
warn!(
"missing blobs in index for unchanged file {path:?}; re-reading file",
);
ParentResult::NotFound
}
}
parent_result => parent_result.map(|_| ()),
};
TreeType::Other((path, node, (open, parent)))
}
};
Ok(result)
}
}