• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
re: testing for file open state
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: testing for file open state


  • Subject: re: testing for file open state
  • From: Alex Reynolds <email@hidden>
  • Date: Sat, 30 Nov 2002 22:07:08 -0500

On Saturday, November 30, 2002, at 12:51 PM, Chris Ridd wrote:

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.

Here's my revised code:

// start recursive check of sourcePath for open file
NSString *testFile;
NSDirectoryEnumerator *testEnumerator = [fileManager enumeratorAtPath:sourcePath];

while (testFile = [testEnumerator nextObject])
{
if (![fileManager fileExistsAtPath:[sourcePath stringByAppendingPathComponent:testFile] isDirectory:&isDir])
{
NSLog (@"Examining %@", [sourcePath stringByAppendingPathComponent:testFile]);
// build NSFileHandle to get file descriptor of testedFile
NSFileHandle *testFileHandle = [NSFileHandle fileHandleForUpdatingAtPath:[sourcePath stringByAppendingPathComponent:testFile]];
NSLog (@"%d", [testFileHandle fileDescriptor]);

// reset errno
errno = 0;

// 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, "return YES;"
NSLog (@"%@ on file %@", [NSString stringWithCString:strerror(errno)], testFile);
}
else
{
NSLog (@"Could not find error with flock() on %@", testFile);
}
[testFileHandle closeFile];
}
else
{
NSLog (@"%@ is a directory", [sourcePath stringByAppendingPathComponent:testFile]);
return NO;
}
}

Now the problem has worsened, in that every file at [sourcePath stringByAppendingPathComponent:testFile] is now a directory!

For example, here are results that I get back via NSLog:

2002-11-30 22:04:06.414 Ramallama[18762] /Volumes/Alex/.Trashes is a directory
2002-11-30 22:04:06.414 Ramallama[18762] /Volumes/Alex/AEGGrant.html is a directory
2002-11-30 22:04:06.415 Ramallama[18762] /Volumes/Alex/Alex's Presentation.ppt is a directory
2002-11-30 22:04:06.415 Ramallama[18762] /Volumes/Alex/Desktop DB is a directory
2002-11-30 22:04:06.415 Ramallama[18762] /Volumes/Alex/Desktop DF is a directory

Of all of these, only /Volumes/Alex/.Trashes is really a directory. The rest are files.

Is there is a bug with stringByAppendingPathComponent or with -(BOOL) fileExistsAtPath: isDirectory:?

I've been looking over the logic again and again and I can't see what is wrong with this call. This is getting very frustrating...

-Alex
_______________________________________________
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.

  • Follow-Ups:
    • Re: testing for file open state
      • From: Bryan Blackburn <email@hidden>
  • Prev by Date: Re: What happens when a "synchronize" occurs to synchronize preferences?
  • Next by Date: Re: testing for file open state
  • Previous by thread: running your own event loop: example code available
  • Next by thread: Re: testing for file open state
  • Index(es):
    • Date
    • Thread