Skip to content

Commit

Permalink
Update Windows code to account for API change
Browse files Browse the repository at this point in the history
I opted to not remove the UTF8 verification for Windows' platform
specific code. While it's unlikely that Windows' API would return
invalid Strings, the extra check for a filename can't hurt whereas
removing it would require modifying a decent chunk of the code.

The old code performed the check, and converting a String to an OsString
is free. Path of least resistance.
  • Loading branch information
joshuamegnauth54 committed Jun 18, 2024
1 parent 559b57b commit e4b7119
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ mod os_limited {
use std::collections::{hash_map::Entry, HashMap};
use std::ffi::{OsStr, OsString};
use std::fs::File;

#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
use std::os::unix::ffi::OsStringExt;

use crate as trash;
Expand Down Expand Up @@ -95,6 +97,7 @@ mod os_limited {
let _ = trash::os_limited::purge_all(items.iter().flat_map(|(_name, item)| item));
}

#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
#[test]
#[serial]
fn list_invalid_utf8() {
Expand Down
5 changes: 4 additions & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
let original_location = OsString::from_wide(original_location_bstr.as_wide());
let date_deleted = get_date_deleted_unix(&item2)?;

// NTFS paths are valid Unicode according to this chart:
// https://en.wikipedia.org/wiki/Filename#Comparison_of_filename_limitations
// Converting a String back to OsString doesn't do extra work
item_vec.push(TrashItem {
id,
name: name.into_string().map_err(|original| Error::ConvertOsString { original })?,
name: name.into_string().map_err(|original| Error::ConvertOsString { original })?.into(),
original_parent: PathBuf::from(original_location),
time_deleted: date_deleted,
});
Expand Down

0 comments on commit e4b7119

Please sign in to comment.