Ramaprasad Upadhyaya
Ramaprasad Upadhyaya

Reputation: 467

FSEvents- Detect type of event on the folder

Any Idea on how to detect type of folder event(FSEvent) raised in the folder in the callback method (gotEvent method in below code)? Ex: File Renamed, File Created? I want to do some operation only with File Renamed, File Created. Want to ignore other events.

I have below implementation -

- (FSEventStreamRef) eventStreamForFileAtPath: (NSString *) fileInputPath  {

    if (![[NSFileManager defaultManager] fileExistsAtPath:fileInputPath]) {
        @throw [NSException exceptionWithName:@"FileNotFoundException"
                                       reason:@"There is not file at path specified in fileInputPath"
                                     userInfo:nil];
    }

    NSString *fileInputDir = [fileInputPath stringByDeletingLastPathComponent];

    NSArray *pathsToWatch = [NSArray arrayWithObjects:fileInputDir, nil];

    void *appPointer = (__bridge void *)self;

    FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};

    NSTimeInterval latency = 3.0;

    FSEventStreamRef stream = FSEventStreamCreate(NULL,
                                                  &gotEvent,
                                                  &context,
                                                  (__bridge CFArrayRef) pathsToWatch,
                                                  kFSEventStreamEventIdSinceNow,
                                                  (CFAbsoluteTime) latency,
                                                  kFSEventStreamCreateFlagUseCFTypes
                                                  );

    FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    FSEventStreamStart(stream);

    return stream;

}

static void gotEvent(ConstFSEventStreamRef stream,
                     void *clientCallBackInfo,
                     size_t numEvents,
                     void *eventPathsVoidPointer,
                     const FSEventStreamEventFlags eventFlags[],
                     const FSEventStreamEventId eventIds[]
                     ) {

    NSLog(@"File Changed!");
}

Upvotes: 1

Views: 625

Answers (1)

Volker
Volker

Reputation: 4660

The FSEventStreamEventFlags should indicate what happened, according to Apple:

kFSEventStreamEventFlagItemCreated = 0x00000100,
kFSEventStreamEventFlagItemRemoved = 0x00000200,
kFSEventStreamEventFlagItemRenamed = 0x00000800,
kFSEventStreamEventFlagItemModified = 0x00001000,

Evaluating which flag is set should do exactly what you want.

Upvotes: 1

Related Questions