Rust中的文件系统监视

问题描述 投票:0回答:1

我正在尝试在Rust中实现文件系统监视程序。当文件系统对象发生更改时,我可以接收事件,但是确定所做的更改使我感到困惑。我在最新发布的Notify程序包here上找到了代码,几乎可以带我到那里。

如何提取路径并从event中键入?该事件是枚举类型,但是以某种方式在打印时,我会看到我想要的所有信息。

我显然缺少了一些非常基本的东西。

use notify::{watcher, RecursiveMode, Watcher};
use std::sync::mpsc::channel;
use std::time::Duration;

fn main() {
    let (tx, rx) = channel();
    let mut watcher = watcher(tx, Duration::from_secs(10)).unwrap();
    watcher
        .watch("/tmp/path", RecursiveMode::Recursive)
        .unwrap();

    loop {
        match rx.recv() {
            Ok(event) => {
                // **>> event.filename? event.type? how?
                println!("{:?}", event);
            }
            Err(e) => println!("watch error: {:?}", e),
        }
    }
}
rust filesystemwatcher
1个回答
1
投票

使用去抖动的观察器,您得到的事件属于DebouncedEvent类型。枚举变量指定类型,其内容为路径。要使其脱离事件,您应该在事件上匹配所需的事件类型:

match &event {
    Read(path) => {
        // do thing
    }
    Rename(src, dest) => {
        // do other thing
    }
    _ => () // don't care about other types
}
© www.soinside.com 2019 - 2024. All rights reserved.