in reply to Race condition in Linux::Inotify2 between dir and new file?

Best practice for putting stuff into such monitored directories is to put the file into the directory with a known-temporary name (using a convention such as appending ".tmp" to the file name) and then to rename the file (to remove the ".tmp" suffix) next. Renaming a file via rename is an atomic file-system operation so if you see a file w/o a *.tmp name, there is no race condition involved if you then try to open it and read it (the file has already been completely written).

But, yes, using IN_CLOSE_WRITE and IN_MOVED_TO make a lot more sense. I didn't find the description of IN_CLOSE_WRITE clear in the module documentation but "(a watched file or) a file within a watched directory was closed, after being opened in writeable mode (this does not necessarily imply the file was written to)" seems clear and what you want.

- tye        

  • Comment on Re: Race condition in Linux::Inotify2 between dir and new file? (cp then mv)

Replies are listed 'Best First'.
Re^2: Race condition in Linux::Inotify2 between dir and new file? (cp then mv)
by Your Mother (Archbishop) on Jun 25, 2009 at 16:11 UTC

    Thanks much (and to Crackers2). IN_CLOSE_WRITE moves the problem around a little. It triggers the -f $file or croak sometimes. IN_MOVED_TO doesn't fire for this particular operation. I like the .tmp idea. I searched for "atomic" and "inotify" but didn't consider looking for atomic Perl stuff. I'll give it a spin.