Re: Avoid changing/moving file that's currently being written.

Christiaan Hofman <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On Apr 25, 2010, at 23:54, John Joyce wrote:

> 
> On Apr 25, 2010, at 2:00 PM, [email protected] wrote:
> 
>> Avoid changing/moving file that's currently being written.
>>     (Fabian Schuiki)
>> 
>> From: Fabian Schuiki <[email protected]>
>> Date: April 25, 2010 1:58:58 PM CDT
>> To: Mac OS X Developer Mailing List <[email protected]>
>> Subject: Avoid changing/moving file that's currently being written.
>> 
>> 
>> Hi!
>> 
>> 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.
>> 
>> With the Finder itself it's not that difficult, as it creates a 0-sized placeholder file of a special type until the copy is done, at which instance the placeholder is swapped for the real thing. Just avoid moving the special files and you're good. But lower level commands like cp don't do any of this, yet I'd rather be able to not interfere with that too! So how do you do that?
>> 
>> Is there some kind of reference count that is being kept by the system for files?
>> 
>> Cheers and thanks in advance,
>> Fabian
> 
> Isn't this normally handled by the file system?
> When a process is using a file, other processes shouldn't be able to get at it.
> 
> There is an HFS dev list too I believe.
> (given that most mac users will be using HFS+ )
> 

It's indeed "should," but not "will," as it's the responsibility of the process that's modifying the file to properly lock or writing atomically. As the system allows processes to do that without any safeguard, you simply cannot avoid this problem in general. Anyway, your daemon sounds like a seriously awful contraption that I would never ever would want on my system, so please make sure you warn me before you'd ever want to install it so I can refuse it.

Christiaan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.