Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Idiomatic inotify wrapper for the Rust programming language

License

NotificationsYou must be signed in to change notification settings

hannobraun/inotify-rs

Repository files navigation

Introduce

Idiomaticinotify wrapper for theRust programming language.This package generally tries to adhere to the underlying inotify API closely, while making access to it safe and convenient.

Examples

Now inotify-rs supports synchronous or asynchronous event monitoring.An example of synchronous is as follows:

use inotify::{EventMask,Inotify,WatchMask};use std::env;fnmain(){letmut inotify =Inotify::init().expect("Failed to initialize inotify");let current_dir = env::current_dir().expect("Failed to determine current directory");    inotify.watches().add(            current_dir,WatchMask::MODIFY |WatchMask::CREATE |WatchMask::DELETE,).expect("Failed to add inotify watch");println!("Watching current directory for activity...");letmut buffer =[0u8;4096];loop{let events = inotify.read_events_blocking(&mut buffer).expect("Failed to read inotify events");for eventin events{if event.mask.contains(EventMask::CREATE){if event.mask.contains(EventMask::ISDIR){println!("Directory created: {:?}", event.name);}else{println!("File created: {:?}", event.name);}}elseif event.mask.contains(EventMask::DELETE){if event.mask.contains(EventMask::ISDIR){println!("Directory deleted: {:?}", event.name);}else{println!("File deleted: {:?}", event.name);}}elseif event.mask.contains(EventMask::MODIFY){if event.mask.contains(EventMask::ISDIR){println!("Directory modified: {:?}", event.name);}else{println!("File modified: {:?}", event.name);}}}}}

Perhaps you want asynchronous monitoring of events.An example of asynchronous is as follows:

use std::{fs::File, io, thread, time::Duration};use futures_util::StreamExt;use inotify::{Inotify,WatchMask};use tempfile::TempDir;#[tokio::main]asyncfnmain() ->Result<(), io::Error>{let inotify =Inotify::init().expect("Failed to initialize inotify");let dir =TempDir::new()?;// Watch for modify and create events.    inotify.watches().add(dir.path(),WatchMask::CREATE |WatchMask::MODIFY)?;// Create a thread to operate on the target directory    thread::spawn::<_,Result<(), io::Error>>(move ||loop{File::create(dir.path().join("file"))?;        thread::sleep(Duration::from_millis(500));});letmut buffer =[0;1024];letmut stream = inotify.into_event_stream(&mut buffer)?;// Read events from async streamwhileletSome(event_or_error) = stream.next().await{println!("event: {:?}", event_or_error?);}Ok(())}

Usage

Add the following to yourCargo.toml:

[dependencies]inotify ="0.11"

Please refer to thedocumentation and the example above, for information on how to use it in your code.

Notice

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 considernotify.

If you need to access inotify in a way that this wrapper doesn't support, consider usinginotify-sys instead.

Documentation

The most important piece of documentation for inotify-rs is theAPI reference, as it contains a thorough description of the complete API, as well as examples.

Additional examples can be found in theexamples directory.

Please also make sure to read theinotify man page. Inotify use can be hard to get right, and this low-level wrapper won't protect you from all mistakes.

License

Copyright (c) Hanno Braun and contributors

Permission to use, copy, modify, and/or distribute this software for any purposewith or without fee is hereby granted, provided that the above copyright noticeand this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITHREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSSOF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHERTORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OFTHIS SOFTWARE.

About

Idiomatic inotify wrapper for the Rust programming language

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp