inotify在文件被删除并再次创建时停止监视文件 [英] inotify stops monitoring file when the file is deleted and created again

查看:878
本文介绍了inotify在文件被删除并再次创建时停止监视文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用inotify时遇到一些问题。
我使用inotify来监视文件的更改。这是我的代码:

I encounter some problem when using inotify. I use inotify to monitor changes on files. Here is my code:

int fd = inotify_init();
int wd = inotify_add_watch(fd, "/root/temp", IN_ALL_EVENTS);
int bufSize = 1000;
char *buf = new char[bufSize];
memset(buf, 0, sizeof(buf));
int nBytes = read(fd, buf, bufSize - 1);
cout << nBytes << " bytes read" << endl;
inotify_event *eventPtr = (inotify_event *)buf;
int offset = 0;
while (offset < nBytes)
{
    cout << eventPtr->mask << endl;
    offset += sizeof(inotify_event) + eventPtr->len;
    eventPtr = (inotify_event *)(buf + offset);
}
delete []buf;

如果我删除/ root / temp并重新创建这样的文件,文件不是由inotify监控,任何人都是这样的?感谢。

If I delete "/root/temp" and re-create such a file, any changes to this file is not monitored by inotify, anyone how is this? Thanks.

cheng

推荐答案

> inotify 监视底层的 inode ,而不是文件名。删除该文件时,您当前正在查看的inode无效,因此,您必须调用 inotify_rm_watch 。如果要监视具有相同名称但不同inode的新文件,则必须通过监视其父文件夹来检测其创建时间。

That's because inotify monitors the underlying inode, not the filename. When you delete that file, the inode you're currently watching becomes invalid, therefore, you must invoke inotify_rm_watch. If you want to monitor a new file with the same name, but a different inode, you must detect when it's created by monitoring its parent folder.

这篇关于inotify在文件被删除并再次创建时停止监视文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