tracking file renaming/deleting with FSEvents on Lion

后端 未结 2 1253
感情败类
感情败类 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:44

    Well, I didn't find the perfect answer to my question, but I found a solution which I eventually was really satisfied with, so I thought I might share ^^

    As I said, when moving stuff to the trash, if you're only watching 1 folder, you won't catch the event generated when the image is put in the trash. So, I decided to do the following : I have a class which creates a stream on the root folder ("/") so that it will catch all the events -> this solves the problem of files being sent to the trash, and all such stuff. Then, this class allow to register delegates on certain pathes. So, instead of creating many streams, I create one big stream, then filter events as needed, and I create many delegates.

    So all I have to do now when I want to watch events on a special folder is the following :

    [[FSEventsListener instance] addListener:self forPath:somePath];
    

    I just have to create an instance of FSEventListener at application start, and release it when the app stops. And I just need to implement the following 3 methods which will be automatically called :

    -(void)fileWasAdded:(NSString *)file;
    -(void)fileWasRemoved:(NSString *)file;
    -(void)fileWasRenamed:(NSString *)oldFile to:(NSString *)newFile;
    

    If you're interested in the source code of this little utility, you can check here : http://blog.pcitron.fr/tools/macosx-imageviewer/ (the utility was added at the version 0.8)

    I developed it as part of a a little image viewer to keep the UI synchronized with the disk content (it displays the number of images contained in each directories, etc.) The source code is available, and the utility is in Utils/FSEventsListener.h/.m.

    And if by any chance someone actually downloads the application and take a look at the sources, if you find anything usefull (performance / feature improvement, whatever) feel free to drop a comment / mail ^^

提交回复
热议问题