Mailing Lists: Apple Mailing Lists

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

[workaround] file monitored by kqueue isn't deleted



Here's the workaround I wrote. I'm submitting it to the list because I would like feedback as to what could go wrong. I'm way out of my API comfort zone at this point, and calling unlink() makes me a little hesitant :-). I have tested the code; it works correctly for me.

Thanks for any feedback/advice.

Regards,

Eric Ocean

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

unsigned
int
InodeForFileDescriptor( int fd )
{
struct stat sb;


if ( fstat( fd, &sb ) == 0 ) return sb.st_ino; // fstat() returns 0 on success
else return 0;
}

- (void)
fileDeletedNotification:(NSNotification *)note;
{
NSString *path = [note object];


unsigned int inode = [kqueue inodeForPathInQueue:path];
[kqueue removePathFromQueue:path];

if ( inode )
{
NSString *parentDirectory = [path stringByDeletingLastPathComponent];
NSArray *files = [[NSFileManager defaultManager] directoryContentsAtPath:parentDirectory];


NSEnumerator *e = [files objectEnumerator];
NSString *lastPathComponent;


while ( lastPathComponent = [e nextObject] )
{
BOOL isDirectory = NO;
NSString *fullPath = [parentDirectory stringByAppendingPathComponent:lastPathComponent];
[[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];


if ( isDirectory == NO ) // we're looking for a file
{
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:fullPath];
int fd = [fh fileDescriptor];
if ( InodeForFileDescriptor( fd ) == inode )
{
// hack to get file to actually delete
unlink( [fullPath cString] );
break;
}
}
}
}


[kqueue addPathToQueue:path]; // the file exists again by the time we get here


[self fileWrittenToNotification:note]; // my file modified code
}
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden

References: 
 >file monitored by kqueue isn't deleted (From: Eric Ocean <email@hidden>)



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.