c++ - inotify 多次错误地通知新文件

标签 c++ c linux boost-thread inotify

通过在目录上添加监视,使用 inotify 监视目录中创建的任何新文件

    fd = inotify_init();
    wd = inotify_add_watch(fd, "filename_with_path", IN_CLOSE_WRITE);
    inotify_add_watch(fd, directory_name, IN_CLOSE_WRITE);

    const int event_size = sizeof(struct inotify_event);
    const int buf_len = 1024 * (event_size + FILENAME_MAX);
    while(true) {
        char buf[buf_len];
        int no_of_events, count = 0;
        no_of_events = read(fd, buf, buf_len);
        while(count < no_of_events) {
            struct inotify_event *event = (struct inotify_event *) &buf[count];
            if (event->len) {
                if (event->mask & IN_CLOSE_WRITE) {
                    if (!(event->mask & IN_ISDIR)) {
                         //It's here multiple times 
                    }
                }
            }
            count += event_size + event->len;
        }

当我将文件 scp 到目录时,会无限循环。这段代码有什么问题?它也显示相同的事件名称和事件掩码。因此,它显示了相同的无限次事件。

没有 break 语句。如果我找到一个事件,我只是打印它并继续等待 read() 上的另一个事件,这应该是一个阻塞调用。相反,它开始无限循环。这意味着,读取不会阻止它,而是无限地为一个文件返回相同的值。

整个操作在单独的 boost::thread 上运行。

编辑: 对不起大家。我得到的错误不是因为 inotify,而是因为 sqlite 一开始很难检测到。我想我在这里跳了枪。通过进一步调查,我确实发现 inotify 工作得很好。但错误实际上来自 sqlite 命令:ATTACH

该命令不是预期的只读命令。它正在将一些元数据写入文件。所以 inotify 一次又一次地收到通知。由于它们发生得如此之快,它搞砸了应用程序。我最终不得不分解代码以了解原因。

谢谢大家

最佳答案

我没有发现您的代码有任何问题...我运行的基本上是相同的东西,而且工作正常。我在想是不是测试有问题,或者是代码的某部分被遗漏了。如果您不介意,让我们看看是否可以消除任何歧义。

你能试试这个吗(我知道这几乎是一样的东西,但只是开玩笑)并让我知道确切测试的结果吗?

1) 将以下代码放入test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/inotify.h>

int main (int argc, char *argv[])
{
   char target[FILENAME_MAX];
   int result;
   int fd;
   int wd;   /* watch descriptor */
   const int event_size = sizeof(struct inotify_event);
   const int buf_len = 1024 * (event_size + FILENAME_MAX);

   strcpy (target, ".");

   fd = inotify_init();
   if (fd < 0) {
      printf ("Error: %s\n", strerror(errno));
      return 1;
   }

   wd = inotify_add_watch (fd, target, IN_CLOSE_WRITE);
   if (wd < 0) {
      printf ("Error: %s\n", strerror(errno));
      return 1;
   }

   while (1) {
     char buff[buf_len];
     int no_of_events, count = 0;

     no_of_events = read (fd, buff, buf_len);

     while (count < no_of_events) {
       struct inotify_event *event = (struct inotify_event *)&buff[count];

       if (event->len){
         if (event->mask & IN_CLOSE_WRITE)
           if(!(event->mask & IN_ISDIR)){
              printf("%s opened for writing was closed\n", target);
              fflush(stdout);
           }
       }
       count += event_size + event->len;
     }
   }

   return 0;
}

2)用gcc编译:

gcc test.c

3) 在一个窗口中启动它:

./a.out

4) 在同一目录的第二个窗口中试试这个:

echo "hi" > blah.txt

让我知道每次写入文件时是否能正常显示输出,并且不会像您的代码那样循环。如果是这样,那么您的代码中遗漏了一些重要的东西。如果不是,则系统存在一些差异。

很抱歉把这个放在“回答”部分,但评论太多了。

关于c++ - inotify 多次错误地通知新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12240456/

相关文章:

c++ - 使用 3ds max sdk 禁用/启用四元菜单项

c++ - 通过宏定义编译标志(C++11 和优化)

c - 位域结构中的未命名整数

c - 提取号码的个数

c++ - opengl不正确的模型渲染

c++ - cygwin 在 g++4.9.2 中支持 C++11

c++ - 实例化函数模板的编译问题

C编程 "To repeat particular steps after getting instead of terminating the program"

linux - linux时间输出中的时间

使用 nohup 执行 PHP