Debian Codegraph @

debian/rust-vendor/inotify/README.md

raw
1 # inotify-rs [![crates.io](https://img.shields.io/crates/v/inotify.svg)](https://crates.io/crates/inotify) [![Documentation](https://docs.rs/inotify/badge.svg)](https://docs.rs/inotify) [![Rust](https://github.com/hannobraun/inotify-rs/actions/workflows/rust.yml/badge.svg)](https://github.com/hannobraun/inotify-rs/actions/workflows/rust.yml)
2
3 ## Introduction
4
5 Idiomatic [inotify] wrapper for the [Rust programming language]. This package generally tries to adhere to the underlying inotify API closely, while making access to it safe and convenient.
6
7 ## Examples
8
9 Now inotify-rs supports synchronous or asynchronous event monitoring.
10 An example of synchronous is as follows:
11
12 ```rust
13 use inotify::{EventMask, Inotify, WatchMask};
14 use std::env;
15
16 fn main() {
17 let mut inotify = Inotify::init().expect("Failed to initialize inotify");
18
19 let current_dir = env::current_dir().expect("Failed to determine current directory");
20
21 inotify
22 .watches()
23 .add(
24 current_dir,
25 WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE,
26 )
27 .expect("Failed to add inotify watch");
28
29 println!("Watching current directory for activity...");
30
31 let mut buffer = [0u8; 4096];
32 loop {
33 let events = inotify
34 .read_events_blocking(&mut buffer)
35 .expect("Failed to read inotify events");
36
37 for event in events {
38 if event.mask.contains(EventMask::CREATE) {
39 if event.mask.contains(EventMask::ISDIR) {
40 println!("Directory created: {:?}", event.name);
41 } else {
42 println!("File created: {:?}", event.name);
43 }
44 } else if event.mask.contains(EventMask::DELETE) {
45 if event.mask.contains(EventMask::ISDIR) {
46 println!("Directory deleted: {:?}", event.name);
47 } else {
48 println!("File deleted: {:?}", event.name);
49 }
50 } else if event.mask.contains(EventMask::MODIFY) {
51 if event.mask.contains(EventMask::ISDIR) {
52 println!("Directory modified: {:?}", event.name);
53 } else {
54 println!("File modified: {:?}", event.name);
55 }
56 }
57 }
58 }
59 }
60 ```
61
62 Perhaps you want asynchronous monitoring of events. An example of asynchronous is as follows:
63
64 ```rust
65 use std::{fs::File, io, thread, time::Duration};
66
67 use inotify::{Inotify, StreamExt, WatchMask};
68 use tempfile::TempDir;
69
70 #[tokio::main]
71 async fn main() -> Result<(), io::Error> {
72 let inotify = Inotify::init().expect("Failed to initialize inotify");
73
74 let dir = TempDir::new()?;
75 // Watch for modify and create events.
76 inotify
77 .watches()
78 .add(dir.path(), WatchMask::CREATE | WatchMask::MODIFY)?;
79 // Create a thread to operate on the target directory
80 thread::spawn::<_, Result<(), io::Error>>(move || loop {
81 File::create(dir.path().join("file"))?;
82 thread::sleep(Duration::from_millis(500));
83 });
84
85 let mut buffer = [0; 1024];
86 let mut stream = inotify.into_event_stream(&mut buffer)?;
87 // Read events from async stream
88 while let Some(event_or_error) = stream.next().await {
89 println!("event: {:?}", event_or_error?);
90 }
91
92 Ok(())
93 }
94 ```
95
96 ## Usage
97
98 Add the following to your `Cargo.toml`:
99
100 ```toml
101 [dependencies]
102 inotify = "0.11"
103 ```
104
105 Please refer to the [documentation] and the example above, for information on how to use it in your code.
106
107 ## Notice
108
109 Please 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]**.
110
111 If you need to access inotify in a way that this wrapper doesn't support, consider using **[inotify-sys]** instead.
112
113 ## Documentation
114
115 The 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.
116
117 Additional examples can be found in the **[examples directory]**.
118
119 Please 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.
120
121 ## License
122
123 Copyright (c) Hanno Braun and contributors
124
125 Permission to use, copy, modify, and/or distribute this software for any purpose
126 with or without fee is hereby granted, provided that the above copyright notice
127 and this permission notice appear in all copies.
128
129 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
130 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
131 FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
132 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
133 OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
134 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
135 THIS SOFTWARE.
136
137 [inotify]: https://en.wikipedia.org/wiki/Inotify
138 [Rust programming language]: https://rust-lang.org/
139 [documentation]: https://docs.rs/inotify
140 [notify]: https://crates.io/crates/notify
141 [inotify-sys]: https://crates.io/crates/inotify-sys
142 [API reference]: https://docs.rs/inotify
143 [examples directory]: https://github.com/hannobraun/inotify-rs/tree/main/examples
144 [inotify man page]: https://man7.org/linux/man-pages/man7/inotify.7.html