Giter Site home page Giter Site logo

anotify's Introduction

anotify

一个异步基于 Linux inotify 的目录监视工具

Example

工具

可作为小程序使用

$ anotify -h
Usage: anotify [OPTIONS] [target]...

Arguments:
  [target]...  [default: ./]

Options:
  -a, --access         File was accessed
  -A, --attrib         Metadata (permissions, timestamps, …) changed
  -x, --close_write    File opened for writing was closed
  -q, --close_nowrite  File or directory not opened for writing was closed
  -c, --create         File/directory created in watched directory
  -d, --delete         File/directory deleted from watched directory
  -D, --delete_self    Watched file/directory was deleted
  -m, --modify         File was modified
  -S, --move_self      Watched file/directory was moved
  -F, --moved_from     File was renamed/moved; watched directory contained old name
  -T, --moved_to       File was renamed/moved; watched directory contains new name
  -o, --open           File or directory was opened
  -M, --move           Watch for all move events
  -Q, --close          Watch for all close events
      --all            Watch for all events
      --dont_follow    Don’t dereference the path if it is a symbolic link
      --excl_unlink    Filter events for directory entries that have been unlinked
      --mask_add       If a watch for the inode exists, amend it instead of replacing it
      --oneshot        Only receive one event, then remove the watch
      --dir            Only watch path, if it is a directory
  -R, --recursive      Recursive monitor a path
  -E, --regex <regex>  Use regex to match file name, only matched will report
  -h, --help           Print help

anotify 库

使用 tokio ,调用 anotfiy 的异步库函数 handler::run()

use std::ffi::OsString;
use async_inotify::{
    Anotify,
    Event,
    WatchMask,
}

#[tokio::main]
async fn main() {
    let anotify = Anotify {
        mask: WatchMask::CREATE,
        regex: None,
        recursive: true,
        targets: vec![OsString::from("/tmp/cc")],
    };

    let (tx, mut rx) = tokio::sync::broadcast::channel::<Event>(128);
    tokio::spawn(async move {
        loop {
            if let Ok(event) = rx.recv().await {
                println!("{:?}: {:?}", event.mask(), event.path());
            }
        }
    });

    match async_inotify::handler::run(anotify, Some(tx), tokio::signal::ctrl_c()).await {
        // press ctrl_c
        Ok(()) => {},
        // catch error
        Err(e) => panic!("{}", e),
    };
}

或直接使用 Watcher 并自定义 handler:

use async_inotify::{WatchMask, Watcher};

#[tokio::main]
async fn main() {
    let mut watcher = Watcher::init();
    let mask = WatchMask::CREATE;

    let wd = watcher.add("/tmp/cc", &mask).unwrap();

    // watch once
    if let Some(event) = watcher.next().await {
        println!("{:?}: {:?}", event.mask(), event.path());
    }

    watcher.remove(wd).unwrap();
}

anotify's People

Contributors

jiachao2130 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.