Skip to content

Commit d2c0723

Browse files
authored
Merge pull request #46 from Skgland/fix-issue-43
Attempt to fix issue 43
2 parents 1e00961 + b7803ad commit d2c0723

File tree

9 files changed

+70
-17
lines changed

9 files changed

+70
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- allow workspaces by having validate_manifest now use `metadata --no-deps` instead of deprecated `read-manifest`
1111
therefor no longer failing on workspaces and TomlTweaker no longer removing the workspace table from `Cargo.toml`
1212
- `Command` now warns when it is not used.
13+
- remove directory/file error now mentions path
1314

1415
## [0.10.0] - 2020-08-08
1516

src/build.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::cmd::{Command, MountKind, Runnable, SandboxBuilder};
22
use crate::prepare::Prepare;
33
use crate::{Crate, Toolchain, Workspace};
44
use failure::Error;
5-
use remove_dir_all::remove_dir_all;
65
use std::path::PathBuf;
76
use std::vec::Vec;
87

@@ -146,7 +145,7 @@ impl BuildDirectory {
146145
) -> Result<R, Error> {
147146
let source_dir = self.source_dir();
148147
if source_dir.exists() {
149-
remove_dir_all(&source_dir)?;
148+
crate::utils::remove_dir_all(&source_dir)?;
150149
}
151150

152151
let mut prepare = Prepare::new(&self.workspace, toolchain, krate, &source_dir, patches);
@@ -159,15 +158,15 @@ impl BuildDirectory {
159158
sandbox,
160159
})?;
161160

162-
remove_dir_all(&source_dir)?;
161+
crate::utils::remove_dir_all(&source_dir)?;
163162
Ok(res)
164163
}
165164

166165
/// Remove all the contents of the build directory, freeing disk space.
167166
pub fn purge(&mut self) -> Result<(), Error> {
168167
let build_dir = self.build_dir();
169168
if build_dir.exists() {
170-
remove_dir_all(build_dir)?;
169+
crate::utils::remove_dir_all(&build_dir)?;
171170
}
172171
Ok(())
173172
}

