tags:

views:

47

answers:

1

I am running a small inotify script that sets up a watch on a file. Each time that file is edited and saved, the script notices that a DELETE_SELF event is triggered. Is that normal and if it is why? Shouldn't the inotify subsystem notice that the file still exists?

+1  A: 

It depends on what the application that is editing the file is doing with it. In this case, it sounds like the behavior of your editor when it saves a file is to delete the old file and write the new contents as a new file with the same name. From the perspective of inotify, this is exactly what happens, so it fires a deletion event and then a creation event. Inotify cannot know that the file that was deleted and the file that was created in its place are logically related.

Tyler McHenry
I've tried with vim, gedit and kate - all of them have the same behaviour. You're explanation definitely makes sense, but I am wondering why is implemented this way?
hyperboreean
How it normally works is this: You have a file named `moo.txt` that you're editing. When you save, your editor writes the file to `moo.txt.blah`, then unlinks `moo.txt` then renames `moo.txt.blah` to `moo.txt`.
Kitsune
Actually, it normally doesn't do the `unlink()` part of that - the `rename('moo.txt.blah', 'moo.txt')` will atomically unlink-and-replace `moo.txt`. This is *why* editors do it this way - because the operation is atomic, if your editor or system crashes, then you're guaranteed to either see the old file or the new one - not a half-written hybrid.
caf