Re: testing for file open state...
Re: testing for file open state...
- Subject: Re: testing for file open state...
- From: Chris Ridd <email@hidden>
- Date: Sat, 30 Nov 2002 17:43:24 +0000
On 30/11/02 12:12 pm, Alex Reynolds <email@hidden> wrote:
>
So I am testing for an exclusive, non-blocking lock using flock(). If I
>
can't get that file lock secured via inclusive-OR of LOCK_EX and
>
LOCK_NB, the man-page says that the flock() should fail and the error
>
EWOULDBLOCK should come back into the global variable errno.
But *only* if the other programs which are opening the files are locking it
using flock() as well.
>
But so far I am only getting EBADF errors on all files being
>
enumerated, whether they are open or closed.
Your code basically works for me... Except I'd suggest constructing the
filename to open using [sourcePath stringByAppendingPathComponent:
testFile], and I'd make sure I sent closeFile to the NSFileHandle before
looping/returning otherwise I'd run out of file descriptors.
You should ensure testFile is a file and not a directory as probably won't
be able to open directories that way (POSIX has different calls to open a
directory and to open a file), and you certainly won't be able to lock them
even if you could. (Use NSFileManager -fileExistsAtPath:isDirectory:)
Library calls typically only set errno when they fail, so you should set
errno to 0 yourself to avoid false errors.
(BTW, strerror(errno) will return a C string description of errno, which
removes your bank of if/else ifs.)
>
Has anyone implemented this kind of test via Carbon or with another
>
framework?
>
>
Or perhaps am I grabbing the wrong type of NSFileHandle to get the file
>
descriptor int value?
It seems to return a plausible fd here.
Were you running it on files that you could open for update? Check the fd is
>
= 0.
>
I am stumped. Any pointers are very greatly appreciated...
I'd consider using fcntl() instead of flock() because it is defined by
POSIX, but fundamentally it should give you the same sort of results.
Cheers,
Chris
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.