This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXFWatchNode.m
More file actions
326 lines (262 loc) · 8.04 KB
/
XFWatchNode.m
File metadata and controls
326 lines (262 loc) · 8.04 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
// XFWatchNode.m
// watch
//
#import "XFWatchNode.h"
static NSString * const XFChangedStatusString = @"CHANGED";
static NSString * const XFRemovedStatusString = @"REMOVED";
static NSString * const XFAddedStatusString = @"ADDED";
static NSString * const XFUnknownStatusString = @"UNKNOWN";
@implementation XFWatchNode
@synthesize path = _path;
@synthesize isDirectory = _isDirectory;
@synthesize isValid = _isValid;
@synthesize depth = _depth;
@synthesize maxDepth = _maxDepth;
@synthesize parent = _parent;
@synthesize executablePath = _executablePath;
static BOOL eventLoggingEnabled = YES;
+ (BOOL)eventLoggingEnabled
{
return eventLoggingEnabled;
}
+ (void)setEventLoggingEnabled:(BOOL)enabled
{
eventLoggingEnabled = enabled;
}
+ (dispatch_queue_t)watchQueue
{
static dispatch_queue_t watchQueue = nil;
if(!watchQueue)
{
watchQueue = dispatch_queue_create("com.crcunningham.watch.watchQueue", NULL);
}
return watchQueue;
}
+ (dispatch_queue_t)exectuteQueue
{
static dispatch_queue_t executeQueue = nil;
if(!executeQueue)
{
executeQueue = dispatch_queue_create("com.crcunningham.watch.executeQueue", NULL);
}
return executeQueue;
}
+ (id)nodeWithPath:(NSString *)path
{
return [[[self alloc] initWithPath:path] autorelease];
}
- (id)initWithPath:(NSString *)path
{
BOOL dir;
if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&dir])
{
self = [super init];
if(self)
{
_path = [path copy];
_isDirectory = dir;
_isValid = YES;
_childNodes = dir ? [[NSMutableArray alloc] init] : nil;
_parent = nil;
_executablePath = nil;
_descriptor = open([_path UTF8String], O_EVTONLY);
_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, _descriptor, DISPATCH_VNODE_LINK|DISPATCH_VNODE_WRITE|DISPATCH_VNODE_REVOKE|DISPATCH_VNODE_RENAME|DISPATCH_VNODE_DELETE|DISPATCH_VNODE_ATTRIB|DISPATCH_VNODE_EXTEND, [XFWatchNode watchQueue]);
_pathToNodeMap = [[NSMutableDictionary alloc] init];
if(_isDirectory)
{
NSError *error = nil;
NSArray *entries = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_path error:&error];
if(entries)
{
_directoryEntries = [entries retain];
}
else
{
NSLog(@"Error getting contents of path: %@\n%@", _path, error);
}
}
else
{
_directoryEntries = nil;
}
dispatch_source_set_event_handler(_source, ^{
NSMutableArray *events = [NSMutableArray array];
if(![[NSFileManager defaultManager] fileExistsAtPath:_path])
{
// Removed
if([XFWatchNode eventLoggingEnabled])
{
printf("[%s] Removed: %s\n", [[[NSDate date] description] UTF8String], [_path UTF8String]);
}
[events addObject:[NSArray arrayWithObjects:XFRemovedStatusString, _path, nil]];
}
else if(_isDirectory)
{
BOOL contentsChanged = NO;
NSError *error = nil;
NSArray *entries = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_path error:&error];
if(entries)
{
// Enumerate the new entries...
// ... see what is in the new that's not in the old
// ...
for(NSString *entry in entries)
{
if(![_directoryEntries containsObject:entry])
{
contentsChanged = YES;
// Added
if([XFWatchNode eventLoggingEnabled])
{
printf("[%s] Added: %s\n", [[[NSDate date] description] UTF8String], [[_path stringByAppendingPathComponent:entry] UTF8String]);
}
[events addObject:[NSArray arrayWithObjects:XFAddedStatusString, [_path stringByAppendingPathComponent:entry], nil]];
// If the node being created is within the allowed depth create a new watch node
if(_depth < _maxDepth)
{
XFWatchNode *child = [XFWatchNode nodeWithPath:[_path stringByAppendingPathComponent:entry]];
[child setDepth:_depth+1];
[child setMaxDepth:_maxDepth];
[child setParent:self];
[child setExecutablePath:[self executablePath]];
[self addChild:child];
// Create child nodes for subdirectory entries
[child createChildNodes];
}
}
}
for(NSString *entry in _directoryEntries)
{
if(![entries containsObject:entry])
{
contentsChanged = YES;
// Removed
if(_depth >= _maxDepth)
{
if([XFWatchNode eventLoggingEnabled])
{
// Log out the removed file since the child won't be logging itself
printf("[%s] Removed: %s\n", [[[NSDate date] description] UTF8String], [[_path stringByAppendingPathComponent:entry] UTF8String]);
}
[events addObject:[NSArray arrayWithObjects:XFRemovedStatusString, [_path stringByAppendingPathComponent:entry], nil]];
}
else
{
// Remove the child node if one exists (_depth < _maxDepth)
XFWatchNode *child = [_pathToNodeMap objectForKey:[_path stringByAppendingPathComponent:entry]];
[self removeChild:child];
}
}
}
[entries retain];
[_directoryEntries release];
_directoryEntries = entries;
}
else
{
NSLog(@"%@", error);
}
// The directory itself has changed
if([XFWatchNode eventLoggingEnabled])
{
printf("[%s] Changed: %s\n", [[[NSDate date] description] UTF8String], [_path UTF8String]);
}
[events addObject:[NSArray arrayWithObjects:XFChangedStatusString, _path, nil]];
}
else
{
if([XFWatchNode eventLoggingEnabled])
{
printf("[%s] Changed: %s\n", [[[NSDate date] description] UTF8String], [_path UTF8String]);
}
[events addObject:[NSArray arrayWithObjects:XFChangedStatusString, _path, nil]];
}
if(_executablePath)
{
dispatch_async([XFWatchNode exectuteQueue], ^(void) {
for(NSArray *event in events)
{
@try
{
[NSTask launchedTaskWithLaunchPath:_executablePath arguments:event];
}
@catch (NSException * e)
{
NSLog(@"Error when trying to launch '%@': %@", _executablePath, [e reason]);
}
@finally
{
}
}
});
}
});
dispatch_source_set_cancel_handler(_source, ^{ close(_descriptor); });
dispatch_resume(_source);
}
}
return self;
}
- (void)dealloc
{
dispatch_source_cancel(_source);
dispatch_release(_source);
[_path release];
[_childNodes release];
[_pathToNodeMap release];
[_directoryEntries release];
[super dealloc];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@:%p> %@", [self class], self, _path];
}
- (void)addChild:(XFWatchNode *)child
{
[child setParent:self];
[_childNodes addObject:child];
[_pathToNodeMap setObject:child forKey:[child path]];
}
- (void)removeChild:(XFWatchNode *)child
{
[_pathToNodeMap removeObjectForKey:[child path]];
[_childNodes removeObject:child];
}
- (void)createChildNodes
{
if(_depth > _maxDepth)
{
// Nodes for the max depth have been created, nothing left to do
return;
}
BOOL needToProcessChildDirectories = ((_depth + 1) < _maxDepth);
NSError *error = nil;
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self path] error:&error];
NSMutableArray *childrenToProcess = needToProcessChildDirectories ? [NSMutableArray array] : nil;
// Create watch nodes for the current depth
for(NSString *entry in contents)
{
NSString *path = [[self path] stringByAppendingPathComponent:entry];
XFWatchNode *child = [XFWatchNode nodeWithPath:path];
[child setDepth:_depth+1];
[child setMaxDepth:_maxDepth];
[child setParent:self];
[child setExecutablePath:[self executablePath]];
[self addChild:child];
if(needToProcessChildDirectories && [child isDirectory])
{
// Add the create child to the list of children to process
[childrenToProcess addObject:child];
}
}
if(needToProcessChildDirectories)
{
for(XFWatchNode *child in childrenToProcess)
{
// Create nodes for children
[child createChildNodes];
}
}
}
@end