当文件被删除并再次创建时,inotify 停止监视文件

发布于 2024-10-11 06:19:01 字数 695 浏览 4 评论 0原文

我在使用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 不会监控对此文件的任何更改,有人知道这是怎么回事吗?谢谢。

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;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞳孔里扚悲伤 2024-10-18 06:19:01

这是因为 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.

半边脸i 2024-10-18 06:19:01

另外两个答案是正确的。另一个有用的点是 inotify 会告诉你手表何时失效。

mask & IN_IGNORED

将不为零。 IN_IGNORED 在以下情况下设置:

“显式删除监视 (inotify_rm_watch(2)) 或自动删除(删除文件或卸载文件系统)。”

因此,如上所述,设置后,您可以重新观看文件(和/或目录,如果文件尚未重新创建)。

The other two answers are correct. Another useful point is that inotify tells you when the watch is invalidated.

mask & IN_IGNORED

will be non-zero. IN_IGNORED is set when:

"Watch was removed explicitly (inotify_rm_watch(2)) or automatically (file was deleted, or file system was unmounted)."

So, as noted, when this is set, you can rewatch the file (and/or the directory if the file has not yet been re-created).

命比纸薄 2024-10-18 06:19:01

每当您使用 API 时,请阅读文档

inotify 使用唯一的文件标识符 inode,而不是文件名来工作。事实上,整个 Linux 内核都与 inode 一起工作。文件名只是查找 inode 的一种方式。

要获得您想要的内容,您需要监视 /root 目录。添加文件时它将报告创建事件。如果该文件名为“temp”,那么您可以在该文件上添加监视。

Whenever you use an API, READ THE DOCUMENTATION.

inotify works using the unique file identifer inode, not a filename. The entire Linux kernel works with inodes in fact. Filenames are only a means to look up inodes.

To get what you want you need to monitor the /root directory. It will report a creation event when a file is added. If that file is named "temp" then you can add a watch on that file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文