linux inotify 中的不尽人意的地方

 

 

   研究了好几天inotify,发现了一个他不尽人意的地方。

 

 

  move 事件:

         http://www.linuxjournal.com/article/8478网站介绍如下:

 

  

       Move events are complicated because inotify may be watching the directory that the file    is moved to or from, but

 

not the other. Because of this, it is not always possible to alert the  user of the source and destination of a file involved

 

in a move. inotify is able to alert the application to both only if the application is watching both directories.

 

      In that case, inotify emits an IN_MOVED_FROM from the watch descriptor of the source directory, and it emits an

 

IN_MOVED_TO from the watch descriptor of the destination directory. If watching only one or the other, only the one

 

event will be sent.   

 

      To tie together two disparate moved to/from events, inotify sets the cookie field in the inotify_event structure to a

 

unique nonzero value. Two events with matching cookies are thus related, one showing the source and one showing

 

the destination of the move.

      

     上面是说 如果目录发生rename,inotify将同时抛出IN_MOVED_FROM 和IN_MOVED_TO事件,前提是这两个目录必须同时在监控目录当中,也就是说用命令mv srcDirname descDirname, srcDirname 和descDirname 都已经在监控队列中注册了。这样的重命名事件是怎么对应的呢,那么就是通过cookie值,rename发生时,他们事件的cookie值是相同的,因此就同步了。

    event结构体如下:

       struct inotify_event {
        __s32 wd;             /* watch descriptor */
        __u32 mask;           /* watch mask */
        __u32 cookie;         /* cookie to synchronize two events */
        __u32 len;            /* length (including nulls) of name */
        char name[0];        /* stub for possible name */
};

 

  

 

     而一般的使用习惯是:rename的srcDirname是知道的,已经在监控队列中了,而descDirname是用户输入的名字,是一个新名称,不在原监控队列中,这样rename时间发生时,就只能监测到IN_MOVED_FROM了,也就是srcDirname被移走了事件,监测不到IN_MOVED_TO,此时cookie值只会出现一次,无法同步了。

 

     这个问题在libinotifytools 库中也没有得到解决。

 

 

  

    这个问题提出在这里,希望哪位同仁能帮帮小弟,不胜感激。

 

 

 

 

 

 

你可能感兴趣的:(linux,Cookies,application,events,Descriptor,structure)