From 5219673fb8b8d787ea69194fb0f208beed01860b Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Arcos Date: Tue, 12 Sep 2023 09:27:44 +0200 Subject: [PATCH] Improve `remove_dir_all` errors (#346) * feat: Improve remove_dir_all errors * feat: Small improvements of download_file fn * feat: Uinstall now deletes everything inside the Xtensa Rust toolchain folder --- src/error.rs | 2 +- src/toolchain/gcc.rs | 3 ++- src/toolchain/llvm.rs | 4 +++- src/toolchain/mod.rs | 6 +++--- src/toolchain/rust.rs | 23 +++++++++++++++-------- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/error.rs b/src/error.rs index 504bd499..4636bec6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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)] diff --git a/src/toolchain/gcc.rs b/src/toolchain/gcc.rs index 69fa907a..73eb35e9 100644 --- a/src/toolchain/gcc.rs +++ b/src/toolchain/gcc.rs @@ -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()))?; } } diff --git a/src/toolchain/llvm.rs b/src/toolchain/llvm.rs index 34155cc4..d117b461 100644 --- a/src/toolchain/llvm.rs +++ b/src/toolchain/llvm.rs @@ -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(()) } diff --git a/src/toolchain/mod.rs b/src/toolchain/mod.rs index 706c72b7..b2151eb7 100644 --- a/src/toolchain/mod.rs +++ b/src/toolchain/mod.rs @@ -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())); } } @@ -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. diff --git a/src/toolchain/rust.rs b/src/toolchain/rust.rs index 656558c0..7365eec7 100644 --- a/src/toolchain/rust.rs +++ b/src/toolchain/rust.rs @@ -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}, }; @@ -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(())