-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYPathWaiter.m
178 lines (134 loc) · 4.7 KB
/
SSYPathWaiter.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
#import "SSYPathWaiter.h"
#import "SSYBlocker.h"
// For debugging
#define SSYPathWaiterNotifeeConditionWaiting 0
#define SSYPathWaiterNotifeeConditionDone 1
NSString* const constKeySSYPathWaiterObserver = @"obsr" ;
NSString* const constKeySSYPathWaiterNotifee = @"ntfe" ;
NSString* const constKeySSYPathWaiterBlocker = @"blkr" ;
NSString* const constKeySSYPathWaiterPaths = @"paths" ;
NSString* const constKeySSYPathWaiterWatchFlags = @"flgs" ;
NSString* const constKeySSYPathWaiterTimeout = @"tmot" ;
@interface SSYPathWaiterNotifee : NSObject {
BOOL m_isDone ;
}
@property (assign) BOOL isDone ;
@end
@interface SSYPathWaiter ()
@property (assign) BOOL succeeded ;
@end
@implementation SSYPathWaiterNotifee
@synthesize isDone = m_isDone ;
- (void)processNote:(NSNotification*)note {
NSDictionary* info = [note userInfo] ;
SSYPathWaiter* waiter = [info objectForKey:SSYPathObserverUserInfoKey] ;
[waiter setSucceeded:YES] ;
[self setIsDone:YES] ;
}
- (void)timeOutTimer:(NSTimer*)timer {
[self setIsDone:YES] ;
}
@end
@implementation SSYPathWaiter
@synthesize succeeded = m_succeeded ;
- (void)waitWithInfo:(NSDictionary*)info {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;
NSTimeInterval timeout = [[info objectForKey:constKeySSYPathWaiterTimeout] doubleValue] ;
NSSet* paths = [info objectForKey:constKeySSYPathWaiterPaths] ;
uint32_t watchFlags = (uint32_t)[[info objectForKey:constKeySSYPathWaiterWatchFlags] unsignedIntegerValue] ;
SSYBlocker* blocker = [info objectForKey:constKeySSYPathWaiterBlocker] ;
SSYPathObserver* observer = [[SSYPathObserver alloc] init] ;
SSYPathWaiterNotifee* notifee = [[SSYPathWaiterNotifee alloc] init] ;
[blocker lockLock] ;
NSError* error = nil ;
BOOL ok = YES ;
for (NSString* path in paths) {
ok = [observer addPath:path
watchFlags:watchFlags
notifyThread:[NSThread currentThread]
userInfo:self
error_p:&error] ;
if (!ok) {
break ;
}
}
if (ok) {
[[NSNotificationCenter defaultCenter] addObserver:notifee
selector:@selector(processNote:)
name:SSYPathObserverChangeNotification
object:observer] ;
[NSTimer scheduledTimerWithTimeInterval:timeout
target:notifee
selector:@selector(timeOutTimer:)
userInfo:info
repeats:NO] ;
#if 0
BOOL keepGoing = YES ;
while (keepGoing) {
BOOL moreToRun = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]] ;
BOOL isDone = [notifee isDone] ;
keepGoing = moreToRun && !isDone ;
}
#else
while (![notifee isDone] && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]]) {
}
#endif
[[NSNotificationCenter defaultCenter] removeObserver:notifee] ;
}
else {
NSLog(@"5485348 error: %@", error) ;
}
[blocker unlockLock] ;
[notifee release] ;
[observer release] ;
[pool drain] ;
}
- (BOOL)blockUntilWatchFlags:(uint32_t)watchFlags
paths:(NSSet*)paths
timeout:(NSTimeInterval)timeout {
BOOL ok = YES ;
// As always when dealing with files, there is a possibility of a race
// condition here. But we check anyhow.
NSMutableSet* pathsExisting = [[NSMutableSet alloc] init] ;
for (NSString* path in paths) {
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[pathsExisting addObject:path] ;
}
}
if ([pathsExisting count] > 0) {
SSYBlocker* blocker = [[SSYBlocker alloc] init] ;
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
blocker, constKeySSYPathWaiterBlocker,
pathsExisting, constKeySSYPathWaiterPaths,
[NSNumber numberWithUnsignedLong:watchFlags], constKeySSYPathWaiterWatchFlags,
[NSNumber numberWithDouble:timeout], constKeySSYPathWaiterTimeout,
nil] ;
NSThread* notifeeThread = [[NSThread alloc] initWithTarget:self
selector:@selector(waitWithInfo:)
object:info] ;
// Name the thread, to help in debugging.
[notifeeThread setName:@"SSYPathWaiter-Notifee"] ;
[notifeeThread start] ;
// Will block here until work is done, or timeout
[blocker blockForLock] ;
ok = [self succeeded] ;
[notifeeThread cancel] ;
[notifeeThread release] ;
[blocker release] ;
}
else {
ok = NO ;
}
[pathsExisting release] ;
return ok ;
}
- (BOOL)blockUntilWatchFlags:(uint32_t)watchFlags
path:(NSString*)path
timeout:(NSTimeInterval)timeout {
return [self blockUntilWatchFlags:watchFlags
paths:[NSSet setWithObject:path]
timeout:timeout] ;
}
@end