Skip to content

Commit

Permalink
fix!: Assure directory deletions on Windows don't put the entire cont…
Browse files Browse the repository at this point in the history
…ents into the trash.

Instead, like on other platforms, on Windows it will now put the folder into the trash instead.

Please note that this is not a breaking change in terms of API, but a *potentially* breaking change with older Windows versions. It's unknown if there are side-effects, as it's unknown why Windows had special behaviour previously.
  • Loading branch information
Byron committed Mar 12, 2024
2 parents b6e2d6c + d03934f commit 146ea03
Showing 1 changed file with 2 additions and 37 deletions.
39 changes: 2 additions & 37 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{Error, TrashContext, TrashItem, TrashItemMetadata, TrashItemSize};
use std::{
borrow::Borrow,
ffi::{c_void, OsStr, OsString},
fs, io,
os::windows::{ffi::OsStrExt, prelude::*},
path::PathBuf,
};
Expand Down Expand Up @@ -78,9 +77,8 @@ impl TrashContext {

/// Removes all files and folder paths recursively.
pub(crate) fn delete_all_canonicalized(&self, full_paths: Vec<PathBuf>) -> Result<(), Error> {
let mut collection = Vec::new();
traverse_paths_recursively(full_paths, &mut collection)?;
self.delete_specified_canonicalized(collection)
self.delete_specified_canonicalized(full_paths)?;
Ok(())
}
}

Expand Down Expand Up @@ -305,36 +303,3 @@ thread_local! {
fn ensure_com_initialized() {
CO_INITIALIZER.with(|_| {});
}

fn traverse_paths_recursively(
paths: impl IntoIterator<Item = PathBuf>,
collection: &mut Vec<PathBuf>,
) -> Result<(), Error> {
for base_path in paths {
if base_path.is_file() || base_path.is_symlink() {
collection.push(base_path);
continue;
}

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)?;
}
collection.push(base_path);
}
Ok(())
}

0 comments on commit 146ea03

Please sign in to comment.