【发布时间】:2011-03-06 17:09:54
【问题描述】:
我一直在研究inotify call,但是在读接口方面我还是有点不爽。这些是我能找到的关于如何使用 read(2) 与 inotify 正确交互的最相关资源:
- http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html
- http://www.linuxjournal.com/article/8478
他们都以相同的方式实现它,他们首先定义了以下大小:
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 )
然后他们以这种方式使用它们:
length = read( fd, buffer, BUF_LEN );
if ( length < 0 ) {
perror( "read" );
}
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
/* some processing */
i += EVENT_SIZE + event->len;
}
现在,我们知道 name 是 struct inotify_event 的一部分,并且它具有可变长度。那么,buffer中最后一个inotify_event不能被截断吗?
假设有 1023 个 inotify_events,路径为 16 字节,一个为 32 字节。那时会发生什么?后期会被截断吗?或者内核会发现它不适合缓冲区并完全保留它吗?
【问题讨论】:
标签: c linux-kernel inotify