File: stream.rs

package info (click to toggle)
rust-inotify 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 208 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 838 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::{
    fs::File,
    io,
    thread,
    time::Duration,
};

use futures_util::StreamExt;
use inotify::{
    Inotify,
    WatchMask,
};
use tempfile::TempDir;

#[tokio::main]
async fn main() -> Result<(), io::Error> {
    let mut inotify = Inotify::init()
        .expect("Failed to initialize inotify");

    let dir = TempDir::new()?;

    inotify.add_watch(dir.path(), WatchMask::CREATE | WatchMask::MODIFY)?;

    thread::spawn::<_, Result<(), io::Error>>(move || {
        loop {
            File::create(dir.path().join("file"))?;
            thread::sleep(Duration::from_millis(500));
        }
    });

    let mut buffer = [0; 1024];
    let mut stream = inotify.event_stream(&mut buffer)?;

    while let Some(event_or_error) = stream.next().await {
        println!("event: {:?}", event_or_error?);
    }

    Ok(())
}