dsoprea / PyInotify

An efficient and elegant inotify (Linux filesystem activity monitor) library for Python. Python 2 and 3 compatible.
GNU General Public License v2.0
242 stars 73 forks source link

Improved single file monitoring. #83

Closed ghost closed 11 months ago

ghost commented 4 years ago

Hi,

I found that the Inotify class is not doing a great job in single file monitoring.

According to man inotify :

           IN_IGNORED
                  Watch  was removed explicitly (inotify_rm_watch(2)) or auto‐
                  matically (file was deleted, or filesystem  was  unmounted).

The event IN_IGNORED is generated when a watch has been automatically removed, this happens when the monitored file is removed, and it also happens when user modify the monitored file with text editors that will replace the original file while saving (such as vim).

But the Inotify class itself doesn't handle this event, so the consequence is: when this happens, the watch has been automatically removed by the kernel but it still exists in the Inotify class (in Inotify.__watches and Inotify.__watches_r), and this causes a bug, when a monitored file is removed/replaced, users can no long re-add it back into the monitoring, if they try to remove it first by calling Inotify.remove_watch() then the system call inotify_rm_watch() will return an error because the watch does not exist in the kernel space.

So, the thing I've done is giving users an optional argument to make the Inotify class handle the IN_IGNORED event and it's disabled by default.

And I changed the usage of superficial parameter in Inotify.remove_watch as well which was not being used.

ghost commented 4 years ago

Hi, I reverted the original commit and did what you asked, but beware, this changes the default behavior of Inotify class and could bring some backward compatibility issue.