Assume a C program which mirrors the changes in a watched directory to a mirror directory. You recursively watch all subdirectories and use a structure indexed on watch descriptors containing the pathnames of the watched directories to be able to reconstruct the full pathname of the file in case of an event. This is all you need to process all events but IN_MOVED_FROM and IN_MOVED_TO. In the case of these two, you, in addition to the aforementioned, seem to also need the following:
- A structure indexed on
cookiesallowing you to store and retrieve thewatch descriptorand thefilenameof a particularIN_MOVED_FROMevent once a matchingIN_MOVED_TOevent appears. - A
timestamp-ordered queue (redundant) containing cookies which have not yet been paired, so that they can be reinterpreted asIN_DELETEevents and removed from both of the structures once an arbitrary period of time has passed. - A
pathname/filename->watch descriptorrelation (redundant), which allows you to effectively find out thewatch descriptorof a timeoutedcookiein case the (re)moved filesystem object was a directory, which you now need to unwatch.
Quite some overhead for supporting rename operations. Is there really no other way to detect a foreign rename other than waiting for an unpaired cookie to time out? Given the fact that there appears to be a way of telling between a local and foreign rename in fsnotify, this seems as a rather odd design choice.
With
inotify, you need to set an timeout to handle the mismatched/unpaired/foreign rename operation(eg.mva file into or out of a watched directory), treat them asIN_CREATEorIN_DELETEevents.Once I worked on a project relating to file synchronization mechanism, faced with the same issue, and solved it the same way.
IMHO, the design of
inotifyAPI is somewhat disgusting. Take the rename operation as an example,inotifyinterpretes it as TWO seperates events. :-(According to the link you give, Linux kernel does distinguish the local/foreign rename operation, and you can achieve this using the
dnotifyinterface. But sincednotifywas already obsoleted byinotifyinterface, I won't recommend this.reference:
http://en.wikipedia.org/wiki/Dnotify