Skip navigation.
 
mltesting for file open state...
FROM : Alex Reynolds
DATE : Sat Nov 30 13:12:53 2002

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_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mltesting for file open state... Alex Reynolds Nov 30, 13:12
mlRe: testing for file open state... Chris Ridd Nov 30, 18:43