| 1 |
use std::env; |
| 2 |
|
| 3 |
use inotify::{EventMask, Inotify, WatchMask}; |
| 4 |
|
| 5 |
fn main() { |
| 6 |
let mut inotify = Inotify::init().expect("Failed to initialize inotify"); |
| 7 |
|
| 8 |
let current_dir = env::current_dir().expect("Failed to determine current directory"); |
| 9 |
|
| 10 |
inotify |
| 11 |
.watches() |
| 12 |
.add( |
| 13 |
current_dir, |
| 14 |
WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE, |
| 15 |
) |
| 16 |
.expect("Failed to add inotify watch"); |
| 17 |
|
| 18 |
println!("Watching current directory for activity..."); |
| 19 |
|
| 20 |
let mut buffer = [0u8; 4096]; |
| 21 |
loop { |
| 22 |
let events = inotify |
| 23 |
.read_events_blocking(&mut buffer) |
| 24 |
.expect("Failed to read inotify events"); |
| 25 |
|
| 26 |
for event in events { |
| 27 |
if event.mask.contains(EventMask::CREATE) { |
| 28 |
if event.mask.contains(EventMask::ISDIR) { |
| 29 |
println!("Directory created: {:?}", event.name); |
| 30 |
} else { |
| 31 |
println!("File created: {:?}", event.name); |
| 32 |
} |
| 33 |
} else if event.mask.contains(EventMask::DELETE) { |
| 34 |
if event.mask.contains(EventMask::ISDIR) { |
| 35 |
println!("Directory deleted: {:?}", event.name); |
| 36 |
} else { |
| 37 |
println!("File deleted: {:?}", event.name); |
| 38 |
} |
| 39 |
} else if event.mask.contains(EventMask::MODIFY) { |
| 40 |
if event.mask.contains(EventMask::ISDIR) { |
| 41 |
println!("Directory modified: {:?}", event.name); |
| 42 |
} else { |
| 43 |
println!("File modified: {:?}", event.name); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |