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.

The drive watch lets go when a volume is about to be locked, so an eject is no longer refused (622)

gelenmedya2d787aea8c49
diff --git a/crates/rf-win/src/removal.rs b/crates/rf-win/src/removal.rs
index f63f9878..9e8a270a 100644
--- a/crates/rf-win/src/removal.rs
+++ b/crates/rf-win/src/removal.rs
@@ -10,6 +10,17 @@
//! - one notification handle per non-system volume, re-synced when drives come and go;
//! - on a removal query: close our handle, call the app, wait (bounded) for its ack, allow;
//! - removal failed (someone else still uses it): register again.
+//!
+//! AND THE SAME WHEN A VOLUME IS ABOUT TO BE LOCKED OR DISMOUNTED (issue 622, found on 2026-12-04 after three fixes that missed it).
+//! An eject of a disk with removable media (a USB stick, a card) does not ask through the device at all: it LOCKS the volume, and
+//! a lock is given only when no handle is open on it. Before it looks, the file system tells everybody who registered a handle
+//! (`DBT_CUSTOMEVENT` with `GUID_IO_VOLUME_LOCK`; its source says "so that folks that like to hold handles on volumes can get out
+//! of the way"). This module registered such a handle on every drive and did not listen for that word, so the handle that exists to
+//! let a drive go was the one that kept it: with no window showing the drive, every eject, from our sidebar, from Explorer or
+//! from any tool, could be refused as "in use". MEASURED with a mounted disc image and a plain lock request from another program:
+//! refused (error 5) while rustyfiles ran, given the moment rustyfiles was stopped, refused again when it was back. The answer is
+//! the one a removal query gets: close our handle, let the app let go, wait a moment. When the lock fails or is taken off again the
+//! drive is watched again (also by the timer, once the grace time is over).
use std::collections::HashMap;
use std::sync::OnceLock;
@@ -24,9 +35,11 @@ use windows::Win32::Storage::FileSystem::{
};
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::UI::WindowsAndMessaging::{
- CreateWindowExW, DBT_DEVICEARRIVAL, DBT_DEVICEQUERYREMOVE, DBT_DEVICEQUERYREMOVEFAILED, DBT_DEVICEREMOVECOMPLETE,
- DBT_DEVICEREMOVEPENDING, DBT_DEVTYP_HANDLE, DEV_BROADCAST_HANDLE, DEV_BROADCAST_HDR, DEVICE_NOTIFY_WINDOW_HANDLE,
- DefWindowProcW, DispatchMessageW, GetMessageW, HDEVNOTIFY, MSG, RegisterClassW, RegisterDeviceNotificationW,
+ CreateWindowExW, DBT_CUSTOMEVENT, DBT_DEVICEARRIVAL, DBT_DEVICEQUERYREMOVE, DBT_DEVICEQUERYREMOVEFAILED,
+ DBT_DEVICEREMOVECOMPLETE, DBT_DEVICEREMOVEPENDING, DBT_DEVTYP_HANDLE, DEV_BROADCAST_HANDLE, DEV_BROADCAST_HDR,
+ DEVICE_NOTIFY_WINDOW_HANDLE, DefWindowProcW, DispatchMessageW, GUID_IO_VOLUME_DISMOUNT, GUID_IO_VOLUME_DISMOUNT_FAILED,
+ GUID_IO_VOLUME_LOCK, GUID_IO_VOLUME_LOCK_FAILED, GUID_IO_VOLUME_MOUNT, GUID_IO_VOLUME_UNLOCK, GetMessageW, HDEVNOTIFY, MSG,
+ RegisterClassW, RegisterDeviceNotificationW,
SetTimer, TranslateMessage, UnregisterDeviceNotification, WM_DEVICECHANGE, WM_TIMER, WNDCLASSW, WS_EX_TOOLWINDOW,
WS_POPUP,
};
@@ -218,24 +231,27 @@ fn on_device_change(hwnd: HWND, event: u32, lp: LPARAM) -> LRESULT {
match event {
DBT_DEVICEQUERYREMOVE => {
if let Some(letter) = handle_info().and_then(|(h, n)| letter_for(h, n)) {
- {
- let mut st = state().lock();
- if let Some(r) = st.regs.remove(&letter) {
- unregister(r);
- }
- st.released.insert(letter, Instant::now());
- }
- let (tx, rx) = std::sync::mpsc::channel();
- if let Some(cb) = CALLBACK.get().and_then(|c| c.lock().as_ref().map(|f| f as *const OnQuery)) {
- // SAFETY: the callback is never replaced or dropped once set
- unsafe { (*cb)(letter, tx) };
- if rx.recv_timeout(APP_WAIT).is_ok() {
- std::thread::sleep(SETTLE);
- }
- }
+ let_go(letter);
}
LRESULT(1) // allow
}
+ // the file system's word about a volume we hold a handle on (see the module's notes)
+ DBT_CUSTOMEVENT => {
+ let told = handle_info().and_then(|(h, n)| {
+ // SAFETY: `handle_info` returned `Some`, so `lp` points at a `DEV_BROADCAST_HANDLE`
+ let guid = unsafe { (*(lp.0 as *const DEV_BROADCAST_HANDLE)).dbch_eventguid };
+ Some((letter_for(h, n)?, volume_word(guid.to_u128())?))
+ });
+ match told {
+ Some((letter, VolumeWord::GetOutOfTheWay)) => let_go(letter),
+ Some((letter, VolumeWord::ItStays)) => {
+ state().lock().released.remove(&letter);
+ sync(hwnd);
+ }
+ None => {}
+ }
+ LRESULT(1)
+ }
DBT_DEVICEQUERYREMOVEFAILED => {
// someone else still holds the drive: it stays, watch it again
state().lock().released.clear();
@@ -250,6 +266,49 @@ fn on_device_change(hwnd: HWND, event: u32, lp: LPARAM) -> LRESULT {
}
}
+/// What a volume's own notice asks of somebody who holds a handle on it.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum VolumeWord {
+ /// It is about to be locked or dismounted: every handle has to go, or the request fails.
+ GetOutOfTheWay,
+ /// The lock or dismount failed, or the lock was taken off, or it is mounted again: it can be watched again.
+ ItStays,
+}
+
+/// The notice a `DBT_CUSTOMEVENT`'s identifier is; `None` for the many that say nothing about handles (a label change, a size
+/// change, BitLocker's state). Pure.
+fn volume_word(guid: u128) -> Option<VolumeWord> {
+ let is = |g: windows::core::GUID| g.to_u128() == guid;
+ if is(GUID_IO_VOLUME_LOCK) || is(GUID_IO_VOLUME_DISMOUNT) {
+ Some(VolumeWord::GetOutOfTheWay)
+ } else if is(GUID_IO_VOLUME_LOCK_FAILED) || is(GUID_IO_VOLUME_DISMOUNT_FAILED) || is(GUID_IO_VOLUME_UNLOCK) || is(GUID_IO_VOLUME_MOUNT) {
+ Some(VolumeWord::ItStays)
+ } else {
+ None
+ }
+}
+
+/// Lets go of drive `letter` while the system waits for our answer: our own handle first, then the app (its panes on the drive go
+/// elsewhere, their watchers stop), for a bounded time. The drive is not watched again until the removal completes or fails, or
+/// the grace time passes.
+fn let_go(letter: char) {
+ {
+ let mut st = state().lock();
+ if let Some(r) = st.regs.remove(&letter) {
+ unregister(r);
+ }
+ st.released.insert(letter, Instant::now());
+ }
+ let (tx, rx) = std::sync::mpsc::channel();
+ if let Some(cb) = CALLBACK.get().and_then(|c| c.lock().as_ref().map(|f| f as *const OnQuery)) {
+ // SAFETY: the callback is never replaced or dropped once set
+ unsafe { (*cb)(letter, tx) };
+ if rx.recv_timeout(APP_WAIT).is_ok() {
+ std::thread::sleep(SETTLE);
+ }
+ }
+}
+
/// Drive letters currently registered (for tests and diagnostics).
pub fn registered() -> Vec<char> {
let mut v: Vec<char> = state().lock().regs.keys().copied().collect();
@@ -262,6 +321,21 @@ mod tests {
use super::*;
use windows::Win32::UI::WindowsAndMessaging::SendMessageW;
+ /// Issue 622: a volume about to be locked or dismounted is let go of; a lock that failed or was taken off, and a mount, make
+ /// it watched again; the other notices of a volume change nothing.
+ #[test]
+ fn a_lock_or_dismount_means_out_of_the_way_and_their_failure_means_it_stays() {
+ use windows::Win32::UI::WindowsAndMessaging::{GUID_IO_VOLUME_CHANGE, GUID_IO_VOLUME_NAME_CHANGE};
+ assert_eq!(volume_word(GUID_IO_VOLUME_LOCK.to_u128()), Some(VolumeWord::GetOutOfTheWay));
+ assert_eq!(volume_word(GUID_IO_VOLUME_DISMOUNT.to_u128()), Some(VolumeWord::GetOutOfTheWay));
+ for g in [GUID_IO_VOLUME_LOCK_FAILED, GUID_IO_VOLUME_DISMOUNT_FAILED, GUID_IO_VOLUME_UNLOCK, GUID_IO_VOLUME_MOUNT] {
+ assert_eq!(volume_word(g.to_u128()), Some(VolumeWord::ItStays));
+ }
+ assert_eq!(volume_word(GUID_IO_VOLUME_CHANGE.to_u128()), None);
+ assert_eq!(volume_word(GUID_IO_VOLUME_NAME_CHANGE.to_u128()), None);
+ assert_eq!(volume_word(0), None);
+ }
+
/// Simulates the system's removal query for one registered volume: the app callback runs,
/// our handle is closed, and a failed removal registers the volume again.
#[test]