FSEventStreamRef sends inconsistent or duplicated events

32 Views Asked by At

I am trying to mirror the A folder to the B folder in the Finder in real time using

FSEventStreamRef streamRef = FSEventStreamCreate(NULL, &realTimeSyncFoundChanges,
                                         &context,
                                         (CFArrayRef)foldersWatched,
                                         kFSEventStreamEventIdSinceNow,
                                         (CFTimeInterval)2.0,
                                         kFSEventStreamCreateFlagMarkSelf |
                                         kFSEventStreamCreateFlagWatchRoot  |
                                         kFSEventStreamCreateFlagFileEvents |
                                         kFSEventStreamCreateFlagUseCFTypes |
                                         kFSEventStreamCreateFlagUseExtendedData);
                                     

it works pretty well except a few cases. When I manually copy the app MyApp.app (which is a filePackage) within the folder A, I cannot properly mirror it to the folder B. Sometimes I get 2 streams with these events (event - path - id)

1st stream

kFSEventStreamEventFlagItemCreated - /A/MyApp.app/Contents/CodeResources - ID -12347
... all the filePackage contents - ID -12346…

2nd stream

kFSEventStreamEventFlagItemCreated - /A/MyApp.app - ID -12345

So on the first stream I create 2 brand new empty folders

/B/MyApp.app
/B/MyApp.app/Contents/

because they don't exist yet, then I copy the file

/A/MyApp.app/Contents/CodeResources to /B/MyApp.app/Contents/CodeResources

and all the other files nested within the filePackage. Everything gets properly mirrored. But two seconds later, on the second stream, when I get the event regarding the filePackage itself

kFSEventStreamEventFlagItemCreated - /A/MyApp.app - ID -12345

I delete the previous whole filePackage /B/MyApp.app and create the folder

/B/MyApp.app

(just the folder, not its contents), therefore the copy is wrong: the filePackage is empty.

So I tried a different approach: when I get an event kFSEventStreamEventFlagItemCreated with a filePackage or folder, I copy the whole filePackage or folder together with its contents. So when I get the last event with the second stream

kFSEventStreamEventFlagItemCreated /A/MyApp.app - ID -12345

I copy the whole filePackage (with its contents) to /B/MyApp.app overwriting all the files I previously copied. This is not a clean solution. I do this way because I don't know how to ignore the previous events regarding the filePackage contents because they come with the first stream.


Worse and surprisingly (!) sometimes, when I manually copy the app MyApp.app within the folder A, I get 3 streams

1sr stream

kFSEventStreamEventFlagItemCreated - /A/MyApp.app - ID -12347

2nd stream

kFSEventStreamEventFlagItemCreated - /A/MyApp.app/Contents/CodeResources - ID -12346
... all the filePackage contents - ID 12345…

3rd stream

kFSEventStreamEventFlagItemCreated - /A/MyApp.app - ID -12347
(sometimes with the same ID as the first event, sometimes not)

On the first stream, if I try to copy the whole filePackage, I fail because the Finder didn't finish yet to fill the filePackage at /A/MyApp.app with its contents. Then I have to copy the app for a second time… This is not a good way to mirror a folder.

So I cannot properly assign a behaviour to the kFSEventStreamEventFlagItemCreated with a filePackage or folder.

How could I solve this trouble?

0

There are 0 best solutions below