Search code examples
objective-cmacoscore-foundationfsevents

Detect old file name and new file name via FSEventsFramework


I am using FSEventsFramework to monitor directories for changes. I was wondering if my logic here is sound, for detecting old file name and new name after a rename.

  • I think on rename, both events are in the same callback.
  • Old file name will have event id X-1
  • New file name will have even id X

Is this true?

Thanks


Solution

  • As far as I experienced it, your assumption is not always true. On FSEventStreamCreate you will pass a latency as well as a flag for the meaning of the latency kFSEventStreamCreateFlagNoDefer. Therefore the events may or may not come in the same callback. Furthermore there are different ways the file can be renamed. Some Filesystem-APIs actually rename the file while keeping the inode like mv, others like NSDocument create a new inode. Sometimes you will receive kFSEventStreamEventFlagItemRenamed in the callback, sometimes not.

    EDIT: Alternatives to FSEvents are Kernel Queues and NSFileCoordinator

    FSEvents are somehow not fully documented in the API-Docs. Have a look at the header file FSEvent.h, there is more to read there.

    Then to easily see what is going on for a rename, run your app and do some renaming with Finder, with terminal mv as well as in a document based app using the small triangle right to the filename.