Skip to content

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.

Removing an add-on ends its running program first

gelenmedya4303d14e720f
diff --git a/crates/rf-app/src/addons.rs b/crates/rf-app/src/addons.rs
index 43bd061b..1390f7dc 100644
--- a/crates/rf-app/src/addons.rs
+++ b/crates/rf-app/src/addons.rs
@@ -75,7 +75,7 @@ pub fn root() -> Option<PathBuf> {
crate::settings::local_data_dir().map(|d| d.join("addons"))
}
-fn dir_of(id: &str) -> Option<PathBuf> {
+pub(crate) fn dir_of(id: &str) -> Option<PathBuf> {
// (an id from the list becomes a folder name: only plain names)
(!id.is_empty() && id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')).then(|| root().map(|r| r.join(id))).flatten()
}
diff --git a/crates/rf-app/src/addons_page.rs b/crates/rf-app/src/addons_page.rs
index 5eadbe4d..d0a8aab0 100644
--- a/crates/rf-app/src/addons_page.rs
+++ b/crates/rf-app/src/addons_page.rs
@@ -116,21 +116,49 @@ fn install(info: AddonInfo, cx: &mut App) {
.detach();
}
+/// Removes an add-on: its running program is ended first, off the interface thread.
+///
+/// WHY (discussions 825 and 827: "clicking the Remove button did nothing ... I had to manually open Task Manager, locate the process,
+/// and end the task before I could remove it"). HyperDownload keeps running in the background after a download, and a browser keeps
+/// its host started from the same folder while the extension is on; Windows does not delete a program that is running, so the folder
+/// stayed and the button only showed an error under the card. The add-on is asked to end (HyperDownload's own shutdown call, which
+/// saves its list), and whatever still runs from its folder a moment later is ended.
fn remove(id: String, cx: &mut App) {
- if id == crate::hyperdownload::ID {
- crate::hyperdownload::unregister_host();
- }
- let got = addons::remove(&id);
- let st = state(cx);
- match got {
- Ok(()) => {
- st.errors.remove(&id);
- }
- Err(e) => {
- st.errors.insert(id, format!("{}: {e}", t!("Kaldırılamadı (program açık olabilir)")));
+ let task = cx.background_spawn({
+ let id = id.clone();
+ async move {
+ if id == crate::hyperdownload::ID {
+ crate::hyperdownload::unregister_host();
+ if let Some(rpc) = crate::hyperdownload::running() {
+ let _ = rpc.call("aria2.shutdown", vec![]);
+ }
+ }
+ if let Some(dir) = addons::dir_of(&id) {
+ let until = std::time::Instant::now() + std::time::Duration::from_secs(3);
+ while std::time::Instant::now() < until && !rf_win::folder_procs::running_in(&dir).is_empty() {
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ }
+ rf_win::folder_procs::end_in(&dir, std::time::Duration::from_secs(3));
+ }
+ addons::remove(&id)
}
- }
- cx.refresh_windows();
+ });
+ cx.spawn(async move |cx| {
+ let got = task.await;
+ let _ = cx.update(|cx| {
+ let st = state(cx);
+ match got {
+ Ok(()) => {
+ st.errors.remove(&id);
+ }
+ Err(e) => {
+ st.errors.insert(id, format!("{}: {e}", t!("Kaldırılamadı (program açık olabilir)")));
+ }
+ }
+ cx.refresh_windows();
+ });
+ })
+ .detach();
}
fn open_url(url: String) {
diff --git a/crates/rf-win/src/folder_procs.rs b/crates/rf-win/src/folder_procs.rs
new file mode 100644
index 00000000..d4b79512
--- /dev/null
+++ b/crates/rf-win/src/folder_procs.rs
@@ -0,0 +1,78 @@
+//! 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("")));
+ }
+}
diff --git a/crates/rf-win/src/lib.rs b/crates/rf-win/src/lib.rs
index e252ea69..1cd0f078 100644
--- a/crates/rf-win/src/lib.rs
+++ b/crates/rf-win/src/lib.rs
@@ -8,6 +8,7 @@ pub mod appvolume;
pub mod apppkg;
pub mod apps;
pub mod datefmt;
+pub mod folder_procs;
pub mod share;
pub mod apps_direct;
pub mod bench;