use std::collections::BTreeSet;
use std::ffi::OsString;
use windows_file_watcher::{
Change, ChangeKind, DEFAULT_BOUND, DesyncCause, FailureCode, FaultDetail, FaultOperation,
Notification, OpenFailure, Outcome, RelativeName, VolumeIdentity, WatchId, channel_with_bound,
};
const ERROR_FILE_NOT_FOUND: u32 = 2;
struct Output<O> {
stdout: O,
}
impl<O: std::io::Write> Output<O> {
fn report(&mut self, message: &str) {
let _ = writeln!(self.stdout, "{message}");
}
}
fn stdio() -> Output<std::io::Stdout> {
Output {
stdout: std::io::stdout(),
}
}
#[derive(Default)]
struct Handler {
present: BTreeSet<OsString>,
rescans: u32,
ended: bool,
subscribed: bool,
last_retry_reason: Option<OpenFailure>,
volume_changed: bool,
}
impl Handler {
fn on(&mut self, notification: &Notification) {
match notification {
Notification::Batch { changes, .. } => {
for change in changes {
let name = change.name.to_os_string();
match &change.kind {
ChangeKind::Removed | ChangeKind::RenamedOldName => {
self.present.remove(&name);
}
_ => {
self.present.insert(name);
}
}
}
}
Notification::Desync { cause, .. } if cause.is_terminal() => self.ended = true,
Notification::Desync { .. } => self.rescans += 1,
Notification::Completion { outcome, .. } => {
if matches!(outcome, Outcome::Subscribed) {
self.subscribed = true;
}
}
Notification::RetryQuestion { detail, .. } => {
self.last_retry_reason = Some(detail.failure);
}
Notification::VolumeChanged { .. } => self.volume_changed = true,
_ => {}
}
}
}
fn main() {
let (sender, receiver) = channel_with_bound(DEFAULT_BOUND);
let watch = WatchId::from_raw(1);
let _ = sender.send(Notification::Completion {
watch,
outcome: Outcome::Subscribed,
});
let _ = sender.send(Notification::Batch {
watch,
changes: vec![
Change {
kind: ChangeKind::Added,
name: RelativeName::for_test("report.tmp"),
},
Change {
kind: ChangeKind::RenamedOldName,
name: RelativeName::for_test("report.tmp"),
},
Change {
kind: ChangeKind::RenamedNewName,
name: RelativeName::for_test("report.csv"),
},
],
});
let _ = sender.send(Notification::Desync {
watch,
cause: DesyncCause::Overflow,
});
let _ = sender.send(Notification::RetryQuestion {
watch,
operation: FaultOperation::Arm,
detail: FaultDetail {
failure: OpenFailure::NotFound,
code: FailureCode::Win32(ERROR_FILE_NOT_FOUND),
},
});
let _ = sender.send(Notification::VolumeChanged {
watch,
previous: VolumeIdentity::for_test(0x1111, "NTFS", "System"),
current: VolumeIdentity::for_test(0x2222, "FAT32", "Removable"),
});
let _ = sender.send(Notification::Desync {
watch,
cause: DesyncCause::Reestablished,
});
let _ = sender.send(Notification::RetryQuestion {
watch,
operation: FaultOperation::Arm,
detail: FaultDetail {
failure: OpenFailure::NotFound,
code: FailureCode::Win32(ERROR_FILE_NOT_FOUND),
},
});
let _ = sender.send(Notification::Desync {
watch,
cause: DesyncCause::Stopped,
});
let mut handler = Handler::default();
while let Some(notification) = receiver.try_recv() {
handler.on(¬ification);
}
assert!(
handler.subscribed,
"the Subscribed completion should mark the subscription registered"
);
assert!(
handler.present.contains(std::ffi::OsStr::new("report.csv")),
"the renamed-in file should be present"
);
assert!(
!handler.present.contains(std::ffi::OsStr::new("report.tmp")),
"the temp file was renamed away, so it should not be present"
);
assert_eq!(
handler.rescans, 2,
"Overflow and Reestablished are both recoverable, so both mean re-scan \
-- but Stopped is not counted among them"
);
assert!(
handler.ended,
"the terminal Stopped cause must be handled as termination, not as one \
more re-scan"
);
assert!(matches!(
handler.last_retry_reason,
Some(OpenFailure::NotFound)
));
assert!(handler.volume_changed);
let mut output = stdio();
output.report(&format!(
"handler reacted as expected: {} name(s) present, {} recoverable re-scan(s), \
watch ended: {}",
handler.present.len(),
handler.rescans,
handler.ended
));
output.report("all assertions passed with no filesystem and no thread pool");
}