src/crates/cratesio.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::Workspace;
33
use failure::Error;
44
use flate2::read::GzDecoder;
55
use log::info;
6-
use remove_dir_all::remove_dir_all;
76
use std::fs::File;
87
use std::io::{BufReader, BufWriter, Read};
98
use std::path::{Path, PathBuf};
@@ -62,7 +61,7 @@ impl CrateTrait for CratesIOCrate {
6261
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
6362
let path = self.cache_path(workspace);
6463
if path.exists() {
65-
std::fs::remove_file(&path)?;
64+
crate::utils::remove_file(&path)?;
6665
}
6766
Ok(())
6867
}
@@ -79,7 +78,7 @@ impl CrateTrait for CratesIOCrate {
7978
dest.display()
8079
);
8180
if let Err(err) = unpack_without_first_dir(&mut tar, dest) {
82-
let _ = remove_dir_all(dest);
81+
let _ = crate::utils::remove_dir_all(dest);
8382
Err(err
8483
.context(format!(
8584
"unable to download {} version {}",

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+
crate::utils::remove_dir_all(&path)?;
125125
}
126126
Ok(())
127127
}

src/crates/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ mod tests {
145145
println!("{} should cause copy to fail", bad_link.display());
146146
assert_copy_err_has_filename();
147147

148-
fs::remove_file(&bad_link)?;
148+
crate::utils::remove_file(&bad_link)?;
149149
// make sure it works without that link
150150
super::copy_dir(tmp_src.path(), tmp_dest.path())?;
151151

src/crates/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ mod local;
55
use crate::Workspace;
66
use failure::Error;
77
use log::info;
8-
use remove_dir_all::remove_dir_all;
98
use std::path::Path;
109

1110
trait CrateTrait: std::fmt::Display {
@@ -69,7 +68,7 @@ impl Crate {
6968
"crate source directory {} already exists, cleaning it up",
7069
dest.display()
7170
);
72-
remove_dir_all(dest)?;
71+
crate::utils::remove_dir_all(dest)?;
7372
}
7473
self.as_trait().copy_source_to(workspace, dest)
7574
}

src/prepare.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::cmd::Command;
22
use crate::{build::CratePatch, Crate, Toolchain, Workspace};
33
use failure::{Error, Fail, ResultExt};
44
use log::info;
5-
use std::fs::remove_file;
65
use std::path::Path;
76
use toml::{
87
value::{Array, Table},
@@ -73,8 +72,8 @@ impl<'a> Prepare<'a> {
7372
fn remove_cargo_config(&self) -> Result<(), Error> {
7473
let path = self.source_dir.join(".cargo").join("config");
7574
if path.exists() {
76-
remove_file(path.clone())?;
77-
info!("removed {}", path.as_path().display());
75+
crate::utils::remove_file(&path)?;
76+
info!("removed {}", path.display());
7877
}
7978
Ok(())
8079
}

src/utils.rs

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

53+
pub(crate) fn remove_file(path: &Path) -> std::io::Result<()> {
54+
std::fs::remove_file(&path).map_err(|error| crate::utils::improve_remove_error(error, &path))
55+
}
56+
57+
pub(crate) fn remove_dir_all(path: &Path) -> std::io::Result<()> {
58+
remove_dir_all::remove_dir_all(path)
59+
.map_err(|error| crate::utils::improve_remove_error(error, path))
60+
}
61+
62+
#[derive(Debug)]
63+
struct RemoveError {
64+
kind: std::io::ErrorKind,
65+
path: PathBuf,
66+
}
67+
68+
impl std::error::Error for RemoveError {}
69+
70+
impl std::fmt::Display for RemoveError {
71+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72+
f.write_fmt(format_args!(
73+
"failed to remove '{}' : {:?}",
74+
self.path.display(),
75+
self.kind
76+
))
77+
}
78+
}
79+
80+
fn improve_remove_error(error: std::io::Error, path: &Path) -> std::io::Error {
81+
std::io::Error::new(
82+
error.kind(),
83+
RemoveError {
84+
kind: error.kind(),
85+
path: path.to_path_buf(),
86+
},
87+
)
88+
}
89+
90+
#[cfg(test)]
91+
mod tests {
92+
use super::*;
93+
94+
#[test]
95+
fn custom_remove_error() {
96+
let path = "test/path".as_ref();
97+
98+
let expected = "failed to remove 'test/path' : PermissionDenied";
99+
let tested = format!(
100+
"{}",
101+
improve_remove_error(
102+
std::io::Error::from(std::io::ErrorKind::PermissionDenied),
103+
path
104+
)
105+
);
106+
assert_eq!(expected, tested);
107+
}
108+
}
109+
53110
pub(crate) fn normalize_path(path: &Path) -> PathBuf {
54111
let mut p = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
55112

src/workspace.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::inside_docker::CurrentContainer;
44
use crate::Toolchain;
55
use failure::{Error, ResultExt};
66
use log::info;
7-
use remove_dir_all::remove_dir_all;
87
use std::path::{Path, PathBuf};
98
use std::sync::Arc;
109
use std::time::Duration;
@@ -213,7 +212,7 @@ impl Workspace {
213212
pub fn purge_all_build_dirs(&self) -> Result<(), Error> {
214213
let dir = self.builds_dir();
215214
if dir.exists() {
216-
remove_dir_all(&dir)?;
215+
crate::utils::remove_dir_all(&dir)?;
217216
}
218217
Ok(())
219218
}
@@ -236,7 +235,7 @@ impl Workspace {
236235

237236
for path in &paths {
238237
if path.exists() {
239-
remove_dir_all(&path)?;
238+
crate::utils::remove_dir_all(&path)?;
240239
}
241240
}
242241

0 commit comments

Comments
 (0)