This repository was archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCPathMonitor.m
More file actions
169 lines (127 loc) · 2.93 KB
/
CCPathMonitor.m
File metadata and controls
169 lines (127 loc) · 2.93 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
//
// CCPathMonitor.m
// Violet
//
#import "CCPathMonitor.h"
NSString * const CCPathOfInterestChangedNotification = @"CCPathOfInterestChangedNotification";
@interface CCPathMonitor (Private)
- (void)updateContents;
@end
@implementation CCPathMonitor
- (id)initWithPath:(NSString *)path
{
self = [super init];
if (self != nil)
{
_path = [path retain];
_contents = nil;
_source = nil;
_notifierIsStarted = NO;
}
return self;
}
- (void)dealloc
{
[self stopNotifier];
[_contents release];
[_path release];
[super dealloc];
}
- (DeviceType)deviceTypeForPath:(NSString *)path
{
DeviceType result = Unknown;
BOOL isDirectory = NO;
if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory)
{
}
else
{
NSLog(@"[Error] File doesn't exist at path: %@", path);
}
return result;
}
- (void)delayedUpdateContents
{
[self performSelector:@selector(updateContents) withObject:nil afterDelay:2.0];
}
- (void)updateContents
{
NSError *error = nil;
NSArray *newContents = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:_path error:&error] retain];
if(!newContents)
{
NSLog(@"[Error] getting directory contents at path: %@ -- %@", _path, error);
return;
}
NSMutableArray *toNotifyAbout = [NSMutableArray arrayWithArray:newContents];
// Removed
for(NSString *path in _contents)
{
BOOL wasRemoved = YES;
for(NSString *item in newContents)
{
if([item isEqualToString:path])
{
wasRemoved = NO;
break;
}
}
if(wasRemoved)
{
[toNotifyAbout addObject:path];
}
}
for(NSString *file in toNotifyAbout)
{
NSString *item = [_path stringByAppendingPathComponent:file];
[[NSNotificationCenter defaultCenter] postNotificationName:CCPathOfInterestChangedNotification object:item];
}
[newContents retain];
[_contents release];
_contents = newContents;
}
- (void)startNotifier
{
if(!_path)
{
NSLog(@"[Error] Can't start notifier with nil path");
return;
}
if(_notifierIsStarted)
{
NSLog(@"[Warning] Notifier is already started");
return;
}
int fd = open([_path UTF8String], O_EVTONLY);
if (fd < 0)
{
NSLog(@"[Error] opening file descriptor at path: %@", _path);
return;
}
_notifierIsStarted = YES;
// Do an initial query
[self updateContents];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fd, DISPATCH_VNODE_LINK, queue);
// Event handler
dispatch_source_set_event_handler(_source, ^{
[self performSelectorOnMainThread:@selector(delayedUpdateContents) withObject:nil waitUntilDone:NO];
});
// Cancel handler
dispatch_source_set_cancel_handler(_source, ^{
close(fd);
});
// Start processing events.
dispatch_resume(_source);
}
- (void)stopNotifier
{
if(_source)
{
dispatch_source_cancel(_source);
dispatch_release(_source);
_source = nil;
}
_notifierIsStarted = NO;
}
@end