如何监控一个包含所有子文件夹和文件的文件夹?

9
我有一个名为“Datas”的文件夹。该文件夹中有一个名为“Inbox”的子文件夹,其中包含多个“.txt”文件。这个“Datas”文件夹可以被修改,在最终会存在多个带有“Inbox”子文件夹和“.txt”文件的子文件夹。我需要监视“Datas”文件夹和“Inbox”文件夹中的“.txt”文件。我该如何做到这一点?
INotify只能监视一个文件夹并在创建子文件夹时弹出事件。如何在创建“.txt”文件(在哪个文件夹中)时弹出事件?
我需要C或C++代码,但我被卡住了。我不知道该如何解决这个问题。

2
@VJovic inotify 是一个特定于 Linux 的功能。 - shadyabhi
1
@AbhijeetRastogi,所以这个问题是关于Linux的吗? - BЈовић
@VJovic 是的,我非常确定。 - shadyabhi
你可以使用libfam - Cornel Ghiban
2个回答

11

来自 inotify 手册:

   IN_CREATE         File/directory created in watched directory (*).

通过捕捉这个事件可以实现。

同样来自手册页面:

  Limitations and caveats
       Inotify  monitoring  of  directories  is  not recursive: to monitor subdirectories under a directory, additional watches must be created.  This can take a significant
       amount time for large directory trees.

所以,你需要自己完成递归部分。你可以先看一下这里的示例。你还应该查看项目notify-tools

如评论中所请求的示例:它监视/tmp/inotify1/tmp/inotify2以获取新创建的文件并显示事件。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

int main( int argc, char **argv ) 
{
    int length, i = 0;
    int fd;
    int wd[2];
    char buffer[BUF_LEN];

    fd = inotify_init();

    if ( fd < 0 ) {
        perror( "inotify_init" );
    }

    wd[0] = inotify_add_watch( fd, "/tmp/inotify1", IN_CREATE);
    wd[1] = inotify_add_watch (fd, "/tmp/inotify2", IN_CREATE);

    while (1){
        struct inotify_event *event;

        length = read( fd, buffer, BUF_LEN );  

        if ( length < 0 ) {
            perror( "read" );
        } 

        event = ( struct inotify_event * ) &buffer[ i ];

        if ( event->len ) {
            if (event->wd == wd[0]) printf("%s\n", "In /tmp/inotify1: ");
            else printf("%s\n", "In /tmp/inotify2: ");
            if ( event->mask & IN_CREATE ) {
                if ( event->mask & IN_ISDIR ) {
                    printf( "The directory %s was created.\n", event->name );       
                }
                else {
                    printf( "The file %s was created.\n", event->name );
                }
            }
        }
    }
    ( void ) inotify_rm_watch( fd, wd[0] );
    ( void ) inotify_rm_watch( fd, wd[1]);
    ( void ) close( fd );

    exit( 0 );
}

测试运行:

shadyabhi@archlinux ~ $ ./a.out 
In /tmp/inotify1: 
The file abhijeet was created.
In /tmp/inotify2: 
The file rastogi was created.
^C
shadyabhi@archlinux ~ $

1
@justAngela 除了我给的 IBM 链接之外,这里还有一个在谷歌搜索中出现的链接。http://www.thegeekstuff.com/2010/04/inotify-c-program-example/ 此外,为什么不看一下 inotiwatch 的源代码 https://github.com/rvoicilas/inotify-tools/blob/master/src/inotify? - shadyabhi
我确实查看了源代码,但是对我都没有用:(。如果您有更好的示例,请打印出来。第二个链接对我无效。 - just ME
@justAngela,https://github.com/rvoicilas/inotify-tools/blob/master/src/inotifywatch.c 可以满足你的需求。请查看第213行。 - shadyabhi
你能写一个简短的例子吗?我好像无法解决它! - just ME
@justAngela 请看这个例子。它可以同时监控两个目录。 - shadyabhi
如果在程序运行时创建了新的 /tmp/inotify 子目录,则此操作不会监视它们。 - Rich

1

还有fanotify。使用它可以在整个挂载点上设置监视器。请查看此处的示例代码here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接