MYDirectoryWatcher.m
author Jens Alfke <jens@mooseyard.com>
Sat Mar 28 09:36:46 2009 -0700 (2009-03-28)
changeset 20 5a71993a1a70
child 27 256370e8935a
permissions -rw-r--r--
Added some new utilities, taken from Murky.
jens@20
     1
//
jens@20
     2
//  MYDirectoryWatcher.m
jens@20
     3
//  Murky
jens@20
     4
//
jens@20
     5
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@20
     6
//
jens@20
     7
jens@20
     8
#import "MYDirectoryWatcher.h"
jens@20
     9
#import <CoreServices/CoreServices.h>
jens@20
    10
jens@20
    11
jens@20
    12
static void directoryWatcherCallback(ConstFSEventStreamRef streamRef,
jens@20
    13
                                     void *clientCallBackInfo,
jens@20
    14
                                     size_t numEvents,
jens@20
    15
                                     void *eventPaths,
jens@20
    16
                                     const FSEventStreamEventFlags eventFlags[],
jens@20
    17
                                     const FSEventStreamEventId eventIds[]);
jens@20
    18
jens@20
    19
@interface MYDirectoryEvent ()
jens@20
    20
- (id) _initWithWatcher: (MYDirectoryWatcher*)itsWatcher
jens@20
    21
                   path: (NSString*)itsPath 
jens@20
    22
                  flags: (FSEventStreamEventFlags)itsFlags
jens@20
    23
                eventID: (FSEventStreamEventId)itsEventID;
jens@20
    24
@end
jens@20
    25
jens@20
    26
jens@20
    27
@implementation MYDirectoryWatcher
jens@20
    28
jens@20
    29
jens@20
    30
- (id) initWithDirectory: (NSString*)path target: (id)target action: (SEL)action
jens@20
    31
{
jens@20
    32
    NSParameterAssert(path);
jens@20
    33
    self = [super init];
jens@20
    34
    if (self != nil) {
jens@20
    35
        _path = path.copy;
jens@20
    36
        _target = target;
jens@20
    37
        _action = action;
jens@20
    38
        _latency = 5.0;
jens@20
    39
        _lastEventID = kFSEventStreamEventIdSinceNow;
jens@20
    40
    }
jens@20
    41
    return self;
jens@20
    42
}
jens@20
    43
jens@20
    44
- (void) dealloc
jens@20
    45
{
jens@20
    46
    [self stop];
jens@20
    47
    [_path release];
jens@20
    48
    [super dealloc];
jens@20
    49
}
jens@20
    50
jens@20
    51
- (void) finalize
jens@20
    52
{
jens@20
    53
    [self stop];
jens@20
    54
    [super finalize];
jens@20
    55
}
jens@20
    56
jens@20
    57
jens@20
    58
@synthesize path=_path, latency=_latency, lastEventID=_lastEventID;
jens@20
    59
jens@20
    60
jens@20
    61
