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
That’s because
inotifymonitors the underlying inode, not the filename. When you delete that file, the inode you’re currently watching becomes invalid, therefore, you must invokeinotify_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.