Re: Avoid changing/moving file that's currently being written.
Uli Kusterer <[email protected]> Wed, 5 May 2010 01:03:50 +0200
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
On 25.04.2010, at 20:58, Fabian Schuiki wrote: > I'm working on a daemon that may change and move files in the background at random moments. Now I'd like to prevent the daemon from tampering with files that are currently being written by another app, copy operation whatever have you. One heuristic nobody mentioned: You can use the "last access date" and the "last changed date" to determine whether a file has been read from or written to recently. Since files that are busy are usually written to periodically, you could just not update a file until it hasn't been changed for a while. That should at least eliminate a few more cases where you would otherwise be moving a half-written file. Also, it's not that bad to move a file that is in use: Most file reference numbers stay valid if you have the file open at the moment and it gets moved. It's only a problem if an application refers to other files next to the moved file (because they might not be in the location anymore from which the application opened it), or if it closes the file and tries to re-open it. You can also use FSEvents and/or kqueue to be notified when files change. Install a timer whose fire date you push back by another 10 seconds or whatever whenever the file is written to. The Timer will fire as soon as there hasn't been any change in the last 10 seconds, and you can do your moving. Cheers, -- Uli Kusterer "The Witnesses of TeachText are everywhere..." http://groups.yahoo.com/group/mac-gui-dev/