Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

testing for file open state...



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 so far I am only getting EBADF errors on all files being enumerated, whether they are open or closed.

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?

I am stumped. Any pointers are very greatly appreciated...

-Alex

Here is the method I wrote:


- (BOOL) isFileOpenInRecursivePathExamination
{
// assume 'sourcePath' is defined as root directory of source files
NSString *testFile;
NSDirectoryEnumerator *testEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:sourcePath];

while (testFile = [testEnumerator nextObject])
{
NSFileHandle *testFileHandle = [NSFileHandle fileHandleForUpdatingAtPath:[[sourcePath stringByAppendingString:@"/"] stringByAppendingString:testFile]];

// use file descriptor and flock() to test whether file is open
if (flock([testFileHandle fileDescriptor], (LOCK_EX || LOCK_NB)) < 0)
{
// if a file is open, hopefully that application will have locked the file and an error will come back
if (errno == EWOULDBLOCK)
{
NSLog (@"Ran into EWOULDBLOCK on flock() of %@", testFile); // return YES;
}
else if (errno == EBADF)
{
NSLog (@"Ran into EBADF on flock() of %@", testFile); // return YES;
}
else if (errno == EINTR)
{
NSLog (@"Ran into EINTR on flock() of %@", testFile); // return YES;
}
else if (errno == EINVAL)
{
NSLog (@"Ran into EINVAL on flock() of %@", testFile); // return YES;
}
else if (errno == ENOLCK)
{
NSLog (@"Ran into ENOLCK on flock() of %@", testFile); // return YES;
}
else if (errno == EDEADLK)
{
NSLog (@"Ran into EDEADLK on flock() of %@", testFile); // return YES;
}
}
else
{
NSLog (@"Could not find error with flock() on %@", testFile);
}
// should I have tried fctrl() instead? how is this implemented?
return NO;
}
}
_______________________________________________
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.


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.