[workaround] file monitored by kqueue isn't deleted
[workaround] file monitored by kqueue isn't deleted
- Subject: [workaround] file monitored by kqueue isn't deleted
- From: Eric Ocean <email@hidden>
- Date: Fri, 15 Oct 2004 16:53:40 -0700
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:
This email sent to email@hidden