如何监控与内部的所有子文件夹和文件的文件夹? [英] How to monitor a folder with all subfolders and files inside?

查看:322
本文介绍了如何监控与内部的所有子文件夹和文件的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为DATAS文件夹中。此文件夹有一个名为收件箱内,其中有多个名为.txt文件的子文件夹。这种DATAS文件夹中可以进行修改,并在年底将有多个子文件夹与收件箱子文件夹和名为.txt文件。我需要监控的DATAS文件夹和收件箱文件夹中的名为.txt文件。我该怎么做?

I have a folder called "Datas". This folder has a subfolder called "Inbox" inside of which there are multiple ".txt" files. This "Datas" folder can be modified and in the end there will be multiple subfolders with "Inbox" subfolders and ".txt" files. I need to monitor the "Datas" folder and the ".txt" file from the "Inbox" folder. How can I do that?

inotify的只是监视文件夹中创建子文件夹时弹出的事件。如何流行事件时创建名为.txt文件(在哪个文件夹)?

INotify is just monitoring a folder and pops events when subfolders are created. How to pop events when ".txt" files are created (in which folder)?

我需要C或C ++ code,但我坚持。我不知道如何解决这个问题。

I need C or C++ code but I am stuck. I do not know how to solve this.

推荐答案

从inotify的手册页:

From the inotify manpage:

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

它可以通过捕捉这个事件来完成。

It can be done by catching this event.

再从手册页:

  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.

所以,你需要自己做递归部分。您可以通过从寻找一个例子这里启动。你也应该看看该项目通知工具

So, you will need to do the recursive part yourself. You can start by looking an example from here. You should also have a look at the project notify-tools

如问评论示例:它监视的/ tmp / inotify1 &安培; 的/ tmp / inotify2 的创建和新文件放;显示事件

EXAMPLE as asked in comments: It monitors /tmp/inotify1 & /tmp/inotify2 for new files created & displays the events

#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 ~ $

这篇关于如何监控与内部的所有子文件夹和文件的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