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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use std::borrow::Cow;
use std::path::Path;
use anyhow::Context;
use super::{Node, NodeFilters, RestorablePath};
use crate::parser::{DirMap, DirectoryState, FileState, NodeStatus};
/// Represents the actual state of a file tree [nodes](super::Node).
#[derive(Debug, Clone)]
pub enum InnerNode {
/// A file or a link.
File(FileState),
/// A directory with zero or more [nodes](super::Node) inside.
Directory(DirectoryState),
}
impl InnerNode {
/// Creates a new empty node of type [InnerNode::Directory] with status [NodeStatus::Added].
pub fn new_empty_dir() -> Self {
InnerNode::Directory(DirectoryState::new_empty())
}
/// Creates a new node of type [InnerNode::Directory] with status [NodeStatus::Added] using the provided size.
pub fn new_dir_with_size(size: u64) -> Self {
InnerNode::Directory(DirectoryState::new_with_size(size))
}
/// Returns `true` if this node's [status](NodeStatus) is [NodeStatus::Added].
pub fn is_added(&self) -> bool {
matches!(self.status(), NodeStatus::Added(_))
}
/// Returns `true` if this node's [status](NodeStatus) is [NodeStatus::Modified].
pub fn is_modified(&self) -> bool {
matches!(self.status(), NodeStatus::Modified(_))
}
/// Returns `true` if this node's [status](NodeStatus) is [NodeStatus::Deleted].
pub fn is_deleted(&self) -> bool {
matches!(self.status(), NodeStatus::Deleted)
}
/// Returns the size of a node unless it's status is [NodeStatus::Deleted], in which case
/// it returns `0`.
pub fn size(&self) -> u64 {
match self.status() {
NodeStatus::Added(size) | NodeStatus::Modified(size) => size,
_ => 0,
}
}
/// Returns `true` if this node is [InnerNode::Directory] and `false` otherwise.
pub fn is_dir(&self) -> bool {
matches!(self, InnerNode::Directory(..))
}
/// Returns a [DirMap] of children for this [InnerNode::Directory] or [Option::None] if the node is a [InnerNode::File].
pub fn children(&self) -> Option<&DirMap> {
match self {
InnerNode::Directory(state) => Some(&state.children),
_ => None,
}
}
/// Returns the total number of children nodes for this [InnerNode::Directory] or [Option::None] if the node is a [InnerNode::File].
pub fn get_n_of_child_nodes(&self) -> Option<usize> {
let children = self.children()?;
let mut n_of_children = children.len();
for (_, child_node) in children.iter() {
n_of_children +=
child_node.inner.get_n_of_child_nodes().unwrap_or(0)
}
Some(n_of_children)
}
/// Returns a reference to the [FileState::actual_file] for this [InnerNode::File] or [Option::None] if the node is a [InnerNode::Directory].
pub fn get_link(&self) -> Option<&Path> {
match self {
InnerNode::File(state) => state.actual_file.as_deref(),
_ => None,
}
}
/// Returns a mutable reference to the [DirectoryState] of this [InnerNode::Directory] or [Option::None] if the node is a [InnerNode::File].
pub(super) fn dir_state_mut(&mut self) -> Option<&mut DirectoryState> {
match self {
InnerNode::Directory(state) => Some(state),
_ => None,
}
}
/// Filters this [InnerNode] using the provided filter.
///
/// Returns `true` if there are one or more nodes remaining after the filtering.
pub(super) fn filter(
&mut self,
node_last_updated_in: u8,
filter: NodeFilters,
) -> bool {
if !filter.any() {
// No filters -> entry is always included
return true;
}
// We ignore files here, as they are handled when processing children of directories
if let InnerNode::Directory(state) = self {
state.children.retain(|path, child| {
// Size-based filtering
if let Some(node_size_filter) = filter.node_size_filter
&& child.inner.size() < node_size_filter
{
return false;
}
// Node state filtering
if let Some(layer_idx) = filter.show_nodes_changed_in_layer
&& (node_last_updated_in != layer_idx
|| child.updated_in != layer_idx)
{
// Omit all files that weren't changed in the specified layer
// if the corresponding filter is present
return false;
}
let is_dir_with_children = child
.inner
.children()
.map(|children| !children.is_empty())
.unwrap_or(false);
let mut include_if_no_children_remained = true;
// Path-based filtering
let path_filter_for_child = if let Some(path_filter) =
filter.path_filter.as_ref()
{
let is_filtered_out = if let Some(leftmost_part) =
path_filter.get_current_component()
{
path != Path::new(".")
&& !path
.as_os_str()
.to_str()
// We need to convert both paths to a str to check for a partial match using `contains`
.and_then(|path| {
leftmost_part.to_str().map(
|leftmost_part| {
path.contains(leftmost_part)
},
)
})
// If anything fails here, exclude the node
.unwrap_or(true)
} else {
if !filter.any_non_path_filter() {
// If there is no longer a filter and we don't have any other
// filters, then we simply retain the node
return true;
}
false
};
// Directories are filtered out based on their children when using relative path, so we don't filter them out here
if is_filtered_out
&& (!path_filter.is_using_relative_path()
|| !is_dir_with_children)
{
// Exclude filtered out nodes (files, empty dirs, and mismatched dirs when using an absolute path)
return false;
}
if is_filtered_out && path_filter.is_using_relative_path() {
// We try to find matching children nodes for this directory, but will
// exclude it if none match
include_if_no_children_remained = false;
}
let possibly_empty_filter = if path != Path::new(".") {
if is_filtered_out {
// This is only reachable when using relative paths, the current node is a dir, and it didn't pass the check.
// In this case, we reset the path filter back to its original state and check children of the current node.
path_filter.restore()
} else {
// In all other cases, we advance the current path filter component by 1
path_filter.advance()
}
} else {
// Pass the filter as is
path_filter.clone()
};
// Don't pass an empty filter if we've already used all of its components,
// use [Option::None] instead.
let path_filter = (!possibly_empty_filter
.get_current_component()
.map(|component| component.as_os_str().is_empty())
.unwrap_or(true))
.then_some(possibly_empty_filter);
if path_filter.is_none()
&& !is_filtered_out
&& !filter.any_non_path_filter()
{
// This is only reachable if the current node matched the last component in the path filter and the global filter doesn't contain any non-path filtering.
// In this case, we simply inclue the node and don't do any further child filtering (as their parent passed the check).
return true;
}
path_filter
} else {
None
};
// Regex-based filtering
let path_regex_for_child =
if let Some(regex) = filter.path_regex.as_deref() {
let Some(path) = path.to_str() else {
// Exclude this node otherwise
return false;
};
if regex.is_match(path) {
if !filter.any_non_path_filter() {
// Include both directories and files if they satisfy the RegEx and the
// filter doesn't contain any non-path filters.
// We don't check children of directories in this case.
return true;
}
// Don't filter children if parent matched directly
None
} else {
// If it's a dir with children, then we also check the children.
// Otherwise (if it's a file or an empty dir), we exclude the node immediately.
if !is_dir_with_children {
return false;
}
include_if_no_children_remained = false;
filter.path_regex.as_deref().map(Cow::Borrowed)
}
} else {
None
};
// Filter the children
child.filter(NodeFilters {
path_filter: path_filter_for_child,
node_size_filter: filter.node_size_filter,
path_regex: path_regex_for_child,
show_nodes_changed_in_layer: filter
.show_nodes_changed_in_layer,
include_dir_if_no_children_remained:
include_if_no_children_remained,
})
});
// Return `true` if the directory should be included even without any children or if one or more of its children are present after filtering
return filter.include_dir_if_no_children_remained
|| !state.children.is_empty();
}
// Include the node if we didn't trigger any other branches
true
}
/// Inserts a new node at the provided path.
pub(super) fn insert(
&mut self,
path: &mut RestorablePath<'_>,
new_node: Self,
layer_digest: u8,
) -> anyhow::Result<()> {
let Some(current_path_component) = path.get_current_component() else {
// Replace the node, as there are no more path components
*self = new_node;
return Ok(());
};
if let InnerNode::Directory(state) = self {
if !state.children.contains_key(current_path_component) {
state.children.insert(
current_path_component.into(),
Node::new_with_inner(
layer_digest,
InnerNode::Directory(DirectoryState::new_empty()),
),
);
}
} else {
// NOTE: This happened in some images when I was testing the app.
// Some images change type of a node from directory to link back and forth before actually
// creating any children inside the directory.
//
// Thus, we may need to replace a node before appending other nodes to it.
let mut dir_state = DirectoryState::new_with_size(new_node.size());
dir_state.children.insert(
current_path_component.into(),
Node::new_with_inner(
layer_digest,
InnerNode::Directory(DirectoryState::new_empty()),
),
);
*self = InnerNode::Directory(dir_state);
};
// Update the size
self.increase_dir_size(new_node.size());
let next_node = self
.children_mut()
.context("impossible: we made sure this component is a directory")?
.get_mut(current_path_component)
.context("impossible: we inserted the missing component above")?;
next_node.insert(&mut path.advance(), new_node, layer_digest)
}
/// Recursively merges two [nodes](InnerNode) together and returns the result.
pub(super) fn merge(mut self, other: Self, digest: u8) -> Self {
match (&mut self, other) {
// Both nodes are directories
(
InnerNode::Directory(left_state),
InnerNode::Directory(right_state),
) => {
for (path, right_node) in right_state.children {
// If a node if present in both left and right parent node, we need to merge the two.
// Otherwise, we use the node from the right parent as is.
let updated_node = if let Some(left_node) =
left_state.children.remove(&path)
{
left_node.inner.merge(right_node.inner, digest)
} else {
right_node.inner
};
// Insert the updated node back into the left parent node
left_state.children.insert(
path,
Node::new_with_inner(
right_node.updated_in,
updated_node,
),
);
}
// Update the node status accordingly
let new_status = match (&left_state.status, &right_state.status)
{
(_, NodeStatus::Added(_)) => {
// Calculate the updated directory size after merging
NodeStatus::Modified(
left_state
.children
.values()
.map(|tree| tree.inner.size())
.sum(),
)
}
(_, _) => right_state.status,
};
left_state.status = new_status;
}
// Both nodes are files
(InnerNode::File(left_state), InnerNode::File(right_state)) => {
// Update the node status accordingly
let new_status = match (&left_state.status, &right_state.status)
{
(_, NodeStatus::Added(new_size)) => {
NodeStatus::Modified(*new_size)
}
(_, _) => right_state.status,
};
left_state.status = new_status;
}
// Nodes are of different type
(left_node, right_node) => {
// Check if a directory was deleted using a whiteout file
if left_node.is_dir() && right_node.is_deleted() {
left_node.mark_as_deleted(digest);
} else {
// Can only happen if the type of a node has changed.
// If this happens, then we simply want to replace the node altogether.
*left_node = right_node
}
}
};
self
}
/// Changes [NodeStatus] of this node and its children (if any) to [NodeStatus::Deleted].
pub(super) fn mark_as_deleted(&mut self, digest_idx: u8) {
match self {
InnerNode::Directory(state) => {
// Mark the directory itself as deleted
state.status = NodeStatus::Deleted;
// Mark each child as 'deleted' recursively
for tree in state.children.values_mut() {
tree.updated_in = digest_idx;
tree.inner.mark_as_deleted(digest_idx);
}
}
InnerNode::File(state) => {
state.status = NodeStatus::Deleted;
}
}
}
/// Returns a mutable reference to the [DirMap] of children for this [InnerNode::Directory] or [Option::None] if the node is a [InnerNode::File].
fn children_mut(&mut self) -> Option<&mut DirMap> {
match self {
InnerNode::Directory(state) => Some(&mut state.children),
_ => None,
}
}
/// Increases size of a [InnerNode::Directory].
///
/// # Note
///
/// Ignores [InnerNode::File].
fn increase_dir_size(&mut self, inc: u64) {
let status = match self {
InnerNode::File(_) => return,
InnerNode::Directory(state) => &mut state.status,
};
match status {
NodeStatus::Added(size) | NodeStatus::Modified(size) => {
*size += inc
}
_ => (),
}
}
/// Returns [NodeStatus] of this [InnerNode].
fn status(&self) -> NodeStatus {
match self {
InnerNode::File(state) => state.status,
InnerNode::Directory(state) => state.status,
}
}
}
impl Default for InnerNode {
fn default() -> Self {
InnerNode::new_empty_dir()
}
}