| 1 |
use std::{fs::File, io, thread, time::Duration}; |
| 2 |
|
| 3 |
use inotify::{Inotify, StreamExt, WatchMask}; |
| 4 |
use tempfile::TempDir; |
| 5 |
|
| 6 |
#[tokio::main] |
| 7 |
async fn main() -> Result<(), io::Error> { |
| 8 |
let inotify = Inotify::init().expect("Failed to initialize inotify"); |
| 9 |
|
| 10 |
let dir = TempDir::new()?; |
| 11 |
|
| 12 |
inotify |
| 13 |
.watches() |
| 14 |
.add(dir.path(), WatchMask::CREATE | WatchMask::MODIFY)?; |
| 15 |
|
| 16 |
thread::spawn::<_, Result<(), io::Error>>(move || loop { |
| 17 |
File::create(dir.path().join("file"))?; |
| 18 |
thread::sleep(Duration::from_millis(500)); |
| 19 |
}); |
| 20 |
|
| 21 |
let mut buffer = [0; 1024]; |
| 22 |
let mut stream = inotify.into_event_stream(&mut buffer)?; |
| 23 |
|
| 24 |
while let Some(event_or_error) = stream.next().await { |
| 25 |
println!("event: {:?}", event_or_error?); |
| 26 |
} |
| 27 |
|
| 28 |
Ok(()) |
| 29 |
} |