From 068c9d47c00b32642d9291bdc8903fd774adba20 Mon Sep 17 00:00:00 2001 From: Arvid Norlander Date: Thu, 25 Jul 2024 17:16:03 +0200 Subject: [PATCH] feat: Better error reporting on removing non-empty directories --- crates/konfigkoll_core/src/apply.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/konfigkoll_core/src/apply.rs b/crates/konfigkoll_core/src/apply.rs index 9bf81b09..7e50bdc8 100644 --- a/crates/konfigkoll_core/src/apply.rs +++ b/crates/konfigkoll_core/src/apply.rs @@ -149,7 +149,17 @@ impl Applicator for InProcessApplicator { let existing = std::fs::symlink_metadata(&instr.path); if let Ok(metadata) = existing { if metadata.is_dir() { - std::fs::remove_dir(&instr.path)?; + match std::fs::remove_dir(&instr.path) { + Ok(_) => (), + Err(err) => match err.raw_os_error() { + Some(libc::ENOTEMPTY) => { + Err(err).context("Failed to remove directory: it is not empty (possibly it contains some ignored files). You will have to investigate and resolve this yourself, since we don't want to delete things we shouldn't.")?; + } + Some(_) | None => { + Err(err).context("Failed to remove directory")?; + } + }, + } } else { std::fs::remove_file(&instr.path)?; }