pub mod inotify;
pub mod stat;
pub mod tree;
pub mod uv;
use crate::ported::lib::watcher::stat::StatFileWatcher;
pub trait FileWatcher {
fn watch(&self, path: &std::path::Path);
fn unwatch(&self, path: &std::path::Path);
fn check(&self, path: &std::path::Path) -> bool;
fn close(&self);
}
impl FileWatcher for StatFileWatcher {
fn watch(&self, path: &std::path::Path) {
StatFileWatcher::watch(self, path);
}
fn unwatch(&self, path: &std::path::Path) {
StatFileWatcher::unwatch(self, path);
}
fn check(&self, path: &std::path::Path) -> bool {
StatFileWatcher::check(self, path)
}
fn close(&self) {
StatFileWatcher::close(self);
}
}
pub fn create_file_watcher(
_pl: &(),
watcher_type: &str,
_expire_time: i32,
) -> Box<dyn FileWatcher + Send + Sync> {
match watcher_type {
"stat" => Box::new(StatFileWatcher::new()),
"inotify" => Box::new(StatFileWatcher::new()),
"uv" => Box::new(StatFileWatcher::new()),
_ => Box::new(StatFileWatcher::new()),
}
}
pub fn create_tree_watcher(
_pl: &(),
_watcher_type: &str,
_expire_time: i32,
) -> Box<dyn FileWatcher + Send + Sync> {
Box::new(StatFileWatcher::new())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_file_watcher_returns_a_watcher() {
let w = create_file_watcher(&(), "auto", 10);
let p = std::env::temp_dir().join("powerliners-create-test.tmp");
std::fs::write(&p, "x").unwrap();
assert!(w.check(&p));
w.unwatch(&p);
w.close();
std::fs::remove_file(&p).ok();
}
#[test]
fn create_file_watcher_stat_explicit() {
let _w = create_file_watcher(&(), "stat", 10);
}
#[test]
fn create_file_watcher_inotify_falls_back_to_stat_on_non_linux() {
let _w = create_file_watcher(&(), "inotify", 10);
}
}