inotify 无法对 IN_DELETE 做出反应
我有以下(粗略)功能,它不断监视目录中的新文件和正在删除的文件,记录此类更改。它正确记录所有新文件和目录,但似乎对删除的文件或目录没有任何反应。
似乎是 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 ;
}
I have the following (crude) function, which continually watches a directory for new files and files being deleted, recording such changes. It correctly records all new files and directories, but doesn't seem to react at all to files or directories being deleted.
It appears to be the read()
call which doesn't return as it should when files are being deleted, though it does for files being created.
The function is being called as one of two independent threads, though at present the other thread doesn't do anything (just an empty, infinite loop as a placeholder).
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 ;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于明白是怎么回事了。 Linux,或者也许是 Gnome,实际上并不删除文件,而只是移动它们。即使文件被简单地重命名,它显然会被移动到某个地方,然后具有新名称的新文件也会从其他地方(某个临时文件夹?)移动到该文件夹中。 rm 命令实际上删除了一个文件,我的代码按预期将其注册为 IN_DELETE 事件。然而,在 Gnome 中删除文件或目录会注册为
IN_MOVED_TO
,而重命名注册为IN_MOVED_TO
后跟IN_MOVED_FROM
。我以为我已经将其作为首要任务之一进行检查,但显然还不够好。
Finally figured out what is going on. Linux, or perhaps Gnome, doesn't actually delete files but simply moves them around. Even when a file is simply renamed it is apparently moved somewhere, then a new file with the new name is moved into the folder from somewhere else (a temp folder somewhere?). The
rm
command actually deletes a file and my code registers that as anIN_DELETE
event as expected. Deleting files or directories in Gnome however registers asIN_MOVED_TO
, while renaming registers asIN_MOVED_TO
followed byIN_MOVED_FROM
.I thought I had checked for this as one of the first things, but clearly not well enough.