-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTIKQDirectoryWatcher.m
180 lines (138 loc) · 5.66 KB
/
TIKQDirectoryWatcher.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2010 Tim Isted
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "TIKQDirectoryWatcher.h"
#import <fcntl.h>
#import <errno.h>
#import <strings.h>
#import <sys/event.h>
@interface TIKQDirectoryWatcher ()
- (void)cancelRunLoopSourceRef;
@end
#pragma mark -
#pragma mark Notification Constants
NSString * const kTIKQDirectoryWatcherObservedDirectoryActivityNotification = @"kTIKQDirectoryWatcherObservedDirectoryActivityNotification";
NSString * const kTIKQDirectory = @"kTIKQDirectory";
NSString * const kTIKQExpandedDirectory = @"kTIKQExpandedDirectory";
#pragma mark -
#pragma mark Function Declarations
void TIKQSocketCallback( CFSocketRef socketRef, CFSocketCallBackType type, CFDataRef address, const void *data, void *info );
#pragma mark -
#pragma mark Primary Implementation
@implementation TIKQDirectoryWatcher
#pragma mark -
#pragma mark Primary Methods
- (BOOL)watchDirectory:(NSString *)aDirectoryName error:(NSError **)outError
{
int directoryFileDescriptor = open( [aDirectoryName UTF8String], O_RDONLY );
if( directoryFileDescriptor == - 1) {
NSLog(@"Could not open file descriptor for %@. Error %d (%s)", aDirectoryName, errno, strerror(errno));
return NO;
}
[[self watchedDirectories] addObject:aDirectoryName];
struct kevent directoryEvent;
EV_SET( &directoryEvent, directoryFileDescriptor, EVFILT_VNODE, EV_ADD | EV_CLEAR | EV_ENABLE, NOTE_WRITE, 0, aDirectoryName);
if( kevent( [self kqFileDescriptor], &directoryEvent, 1, NULL, 0, NULL ) == -1 ) {
NSLog(@"Could not kevent watching %@. Error %d (%s)", aDirectoryName, errno, strerror(errno));
return NO;
}
return YES;
}
- (BOOL)scheduleWatcherOnMainRunLoop:(NSError **)outError
{
[self cancelRunLoopSourceRef];
CFSocketContext socketContext = { 0, self, NULL, NULL, NULL };
CFSocketRef runLoopSocket = CFSocketCreateWithNative(NULL, [self kqFileDescriptor], kCFSocketReadCallBack, TIKQSocketCallback, &socketContext);
if( runLoopSocket == NULL ) {
NSLog(@"Failed to create run loop socket using CFSocketCreateWithNative()");
return NO;
}
_runLoopSourceRef = CFSocketCreateRunLoopSource(NULL, runLoopSocket, 0);
if( _runLoopSourceRef == NULL ) {
NSLog(@"Could not create a run loop source reference");
CFRelease(runLoopSocket);
return NO;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), _runLoopSourceRef, kCFRunLoopDefaultMode);
CFRelease(runLoopSocket);
return YES;
}
- (void)notifyActivityOnPath:(NSString *)aPath
{
[[NSNotificationCenter defaultCenter]
postNotificationName:kTIKQDirectoryWatcherObservedDirectoryActivityNotification
object:self userInfo:
[NSDictionary dictionaryWithObjectsAndKeys:
[aPath stringByAbbreviatingWithTildeInPath], kTIKQDirectory,
[aPath stringByExpandingTildeInPath], kTIKQExpandedDirectory, nil]];
}
#pragma mark -
#pragma mark Removing the Run Loop
- (void)cancelRunLoopSourceRef
{
if( _runLoopSourceRef ) {
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), _runLoopSourceRef, kCFRunLoopDefaultMode);
CFRelease(_runLoopSourceRef), _runLoopSourceRef = NULL;
}
}
#pragma mark -
#pragma mark Socket Callback
void TIKQSocketCallback( CFSocketRef socketRef, CFSocketCallBackType type, CFDataRef address, const void *data, void *info )
{
TIKQDirectoryWatcher *watcher = (TIKQDirectoryWatcher *)info;
struct kevent event;
if( kevent(watcher->_kqFileDescriptor, NULL, 0, &event, 1, NULL) == -1 ) {
NSLog(@"TIKQDirectoryWatcher could not pick up an event. Error %d (%s)", errno, strerror(errno));
} else {
[watcher notifyActivityOnPath:(NSString *)event.udata];
}
}
#pragma mark -
#pragma mark Lazy Generators
- (int)kqFileDescriptor
{
if( _kqFileDescriptor ) return _kqFileDescriptor;
_kqFileDescriptor = kqueue();
if( _kqFileDescriptor == -1 ) {
NSLog(@"Could not create kqueue. Error %d (%s)", errno, strerror(errno));
}
return _kqFileDescriptor;
}
- (NSMutableArray *)watchedDirectories
{
if( _watchedDirectories ) {
return _watchedDirectories;
}
_watchedDirectories = [[NSMutableArray alloc] init];
return _watchedDirectories;
}
#pragma mark -
#pragma mark Initialization and Deallocation
- (void)dealloc
{
[self cancelRunLoopSourceRef];
[_watchedDirectories release], _watchedDirectories = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Properties
@synthesize kqFileDescriptor = _kqFileDescriptor;
@synthesize runLoopSourceRef = _runLoopSourceRef;
@synthesize watchedDirectories = _watchedDirectories;
@end