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 库中也没有得到解决。

 

 

  

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

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值