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

Fabian Schuiki <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
Am 26.04.2010 um 00:08 schrieb Christiaan Hofman:

> 
> 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

Thanks for the advice guys! Well the daemon isn't as awful as it sounds, I just wanted to make clear what it *could* do. It actually keeps several files synced, so when you change one of the files it syncs the changes to the other one. If the other file is busy at the moment the daemon shouldn't do anything to it, but come back later to finish it up. I'd rather not move a file that is currently being modified into the trash and placing an outdate version of it in its original place :).

Fabian
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.