Skip to content

Commit

Permalink
Improve remove_dir_all errors (#346)
Browse files Browse the repository at this point in the history
* feat: Improve remove_dir_all errors

* feat: Small improvements of download_file fn

* feat: Uinstall now deletes everything inside the Xtensa Rust toolchain folder
  • Loading branch information
SergioGasquez authored Sep 12, 2023
1 parent 2ac988b commit 5219673
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum Error {
MissingRust,

#[diagnostic(code(espup::remove_directory))]
#[error("{} Failed to remove '{0}' directory.", emoji::ERROR)]
#[error("{} Failed to remove '{0}'.", emoji::ERROR)]
RemoveDirectory(String),

#[error(transparent)]
Expand Down
3 changes: 2 additions & 1 deletion src/toolchain/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ pub fn uninstall_gcc_toolchains(toolchain_path: &Path) -> Result<(), Error> {
.replace(&format!("{gcc_path};"), ""),
);
}
remove_dir_all(gcc_path)?;
remove_dir_all(&gcc_path)
.map_err(|_| Error::RemoveDirectory(gcc_path.display().to_string()))?;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/toolchain/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ impl Llvm {
);
set_environment_variable("PATH", &updated_path)?;
}
remove_dir_all(toolchain_path.join(CLANG_NAME))?;
let path = toolchain_path.join(CLANG_NAME);
remove_dir_all(&path)
.map_err(|_| Error::RemoveDirectory(path.display().to_string()))?;
}
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/toolchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub async fn download_file(
emoji::WRENCH,
output_directory
);
if let Err(_e) = create_dir_all(output_directory) {
if create_dir_all(output_directory).is_err() {
return Err(Error::CreateDirectory(output_directory.to_string()));
}
}
Expand Down Expand Up @@ -136,10 +136,10 @@ pub async fn download_file(
}
} else {
info!("{} Creating file: '{}'", emoji::WRENCH, file_path);
let mut out = File::create(file_path)?;
let mut out = File::create(&file_path)?;
out.write_all(&bytes)?;
}
Ok(format!("{output_directory}/{file_name}"))
Ok(file_path)
}

/// Installs or updates the Espressif Rust ecosystem.
Expand Down
23 changes: 15 additions & 8 deletions src/toolchain/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use regex::Regex;
use std::{
env,
fmt::Debug,
fs::{read_dir, remove_dir_all},
fs::{read_dir, remove_dir_all, remove_file},
path::{Path, PathBuf},
process::{Command, Stdio},
};
Expand Down Expand Up @@ -158,14 +158,21 @@ impl XtensaRust {
info!("{} Uninstalling Xtensa Rust toolchain", emoji::WRENCH);
let dir = read_dir(toolchain_path)?;
for entry in dir {
let subdir_name = entry.unwrap().path().display().to_string();
if !subdir_name.contains(RISCV_GCC)
&& !subdir_name.contains(ESP32_GCC)
&& !subdir_name.contains(ESP32S2_GCC)
&& !subdir_name.contains(ESP32S3_GCC)
&& !subdir_name.contains(CLANG_NAME)
let entry_path = entry.unwrap().path();
let entry_name = entry_path.display().to_string();
if !entry_name.contains(RISCV_GCC)
&& !entry_name.contains(ESP32_GCC)
&& !entry_name.contains(ESP32S2_GCC)
&& !entry_name.contains(ESP32S3_GCC)
&& !entry_name.contains(CLANG_NAME)
{
remove_dir_all(Path::new(&subdir_name)).unwrap();
if entry_path.is_dir() {
remove_dir_all(Path::new(&entry_name))
.map_err(|_| Error::RemoveDirectory(entry_name))?;
} else {
// If the entry is a file, delete the file
remove_file(&entry_name)?;
}
}
}
Ok(())
Expand Down

0 comments on commit 5219673

Please sign in to comment.