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