From: Brian Candler Date: 2009-07-17T17:28:55+09:00 Subject: Re: making sure a file isn't being written/copied before moving Paul Archer wrote: >> Paul Archer wrote: >>> Can someone suggest an easy (or at least reliable) way to make sure >>> that >>> any file I'm about to modify isn't being touched by another program? >> >> As you said yourself: rely on the atomic semantics of the filesystem. >> Rename it to an extension which the other program will not recognise, or >> into another directory which the other program won't be looking in. >> > > Perhaps you missed it in my original post (or perhaps I simply wasn't > clear), but I may be operating on files that are added arbitrarily. My > concern is that the script starts acting on a file that is still being > copied. Renaming it won't help there. The program which drops files into the directory has to work the same way: - open temporary file - write to it - close it - fsync if you want to be sure it's on disk even if power is pulled - rename it to final location That's why I said to look at Maildir semantics - adding new E-mails to a maildir works like this. (They are written into the tmp/ directory, and then renamed into the new/ directory) -- Posted via http://www.ruby-forum.com/.