-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatcher.js
More file actions
306 lines (274 loc) · 7.75 KB
/
watcher.js
File metadata and controls
306 lines (274 loc) · 7.75 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
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
var inotify = require('inotify'),
Inotify = inotify.Inotify,
path = require('path'),
util = require('util'),
walker = require('walk'),
log = require('winston'),
fs = require('fs'),
events = require('events');
/**
* Creates a new watcher.
*
* @constructor
* @extends events.EventEmitter
* @param {string} root The directory to watch
*/
var Watcher = function(root) {
Watcher.super_.call(this);
this._root = path.normalize(root);
this._inotify = new Inotify();
this._watches = {};
this._files = {};
this.on('newListener', this._replay.bind(this));
this._watch(this._root);
};
util.inherits(Watcher, events.EventEmitter);
/**
* Recursively watches the given directory and notifies an update for every
* nested file.
*
* @private
* @param {string} directory The directory to recursively watch
*/
Watcher.prototype._watch = function(directory) {
var self = this;
// watch directory
this._addWatch(directory);
// recursively watch subdirectories and notify existing files
walker.walk(directory).on('directory', function(root, stat, next) {
self._addWatch(path.join(root, stat.name));
next();
}).on('file', function(root, stat, next) {
var file = path.join(root, stat.name);
self._update(file, stat);
next();
});
};
/**
* Watches the given directory.
*
* @private
* @param {string} directory The directory to watch
*/
Watcher.prototype._addWatch = function(directory) {
log.info('watching directory', directory);
var wd = this._inotify.addWatch({
path: directory,
watch_for: Inotify.IN_CLOSE_WRITE | Inotify.IN_CREATE | Inotify.IN_DELETE | Inotify.IN_MOVED_FROM | Inotify.IN_MOVED_TO,
callback: this._onEvent.bind(this)
});
this._watches[wd] = {
directory: directory,
watch: wd
};
};
/**
* Recursively unwatches the given directory and notifies a delete for every
* nested file.
*
* @private
* @param {string} directory The directory to recursively unwatch
*/
Watcher.prototype._unwatch = function(directory) {
var prefix = directory + '/';
// unwatch directory and subdirectories
for (var wd in this._watches) {
if (this._watches[wd].directory === directory || this._watches[wd].directory.indexOf(prefix) === 0) {
this._removeWatch(this._watches[wd].watch, false);
}
}
// notify deletions
for (var file in this._files) {
if (file.indexOf(prefix) === 0) {
this._delete(file);
}
}
};
/**
* Unwatches the given directory.
*
* @private
* @param {number} wd The watch descriptor to unwatch
* @param {boolean} auto When set to true, it means that the watch has been
* automatically removed by inotify and we only have to clean our stuff
*/
Watcher.prototype._removeWatch = function(wd, auto) {
var descriptor = this._watches[wd];
if (descriptor === undefined) {
return;
}
log.info('unwatching directory', descriptor.directory);
if (!auto) {
this._inotify.removeWatch(wd);
}
delete this._watches[wd];
};
/**
* Fires a file creation event.
*
* @private
* @param {string} file The created file
*/
Watcher.prototype._create = function(file) {
log.debug('firing create', file);
this.emit('create', file);
// when hardlinking, consider this as an instantaneous create/write
var stat = fs.statSync(file);
if (stat.nlink > 1) {
this._update(file, stat);
}
};
/**
* Fires a file update event.
*
* @private
* @param {string} file The updated file
* @param {fs.Stats} stat The file stat (when undefined, this method will
* synchronously retrieve it)
*/
Watcher.prototype._update = function(file, stat) {
if (stat === undefined) {
stat = fs.statSync(file);
}
this._files[file] = stat;
log.debug('firing update', file);
this.emit('update', file, stat);
};
/**
* Fires a file deletion event.
*
* @private
* @param {string} file The deleted file
*/
Watcher.prototype._delete = function(file) {
var stat = this._files[file];
if (stat !== undefined) {
delete this._files[file];
log.debug('firing delete', file);
this.emit('delete', file);
}
};
/**
* Replays creation and update events for new listeners.
*
* @private
* @param {string} event The event to replay
* @param {Function} listener The new listener
*/
Watcher.prototype._replay = function(event, listener) {
for (var file in this._files) {
if (typeof this._files[file] === 'boolean' && event === 'create') {
listener(file);
}
else if (event === 'update') {
listener(file, this._files[file]);
}
}
};
/**
* Converts an event to the path of the file that triggered this event.
*
* @private
* @param {Object} event The inotify event
* @return {string} the path of the file that triggered this event
*/
Watcher.prototype._toPath = function(event) {
var descriptor = this._watches[event.watch];
if (descriptor !== undefined) {
return path.join(descriptor.directory, event.name);
}
else {
log.warn('can\'t convert event to path, unknown wd', event);
return undefined;
}
};
/**
* The inotify event listener.
*
* @private
* @param {Object} event The inotify event
*/
Watcher.prototype._onEvent = function(event) {
if (event.mask & Inotify.IN_CLOSE_WRITE) {
log.debug('IN_CLOSE_WRITE', event);
// fire update
this._update(this._toPath(event), undefined);
}
else if (event.mask & Inotify.IN_CREATE) {
log.debug('IN_CREATE', event);
// watch new directory
if (event.mask & Inotify.IN_ISDIR) {
this._addWatch(this._toPath(event));
}
// fire create
else {
this._create(this._toPath(event));
}
}
else if (event.mask & Inotify.IN_DELETE) {
log.debug('IN_DELETE', event);
// fire delete
if ((event.mask & Inotify.IN_ISDIR) === 0) {
this._delete(this._toPath(event));
}
}
else if (event.mask & Inotify.IN_MOVED_FROM) {
log.debug('IN_MOVED_FROM', event);
// unwatch moved away directory
if (event.mask & Inotify.IN_ISDIR) {
this._unwatch(this._toPath(event));
}
// fire delete
else {
this._delete(this._toPath(event));
}
}
else if (event.mask & Inotify.IN_MOVED_TO) {
log.debug('IN_MOVED_TO', event);
// watch moved in directory
if (event.mask & Inotify.IN_ISDIR) {
this._watch(this._toPath(event));
}
// fire create
else {
this._create(this._toPath(event));
}
}
else if (event.mask & Inotify.IN_IGNORED) {
log.debug('IN_IGNORED', event);
// cleanup
this._removeWatch(event.watch, true);
}
else {
log.warn('unexpected event', event);
}
};
/**
* Closes the watcher object.
*/
Watcher.prototype.close = function() {
this._inotify.close();
};
/**
* Exports the Watcher class.
*/
module.exports.Watcher = Watcher;
/**
* Watches a location for file creation, deletion or modifications. The method
* returns an event emitter which sends 3 different events:
* - 'create': when a new file is created (argument: the file)
* - 'delete': when a file is deleted (argument: the file)
* - 'update': when a file is updated (argument: the file and its stats)
*
* Note that upon registration, the watcher sends an 'update' event for every
* existing file.
*
* When done, the watcher can be closed using its close() method and safely
* discarded.
*
* @param {string} location The location to watch
* @return {Watcher} the watcher
*/
module.exports.watch = function(location) {
return new Watcher(location);
};