1# inotify-rs [](https://crates.io/crates/inotify) [](https://docs.rs/inotify) [](https://github.com/hannobraun/inotify-rs/actions/workflows/rust.yml) 2 3Idiomatic [inotify] wrapper for the [Rust programming language]. 4 5```Rust 6extern crate inotify; 7 8 9use std::env; 10 11use inotify::{ 12 EventMask, 13 WatchMask, 14 Inotify, 15}; 16 17 18fn main() { 19 let mut inotify = Inotify::init() 20 .expect("Failed to initialize inotify"); 21 22 let current_dir = env::current_dir() 23 .expect("Failed to determine current directory"); 24 25 inotify 26 .watches() 27 .add( 28 current_dir, 29 WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE, 30 ) 31 .expect("Failed to add inotify watch"); 32 33 println!("Watching current directory for activity..."); 34 35 let mut buffer = [0u8; 4096]; 36 loop { 37 let events = inotify 38 .read_events_blocking(&mut buffer) 39 .expect("Failed to read inotify events"); 40 41 for event in events { 42 if event.mask.contains(EventMask::CREATE) { 43 if event.mask.contains(EventMask::ISDIR) { 44 println!("Directory created: {:?}", event.name); 45 } else { 46 println!("File created: {:?}", event.name); 47 } 48 } else if event.mask.contains(EventMask::DELETE) { 49 if event.mask.contains(EventMask::ISDIR) { 50 println!("Directory deleted: {:?}", event.name); 51 } else { 52 println!("File deleted: {:?}", event.name); 53 } 54 } else if event.mask.contains(EventMask::MODIFY) { 55 if event.mask.contains(EventMask::ISDIR) { 56 println!("Directory modified: {:?}", event.name); 57 } else { 58 println!("File modified: {:?}", event.name); 59 } 60 } 61 } 62 } 63} 64``` 65 66 67## Usage 68 69Include it in your `Cargo.toml`: 70 71```toml 72[dependencies] 73inotify = "0.11" 74``` 75 76Please refer to the [documentation] and the example above, for information on how to use it in your code. 77 78Please note that inotify-rs is a relatively low-level wrapper around the original inotify API. And, of course, it is Linux-specific, just like inotify itself. If you are looking for a higher-level and platform-independent file system notification library, please consider [notify]. 79 80If you need to access inotify in a way that this wrapper doesn't support, consider using [inotify-sys] instead. 81 82 83## Documentation 84 85The most important piece of documentation for inotify-rs is the **[API reference]**, as it contains a thorough description of the complete API, as well as examples. 86 87Additional examples can be found in the **[examples directory]**. 88 89Please also make sure to read the **[inotify man page]**. Inotify use can be hard to get right, and this low-level wrapper won't protect you from all mistakes. 90 91 92## License 93 94Copyright (c) Hanno Braun and contributors 95 96Permission to use, copy, modify, and/or distribute this software for any purpose 97with or without fee is hereby granted, provided that the above copyright notice 98and this permission notice appear in all copies. 99 100THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 101REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 102FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 103INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 104OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 105TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 106THIS SOFTWARE. 107 108 109[inotify]: http://en.wikipedia.org/wiki/Inotify 110[Rust programming language]: http://rust-lang.org/ 111[documentation]: https://docs.rs/inotify 112[notify]: https://crates.io/crates/notify 113[inotify-sys]: https://crates.io/crates/inotify-sys 114[API reference]: https://docs.rs/inotify 115[examples directory]: https://github.com/inotify-rs/inotify/tree/main/examples 116[inotify man page]: http://man7.org/linux/man-pages/man7/inotify.7.html 117