我一直在研究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;
}
现在,我们知道名称是其中的一部分,struct inotify_event
并且它具有可变长度。那么,缓冲区中的最后一个 inotify_event 不能被截断吗?
假设有 1023 个 inotify_events,路径为 16 字节,一个为 32 字节。那时会发生什么?后期会被截断吗?或者内核会发现它不适合缓冲区并完全保留它吗?