File: test_inotify.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (65 lines) | stat: -rw-r--r-- 2,129 bytes parent folder | download | duplicates (39)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use nix::errno::Errno;
use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify};
use std::ffi::OsString;
use std::fs::{rename, File};

#[test]
pub fn test_inotify() {
    let instance = Inotify::init(InitFlags::IN_NONBLOCK).unwrap();
    let tempdir = tempfile::tempdir().unwrap();

    instance
        .add_watch(tempdir.path(), AddWatchFlags::IN_ALL_EVENTS)
        .unwrap();

    let events = instance.read_events();
    assert_eq!(events.unwrap_err(), Errno::EAGAIN);

    File::create(tempdir.path().join("test")).unwrap();

    let events = instance.read_events().unwrap();
    assert_eq!(events[0].name, Some(OsString::from("test")));
}

#[test]
pub fn test_inotify_multi_events() {
    let instance = Inotify::init(InitFlags::IN_NONBLOCK).unwrap();
    let tempdir = tempfile::tempdir().unwrap();

    instance
        .add_watch(tempdir.path(), AddWatchFlags::IN_ALL_EVENTS)
        .unwrap();

    let events = instance.read_events();
    assert_eq!(events.unwrap_err(), Errno::EAGAIN);

    File::create(tempdir.path().join("test")).unwrap();
    rename(tempdir.path().join("test"), tempdir.path().join("test2")).unwrap();

    // Now there should be 5 events in queue:
    //   - IN_CREATE on test
    //   - IN_OPEN on test
    //   - IN_CLOSE_WRITE on test
    //   - IN_MOVED_FROM on test with a cookie
    //   - IN_MOVED_TO on test2 with the same cookie

    let events = instance.read_events().unwrap();
    assert_eq!(events.len(), 5);

    assert_eq!(events[0].mask, AddWatchFlags::IN_CREATE);
    assert_eq!(events[0].name, Some(OsString::from("test")));

    assert_eq!(events[1].mask, AddWatchFlags::IN_OPEN);
    assert_eq!(events[1].name, Some(OsString::from("test")));

    assert_eq!(events[2].mask, AddWatchFlags::IN_CLOSE_WRITE);
    assert_eq!(events[2].name, Some(OsString::from("test")));

    assert_eq!(events[3].mask, AddWatchFlags::IN_MOVED_FROM);
    assert_eq!(events[3].name, Some(OsString::from("test")));

    assert_eq!(events[4].mask, AddWatchFlags::IN_MOVED_TO);
    assert_eq!(events[4].name, Some(OsString::from("test2")));

    assert_eq!(events[3].cookie, events[4].cookie);
}