Search code examples
rustnotify

Monitoring an Windows/Samba directory with Rust and notify - application does not register any change


I using a Rust application to monitor changes in a file directory. I use notify - a cross-platform file system notification library. And it works very well with Linux directories.

Now the application should also monitor a mounted Windows/Samba directory (type cifs rw).

The Rust application continues to run on a Linux computer.

Unfortunately, it does not work. The application does not register any change in the directory.

Does anyone have a good idea? Or a hint?

use notify::*;
use notify::EventKind::Access;
use notify::event::AccessKind::Close;
use std::{path::Path, time::Duration};
use std::process::Command;
use std::path::PathBuf;
use std::fs;
use chrono::prelude::*;

fn main() {

    let (tx, rx) = std::sync::mpsc::channel();
    let mut watcher: Box<dyn Watcher> = if RecommendedWatcher::kind() == 
        WatcherKind::PollWatcher {
            let config = Config::default().with_poll_interval(Duration::from_secs(1));
            Box::new(PollWatcher::new(tx, config).unwrap())
    } else {
        Box::new(RecommendedWatcher::new(tx, Config::default()).unwrap())
    };

    // watch directory
    watcher
        .watch(Path::new("/home/host2pc/gemini/ED/Output/"), RecursiveMode::NonRecursive)
        .unwrap();

    // loop forever
    for e in rx {
        println!("{:?}", e);
        match e {
          Ok(e) => match e.kind {
                     Access(Close(_)) => process_file(e.paths.clone().into_iter().next()),
                 _        => println!(),
          }
          Err(err)  => println!("{}", err),
        }
    }
}

Cargo.toml

[package]
name = "watch-dog"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust- 
lang.org/cargo/reference/manifest.html

[dependencies]
notify = "6.0.1"
xml = "0.8"
regex = "1"
chrono = "0.4.26"

Solution

  • Looking the documentation, I see this which probably applies to SMB:

    Pseudo Filesystems like /proc,/sys

    Some filesystems like /proc and /sys on *nix do not emit change events or use correct file change dates. To circumvent that problem you can use the PollWatcher with the compare_contents option.

    Looks like you'll need to use the PollWatcher unconditionally to support this.

        let mut watcher: Box<dyn Watcher> = {
            let config = Config::default()
                .with_compare_contents(true)
                .with_poll_interval(Duration::from_secs(1));
            Box::new(PollWatcher::new(tx, config).unwrap())
        };