use std::collections::HashMap;
use std::ffi::CString;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::sync::Mutex;
const MASK: u32 = libc::IN_CREATE
| libc::IN_DELETE
| libc::IN_MODIFY
| libc::IN_CLOSE_WRITE
| libc::IN_MOVED_FROM
| libc::IN_MOVED_TO
| libc::IN_ATTRIB
| libc::IN_DELETE_SELF
| libc::IN_MOVE_SELF
| libc::IN_ONLYDIR
| libc::IN_DONT_FOLLOW;
pub enum Event {
Changed(u64),
Overflow,
}
pub struct Watcher {
fd: tokio::io::unix::AsyncFd<OwnedFd>,
owners: Mutex<HashMap<i32, u64>>,
}
impl Watcher {
pub fn new() -> std::io::Result<Watcher> {
let fd = unsafe { libc::inotify_init1(libc::IN_NONBLOCK | libc::IN_CLOEXEC) };
if fd < 0 {
return Err(std::io::Error::last_os_error());
}
let fd = unsafe { OwnedFd::from_raw_fd(fd) };
let fd = unsafe { tokio::io::unix::AsyncFd::register(fd) }.map_err(std::io::Error::from)?;
Ok(Watcher { fd, owners: Mutex::new(HashMap::new()) })
}
pub fn add(&self, dir: &Path, owner: u64) -> std::io::Result<i32> {
let c = CString::new(dir.as_os_str().as_bytes()).map_err(std::io::Error::other)?;
let wd = unsafe { libc::inotify_add_watch(self.fd.get_ref().as_raw_fd(), c.as_ptr(), MASK) };
if wd < 0 {
return Err(std::io::Error::last_os_error());
}
self.owners.lock().unwrap().insert(wd, owner);
Ok(wd)
}
pub fn forget(&self, owner: u64) {
let mut owners = self.owners.lock().unwrap();
let wds: Vec<i32> = owners.iter().filter(|(_, o)| **o == owner).map(|(wd, _)| *wd).collect();
for wd in wds {
owners.remove(&wd);
unsafe { libc::inotify_rm_watch(self.fd.get_ref().as_raw_fd(), wd) };
}
}
pub async fn next(&self) -> std::io::Result<Vec<Event>> {
let mut buf = vec![0u8; 64 << 10];
loop {
let mut guard = self.fd.readable().await?;
let n = unsafe { libc::read(self.fd.get_ref().as_raw_fd(), buf.as_mut_ptr() as *mut libc::c_void, buf.len()) };
if n < 0 {
let e = std::io::Error::last_os_error();
if e.kind() == std::io::ErrorKind::WouldBlock {
guard.clear_ready();
continue;
}
return Err(e);
}
let mut out: Vec<Event> = Vec::new();
let mut seen: Vec<u64> = Vec::new();
let mut at = 0usize;
let n = n as usize;
const HEAD: usize = std::mem::size_of::<libc::inotify_event>();
while at + HEAD <= n {
let ev: libc::inotify_event = unsafe { std::ptr::read_unaligned(buf.as_ptr().add(at) as *const _) };
at += HEAD + ev.len as usize;
if ev.mask & libc::IN_Q_OVERFLOW != 0 {
out.push(Event::Overflow);
continue;
}
let mut owners = self.owners.lock().unwrap();
let owner = owners.get(&ev.wd).copied();
if ev.mask & libc::IN_IGNORED != 0 {
owners.remove(&ev.wd);
}
if let Some(o) = owner {
if !seen.contains(&o) {
seen.push(o);
out.push(Event::Changed(o));
}
}
}
if !out.is_empty() {
return Ok(out);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reports_changes_per_worktree() {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(async {
let a = tempfile::tempdir().unwrap();
let b = tempfile::tempdir().unwrap();
std::fs::create_dir(a.path().join("src")).unwrap();
let w = Watcher::new().unwrap();
w.add(a.path(), 1).unwrap();
w.add(&a.path().join("src"), 1).unwrap();
w.add(b.path(), 2).unwrap();
std::fs::write(a.path().join("src/lib.rs"), "x").unwrap();
let ev = tokio::time::timeout(std::time::Duration::from_secs(5), w.next()).await.unwrap().unwrap();
assert!(matches!(ev[..], [Event::Changed(1)]));
std::fs::write(b.path().join("new"), "y").unwrap();
let ev = tokio::time::timeout(std::time::Duration::from_secs(5), w.next()).await.unwrap().unwrap();
assert!(matches!(ev[..], [Event::Changed(2)]));
w.forget(2);
std::fs::write(b.path().join("newer"), "z").unwrap();
std::fs::remove_file(a.path().join("src/lib.rs")).unwrap();
let ev = tokio::time::timeout(std::time::Duration::from_secs(5), w.next()).await.unwrap().unwrap();
assert!(matches!(ev[..], [Event::Changed(1)]));
});
}
}