-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWKQueue.m
146 lines (110 loc) · 3.94 KB
/
SWKQueue.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
//
// SWKQueue.m
// This file is part of the "SWApplicationSupport" project, and is distributed under the MIT License.
//
// Created by Samuel Williams on 18/02/06.
// Copyright 2006 Samuel Williams. All rights reserved.
//
#import "SWKQueue.h"
@implementation SWKQueue
- (id) init {
if (self = [super init]) {
NSLog (@"Setting up SWKQueue...");
if ((self->queueDesc = kqueue()) == -1) {
NSLog (@"SWKQueue: Failed to create kqueue!");
[self release];
return nil;
}
self->running = YES;
self->paths = [[NSMutableDictionary new] retain];
NSLog (@"Detatching thread...");
[NSThread detachNewThreadSelector:@selector(watcherThread:) toTarget:self withObject:nil];
}
return self;
}
-(oneway void) release {
@synchronized(self) {
if( [self retainCount] == 2 )
self->running = NO;
}
[super release];
}
-(void) dealloc {
self->running = NO;
NSEnumerator *enumerator = [self->paths objectEnumerator];
NSNumber *fd;
while (fd = [enumerator nextObject]) {
if( close( [fd intValue] ) == -1 )
NSLog(@"dealloc: Couldn't close file descriptor (%d)", errno);
}
[self->paths release];
[super dealloc];
}
- (void) addPaths: (NSArray*)paths notifyingAbout: (unsigned int)fflags {
NSEnumerator *enumerator = [paths objectEnumerator];
NSString *path;
while (path = [enumerator nextObject])
[self addPath:path notifyingAbout:fflags];
}
-(void) addPath: (NSString*)path notifyingAbout: (unsigned int)fflags {
struct timespec nullts = { 0, 0 };
struct kevent ev;
int fd = open( [path fileSystemRepresentation], O_EVTONLY, 0 );
NSAssert (fd >= 0, ([NSString stringWithFormat: @"Could not add path (%@)!", path]));
if( fd >= 0 ) {
SWPathReference *pathReference = [[SWPathReference alloc] initWithPath:path];
NSAssert (pathReference, @"Could not get path reference");
EV_SET( &ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, fflags, 0, (void*)pathReference);
@synchronized (self) {
[self->paths setValue:pathReference forKey:[NSNumber numberWithInt: fd]];
kevent( self->queueDesc, &ev, 1, NULL, 0, &nullts );
}
}
}
-(void) removePath: (NSString*)path {
int fd = -1;
@synchronized( self ) {
fd = [[self->paths objectForKey:path] intValue];
[self->paths removeObjectForKey:path];
}
if( close( fd ) == -1 )
NSLog(@"removePathFromQueue: Couldn't close file descriptor (%d)", errno);
}
- (void) watcherThread: (id)argument {
int n;
struct kevent ev;
struct timespec timeout = { 500, 0 }; // 5 seconds timeout.
int qfd = self->queueDesc;
NSLog (@"watcherThread: waiting for events...");
while( self->running ) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
@try {
n = kevent( qfd, NULL, 0, &ev, 1, &timeout );
if( n > 0 && ev.filter == EVFILT_VNODE && ev.fflags ) {
SWPathReference* pref = [[(NSString *)ev.udata retain] autorelease]; // In case one of the notified folks removes the path.
//[[NSWorkspace sharedWorkspace] noteFileSystemChanged: [pref path]];
id eventStruct = [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:ev.fflags], pref, nil];
[self performSelectorOnMainThread:@selector(_processEvent:) withObject:eventStruct waitUntilDone:NO];
} if (n == -1) {
NSLog (@"Error in SWKQueue watcherThread: #%d (%s)", errno, strerror(errno));
}
} @catch (id exception) {
NSLog(@"Error in SWKQueue watcherThread: %@", exception);
}
[pool release];
}
// Close our kqueue's file descriptor:
if( close( qfd ) == -1 )
NSLog(@"release: Couldn't close kqueue (%d)", errno);
NSLog(@"exiting kqueue watcher thread.");
}
- (void) _processEvent: (NSArray*)event {
[self->delegate kqueue: self eventDidOccur: [[event objectAtIndex:0] unsignedIntValue] forPath:[event objectAtIndex:1]];
}
- (void) setDelegate: (id)newDelegate {
self->delegate = newDelegate;
}
- (id) delegate {
return self->delegate;
}
@end