You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Rust+notify监控Windows/Samba目录无变更通知问题求助

解决Linux下用notify监控CIFS挂载目录无响应的问题

问题背景

用Rust的notify库开发的目录监控程序在Linux本地目录运行正常,但监控挂载的Windows/Samba(CIFS类型)目录时,无法捕获任何文件变更。相关代码及依赖配置如下:

监控代码

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"

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

解决思路

  • 强制使用PollWatcher轮询机制
    Linux上notify的RecommendedWatcher默认依赖inotify实现,但CIFS作为网络文件系统,不支持inotify所需的内核级文件变更通知。必须强制使用PollWatcher通过定期扫描目录来检测变更,替换原代码中的条件判断逻辑:

    let (tx, rx) = std::sync::mpsc::channel();
    let config = Config::default().with_poll_interval(Duration::from_secs(1));
    let mut watcher = PollWatcher::new(tx, config).unwrap();
    
  • 调整CIFS挂载参数
    CIFS客户端默认会缓存文件元数据,可能导致轮询无法及时感知远程变更。重新挂载目录时添加以下参数:

    sudo mount -t cifs //server/share /mount/point -o username=xxx,password=xxx,cache=none,actimeo=1
    
    • cache=none:禁用客户端文件内容缓存
    • actimeo=1:将元数据缓存时间缩短至1秒,确保轮询能获取最新状态
  • 扩大事件监听范围
    当前代码仅监听Access(Close(_))事件,远程操作触发的事件类型可能与本地不同(比如文件创建对应Create事件)。先改为监听所有事件,确认有效事件类型后再针对性处理:

    Ok(e) => {
        println!("捕获到事件: {:?}", e.kind);
        match e.kind {
            Access(Close(_)) => process_file(e.paths.clone().into_iter().next()),
            EventKind::Create(_) => println!("文件创建: {:?}", e.paths),
            // 可按需添加其他事件类型
            _ => println!(),
        }
    }
    
  • 验证目录权限
    确保运行程序的用户对CIFS挂载目录拥有足够的读写权限,避免因权限不足导致轮询时无法读取目录内容。


内容的提问来源于stack exchange,提问作者Twistleton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 07:37:22