我一直在研究inotify调用,但在读界面方面,我还是有点古怪。关于如何使用read(2)正确地与inotify接口,我可以找到以下最相关的资源:
它们都以相同的方式实现它,它们首先定义以下大小:
#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的一部分,并且它有可变的长度。那么,不能截断缓冲区中的最后一个inotify_event吗?
假设有一个路径为16个字节的1023个inotify_events,一个路径为32个字节。然后会发生什么呢?后一个会被截断吗?或者,内核会看到它不适合缓冲区,而完全离开它吗?
发布于 2012-11-08 20:37:46
基本用法
根据inotify(7),您可以使用FIONREAD来查找需要读取的数据数量,并相应地调整缓冲区大小。以下是一些(非常粗糙的)代码可以实现这一点:
unsigned int avail;
ioctl(inotify_fd, FIONREAD, &avail);
char buffer[avail];
read(fd, buffer, avail);
int offset = 0;
while (offset < avail) {
struct inotify_event *event = (inotify_event*)(buffer + offset);
// Insert logic here
my_process_inotify_event(event);
offset = offset + sizeof(inotify_event) + event->len;
}更稳健的使用
inotify-tools为inotify提供了更高级别的接口.您可以使用它而不是访问inotify,或者可以看到它是如何实现inotifytools_next_events来安全可靠地读取所有可用事件的。
部分事件与截断
在回答您有关截断的问题时,我不认为如果给定的缓冲区对于所有事件来说都太小,内核就不会返回部分inotify_event或截断inotify_event。inotify(7)手册中的以下段落说明了这一点:
当提供给read(2)的缓冲区太小而无法返回有关下一个事件的信息时,
的行为取决于内核版本:在2.6.21之前的内核中,read(2)返回0;因为内核2.6.21,read(2)由于错误EINVAL而失败。
与inotifytools.c中的以下注释一样
// oh... no. this can't be happening. An incomplete event.
// Copy what we currently have into first element, call self to
// read remainder.
// oh, and they BETTER NOT overlap.
// Boy I hope this code works.
// But I think this can never happen due to how inotify is written.https://stackoverflow.com/questions/5211993
复制相似问题