summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/filesystem/dirmon/FileWatcher/FileWatcherOSX.cpp
blob: a47d6351c19feb338346f652fd4568acc56365b8 (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
	Copyright (c) 2009 James Wynn (james@jameswynn.com)

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.

	James Wynn james@jameswynn.com
*/

#include <FileWatcher/FileWatcherOSX.h>

#if FILEWATCHER_PLATFORM == FILEWATCHER_PLATFORM_KQUEUE

#include <sys/event.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <iostream>

// this is more suited:
// https://developer.apple.com/library/mac/#documentation/Darwin/Conceptual/FSEvents_ProgGuide/UsingtheFSEventsFramework/UsingtheFSEventsFramework.html

namespace FW {

#define MAX_CHANGE_EVENT_SIZE 2000

typedef struct kevent KEvent;

struct EntryStruct {
	EntryStruct(const char* filename, time_t mtime = 0)
		: mFilename(filename), mModifiedTime(mtime) {
	}
	~EntryStruct() {
		delete[] mFilename;
	}
	const char* mFilename;
	time_t mModifiedTime;
};

int comparator(const void* ke1, const void* ke2) {
	/*KEvent* kevent1 = (KEvent*) ke1;
	KEvent* kevent2 = (KEvent*) ke2;

	EntryStruct* event1 = (EntryStruct*)kevent1->udata;
	EntryStruct* event2 = (EntryStruct*)kevent2->udata;
	return strcmp(event1->mFilename, event2->mFilename);
	*/
	return strcmp(((EntryStruct*)(((KEvent*)(ke1))->udata))->mFilename, ((EntryStruct*)(((KEvent*)(ke2))->udata))->mFilename);
}

struct WatchStruct {
	WatchID mWatchID;
	String mDirName;
	FileWatchListener* mListener;
	FileWatcherOSX* mWatcher;
	bool mRecursive;

	// index 0 is always the directory
	KEvent mChangeList[MAX_CHANGE_EVENT_SIZE];
	size_t mChangeListCount;

	WatchStruct(WatchID watchid, const String& dirname, FileWatchListener* listener, FileWatcherOSX* watcher, bool recursive = false)
		: mWatchID(watchid), mDirName(dirname), mListener(listener), mWatcher(watcher), mRecursive(recursive) {
		mChangeListCount = 0;
		addAll();
	}

	void addFile(const String& name, bool emitEvents = true) {
		//fprintf(stderr, "ADDED: %s\n", name.c_str());

		// create entry
		struct stat attrib;
		stat(name.c_str(), &attrib);

		int fd = open(name.c_str(), O_RDONLY);

		if(fd == -1)
			throw FileNotFoundException(name);

		++mChangeListCount;

		char* namecopy = new char[name.length() + 1];
		strncpy(namecopy, name.c_str(), name.length());
		namecopy[name.length()] = 0;
		EntryStruct* entry = new EntryStruct(namecopy, attrib.st_mtime);

		// set the event data at the end of the list
		EV_SET(&mChangeList[mChangeListCount], fd, EVFILT_VNODE,
		       EV_ADD | EV_ENABLE | EV_ONESHOT,
		       NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB | NOTE_RENAME,
		       0, (void*)entry);

		// qsort
		qsort(mChangeList + 1, mChangeListCount, sizeof(KEvent), comparator);

		// handle action
		if(emitEvents)
			handleAction(name, Actions::Add);
	}

	void removeFile(const String& name, bool emitEvents = true) {
		// bsearch
		KEvent target;
		EntryStruct tempEntry(name.c_str(), 0);
		target.udata = &tempEntry;
		KEvent* ke = (KEvent*)bsearch(&target, &mChangeList, mChangeListCount + 1, sizeof(KEvent), comparator);
		if(!ke)
			throw FileNotFoundException(name);

		tempEntry.mFilename = 0;

		// delete
		close(ke->ident);
		delete((EntryStruct*)ke->udata);
		memset(ke, 0, sizeof(KEvent));

		// move end to current
		memcpy(ke, &mChangeList[mChangeListCount], sizeof(KEvent));
		memset(&mChangeList[mChangeListCount], 0, sizeof(KEvent));
		--mChangeListCount;

		// qsort
		qsort(mChangeList + 1, mChangeListCount, sizeof(KEvent), comparator);

		// handle action
		if(emitEvents)
			handleAction(name, Actions::Delete);
	}

	// called when the directory is actually changed
	// means a file has been added or removed
	// rescans the watched directory adding/removing files and sending notices
	void rescan() {
		// if new file, call addFile
		// if missing file, call removeFile
		// if timestamp modified, call handleAction(filename, ACTION_MODIFIED);
		DIR* dir = opendir(mDirName.c_str());
		if(!dir)
			return;

		struct dirent* dentry;
		KEvent* ke = &mChangeList[1];
		EntryStruct* entry = 0;
		struct stat attrib;

		while((dentry = readdir(dir)) != NULL) {
			String fname = mDirName + "/" + dentry->d_name;
			stat(fname.c_str(), &attrib);
			if(!S_ISREG(attrib.st_mode))
				continue;

			if(ke <= &mChangeList[mChangeListCount]) {
				entry = (EntryStruct*)ke->udata;
				int result = strcmp(entry->mFilename, fname.c_str());
				//fprintf(stderr, "[%s cmp %s]\n", entry->mFilename, fname.c_str());
				if(result == 0) {
					stat(entry->mFilename, &attrib);
					time_t timestamp = attrib.st_mtime;

					if(entry->mModifiedTime != timestamp) {
						entry->mModifiedTime = timestamp;
						handleAction(entry->mFilename, Actions::Modified);
					}
					ke++;
				} else if(result < 0) {
					// f1 was deleted
					removeFile(entry->mFilename);
					ke++;
				} else {
					// f2 was created
					addFile(fname);
					ke++;
				}
			} else {
				// just add
				addFile(fname);
				ke++;
			}
		}//end while

		closedir(dir);
	};

	void handleAction(const String& filename, FW::Action action) {
		mListener->handleFileAction(mWatchID, mDirName, filename, action);
	}

	void addAll() {
		// add base dir
		int fd = open(mDirName.c_str(), O_RDONLY);
		EV_SET(&mChangeList[0], fd, EVFILT_VNODE,
		       EV_ADD | EV_ENABLE | EV_ONESHOT,
		       NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB | NOTE_RENAME,
		       0, 0);

		//fprintf(stderr, "ADDED: %s\n", mDirName.c_str());

		// scan directory and call addFile(name, false) on each file
		DIR* dir = opendir(mDirName.c_str());
		if(!dir)
			throw FileNotFoundException(mDirName);

		struct dirent* entry;
		struct stat attrib;
		while((entry = readdir(dir)) != NULL) {
			String fname = (mDirName + "/" + String(entry->d_name));
			stat(fname.c_str(), &attrib);
			if(S_ISREG(attrib.st_mode)) {
				addFile(fname, false);
			} else if(S_IFDIR && mRecursive && entry->d_name[0] != '.') {
				mWatcher->addWatch(fname, mListener, mRecursive);
			} else {
				fprintf(stderr, "NOT ADDED: %s (%d)\n", fname.c_str(), attrib.st_mode);
			}
		}//end while

		closedir(dir);
	}

	void removeAll() {
		KEvent* ke = NULL;

		// go through list removing each file and sending an event
		for(int i = 0; i < mChangeListCount; ++i) {
			ke = &mChangeList[i];
			//handleAction(name, Action::Delete);
			EntryStruct* entry = (EntryStruct*)ke->udata;

			handleAction(entry->mFilename, Actions::Delete);

			// delete
			close(ke->ident);
			delete((EntryStruct*)ke->udata);
		}
	}
};

void FileWatcherOSX::update() {
	int nev = 0;
	struct kevent event;

	WatchMap::iterator iter = mWatches.begin();
	WatchMap::iterator end = mWatches.end();
	for(; iter != end; ++iter) {
		WatchStruct* watch = iter->second;

		while((nev = kevent(mDescriptor, (KEvent*)&(watch->mChangeList), watch->mChangeListCount + 1, &event, 1, &mTimeOut)) != 0) {
			if(nev == -1)
				perror("kevent");
			else {
				if (event.fflags & NOTE_DELETE) {
					fprintf(stderr, "NOTE_DELETE ");
				}
				if (event.fflags & NOTE_EXTEND) {
					fprintf(stderr, "NOTE_EXTEND ");
				}
				if (event.fflags & NOTE_WRITE) {
					fprintf(stderr, "NOTE_WRITE ");
				}
				if (event.fflags & NOTE_ATTRIB) {
					fprintf(stderr, "NOTE_ATTRIB ");
				}
				if (event.fflags & NOTE_RENAME) {
					fprintf(stderr, "NOTE_RENAME ");
				}

				EntryStruct* entry = 0;
				if((entry = (EntryStruct*)event.udata) != 0) {
					fprintf(stderr, " to %s -- \n", (char*)entry->mFilename);

					if(event.fflags & NOTE_DELETE) {
						//watch->handleAction(entry->mFilename, Action::Delete);
						watch->removeFile(entry->mFilename);
					}
					if(event.fflags & NOTE_EXTEND ||
					        event.fflags & NOTE_WRITE ||
					        event.fflags & NOTE_ATTRIB) {
						//watch->rescan();
						struct stat attrib;
						stat(entry->mFilename, &attrib);
						entry->mModifiedTime = attrib.st_mtime;
						watch->handleAction(entry->mFilename, FW::Actions::Modified);
					}
				} else {
					fprintf(stderr, " in %s -- rescanning\n", watch->mDirName.c_str());
					watch->rescan();
				}
			}
		}
	}
}

//--------
FileWatcherOSX::FileWatcherOSX() {
	mDescriptor = kqueue();
	mTimeOut.tv_sec = 0;
	mTimeOut.tv_nsec = 20000000;
}

//--------
FileWatcherOSX::~FileWatcherOSX() {
	WatchMap::iterator iter = mWatches.begin();
	WatchMap::iterator end = mWatches.end();
	for(; iter != end; ++iter) {
		delete iter->second;
	}
	mWatches.clear();

	close(mDescriptor);
}

//--------
WatchID FileWatcherOSX::addWatch(const String& directory, FileWatchListener* watcher, bool recursive) {
	/*		int fd = open(directory.c_str(), O_RDONLY);
			if(fd == -1)
				perror("open");

			EV_SET(&change, fd, EVFILT_VNODE,
				   EV_ADD | EV_ENABLE | EV_ONESHOT,
				   NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB,
				   0, (void*)"testing");
	*/

	std::cout << "Adding watch for " << directory << std::endl;

	WatchID currWatch = ++mLastWatchID;
	WatchStruct* watch = new WatchStruct(currWatch, directory, watcher, this, recursive);
	mWatches.insert(std::make_pair(currWatch, watch));
	return currWatch;
}

//--------
void FileWatcherOSX::removeWatch(const String& directory) {
	WatchMap::iterator iter = mWatches.begin();
	WatchMap::iterator end = mWatches.end();
	for(; iter != end; ++iter) {
		if(directory == iter->second->mDirName) {
			removeWatch(iter->first);
			return;
		}
	}
}

//--------
void FileWatcherOSX::removeWatch(WatchID watchid) {
	WatchMap::iterator iter = mWatches.find(watchid);

	if(iter == mWatches.end())
		return;

	WatchStruct* watch = iter->second;
	mWatches.erase(iter);

	//inotify_rm_watch(mFD, watchid);

	delete watch;
	watch = 0;
}

//--------
void FileWatcherOSX::handleAction(WatchStruct* watch, const String& filename, unsigned long action) {
}

};//namespace FW

#endif//FILEWATCHER_PLATFORM_KQUEUE