- (BOOL) start
jens@20
    62
{
jens@20
    63
    if( ! _stream ) {
jens@20
    64
        FSEventStreamContext context = {0,self,NULL,NULL,NULL};
jens@20
    65
        _stream = FSEventStreamCreate(NULL, 
jens@20
    66
                                      &directoryWatcherCallback, &context,
jens@20
    67
                                      (CFArrayRef)[NSArray arrayWithObject: _path], 
jens@20
    68
                                      _lastEventID, 
jens@20
    69
                                      _latency, 
jens@20
    70
                                      kFSEventStreamCreateFlagUseCFTypes);
jens@20
    71
        if( ! _stream )
jens@20
    72
            return NO;
jens@20
    73
        FSEventStreamScheduleWithRunLoop(_stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
jens@20
    74
        if( ! FSEventStreamStart(_stream) ) {
jens@20
    75
            [self stop];
jens@20
    76
            return NO;
jens@20
    77
        }
jens@20
    78
        _historyDone = (_lastEventID == kFSEventStreamEventIdSinceNow);
jens@20
    79
        Log(@"MYDirectoryWatcher: Started on %@ (latency=%g, lastEvent=%llu)",_path,_latency,_lastEventID);
jens@20
    80
    }
jens@20
    81
    return YES;
jens@20
    82
}
jens@20
    83
jens@20
    84
- (void) pause
jens@20
    85
{
jens@20
    86
    if( _stream ) {
jens@20
    87
        FSEventStreamStop(_stream);
jens@20
    88
        FSEventStreamInvalidate(_stream);
jens@20
    89
        FSEventStreamRelease(_stream);
jens@20
    90
        _stream = NULL;
jens@20
    91
        Log(@"MYDirectoryWatcher: Stopped on %@ (lastEvent=%llu)",_path,_lastEventID);
jens@20
    92
    }
jens@20
    93
}
jens@20
    94
jens@20
    95
- (void) stop
jens@20
    96
{
jens@20
    97
    [self pause];
jens@20
    98
    _lastEventID = kFSEventStreamEventIdSinceNow;   // so events from now till next start will be dropped
jens@20
    99
    [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(start) object: nil];
jens@20
   100
}
jens@20
   101
jens@20
   102
- (void) stopTemporarily
jens@20
   103
{
jens@20
   104
    if( _stream ) {
jens@20
   105
        [self stop];
jens@20
   106
        [self performSelector: @selector(start) withObject: nil afterDelay: 0.0];
jens@20
   107
    }
jens@20
   108
}
jens@20
   109
jens@20
   110
jens@20
   111
- (void) _notifyEvents: (size_t)numEvents
jens@20
   112
                 paths: (NSArray*)paths
jens@20
   113
                 flags: (const FSEventStreamEventFlags[])eventFlags
jens@20
   114
              eventIDs: (const FSEventStreamEventId[])eventIDs
jens@20
   115
{
jens@20
   116
    for (size_t i=0; i<numEvents; i++) {
jens@20
   117
        NSString *path = [paths objectAtIndex: i];
jens@20
   118
        FSEventStreamEventFlags flags = eventFlags[i];
jens@20
   119
        FSEventStreamEventId eventID = eventIDs[i];
jens@20
   120
        if( flags & (kFSEventStreamEventFlagMount | kFSEventStreamEventFlagUnmount) ) {
jens@20
   121
            if( flags & kFSEventStreamEventFlagMount )
jens@20
   122
                Log(@"MYDirectoryWatcher: Volume mounted: %@",path);
jens@20
   123
            else
jens@20
   124
                Log(@"MYDirectoryWatcher: Volume unmounted: %@",path);
jens@20
   125
        } else if( flags & kFSEventStreamEventFlagHistoryDone ) {
jens@20
   126
            Log(@"MYDirectoryWatcher: Event #%llu History done",eventID);
jens@20
   127
            _historyDone = YES;
jens@20
   128
        } else {
jens@20
   129
            Log(@"MYDirectoryWatcher: Event #%llu flags=%02x path=%@",eventID,flags,path);
jens@20
   130
            if( _historyDone )
jens@20
   131
                flags |= kFSEventStreamEventFlagHistoryDone;
jens@20
   132
            
jens@20
   133
            MYDirectoryEvent *event = [[MYDirectoryEvent alloc] _initWithWatcher: self
jens@20
   134
                                                                        path: path 
jens@20
   135
                                                                       flags: flags
jens@20
   136
                                                                     eventID: eventID];
jens@20
   137
            [_target performSelector: _action withObject: event];
jens@20
   138
            [event release];
jens@20
   139
        }
jens@20
   140
        _lastEventID = eventIDs[i];
jens@20
   141
    }
jens@20
   142
}
jens@20
   143
jens@20
   144
jens@20
   145
static void directoryWatcherCallback(ConstFSEventStreamRef streamRef,
jens@20
   146
                                     void *watcher,
jens@20
   147
                                     size_t numEvents,
jens@20
   148
                                     void *eventPaths,
jens@20
   149
                                     const FSEventStreamEventFlags eventFlags[],
jens@20
   150
                                     const FSEventStreamEventId eventIDs[])
jens@20
   151
{
jens@20
   152
    [(MYDirectoryWatcher*)watcher _notifyEvents: numEvents
jens@20
   153
                                          paths: (NSArray*)eventPaths
jens@20
   154
                                          flags: eventFlags
jens@20
   155
                                       eventIDs: eventIDs];
jens@20
   156
}
jens@20
   157
jens@20
   158
jens@20
   159
jens@20
   160
@end
jens@20
   161
jens@20
   162
jens@20
   163
jens@20
   164
jens@20
   165
@implementation MYDirectoryEvent
jens@20
   166
jens@20
   167
- (id) _initWithWatcher: (MYDirectoryWatcher*)itsWatcher
jens@20
   168
                   path: (NSString*)itsPath 
jens@20
   169
                  flags: (FSEventStreamEventFlags)itsFlags
jens@20
   170
                eventID: (FSEventStreamEventId)itsEventID
jens@20
   171
{
jens@20
   172
    self = [super init];
jens@20
   173
    if (self != nil) {
jens@20
   174
        watcher = itsWatcher;
jens@20
   175
        path = itsPath.copy;
jens@20
   176
        flags = itsFlags;
jens@20
   177
        eventID = itsEventID;
jens@20
   178
    }
jens@20
   179
    return self;
jens@20
   180
}
jens@20
   181
jens@20
   182
- (void) dealloc
jens@20
   183
{
jens@20
   184
    [path release];
jens@20
   185
    [super dealloc];
jens@20
   186
}
jens@20
   187
jens@20
   188
@synthesize watcher,path,flags,eventID;
jens@20
   189
jens@20
   190
- (NSString*) relativePath
jens@20
   191
{
jens@20
   192
    NSString *base = watcher.path;
jens@20
   193
    if( ! [path hasPrefix: base] )
jens@20
   194
        return nil;
jens@20
   195
    int length = base.length;
jens@20
   196
    while( length < path.length && [path characterAtIndex: length]=='/' )
jens@20
   197
        length++;
jens@20
   198
    return [path substringFromIndex: length];
jens@20
   199
}
jens@20
   200
jens@20
   201
- (BOOL) mustScanSubdirectories     {return (flags & kFSEventStreamEventFlagMustScanSubDirs) != 0;}
jens@20
   202
- (BOOL) eventsWereDropped          {return (flags & (kFSEventStreamEventFlagUserDropped|kFSEventStreamEventFlagKernelDropped)) != 0;}
jens@20
   203
- (BOOL) isHistorical               {return (flags & kFSEventStreamEventFlagHistoryDone)==0;}
jens@20
   204
- (BOOL) rootChanged                {return (flags & kFSEventStreamEventFlagRootChanged)!=0;}
jens@20
   205
jens@20
   206
@end