2

ディレクトリ (/test) を監視して通知するプログラムがあります。これを改善して、別のディレクトリ (/opt など) を監視できるようにしたいと考えています。また、そのサブディレクトリを監視する方法も、現在 /test の下のファイルに変更が加えられた場合に通知されます。しかし、変更が /test のサブディレクトリ、つまり /test/sub-dir/files.txt に変更された場合、私は何の通知も受けていません..

ここに私の現在のコード - これが役立つことを願っています

/*

Simple example for inotify in Linux.

inotify has 3 main functions.
inotify_init1 to initialize
inotify_add_watch to add monitor
then inotify_??_watch to rm monitor.you the what to replace with ??.
yes third one is  inotify_rm_watch()
*/


#include <sys/inotify.h>

int main(){
    int fd,wd,wd1,i=0,len=0;
    char pathname[100],buf[1024];
    struct inotify_event *event;

    fd=inotify_init1(IN_NONBLOCK);
    /* watch /test directory for any activity and report it back to me */
    wd=inotify_add_watch(fd,"/test",IN_ALL_EVENTS);

    while(1){
        //read 1024  bytes of events from fd into buf
        i=0;
        len=read(fd,buf,1024);
        while(i<len){
            event=(struct inotify_event *) &buf[i];


            /* check for changes */
            if(event->mask & IN_OPEN)
                printf("%s :was opened\n",event->name);

            if(event->mask & IN_MODIFY)
                printf("%s : modified\n",event->name);

            if(event->mask & IN_ATTRIB)
                printf("%s :meta data changed\n",event->name);

            if(event->mask & IN_ACCESS)
                printf("%s :was read\n",event->name);

            if(event->mask & IN_CLOSE_WRITE)
                printf("%s :file opened for writing was closed\n",event->name);

            if(event->mask & IN_CLOSE_NOWRITE)
                printf("%s :file opened not for writing was closed\n",event->name);

            if(event->mask & IN_DELETE_SELF)
                printf("%s :deleted\n",event->name);

            if(event->mask & IN_DELETE)
                printf("%s :deleted\n",event->name);

            /* update index to start of next event */
            i+=sizeof(struct inotify_event)+event->len;
        }

    }

}
4

3 に答える 3

4

inotify_add_watchサブディレクトリの変更をリッスンしません。これらのサブディレクトリがいつ作成されたかを検出する必要がありinotify_add_watchます。

注意すべき最も重要なことは、サブディレクトリが作成された後、それに応じて通知されることですが、その通知を受け取った時点で、ファイルとサブディレクトリがそのディレクトリ内に既に作成されている可能性があるため、「失うことになります」 " これらのイベントは、新しいサブディレクトリの監視をまだ追加する機会がなかったためです。

この問題を回避する 1 つの方法は、通知を受け取った後にディレクトリの内容をスキャンして、ディレクトリの内容を確認することです。これにより、さらに時計を追加する機会が生まれます。

于 2012-09-03T07:22:04.093 に答える
0

サブフォルダーを削除して、何かを追加する必要があるたびに再作成することができます。

于 2012-01-20T11:04:07.167 に答える
0

inotify では、ディレクトリごとに 1 つのウォッチが必要です。グローバル通知には、fanotify などがあります。

于 2012-01-18T11:05:42.953 に答える