Cocoa, FSEvents, kFSEventStreamCreateFlagFileEvents flag and “renamed” events

前端 未结 3 1388
逝去的感伤
逝去的感伤 2021-02-09 06:11

I\'ve been playing with FSEvents in a little application of mine to synchronize the content of my application with what\'s on the hard drive (basically, it\'s a lit

相关标签:
3条回答
  • 2021-02-09 06:52

    You need to use the flag kFSEventStreamCreateFlagUseExtendedData (available since OS X 10.13). A stream created with that flag will include the inode of the event file. That way you can detect "rename chains" that happened in a reported event batch.

    P.S. macOS may reuse the inode of a deleted file for a newly created one, although if you process the events right away, the risk is negligible.

    0 讨论(0)
  • 2021-02-09 06:59

    You can get the file ID by creating an URL from the path, then calling:

    NSString *fileID = nil;
    
    [url getResourceValue:&fileID forKey:NSURLFileResourceIdentifierKey error:&error]; //NS_AVAILABLE(10_7, 5_0);
    

    (This identifier is not persistent across system restarts)

    0 讨论(0)
  • 2021-02-09 07:01

    Since these events only deal with paths, you'll have to do some extra work to handle renames. One option is to track the inode numbers of the files you're interested in. So when you do that stat call, also note the inode number and see if it matches any files you're tracking.

    Be aware, though, that the OS may reuse the inode number of a deleted file, so depending on them as unique identifiers isn't infallible.

    0 讨论(0)
提交回复
热议问题