use std::path::Path;
use windows_sys::Win32::Foundation::{HANDLE, INVALID_HANDLE_VALUE};
use windows_sys::Win32::Storage::FileSystem::{
FindCloseChangeNotification, FindFirstChangeNotificationW,
};
use windows_threadpool_sys::wait::WaitableHandle;
use crate::directory::{OpenError, classify, wide_path};
pub(crate) struct CoarseHandle {
handle: HANDLE,
}
impl CoarseHandle {
pub(crate) fn open(path: &Path, subtree: bool, filter: u32) -> Result<Self, OpenError> {
let wide = wide_path(path)?;
let raw = unsafe {
FindFirstChangeNotificationW(wide.as_terminated_ptr(), i32::from(subtree), filter)
};
if raw == INVALID_HANDLE_VALUE {
let source = std::io::Error::last_os_error();
return Err(OpenError::new(classify(&source), source));
}
Ok(Self { handle: raw })
}
pub(crate) unsafe fn into_waitable(self) -> WaitableHandle {
let handle = self.handle;
std::mem::forget(self);
unsafe { WaitableHandle::assume_waitable_with(handle, FindCloseChangeNotification) }
}
}
impl std::fmt::Debug for CoarseHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CoarseHandle")
.field("handle", &self.handle)
.finish()
}
}
impl Drop for CoarseHandle {
fn drop(&mut self) {
unsafe {
FindCloseChangeNotification(self.handle);
}
}
}
#[cfg(test)]
mod tests;