tracking file renaming/deleting with FSEvents on Lion

后端 未结 2 1267
感情败类
感情败类 2020-12-30 06:15

I\'m trying to use FSEvents to detect when files were added/removed from a specific folder. For the moment, I implemented a simple wrapper around FSEvents, and it works fine

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 06:22

    You are actually raising two issues related to FSEvents and renames. 1. A file is renamed and both the old and new file names are within the directory trees being monitored. 2. A file is renamed and one of the names is not in the directory trees being monitored.

    You have solved (almost) the first issue. It is also necessary to provide your application with a means of knowing which events are being reported in the same FSEvent group of events. Your method of knowing that two renames are reported consecutively only works if they are within the same group of events being reported within the same latency period. If two rename events of type 2 occur one after another but are not within the same group of events being reported in the same latency group, they actually have nothing to do with each other - and you will mistakenly think one file has be renamed to another.

    It is possible to handle the second type of rename by simply monitoring every directory in the system using the root, but this will flood you with many unnecessary events. You can determine if a "partial" rename is the result of a file being moved out of the directory tree being monitored or into the directory tree being monitored by doing a stat() on the file. If stat() fails with an errno of 2, then the file has been moved outside the directory being monitored, and it can be treated as if it has been deleted. If stat() succeeds, the event can be treated as if the file has been created.

提交回复
热议问题