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

Fabian Schuiki <[email protected]> Fri, 30 Apr 2010 10:12:43 +0200
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
Thanks! I think I won't go with the Kernel Extension as it's way to much hassle for what my app should do. So I guess I'll give the various counts and locks Christiaan suggested a try, to make things at least a bit secure.

Cheers
Fabian

Fabian Schuiki
[email protected]

> Hi Fabian,
> 
> On Wed, Apr 28, 2010 at 9:42 PM, Fabian Schuiki
> <[email protected]> wrote:
>> There seems to be a file attribute, can't remember the exact letters but it's something like NSFileReferenceCount. Anyone familiar with that?
> 
> I've never heard of it but I suspect it's the hard link count, which
> isn't what you want. As I said, I don't think there's an easy way of
> finding out whether a file is in use. I searched the Kernel code
> pretty thoroughly and ended up writing a Kernel Extension.
> 
> Regards,
> 
> Chris


> 
> On Apr 28, 2010, at 13:42, Fabian Schuiki wrote:
> 
>> There seems to be a file attribute, can't remember the exact letters but it's something like NSFileReferenceCount. Anyone familiar with that?
>> 
>> Thanks,
>> Fabian
> 
> NSFileReferenceCount gives the number of hard links. As hard links are virtually never used on MacOSX this will always be one with a probability of 100% (give or take a billionth or something.) This does not change when some process is writing to the file.
> 
> There are various things like reference counts, locks, and such that can say whether something references a file in some particular way. They're all optional, so they all have the same property: when it's there, you know it's in use in some way or another, when it's not there you still know absolutely nothing (i.e. it could be in use or not.) So the best you can do is check some of the most relevant of those counts or locks to reduce your chances of moving a file that's in use. You will never be able to be sure. And NSFileReferenceCount is certainly not one of the most relevant ones, it's probably the most irrelevant one, so you can just ignore it.
> 
> Christiaan
>