Skip to content

Commit

Permalink
Windows implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Jan 7, 2024
1 parent 3bea3e2 commit 1a1f75e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ once_cell = "1.7.2"
[target.'cfg(windows)'.dependencies]
windows = { version = "0.44.0", features = [
"Win32_Foundation",
"Win32_Storage_EnhancedStorage",
"Win32_System_Com_StructuredStorage",
"Win32_System_SystemServices",
"Win32_UI_Shell_PropertiesSystem",
] }
scopeguard = "1.2.0"
Expand All @@ -61,4 +63,4 @@ pre-build = [
"cp /tmp/netbsd/usr/lib/libexecinfo.so /usr/local/x86_64-unknown-netbsd/lib",
"rm base.tar.xz",
"rm -rf /tmp/netbsd",
]
]
46 changes: 43 additions & 3 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Error, TrashContext, TrashItem};
use crate::{Error, TrashContext, TrashItem, TrashItemMetadata};
use std::{
borrow::Borrow,
ffi::{c_void, OsStr, OsString},
Expand All @@ -7,7 +7,10 @@ use std::{
path::PathBuf,
};
use windows::core::{Interface, GUID, PCWSTR, PWSTR};
use windows::Win32::{Foundation::*, System::Com::*, UI::Shell::PropertiesSystem::*, UI::Shell::*};
use windows::Win32::{
Foundation::*, Storage::EnhancedStorage::*, System::Com::*, System::SystemServices::*,
UI::Shell::PropertiesSystem::*, UI::Shell::*,
};

///////////////////////////////////////////////////////////////////////////
// These don't have bindings in windows-rs for some reason
Expand Down Expand Up @@ -70,7 +73,7 @@ impl TrashContext {
}
}

/// Removes all files and folder paths recursively.
/// 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)?;
Expand Down Expand Up @@ -124,6 +127,43 @@ pub fn list() -> Result<Vec<TrashItem>, Error> {
}
}

pub fn metadata(item: &TrashItem) -> Result<TrashItemMetadata, Error> {
ensure_com_initialized();
unsafe {
let id_as_wide: Vec<u16> = item.id.encode_wide().chain(std::iter::once(0)).collect();
let parsing_name = PCWSTR(id_as_wide.as_ptr());
let item: IShellItem = SHCreateItemFromParsingName(parsing_name, None)?;
let is_dir = item.GetAttributes(SFGAO_FOLDER)? == SFGAO_FOLDER;
let size = if is_dir {
let pesi: IEnumShellItems = item.BindToHandler(None, &BHID_EnumItems)?;
let mut size = 0;
loop {
let mut fetched_count: u32 = 0;
let mut arr = [None];
pesi.Next(&mut arr, Some(&mut fetched_count as *mut u32))?;

if fetched_count == 0 {
break;
}

match &arr[0] {
Some(_item) => {
size += 1;
}
None => {
break;
}
}
}
size
} else {
let item2: IShellItem2 = item.cast()?;
item2.GetUInt64(&PKEY_Size)?
};
Ok(TrashItemMetadata { is_dir, size })
}
}

pub fn purge_all<I>(items: I) -> Result<(), Error>
where
I: IntoIterator,
Expand Down

0 comments on commit 1a1f75e

Please sign in to comment.