Search code examples
clinuxlinux-kernelinotify

iNotify how to detect move-out


Hi there I am using iNotify to detect changes in a directory. The flags I am using are IN_CLOSE_WRITE | IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE

I am trying to differentiate between a IN_MOVED_FROM when renaming, and the IN_MOVED_FROM when a file is moved out of the folder.

  • I thought to check if the cookie field is not 0 when user moved the file out. As I thought cookie was only for rename procedure. However even when I move the file out of the directory it still has a cookie.
  • I also though to check for IN_MODIFY as I was hoping that would be there on rename but not on move, but its not there for either.

Does anyone have any ideas on how to detect if it was just "moved-out" or it is the "renamed-from-oldfilename"?

Thanks


Solution

  • You need to check for the IN_MOVED_FROM event and a following IN_MOVED_TO event. If the cookie is the same, the file has been renamed in the same folder. If you don't receive a IN_MOVED_TO event with the same cookie, the file has been moved outside of the watched folder.