0

我正在使用以下代码(实际上是从这里复制的FSEvents C++ 示例),但它只能用于路径,不能用于文件。我只想监视一个特定的文件。我怎样才能做到这一点?谢谢

#include <fcntl.h>       // for O_RDONLY
#include <stdio.h>       // for fprintf()
#include <stdlib.h>      // for EXIT_SUCCESS
#include <string.h>      // for strerror()
#include <sys/event.h>   // for kqueue() etc.
#include <unistd.h>      // for close()

int main (int argc, const char *argv[])
{
   int kq = kqueue ();
   // dir name is in argv[1], NO checks for errors here
   int dirfd = open (argv[1], O_RDONLY);

   struct kevent direvent;
    EV_SET (&direvent, dirfd, EVFILT_VNODE, EV_ADD | EV_CLEAR | EV_ENABLE,
            NOTE_WRITE, 0, (void *)argv[1]);

   kevent(kq, &direvent, 1, NULL, 0, NULL);

   // Register interest in SIGINT with the queue.  The user data
   // is NULL, which is how we'll differentiate between
   // a directory-modification event and a SIGINT-received event.
   struct kevent sigevent;
   EV_SET (&sigevent, SIGINT, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, NULL);
   // kqueue event handling happens after the legacy API, so make
   // sure it doesn eat the signal before the kqueue can see it.
   signal (SIGINT, SIG_IGN);

   // Register the signal event.
   kevent(kq, &sigevent, 1, NULL, 0, NULL);

   while (1) {
       // camp on kevent() until something interesting happens
       struct kevent change;
       if (kevent(kq, NULL, 0, &change, 1, NULL) == -1) { exit(1); }
       // The signal event has NULL in the user data.  Check for that first.
       if (change.udata == NULL) {
           break;
       } else {
        // udata is non-null, so it's the name of the directory
        printf ("%s\n", (char*)change.udata);
       }
   }
   close (kq);
   return 0;
}    
4

1 回答 1

0

您缺少对文件执行的哪些操作?

请注意,许多写入文件的东西不会打开现有文件并对其进行写入;他们取消链接现有文件并将其替换为新文件。因此,kqueue()不会报告您打开的文件已被写入。你可以添加NOTE_DELETEfflags试图抓住它。

于 2013-11-11T06:20:37.233 回答