From e4b7119fcc369c5594e9e2b5dad8f1a6616593f7 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Tue, 18 Jun 2024 01:16:20 -0400 Subject: [PATCH] Update Windows code to account for API change 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. --- src/tests.rs | 3 +++ src/windows.rs | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/tests.rs b/src/tests.rs index 99d04eb..72e72ea 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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; @@ -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() { diff --git a/src/windows.rs b/src/windows.rs index 9817336..da496f1 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -97,9 +97,12 @@ pub fn list() -> Result, 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, });