File: GVFileNotificationCenter.m

package info (click to toggle)
graphviz 2.40.1-6
  • links: PTS
  • area: main
  • in suites: buster, sid
  • size: 81,972 kB
  • sloc: ansic: 1,033,400; sh: 17,772; cpp: 11,186; makefile: 4,656; yacc: 3,348; xml: 2,466; tcl: 1,934; cs: 1,890; objc: 1,163; lex: 423; perl: 422; awk: 241; python: 76; ruby: 41; php: 26
file content (167 lines) | stat: -rw-r--r-- 5,015 bytes parent folder | download | duplicates (3)
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
/* $Id$ $Revision$ */
/* vim:set shiftwidth=4 ts=8: */

/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: See CVS logs. Details at http://www.graphviz.org/
 *************************************************************************/

#include <fcntl.h>
#include <sys/event.h>

#import "GVFileNotificationCenter.h"

static GVFileNotificationCenter *_defaultCenter = nil;

@interface GVFileNotificationRecord : NSObject
{
	id _observer;
	NSString *_path;
	SEL _selector;
	int _fileDescriptor;
}

@property(readonly) id observer;
@property(readonly) NSString *path;
@property SEL selector;
@property int fileDescriptor;

@end

@implementation GVFileNotificationRecord

@synthesize observer = _observer;
@synthesize path = _path;
@synthesize selector = _selector;
@synthesize fileDescriptor = _fileDescriptor;

- (id)initWithObserver:(id)observer path:(NSString *)path
{
	if (self = [super init]) {
		_observer = observer;
		_path = [path retain];
	}
	return self;
}

- (BOOL)isEqual:(id)anObject
{
	/* if the other object is one of us, compare observers + paths only */
	return [anObject isKindOfClass:[GVFileNotificationRecord class]] ? [self observer] == [anObject observer] && [[self path] isEqualToString:[anObject path]] : NO;
}

- (NSUInteger)hash
{
	/* hash based on observers + paths only */
	return (NSUInteger)_observer ^ [_path hash];
}

- (void)dealloc
{
	[_path release];
	[super dealloc];
}

@end

static void noteFileChanged(CFFileDescriptorRef queue, CFOptionFlags callBackTypes, void *info)
{
	int queueDescriptor = CFFileDescriptorGetNativeDescriptor(queue);
    
	/* grab the next event from the kernel queue */
	struct kevent event;
    kevent(queueDescriptor, NULL, 0, &event, 1, NULL);
	
	GVFileNotificationRecord *record = (GVFileNotificationRecord *)event.udata;
	if (record) {
		/* report the file change to the observer */
		[record.observer performSelector:record.selector withObject:record.path];

		/* if watched file is deleted, try to reopen the file and watch it again */
		if (event.fflags & NOTE_DELETE) {
			close(record.fileDescriptor);
			
			int fileDescriptor = open([record.path UTF8String], O_EVTONLY);
			if (fileDescriptor != -1) {
				struct kevent change;
				EV_SET (&change, fileDescriptor, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND, 0, record);
				if (kevent (queueDescriptor, &change, 1, NULL, 0, NULL) != -1)
					record.fileDescriptor = fileDescriptor;
			}
		}
		
	}
	
	/* reenable the callbacks (they were automatically disabled when handling the CFFileDescriptor) */
    CFFileDescriptorEnableCallBacks(queue, kCFFileDescriptorReadCallBack);
}


@implementation GVFileNotificationCenter

+ (void)initialize
{
	if (!_defaultCenter)
		_defaultCenter = [[GVFileNotificationCenter alloc] init];
}

+ (id)defaultCenter
{
	return _defaultCenter;
}

- (id)init
{
	if (self = [super init]) {
		/* create kernel queue, wrap a CFFileDescriptor around it and schedule it on the Cocoa run loop */
		_queue = CFFileDescriptorCreate(kCFAllocatorDefault, kqueue(), true, noteFileChanged, NULL);
		CFFileDescriptorEnableCallBacks(_queue, kCFFileDescriptorReadCallBack);
		CFRunLoopAddSource(
			[[NSRunLoop currentRunLoop] getCFRunLoop],
			CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, _queue, 0),
			kCFRunLoopDefaultMode);
			
		/* need to keep track of observers */
		_records = [[NSMutableSet alloc] init];
	}
	return self;
}

- (void)addObserver:(id)observer selector:(SEL)selector path:(NSString *)path
{
	GVFileNotificationRecord *record = [[[GVFileNotificationRecord alloc] initWithObserver:observer path:path] autorelease];
	GVFileNotificationRecord *oldRecord = [_records member:record];
	
	if (oldRecord)
		/* record already exists, just update the selector */
		oldRecord.selector = selector;
	else {
		/* new record, start monitoring the path */
		int fileDescriptor = open([path UTF8String], O_EVTONLY);
		if (fileDescriptor != -1) {
			struct kevent change;
			EV_SET (&change, fileDescriptor, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND, 0, record);
			if (kevent (CFFileDescriptorGetNativeDescriptor(_queue), &change, 1, NULL, 0, NULL) != -1) {
				record.selector = selector;
				record.fileDescriptor = fileDescriptor;
				[_records addObject:record];
			}
		}
	}
}

- (void)removeObserver:(id)observer path:(NSString *)path
{
	GVFileNotificationRecord *record = [_records member:[[[GVFileNotificationRecord alloc] initWithObserver:observer path:path] autorelease]];
	if (record) {
		close(record.fileDescriptor);	/* closing the file descriptor also removes it from the kqueue */
		[_records removeObject:record];
	}
	
}
@end