Skip to content

Commit 87e4a1c

Browse files
Get removed files by traversing
Signed-off-by: Johan-Liebert1 <[email protected]>
1 parent afe2b1c commit 87e4a1c

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

crates/etc-merge/src/lib.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,51 @@ fn collect_all_files(root: &Directory<CustomMetadata>) -> Vec<PathBuf> {
9999
return files;
100100
}
101101

102+
fn get_deletions(
103+
pristine: &Directory<CustomMetadata>,
104+
current: &Directory<CustomMetadata>,
105+
mut current_path: PathBuf,
106+
diff: &mut Diff,
107+
) -> anyhow::Result<()> {
108+
for (file_name, inode) in pristine.entries() {
109+
current_path.push(file_name);
110+
111+
match inode {
112+
Inode::Directory(pristine_dir) => {
113+
match current.get_directory(file_name) {
114+
Ok(curr_dir) => {
115+
get_deletions(pristine_dir, curr_dir, current_path.clone(), diff)?
116+
}
117+
118+
Err(ImageError::NotFound(..)) => {
119+
// Directory was deleted
120+
diff.removed.push(current_path.clone());
121+
}
122+
123+
Err(e) => Err(e)?,
124+
}
125+
}
126+
127+
Inode::Leaf(..) => match current.ref_leaf(file_name) {
128+
Ok(..) => {
129+
// Empty as all additions/modifications are tracked above
130+
}
131+
132+
Err(ImageError::NotFound(..)) => {
133+
// File was deleted
134+
diff.removed.push(current_path.clone());
135+
}
136+
137+
Err(e) => Err(e)?,
138+
},
139+
}
140+
141+
current_path.pop();
142+
}
143+
144+
Ok(())
145+
}
146+
102147
// 1. Files in the currently booted deployment’s /etc which were modified from the default /usr/etc (of the same deployment) are retained.
103148
//
104149
// 2. Files in the currently booted deployment’s /etc which were not modified from the default /usr/etc (of the same deployment)
@@ -215,7 +260,12 @@ fn compute_diff(
215260
&mut diff,
216261
)?;
217262

218-
diff.removed = collect_all_files(&pristine_etc_files);
263+
get_deletions(
264+
&pristine_etc_files,
265+
&current_etc_files,
266+
PathBuf::new(),
267+
&mut diff,
268+
)?;
219269

220270
Ok(diff)
221271
}
@@ -334,9 +384,6 @@ mod tests {
334384
p.create_dir_all("a/b/c")?;
335385
c.create_dir_all("a/b/c")?;
336386

337-
let mut open_options = cap_std::fs::OpenOptions::new();
338-
open_options.create(true).write(true);
339-
340387
for (file, content) in FILES {
341388
p.write(file, content.as_bytes())?;
342389
c.write(file, content.as_bytes())?;

0 commit comments

Comments
 (0)