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
//! The programs that run from a folder: found and ended before the folder is removed (an add-on's folder, whose program may be
//! running in the background or started by a browser).
use std::path::{Path, PathBuf};
/// Whether `exe` lies inside `dir` (any letter case, `dir` with or without a trailing separator). Pure.
pub fn lies_in(exe: &Path, dir: &Path) -> bool {
let (exe, dir) = (exe.to_string_lossy().to_lowercase(), dir.to_string_lossy().to_lowercase());
let dir = dir.trim_end_matches(['\\', '/']);
!dir.is_empty() && exe.len() > dir.len() + 1 && exe.starts_with(dir) && matches!(exe.as_bytes()[dir.len()], b'\\' | b'/')
}
/// The running processes of this user whose program file lies in `dir`: (process id, program).
pub fn running_in(dir: &Path) -> Vec<(u32, PathBuf)> {
use windows::Win32::Foundation::CloseHandle;
use windows::Win32::System::Diagnostics::ToolHelp::{CreateToolhelp32Snapshot, PROCESSENTRY32W, Process32FirstW, Process32NextW, TH32CS_SNAPPROCESS};
use windows::Win32::System::Threading::{OpenProcess, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION, QueryFullProcessImageNameW};
let mut out = Vec::new();
// SAFETY: a snapshot walked with a properly sized entry; every handle opened here is closed here.
unsafe {
let Ok(snap) = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) else { return out };
let mut e = PROCESSENTRY32W { dwSize: std::mem::size_of::<PROCESSENTRY32W>() as u32, ..Default::default() };
let mut more = Process32FirstW(snap, &mut e).is_ok();
while more {
if let Ok(p) = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, e.th32ProcessID) {
let mut buf = [0u16; 1024];
let mut len = buf.len() as u32;
if QueryFullProcessImageNameW(p, PROCESS_NAME_WIN32, windows::core::PWSTR(buf.as_mut_ptr()), &mut len).is_ok() {
let exe = PathBuf::from(String::from_utf16_lossy(&buf[..len as usize]));
if lies_in(&exe, dir) {
out.push((e.th32ProcessID, exe));
}
}
let _ = CloseHandle(p);
}
more = Process32NextW(snap, &mut e).is_ok();
}
let _ = CloseHandle(snap);
}
out
}
/// Ends every process whose program lies in `dir` and waits up to `wait` for them to be gone. Returns how many were ended.
pub fn end_in(dir: &Path, wait: std::time::Duration) -> usize {
use windows::Win32::Foundation::CloseHandle;
use windows::Win32::System::Threading::{OpenProcess, PROCESS_SYNCHRONIZE, PROCESS_TERMINATE, TerminateProcess, WaitForSingleObject};
let mut ended = 0;
for (pid, exe) in running_in(dir) {
// SAFETY: a handle to one process, closed here.
unsafe {
let Ok(p) = OpenProcess(PROCESS_TERMINATE | PROCESS_SYNCHRONIZE, false, pid) else { continue };
if TerminateProcess(p, 1).is_ok() {
ended += 1;
log::info!("ended {} ({pid}) before its folder is removed", exe.display());
let _ = WaitForSingleObject(p, wait.as_millis().min(u32::MAX as u128) as u32);
}
let _ = CloseHandle(p);
}
}
ended
}
#[cfg(test)]
mod tests {
use super::lies_in;
use std::path::Path;
#[test]
fn a_program_lies_in_its_folder_and_not_in_a_folder_of_a_longer_name() {
let dir = Path::new(r"C:\Users\me\AppData\Roaming\rustyfiles\addons\hyperdownload");
assert!(lies_in(Path::new(r"C:\Users\me\AppData\Roaming\rustyfiles\addons\hyperdownload\hyperdownload.exe"), dir));
assert!(lies_in(Path::new(r"c:\users\ME\appdata\roaming\rustyfiles\addons\HyperDownload\bin\x.exe"), dir));
assert!(lies_in(Path::new(r"C:\Users\me\AppData\Roaming\rustyfiles\addons\hyperdownload\a.exe"), Path::new(r"C:\Users\me\AppData\Roaming\rustyfiles\addons\hyperdownload\")));
assert!(!lies_in(Path::new(r"C:\Users\me\AppData\Roaming\rustyfiles\addons\hyperdownload2\x.exe"), dir));
assert!(!lies_in(Path::new(r"C:\Users\me\AppData\Roaming\rustyfiles\addons\hyperdownload"), dir));
assert!(!lies_in(Path::new(r"C:\x.exe"), Path::new("")));
}
}