我有以下(粗略的)功能,它不断监视目录中的新文件和被删除的文件,记录此类更改。它正确记录了所有新文件和目录,但似乎对被删除的文件或目录根本没有反应。
它似乎read()
是在删除文件时没有返回的调用,尽管它对于正在创建的文件确实如此。
该函数被称为两个独立线程之一,尽管目前另一个线程不做任何事情(只是一个空的无限循环作为占位符)。
void* watchfs(void* arg) {
int infp, watch, length, i ;
char buffer[EVENT_BUF_LEN] ;
struct inotify_event* event ;
if ((infp = inotify_init()) < 0) {
fatal("inotify: Could not initialize") ;
}
watch = inotify_add_watch(infp, userdir, IN_CREATE | IN_DELETE) ;
for (;;) {
length = read(infp, buffer, EVENT_BUF_LEN) ;
if (length < 0) {
fatal("inotify: Could not read events") ;
}
i = 0 ;
while (i < length) {
event = (struct inotify_event*) &buffer[i] ;
if (event->len) {
if (event->mask & IN_CREATE) {
if (event->mask & IN_ISDIR) {
record(LOG_FILESYS, "New directory created") ;
} else {
record(LOG_FILESYS, "New file created") ;
}
} else if (event->mask & IN_DELETE) {
if (event->mask & IN_ISDIR) {
record(LOG_FILESYS, "Directory deleted") ;
} else {
record(LOG_FILESYS, "File deleted") ;
}
}
}
i += EVENT_SIZE + event->len ;
}
}
inotify_rm_watch(infp, watch) ;
close(infp) ;
return 0 ;
}