From 671cef91f4e3c216f84683e07c82c5849d641b3b Mon Sep 17 00:00:00 2001 From: Brian Carlsen Date: Sun, 4 Feb 2024 20:39:33 +0100 Subject: [PATCH 1/3] Enhanced error reporting. --- src/lib.rs | 5 +++++ src/macos.rs | 21 ++++++++++++++------- src/windows.rs | 22 +++++++++++++++++++--- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a039985..8e33dc9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,6 +138,11 @@ pub enum Error { description: String, }, + Os { + code: i32, + description: String, + }, + /// **freedesktop only** /// /// Error coming from file system diff --git a/src/macos.rs b/src/macos.rs index dbdff4c..063f238 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -153,13 +153,20 @@ fn delete_using_finder(full_paths: Vec) -> Result<(), Error> { let result = command.output().map_err(into_unknown)?; if !result.status.success() { let stderr = String::from_utf8_lossy(&result.stderr); - return Err(Error::Unknown { - description: format!( - "The AppleScript exited with error. Error code: {:?}, stderr: {}", - result.status.code(), - stderr - ), - }); + match result.status.code() { + None => { + return Err(Error::Unknown { + description: format!("The AppleScript exited with error. stderr: {}", stderr), + }) + } + + Some(code) => { + return Err(Error::Os { + code: result.status.code(), + description: format!("The AppleScript exited with error. stderr: {}", stderr), + }) + } + }; } Ok(()) } diff --git a/src/windows.rs b/src/windows.rs index 12c1a82..3cc8be6 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -2,7 +2,7 @@ use crate::{Error, TrashContext, TrashItem, TrashItemMetadata, TrashItemSize}; use std::{ borrow::Borrow, ffi::{c_void, OsStr, OsString}, - fs, + fs, io, os::windows::{ffi::OsStrExt, prelude::*}, path::PathBuf, }; @@ -34,7 +34,7 @@ const FOFX_EARLYFAILURE: u32 = 0x00100000; impl From for Error { fn from(err: windows::core::Error) -> Error { - Error::Unknown { description: format!("windows error: {err}") } + Error::Os { code: err.code().0, description: format!("windows error: {err}") } } } @@ -79,7 +79,9 @@ impl TrashContext { /// Removes all files and folder paths recursively. pub(crate) fn delete_all_canonicalized(&self, full_paths: Vec) -> Result<(), Error> { let mut collection = Vec::new(); + log::trace!("Starting traverse_paths_recursively"); traverse_paths_recursively(full_paths, &mut collection)?; + log::trace!("Finished traverse_paths_recursively"); self.delete_specified_canonicalized(collection) } } @@ -316,7 +318,21 @@ fn traverse_paths_recursively( continue; } - for entry in fs::read_dir(&base_path).map_err(|err| Error::Unknown { description: err.to_string() })? { + let entries = match fs::read_dir(&base_path) { + Ok(entries) => entries, + Err(err) => { + let err = match err.kind() { + io::ErrorKind::NotFound | io::ErrorKind::PermissionDenied => { + Error::CouldNotAccess { target: base_path.to_string_lossy().to_string() } + } + _ => Error::Unknown { description: err.to_string() }, + }; + + return Err(err); + } + }; + + for entry in entries { let entry = entry.map_err(|err| Error::Unknown { description: err.to_string() })?; traverse_paths_recursively(Some(entry.path()), collection)?; } From b238938d7d6387d7340f9c6a30025c9255973180 Mon Sep 17 00:00:00 2001 From: Brian Carlsen Date: Sun, 4 Feb 2024 20:48:37 +0100 Subject: [PATCH 2/3] Bug fix for macOS. --- src/macos.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macos.rs b/src/macos.rs index 063f238..ace1e8c 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -162,7 +162,7 @@ fn delete_using_finder(full_paths: Vec) -> Result<(), Error> { Some(code) => { return Err(Error::Os { - code: result.status.code(), + code, description: format!("The AppleScript exited with error. stderr: {}", stderr), }) } From 2b1c9fa2a9743c1d5477bf5512ba0f260cfdacb5 Mon Sep 17 00:00:00 2001 From: Brian Carlsen Date: Mon, 5 Feb 2024 09:11:10 +0100 Subject: [PATCH 3/3] Removed tracing. --- src/windows.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 3cc8be6..b3a7f3e 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -79,9 +79,7 @@ impl TrashContext { /// Removes all files and folder paths recursively. pub(crate) fn delete_all_canonicalized(&self, full_paths: Vec) -> Result<(), Error> { let mut collection = Vec::new(); - log::trace!("Starting traverse_paths_recursively"); traverse_paths_recursively(full_paths, &mut collection)?; - log::trace!("Finished traverse_paths_recursively"); self.delete_specified_canonicalized(collection) } }