Skip to content

Commit 18bbbb6

Browse files
committed
improve error on remove directory failiure
this should fix issue #43
1 parent 1e00961 commit 18bbbb6

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

src/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl BuildDirectory {
146146
) -> Result<R, Error> {
147147
let source_dir = self.source_dir();
148148
if source_dir.exists() {
149-
remove_dir_all(&source_dir)?;
149+
remove_dir_all(&source_dir).map_err(|error|crate::utils::improve_remove_dir_error(error, &source_dir))?;
150150
}
151151

152152
let mut prepare = Prepare::new(&self.workspace, toolchain, krate, &source_dir, patches);
@@ -159,15 +159,15 @@ impl BuildDirectory {
159159
sandbox,
160160
})?;
161161

162-
remove_dir_all(&source_dir)?;
162+
remove_dir_all(&source_dir).map_err(|error|crate::utils::improve_remove_dir_error(error, &source_dir))?;
163163
Ok(res)
164164
}
165165

166166
/// Remove all the contents of the build directory, freeing disk space.
167167
pub fn purge(&mut self) -> Result<(), Error> {
168168
let build_dir = self.build_dir();
169169
if build_dir.exists() {
170-
remove_dir_all(build_dir)?;
170+
remove_dir_all(&build_dir).map_err(|error|crate::utils::improve_remove_dir_error(error, &build_dir))?;
171171
}
172172
Ok(())
173173
}

src/crates/git.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl CrateTrait for GitRepo {
121121
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
122122
let path = self.cached_path(workspace);
123123
if path.exists() {
124-
remove_dir_all::remove_dir_all(&path)?;
124+
remove_dir_all::remove_dir_all(&path).map_err(|error|crate::utils::improve_remove_dir_error(error, &path))?;
125125
}
126126
Ok(())
127127
}

src/crates/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Crate {
6969
"crate source directory {} already exists, cleaning it up",
7070
dest.display()
7171
);
72-
remove_dir_all(dest)?;
72+
remove_dir_all(dest).map_err(|error|crate::utils::improve_remove_dir_error(error, dest))?;
7373
}
7474
self.as_trait().copy_source_to(workspace, dest)
7575
}

src/utils.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ fn strip_verbatim_from_prefix(prefix: &PrefixComponent<'_>) -> Option<PathBuf> {
5050
Some(ret)
5151
}
5252

53+
#[derive(Debug)]
54+
struct DirRemoveError {
55+
kind: std::io::ErrorKind,
56+
path: String,
57+
}
58+
59+
impl std::error::Error for DirRemoveError {}
60+
61+
impl std::fmt::Display for DirRemoveError {
62+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63+
f.write_fmt(format_args!("failed to remove directory '{}' : {:?}",self.path, self.kind))
64+
}
65+
}
66+
67+
pub(crate) fn improve_remove_dir_error(error: std::io::Error, path: &Path) -> std::io::Error {
68+
std::io::Error::new(error.kind(), DirRemoveError{kind: error.kind(),path: path.display().to_string()})
69+
}
70+
71+
#[test]
72+
fn custom_remove_dir_error() {
73+
74+
let path = "test/path".as_ref();
75+
76+
let expected = "failed to remove directory 'test/path' : PermissionDenied";
77+
let tested = format!("{}", improve_remove_dir_error(std::io::Error::from(std::io::ErrorKind::PermissionDenied), path));
78+
assert_eq!(expected, tested);
79+
80+
let expected = "Custom { kind: PermissionDenied, error: DirRemoveError { kind: PermissionDenied, path: \"test/path\" } }";
81+
let tested = format!("{:?}",improve_remove_dir_error(std::io::Error::from(std::io::ErrorKind::PermissionDenied), path));
82+
assert_eq!(expected, tested);
83+
}
84+
5385
pub(crate) fn normalize_path(path: &Path) -> PathBuf {
5486
let mut p = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
5587

src/workspace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl Workspace {
213213
pub fn purge_all_build_dirs(&self) -> Result<(), Error> {
214214
let dir = self.builds_dir();
215215
if dir.exists() {
216-
remove_dir_all(&dir)?;
216+
remove_dir_all(&dir).map_err(|error|crate::utils::improve_remove_dir_error(error, &dir))?;
217217
}
218218
Ok(())
219219
}
@@ -236,7 +236,7 @@ impl Workspace {
236236

237237
for path in &paths {
238238
if path.exists() {
239-
remove_dir_all(&path)?;
239+
remove_dir_all(&path).map_err(|error|crate::utils::improve_remove_dir_error(error, &path))?;
240240
}
241241
}
242242

0 commit comments

Comments
 (0)