Skip to content

Commit 0d199e6

Browse files
Johan-Liebert1cgwalters
authored andcommitted
etc-merge: Fix directory removal
We weren't checking if the deleted path is a file or a directory and were calling `remove_file` unconditionally. Update to check for file/directory first Signed-off-by: Pragyan Poudyal <[email protected]>
1 parent 78f3439 commit 0d199e6

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

crates/etc-merge/src/lib.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -732,14 +732,33 @@ pub fn merge(
732732
.context("Merging modified files")?;
733733

734734
for removed in diff.removed {
735-
match new_etc_fd.remove_file(&removed) {
736-
Ok(..) => { /* no-op */ }
737-
// Removed file's not present in the new etc dir, nothing to do
738-
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
739-
Err(e) => Err(e)?,
735+
let stat = new_etc_fd.metadata_optional(&removed)?;
736+
737+
let stat = match stat {
738+
Some(s) => s,
739+
740+
None => {
741+
// File/dir doesn't exist in new_etc
742+
// Basically a no-op
743+
continue;
744+
}
745+
};
746+
747+
if stat.is_file() || stat.is_symlink() {
748+
match new_etc_fd.remove_file(&removed) {
749+
Ok(..) => { /* no-op */ }
750+
Err(e) => Err(e)?,
751+
}
740752
}
741753

742-
println!("- Removed file {removed:?}");
754+
if stat.is_dir() {
755+
// We only add the directory to the removed array, if the entire directory was deleted
756+
// So `remove_dir_all` should be okay here
757+
match new_etc_fd.remove_dir_all(&removed) {
758+
Ok(..) => { /* no-op */ }
759+
Err(e) => Err(e)?,
760+
}
761+
}
743762
}
744763

745764
Ok(())

0 commit comments

Comments
 (0)