Skip to content

Commit

Permalink
consistently warn to stderr if a file could not be deleted
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
matthiaskrgr committed Dec 20, 2018
1 parent bcbeabd commit 183ee36
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
23 changes: 12 additions & 11 deletions src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub(crate) enum ErrorKind {
CargoFailedGetConfig,
CargoHomeNotDirectory,
InvalidDeletableDir,
RemoveFailed,
RemoveDirNoArg,
}

Expand Down Expand Up @@ -238,9 +237,9 @@ pub(crate) fn rm_old_crates(
);
} else {
println!("deleting: {} {} at {}", pkgname, pkgver, pkgpath.display());
fs::remove_file(pkgpath).unwrap_or_else(|_| {
panic!("Failed to remove file '{}'", pkgpath.display())
});
if fs::remove_file(pkgpath).is_err() {
warn_on_undeletable_file(&pkgpath);
}
*size_changed = true;
}
continue;
Expand All @@ -265,9 +264,10 @@ pub(crate) fn rm_old_crates(
);
} else {
println!("deleting: {} {} at {}", pkgname, pkgver, pkgpath.display());
fs::remove_file(pkgpath).unwrap_or_else(|_| {
panic!("Failed to remove file '{}'", pkgpath.display())
});
if fs::remove_file(pkgpath).is_err() {
warn_on_undeletable_file(&pkgpath);
}

*size_changed = true;
}
}
Expand Down Expand Up @@ -415,10 +415,7 @@ pub(crate) fn remove_dir_via_cmdline(
} else {
println!("removing: '{}'", dir.display());
if fs::remove_dir_all(&dir).is_err() {
return Err((
ErrorKind::RemoveFailed,
format!("failed to remove directory {}", dir.display()),
));
warn_on_undeletable_file(&dir);
}
*size_changed = true;
}
Expand Down Expand Up @@ -516,6 +513,10 @@ pub(crate) fn remove_dir_via_cmdline(
Ok(())
}

pub(crate) fn warn_on_undeletable_file(path: &PathBuf) {
eprintln!("Warning: failed to remove \"{}\".", path.display());
}

#[cfg(test)]
mod libtests {
use super::*;
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ fn main() {
if config.is_present("dry-run") {
println!("would remove directory '{}'", dir.display());
} else {
fs::remove_dir_all(&dir)
.unwrap_or_else(|_| panic!("Failed to remove file '{}'", dir.display()));
if fs::remove_dir_all(&dir).is_err() {
warn_on_undeletable_file(&dir);
}
size_changed = true;
}
}
Expand Down

0 comments on commit 183ee36

Please sign in to comment.