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