我在使用inotify时遇到一些问题。
我使用 inotify 来监视文件的更改。这是我的代码:
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 不会监控对此文件的任何更改,有人知道这是怎么回事吗?谢谢。
cheng
那是因为inotify
监控底层inode http://en.wikipedia.org/wiki/Inode,而不是文件名。当您删除该文件时,您当前正在监视的索引节点将变得无效,因此,您必须调用inotify_rm_watch
。如果要监视具有相同名称但 inode 不同的新文件,则必须通过监视其父文件夹来检测该文件的创建时间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)