A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
//! Virtual shell folders: places in the shell namespace that have no file-system path (an MTP
//! phone under This PC, Phone Link's mobile devices, Network, a library…). Items are addressed
//! by their desktop-absolute parsing name, which `SHCreateItemFromParsingName` takes back, so a
//! location can be stored, restored and navigated to later.
//!
//! Everything here blocks (device I/O, shell extensions): call from a worker thread. COM is
//! initialized per call (see `shell::ensure_com`).
use std::path::{Path, PathBuf};
use windows::Win32::Foundation::FILETIME;
use windows::Win32::Storage::EnhancedStorage::{PKEY_DateModified, PKEY_Size};
use windows::Win32::System::Com::CoTaskMemFree;
use windows::Win32::System::SystemServices::{SFGAO_FILESYSTEM, SFGAO_FLAGS, SFGAO_FOLDER, SFGAO_STREAM};
use windows::Win32::UI::Shell::Common::ITEMIDLIST;
use windows::Win32::UI::Shell::{
BHID_EnumItems, IEnumShellItems, IShellItem, IShellItem2, SEE_MASK_FLAG_NO_UI, SEE_MASK_IDLIST, SEE_MASK_NOASYNC,
SHCreateItemFromParsingName, SHELLEXECUTEINFOW, SHParseDisplayName,
ShellExecuteExW, SIGDN, SIGDN_DESKTOPABSOLUTEPARSING, SIGDN_FILESYSPATH, SIGDN_NORMALDISPLAY,
};
use windows::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL;
use windows::core::{Interface, PCWSTR};
use crate::shell::ensure_com;
/// "This PC": its non-file-system children are the devices (phones, cameras).
pub const THIS_PC: &str = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}";
/// Windows' Network folder: the computers and devices found on the local network (issue 468).
pub const NETWORK: &str = "::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}";
/// One child of a shell folder.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShellChild {
/// What the shell shows ("Internal storage", "DCIM", "IMG_0001.jpg").
pub name: String,
/// Desktop-absolute parsing name: navigate to it, copy from it, open it.
pub parsing: String,
/// A folder to browse into (a `.zip` is a shell folder too, but a stream: not counted).
pub is_folder: bool,
/// The real file-system path when the item is one (`SFGAO_FILESYSTEM`).
pub fs_path: Option<PathBuf>,
pub size: Option<u64>,
/// Unix time in milliseconds.
pub modified_ms: Option<i64>,
}
/// A resolved shell folder.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Place {
pub parsing: String,
pub name: String,
pub fs_path: Option<PathBuf>,
}
fn display(item: &IShellItem, sigdn: SIGDN) -> Option<String> {
unsafe {
let p = item.GetDisplayName(sigdn).ok()?;
let s = p.to_string().ok();
CoTaskMemFree(Some(p.0 as *const _));
s
}
}
fn attrs(item: &IShellItem, mask: SFGAO_FLAGS) -> SFGAO_FLAGS {
unsafe { item.GetAttributes(mask).unwrap_or_default() }
}
fn filetime_ms(ft: FILETIME) -> i64 {
let t = (u64::from(ft.dwHighDateTime) << 32) | u64::from(ft.dwLowDateTime);
(t / 10_000) as i64 - 11_644_473_600_000
}
/// The shell item a parsing name stands for. By the name first; when the shell does not take its own name back, by walking down
/// from the nearest folder above it that it does take.
///
/// WHY THE WALK (issue 49, a Pixel 8 Pro over USB): the phone listed "Internal shared storage", and opening it said "The parameter
/// is incorrect (0x80070057)". The storage's name is the one the shell handed out for it (`SIGDN_DESKTOPABSOLUTEPARSING`), but the
/// portable-device folder does not parse it back. Its parent (the phone) parses, and the storage is among the parent's children
/// under that very name, so it is found there. (Not seen here: no phone on this PC. The walk is the reading of the report.)
pub(crate) fn bind(parsing: &str) -> anyhow::Result<IShellItem> {
ensure_com();
let target = parsing.trim();
match by_name(target) {
Ok(item) => Ok(item),
Err(e) => walk_to(target).ok_or_else(|| anyhow::Error::from(e)),
}
}
fn by_name(parsing: &str) -> windows::core::Result<IShellItem> {
let w = crate::wide::shell_name(parsing);
unsafe { SHCreateItemFromParsingName(PCWSTR(w.as_ptr()), None) }
}
/// Finds `target` from the nearest folder above it that binds by name, one level at a time: at each level the child whose name is
/// `target` or lies above it. `None` when no folder above binds or a level has no such child.
fn walk_to(target: &str) -> Option<IShellItem> {
// the nearest folder above that binds (a device path has backslashes of its own: `\\?\usb#...`, skipped)
let mut cut = target.len();
let mut found = None;
for _ in 0..12 {
cut = target[..cut].rfind('\\')?;
let above = &target[..cut];
if above.len() < 3 || above.ends_with('\\') || above.ends_with('?') {
continue;
}
if let Ok(item) = by_name(above) {
found = Some(item);
break;
}
}
let mut item = found?;
for _ in 0..64 {
let here = display(&item, SIGDN_DESKTOPABSOLUTEPARSING)?;
if here.eq_ignore_ascii_case(target) {
return Some(item);
}
item = child_toward(&item, target)?;
}
None
}
/// The child of `item` that is `target` or one of the folders above it (by their parsing names, ignoring the case of letters).
fn child_toward(item: &IShellItem, target: &str) -> Option<IShellItem> {
let en: IEnumShellItems = unsafe { item.BindToHandler(None, &BHID_EnumItems).ok()? };
loop {
let mut buf: [Option<IShellItem>; 32] = Default::default();
let mut got = 0u32;
let _ = unsafe { en.Next(&mut buf, Some(&mut got)) };
if got == 0 {
return None;
}
for c in buf.into_iter().take(got as usize).flatten() {
if display(&c, SIGDN_DESKTOPABSOLUTEPARSING).is_some_and(|name| leads_to(&name, target)) {
return Some(c);
}
}
if got < 32 {
return None;
}
}
}
/// Whether the parsing name `name` is `target` or a folder above it.
fn leads_to(name: &str, target: &str) -> bool {
let (n, t) = (name.to_lowercase(), target.to_lowercase());
t == n || t.strip_prefix(&n).is_some_and(|rest| rest.starts_with('\\'))
}
fn child_of(item: &IShellItem) -> Option<ShellChild> {
let parsing = display(item, SIGDN_DESKTOPABSOLUTEPARSING)?;
let name = display(item, SIGDN_NORMALDISPLAY).unwrap_or_else(|| parsing.clone());
let a = attrs(item, SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_STREAM);
let is_folder = a.contains(SFGAO_FOLDER) && !a.contains(SFGAO_STREAM);
let fs_path = a.contains(SFGAO_FILESYSTEM).then(|| display(item, SIGDN_FILESYSPATH)).flatten().map(PathBuf::from);
let (mut size, mut modified_ms) = (None, None);
if let Ok(i2) = item.cast::<IShellItem2>() {
if !is_folder {
size = unsafe { i2.GetUInt64(&PKEY_Size).ok() };
}
modified_ms = unsafe { i2.GetFileTime(&PKEY_DateModified).ok() }.map(filetime_ms).filter(|&m| m > 0);
}
Some(ShellChild { name, parsing, is_folder, fs_path, size, modified_ms })
}
fn enumerate(item: &IShellItem, per_batch: usize, cancel: &dyn Fn() -> bool, on_batch: &mut dyn FnMut(Vec<ShellChild>)) -> anyhow::Result<usize> {
let en: IEnumShellItems = unsafe { item.BindToHandler(None, &BHID_EnumItems)? };
let mut total = 0;
let per_batch = per_batch.clamp(1, 32);
loop {
if cancel() {
break;
}
let mut buf: [Option<IShellItem>; 32] = Default::default();
let mut got = 0u32;
// S_FALSE with fewer items than asked = the end
let _ = unsafe { en.Next(&mut buf[..per_batch], Some(&mut got)) };
if got == 0 {
break;
}
let batch: Vec<ShellChild> = buf.iter().take(got as usize).flatten().filter_map(child_of).collect();
total += batch.len();
if !batch.is_empty() {
on_batch(batch);
}
if (got as usize) < per_batch {
break;
}
}
Ok(total)
}
/// Lists the children of the shell folder `parsing`, in batches as the shell hands them out
/// (a slow device shows its first items while the rest is still coming). Stops early when
/// `cancel` turns true. Returns the number of items.
pub fn list(parsing: &str, cancel: &dyn Fn() -> bool, on_batch: &mut dyn FnMut(Vec<ShellChild>)) -> anyhow::Result<usize> {
// (which of the two steps failed goes to the log: a report's screenshot shows only the shell's message, issue 49)
let item = bind(parsing).inspect_err(|e| log::warn!("shell folder: could not open {parsing}: {e:#}"))?;
// Windows' Network folder finds computers one by one, seconds apart: each is handed on as it comes, not 32 at a time (issue 468:
// "In Windows Explorer, computers appear progressively as they are discovered")
let per_batch = if parsing.eq_ignore_ascii_case(NETWORK) { 1 } else { 32 };
enumerate(&item, per_batch, cancel, on_batch).inspect_err(|e| log::warn!("shell folder: could not list {parsing}: {e:#}"))
}
/// The children of a folder as one line each (`folder flag \t parsing name \t name`), to keep the last list of a slow folder between
/// runs (the Network folder, issue 468). Sizes and dates are left out.
pub fn to_lines(children: &[ShellChild]) -> String {
children
.iter()
.filter(|c| !c.parsing.contains(['\t', '\n']) && !c.name.contains(['\t', '\n']))
.map(|c| format!("{}\t{}\t{}\n", u8::from(c.is_folder), c.parsing, c.name))
.collect()
}
/// [`to_lines`] read back; lines that don't parse are skipped.
pub fn from_lines(text: &str) -> Vec<ShellChild> {
text.lines()
.filter_map(|l| {
let mut parts = l.splitn(3, '\t');
let folder = parts.next()? == "1";
let parsing = parts.next().filter(|p| !p.is_empty())?.to_string();
let name = parts.next()?.to_string();
Some(ShellChild { name, parsing, is_folder: folder, fs_path: None, size: None, modified_ms: None })
})
.collect()
}
/// The shell's name for a folder (what an address bar would show).
pub fn display_name(parsing: &str) -> Option<String> {
display(&bind(parsing).ok()?, SIGDN_NORMALDISPLAY)
}
/// Folder one level up in the namespace, if there is one (`None` at the desktop).
pub fn parent(parsing: &str) -> Option<Place> {
let item = bind(parsing).ok()?;
let up = unsafe { item.GetParent().ok()? };
place_of(&up)
}
fn place_of(item: &IShellItem) -> Option<Place> {
let parsing = display(item, SIGDN_DESKTOPABSOLUTEPARSING)?;
let name = display(item, SIGDN_NORMALDISPLAY).unwrap_or_else(|| parsing.clone());
let fs_path = attrs(item, SFGAO_FILESYSTEM).contains(SFGAO_FILESYSTEM).then(|| display(item, SIGDN_FILESYSPATH)).flatten().map(PathBuf::from);
Some(Place { parsing, name, fs_path })
}
/// Namespaces Explorer shows with a UI of its own (Control Panel and its applets, printers,
/// network connections, the apps folder…): not browsable as a plain list, left to Explorer.
const APPLETS: &[&str] = &[
"26ee0668-a00a-44d7-9371-beb064c98683", // Control Panel (all categories)
"21ec2020-3aea-1069-a2dd-08002b30309d", // Control Panel (all items)
"5399e694-6ce5-4d6c-8fce-1d8870fdcba0", // Control Panel (category view)
"ed7ba470-8e54-465e-825c-99712043e01c", // All Tasks ("God mode")
"2227a280-3aea-1069-a2de-08002b30309d", // Printers
"7007acc7-3202-11d1-aad2-00805fc1270e", // Network Connections
"7b81be6a-ce2b-4676-a29e-eb907a5126c5", // Programs and Features
"4234d49b-0245-4df3-b780-3893943456e1", // Apps (AppsFolder)
"e7de9b1a-7533-4556-b2f7-9b3ac4b0cb2d", // Windows Update (legacy)
"9343812e-1c37-4a49-a12e-4b2d810d956b", // Search
];
/// True when `parsing` is (inside) one of the applet namespaces.
pub fn is_applet(parsing: &str) -> bool {
let p = parsing.to_ascii_lowercase();
APPLETS.iter().any(|g| p.contains(g))
}
/// Resolves a `::{GUID}` / `shell:` / parsing-name argument to a shell folder that rustyfiles
/// can list itself: it must be a folder, not an applet namespace, and its enumerator must
/// open. Folders on the file system come back with their `fs_path` (the caller opens those as
/// ordinary folders). `None` = leave it to Explorer.
pub fn browsable(s: &str) -> Option<Place> {
let item = bind(s).ok()?;
if !attrs(&item, SFGAO_FOLDER).contains(SFGAO_FOLDER) {
return None;
}
let place = place_of(&item)?;
if is_applet(&place.parsing) || is_applet(s) {
return None;
}
// the enumerator has to open (a namespace extension that cannot is no folder to us)
unsafe { item.BindToHandler::<_, IEnumShellItems>(None, &BHID_EnumItems).ok()? };
Some(place)
}
/// Devices under This PC that are not drives: MTP phones and cameras, Phone Link's mobile
/// devices. Empty when none is connected (the usual case).
pub fn devices() -> Vec<ShellChild> {
let mut out = Vec::new();
let Ok(pc) = bind(THIS_PC) else { return out };
let _ = enumerate(&pc, 32, &|| false, &mut |b| out.extend(b.into_iter().filter(|c| c.fs_path.is_none() && c.is_folder)));
out.retain(|c| !is_applet(&c.parsing));
out
}
/// Copies shell items (given by parsing name) into the real folder `dest` with the shell's own
/// copy engine, which knows how to pull a file off a phone. `Ok(false)` = cancelled.
pub fn copy_out(sources: &[String], dest: &Path) -> anyhow::Result<bool> {
use windows::Win32::UI::Shell::FOF_NOCONFIRMMKDIR;
let src: Vec<PathBuf> = sources.iter().map(PathBuf::from).collect();
match crate::fileops::shell_copy(&src, dest, FOF_NOCONFIRMMKDIR.0 as u32) {
Ok(()) => Ok(true),
Err(e) if e.to_string().contains("aborted") => Ok(false),
Err(e) => Err(e),
}
}
/// Whether `s` is a name in the shell's namespace (`::{CLSID}\...`: a phone, This PC's devices) rather than a path on a disk.
pub(crate) fn is_namespace_name(s: &str) -> bool {
s.trim_start().starts_with("::")
}
/// The absolute id list of the shell item `parsing` names: by the name, and for a name in the shell's namespace that the shell does
/// not take back, through [`bind`]'s walk (issue 221: a phone's photos could not be copied or given their menu, "The parameter is
/// incorrect (0x80070057)": the copy and the menu asked the shell by the name only, which issue 49 found the portable-device folder
/// refuses). The caller frees it with `CoTaskMemFree`.
pub(crate) fn id_list(parsing: &str) -> anyhow::Result<*mut ITEMIDLIST> {
ensure_com();
let w = crate::wide::shell_name(parsing.trim());
let mut pidl: *mut ITEMIDLIST = std::ptr::null_mut();
match unsafe { SHParseDisplayName(PCWSTR(w.as_ptr()), None, &mut pidl, 0, None) } {
Ok(()) => Ok(pidl),
Err(e) if is_namespace_name(parsing) => {
let item = bind(parsing).map_err(|_| anyhow::Error::from(e))?;
Ok(unsafe { windows::Win32::UI::Shell::SHGetIDListFromObject(&item)? })
}
Err(e) => Err(e.into()),
}
}
/// Opens an item with the shell's default verb (what a double-click in Explorer does).
pub fn open(parsing: &str) -> anyhow::Result<()> {
let pidl = id_list(parsing)?;
unsafe {
let mut info = SHELLEXECUTEINFOW {
cbSize: std::mem::size_of::<SHELLEXECUTEINFOW>() as u32,
fMask: SEE_MASK_IDLIST | SEE_MASK_NOASYNC | SEE_MASK_FLAG_NO_UI,
lpIDList: pidl as *mut _,
nShow: SW_SHOWNORMAL.0,
..Default::default()
};
let r = ShellExecuteExW(&mut info);
CoTaskMemFree(Some(pidl as *const _));
r?;
}
Ok(())
}
/// Last part of a parsing name, for a title when the shell cannot be asked: `...\Documents`
/// → `Documents`.
pub fn last_segment(parsing: &str) -> &str {
let t = parsing.trim_end_matches('\\');
t.rsplit('\\').next().unwrap_or(t)
}
#[cfg(test)]
mod tests {
use super::*;
const LIBRARIES: &str = "::{031E4825-7B94-4DC3-B131-E946B44C8DD5}";
#[test]
fn a_kept_list_reads_back() {
let kids = vec![
ShellChild { name: "NAS".into(), parsing: r"\\NAS".into(), is_folder: true, fs_path: None, size: None, modified_ms: None },
ShellChild { name: "Printer".into(), parsing: "::{X}\\uuid:1".into(), is_folder: false, fs_path: None, size: None, modified_ms: None },
];
assert_eq!(from_lines(&to_lines(&kids)), kids);
assert!(from_lines("garbage\n\t\t\n").is_empty());
}
#[test]
fn libraries_list_and_parsing_names_round_trip() {
let mut got = Vec::new();
let n = list(LIBRARIES, &|| false, &mut |b| got.extend(b)).expect("libraries enumerate");
assert_eq!(n, got.len());
for c in &got {
assert!(!c.name.is_empty() && !c.parsing.is_empty(), "{c:?}");
// what we hand out can be bound again
assert_eq!(display_name(&c.parsing).as_deref(), Some(c.name.as_str()), "{c:?}");
}
eprintln!("libraries: {:?}", got.iter().map(|c| (&c.name, &c.parsing, c.is_folder)).collect::<Vec<_>>());
}
#[test]
fn this_pc_children_and_devices_do_not_panic() {
let mut got = Vec::new();
list(THIS_PC, &|| false, &mut |b| got.extend(b)).expect("this pc");
assert!(got.iter().any(|c| c.fs_path.is_some()), "a drive is expected: {got:?}");
for d in devices() {
assert!(d.fs_path.is_none() && d.is_folder, "{d:?}");
}
}
#[test]
fn network_enumerates_or_fails_cleanly() {
let mut n = 0;
let _ = list(NETWORK, &|| false, &mut |b| n += b.len());
eprintln!("network items: {n}");
}
#[test]
fn cancel_stops_early() {
let mut batches = 0;
let _ = list(THIS_PC, &|| true, &mut |_| batches += 1);
assert_eq!(batches, 0);
}
#[test]
fn applets_are_left_to_explorer() {
assert!(is_applet("::{26EE0668-A00A-44D7-9371-BEB064C98683}"));
assert!(is_applet("::{26EE0668-A00A-44D7-9371-BEB064C98683}\\0\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}"));
assert!(!is_applet(LIBRARIES));
assert!(browsable("::{26EE0668-A00A-44D7-9371-BEB064C98683}").is_none());
assert!(browsable("shell:ControlPanelFolder").is_none());
let lib = browsable(LIBRARIES).expect("libraries browse");
assert!(!lib.name.is_empty());
assert!(browsable("::{00000000-0000-0000-0000-000000000000}").is_none());
}
#[test]
fn parent_goes_up_the_namespace() {
let mut got = Vec::new();
list(LIBRARIES, &|| false, &mut |b| got.extend(b)).unwrap();
if let Some(c) = got.first() {
let up = parent(&c.parsing).expect("a library has a parent");
assert!(!up.name.is_empty());
}
}
/// A `.zip` is a shell folder whose items have no file-system path: the same shape as a
/// phone's. Lists it, copies an item out with the shell's engine, opens nothing.
#[test]
fn items_without_a_path_copy_out() {
if crate::engine_guard::not_run_without_shell("items_without_a_path_copy_out") {
return;
}
let dir = std::env::temp_dir().join(format!("rf-shellfolder-test-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("src")).unwrap();
std::fs::write(dir.join("src").join("hello.txt"), b"hello from a shell folder").unwrap();
let zip = dir.join("t.zip");
let status = std::process::Command::new("powershell")
.args(["-NoProfile", "-Command", &format!("Compress-Archive -Path '{}' -DestinationPath '{}'", dir.join("src").join("hello.txt").display(), zip.display())])
.status()
.expect("powershell");
assert!(status.success());
let mut got = Vec::new();
list(&zip.to_string_lossy(), &|| false, &mut |b| got.extend(b)).expect("zip enumerates");
let item = got.iter().find(|c| c.name.starts_with("hello")).unwrap_or_else(|| panic!("hello.txt in {got:?}"));
assert!(item.fs_path.is_none(), "{item:?}");
assert!(!item.is_folder);
assert_eq!(item.size, Some(25), "{item:?}");
let out = dir.join("out");
std::fs::create_dir_all(&out).unwrap();
assert!(copy_out(&[item.parsing.clone()], &out).expect("copy out"));
let copied: Vec<_> = std::fs::read_dir(&out).unwrap().map(|e| e.unwrap().path()).collect();
assert_eq!(copied.len(), 1, "{copied:?}");
assert_eq!(std::fs::read(&copied[0]).unwrap(), b"hello from a shell folder");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn last_segment_of_parsing_names() {
assert_eq!(last_segment(r"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\Foo"), "Foo");
assert_eq!(last_segment("Foo"), "Foo");
}
}
#[cfg(test)]
mod walk_tests {
use super::leads_to;
#[test]
fn a_child_leads_to_the_target_when_it_is_the_target_or_a_folder_above_it() {
let phone = r"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_18d1&pid_4ee1#3a1#{6ac27878-a6fa-4155-ba85-f98f491d4f33}";
let storage = format!(r"{phone}\SID-{{10001,,118648832000}}");
let dcim = format!(r"{storage}\{{0000000A-0004-0000-0000-000000000000}}");
assert!(leads_to(&storage, &storage));
assert!(leads_to(&storage, &dcim), "the storage lies above DCIM");
assert!(leads_to(&storage.to_uppercase(), &dcim), "letters' case does not matter");
assert!(!leads_to(&format!(r"{phone}\SID-{{10001,,1}}"), &dcim), "another storage, whose name starts the same, does not");
assert!(!leads_to(&dcim, &storage), "below the target is not toward it");
}
/// The walk finds a real folder by its parsing name from the folder above it (here a file-system folder, which binds by name
/// anyway: what is tested is the walk itself).
#[test]
fn the_walk_reaches_a_folder_from_the_one_above_it() {
let dir = std::env::temp_dir().join(format!("rf-walk-test-{}", std::process::id()));
std::fs::create_dir_all(dir.join("inner")).unwrap();
// the long form of the path: TEMP can come in 8.3 names (GitHub's runners: C:\Users\RUNNER~1), and the shell's parsing names
// are the long ones, so the walk would look for a "RUNNER~1" it never meets
let long = std::fs::canonicalize(&dir).unwrap();
let dir = std::path::PathBuf::from(long.to_string_lossy().trim_start_matches(r"\\?\"));
let target = dir.join("inner").to_string_lossy().into_owned();
super::ensure_com();
let found = super::walk_to(&target).and_then(|i| super::display(&i, super::SIGDN_DESKTOPABSOLUTEPARSING));
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(found.map(|s| s.to_lowercase()), Some(target.to_lowercase()));
}
}