Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- allow workspaces by having validate_manifest now use `metadata --no-deps` instead of deprecated `read-manifest`
therefor no longer failing on workspaces and TomlTweaker no longer removing the workspace table from `Cargo.toml`
- `Command` now warns when it is not used.
- remove directory/file error now mentions path

## [0.10.0] - 2020-08-08

Expand Down
9 changes: 6 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ impl BuildDirectory {
) -> Result<R, Error> {
let source_dir = self.source_dir();
if source_dir.exists() {
remove_dir_all(&source_dir)?;
remove_dir_all(&source_dir)
.map_err(|error| crate::utils::improve_remove_error(error, &source_dir))?;
}

let mut prepare = Prepare::new(&self.workspace, toolchain, krate, &source_dir, patches);
Expand All @@ -159,15 +160,17 @@ impl BuildDirectory {
sandbox,
})?;

remove_dir_all(&source_dir)?;
remove_dir_all(&source_dir)
.map_err(|error| crate::utils::improve_remove_error(error, &source_dir))?;
Ok(res)
}

/// Remove all the contents of the build directory, freeing disk space.
pub fn purge(&mut self) -> Result<(), Error> {
let build_dir = self.build_dir();
if build_dir.exists() {
remove_dir_all(build_dir)?;
remove_dir_all(&build_dir)
.map_err(|error| crate::utils::improve_remove_error(error, &build_dir))?;
}
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/crates/cratesio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ impl CrateTrait for CratesIOCrate {
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
let path = self.cache_path(workspace);
if path.exists() {
std::fs::remove_file(&path)?;
std::fs::remove_file(&path)
.map_err(|error| crate::utils::improve_remove_error(error, &path))?;
}
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/crates/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ impl CrateTrait for GitRepo {
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
let path = self.cached_path(workspace);
if path.exists() {
remove_dir_all::remove_dir_all(&path)?;
remove_dir_all::remove_dir_all(&path)
.map_err(|error| crate::utils::improve_remove_error(error, &path))?;
}
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/crates/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ mod tests {
println!("{} should cause copy to fail", bad_link.display());
assert_copy_err_has_filename();

fs::remove_file(&bad_link)?;
fs::remove_file(&bad_link)
.map_err(|error| crate::utils::improve_remove_error(error, &bad_link))?;
// make sure it works without that link
super::copy_dir(tmp_src.path(), tmp_dest.path())?;

Expand Down
3 changes: 2 additions & 1 deletion src/crates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ impl Crate {
"crate source directory {} already exists, cleaning it up",
dest.display()
);
remove_dir_all(dest)?;
remove_dir_all(dest)
.map_err(|error| crate::utils::improve_remove_error(error, dest))?;
}
self.as_trait().copy_source_to(workspace, dest)
}
Expand Down
4 changes: 2 additions & 2 deletions src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ impl<'a> Prepare<'a> {
fn remove_cargo_config(&self) -> Result<(), Error> {
let path = self.source_dir.join(".cargo").join("config");
if path.exists() {
remove_file(path.clone())?;
info!("removed {}", path.as_path().display());
remove_file(&path).map_err(|error| crate::utils::improve_remove_error(error, &path))?;
info!("removed {}", path.display());
}
Ok(())
}
Expand Down
52 changes: 52 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,58 @@ fn strip_verbatim_from_prefix(prefix: &PrefixComponent<'_>) -> Option<PathBuf> {
Some(ret)
}

#[derive(Debug)]
struct RemoveError {
kind: std::io::ErrorKind,
path: String,
}

impl std::error::Error for RemoveError {}

impl std::fmt::Display for RemoveError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"failed to remove '{}' : {:?}",
self.path, self.kind
))
}
}

pub(crate) fn improve_remove_error(error: std::io::Error, path: &Path) -> std::io::Error {
std::io::Error::new(
error.kind(),
RemoveError {
kind: error.kind(),
path: path.display().to_string(),
},
)
}

#[test]
fn custom_remove_error() {
let path = "test/path".as_ref();

let expected = "failed to remove 'test/path' : PermissionDenied";
let tested = format!(
"{}",
improve_remove_error(
std::io::Error::from(std::io::ErrorKind::PermissionDenied),
path
)
);
assert_eq!(expected, tested);

let expected = "Custom { kind: PermissionDenied, error: RemoveError { kind: PermissionDenied, path: \"test/path\" } }";
let tested = format!(
"{:?}",
improve_remove_error(
std::io::Error::from(std::io::ErrorKind::PermissionDenied),
path
)
);
assert_eq!(expected, tested);
}

pub(crate) fn normalize_path(path: &Path) -> PathBuf {
let mut p = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());

Expand Down
6 changes: 4 additions & 2 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ impl Workspace {
pub fn purge_all_build_dirs(&self) -> Result<(), Error> {
let dir = self.builds_dir();
if dir.exists() {
remove_dir_all(&dir)?;
remove_dir_all(&dir)
.map_err(|error| crate::utils::improve_remove_error(error, &dir))?;
}
Ok(())
}
Expand All @@ -236,7 +237,8 @@ impl Workspace {

for path in &paths {
if path.exists() {
remove_dir_all(&path)?;
remove_dir_all(&path)
.map_err(|error| crate::utils::improve_remove_error(error, &path))?;
}
}

Expand Down