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.

3.0 KB
Raw Blame
//! The shell host tells the guard that started it that the desktop is usable.
//!
//! The guard (`rustyfiles-shellguard`, rustyfiles as the Windows shell) makes a named event, starts the host with its name in
//! `RUSTYFILES_SHELL_READY_EVENT`, and waits for it: a host that has not signalled within its deadline counts as crashed, and a few
//! of those in a row give the session back to Explorer.
//!
//! WHY THIS IS HERE (issue 757, 1.0.7): the host half of that handshake was missing. Nothing in the app read the variable or set the
//! event, so every sign-in with the switch on went through six "crashes" in three minutes and ended in the guard's fall-back, with
//! the switch off again. Only a host the guard started has the variable; a dock started by the file manager has none.

use std::ffi::OsStr;

use windows::Win32::Foundation::CloseHandle;
use windows::Win32::System::Threading::{EVENT_MODIFY_STATE, OpenEventW, SetEvent};
use windows::core::PCWSTR;

/// The variable the guard puts the event's name in.
pub const VAR: &str = "RUSTYFILES_SHELL_READY_EVENT";

/// Signals the guard's event when this process was started by the guard. `Ok(false)`: not started so, nothing to do.
pub fn signal_if_asked() -> Result<bool, String> {
    match std::env::var_os(VAR) {
        Some(name) if !name.is_empty() => signal(&name).map(|_| true),
        _ => Ok(false),
    }
}

/// Sets the named event `name` (opened for that alone, closed again).
pub fn signal(name: &OsStr) -> Result<(), String> {
    use std::os::windows::ffi::OsStrExt;
    let w: Vec<u16> = name.encode_wide().chain(std::iter::once(0)).collect();
    unsafe {
        let h = OpenEventW(EVENT_MODIFY_STATE, false, PCWSTR(w.as_ptr())).map_err(|e| format!("open {}: {e}", name.to_string_lossy()))?;
        let r = SetEvent(h).map_err(|e| format!("set {}: {e}", name.to_string_lossy()));
        let _ = CloseHandle(h);
        r
    }
}

#[cfg(test)]
mod tests {
    use windows::Win32::Foundation::{CloseHandle, WAIT_OBJECT_0, WAIT_TIMEOUT};
    use windows::Win32::System::Threading::{CreateEventW, WaitForSingleObject};
    use windows::core::PCWSTR;

    /// As the guard makes it (manual reset, not signalled): after `signal` it is signalled.
    #[test]
    fn the_guards_event_is_signalled() {
        let name = format!("Local\\rustyfiles-shell-ready-test-{}", std::process::id());
        let w: Vec<u16> = name.encode_utf16().chain(std::iter::once(0)).collect();
        unsafe {
            let ev = CreateEventW(None, true, false, PCWSTR(w.as_ptr())).unwrap();
            assert_eq!(WaitForSingleObject(ev, 0), WAIT_TIMEOUT, "not signalled yet");
            super::signal(std::ffi::OsStr::new(&name)).unwrap();
            assert_eq!(WaitForSingleObject(ev, 0), WAIT_OBJECT_0, "signalled");
            let _ = CloseHandle(ev);
        }
    }

    /// An event that does not exist is an error, not a panic.
    #[test]
    fn a_missing_event_is_an_error() {
        assert!(super::signal(std::ffi::OsStr::new("Local\\rustyfiles-no-such-event-for-a-test")).is_err());
    }
}