#![allow(unsafe_code)]
use std::path::{Path, PathBuf};
use once_cell::sync::OnceCell;
use windows_sys::Win32::Foundation::BOOL;
use windows_sys::Win32::Foundation::{FALSE, TRUE};
use windows_sys::Win32::System::Console::SetConsoleCtrlHandler;
use windows_sys::Win32::System::Console::{CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT};
type DWORD = u32;
static HANDLER_DATA: OnceCell<PathBuf> = OnceCell::new();
pub fn setup(path: &Path) {
match HANDLER_DATA.set(path.to_path_buf()) {
Ok(()) =>
unsafe {
SetConsoleCtrlHandler(Some(win32_handler), TRUE );
},
Err(_) => eprintln!("warning: handler::setup() already called."),
};
}
unsafe extern "system" fn win32_handler(ctrl_type: DWORD) -> BOOL {
match ctrl_type {
CTRL_CLOSE_EVENT | CTRL_LOGOFF_EVENT | CTRL_SHUTDOWN_EVENT => {
if let Some(path) = HANDLER_DATA.get() {
super::wipe_files(path);
}
TRUE }
_ => FALSE, }
}