nodefs-handler.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. 'use strict';
  2. var fs = require('fs');
  3. var sysPath = require('path');
  4. var readdirp = require('readdirp');
  5. var isBinaryPath = require('is-binary-path');
  6. // fs.watch helpers
  7. // object to hold per-process fs.watch instances
  8. // (may be shared across chokidar FSWatcher instances)
  9. var FsWatchInstances = Object.create(null);
  10. // Private function: Instantiates the fs.watch interface
  11. // * path - string, path to be watched
  12. // * options - object, options to be passed to fs.watch
  13. // * listener - function, main event handler
  14. // * errHandler - function, handler which emits info about errors
  15. // * emitRaw - function, handler which emits raw event data
  16. // Returns new fsevents instance
  17. function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
  18. var handleEvent = function(rawEvent, evPath) {
  19. listener(path);
  20. emitRaw(rawEvent, evPath, {watchedPath: path});
  21. // emit based on events occuring for files from a directory's watcher in
  22. // case the file's watcher misses it (and rely on throttling to de-dupe)
  23. if (evPath && path !== evPath) {
  24. fsWatchBroadcast(
  25. sysPath.resolve(path, evPath), 'listeners', sysPath.join(path, evPath)
  26. );
  27. }
  28. };
  29. try {
  30. return fs.watch(path, options, handleEvent);
  31. } catch (error) {
  32. errHandler(error);
  33. }
  34. }
  35. // Private function: Helper for passing fs.watch event data to a
  36. // collection of listeners
  37. // * fullPath - string, absolute path bound to the fs.watch instance
  38. // * type - string, listener type
  39. // * val[1..3] - arguments to be passed to listeners
  40. // Returns nothing
  41. function fsWatchBroadcast(fullPath, type, val1, val2, val3) {
  42. if (!FsWatchInstances[fullPath]) return;
  43. FsWatchInstances[fullPath][type].forEach(function(listener) {
  44. listener(val1, val2, val3);
  45. });
  46. }
  47. // Private function: Instantiates the fs.watch interface or binds listeners
  48. // to an existing one covering the same file system entry
  49. // * path - string, path to be watched
  50. // * fullPath - string, absolute path
  51. // * options - object, options to be passed to fs.watch
  52. // * handlers - object, container for event listener functions
  53. // Returns close function
  54. function setFsWatchListener(path, fullPath, options, handlers) {
  55. var listener = handlers.listener;
  56. var errHandler = handlers.errHandler;
  57. var rawEmitter = handlers.rawEmitter;
  58. var container = FsWatchInstances[fullPath];
  59. var watcher;
  60. if (!options.persistent) {
  61. watcher = createFsWatchInstance(
  62. path, options, listener, errHandler, rawEmitter
  63. );
  64. return watcher.close.bind(watcher);
  65. }
  66. if (!container) {
  67. watcher = createFsWatchInstance(
  68. path,
  69. options,
  70. fsWatchBroadcast.bind(null, fullPath, 'listeners'),
  71. errHandler, // no need to use broadcast here
  72. fsWatchBroadcast.bind(null, fullPath, 'rawEmitters')
  73. );
  74. if (!watcher) return;
  75. var broadcastErr = fsWatchBroadcast.bind(null, fullPath, 'errHandlers');
  76. watcher.on('error', function(error) {
  77. // Workaround for https://github.com/joyent/node/issues/4337
  78. if (process.platform === 'win32' && error.code === 'EPERM') {
  79. fs.open(path, 'r', function(err, fd) {
  80. if (!err) fs.close(fd, function(err) {
  81. if (!err) broadcastErr(error);
  82. });
  83. });
  84. } else {
  85. broadcastErr(error);
  86. }
  87. });
  88. container = FsWatchInstances[fullPath] = {
  89. listeners: [listener],
  90. errHandlers: [errHandler],
  91. rawEmitters: [rawEmitter],
  92. watcher: watcher
  93. };
  94. } else {
  95. container.listeners.push(listener);
  96. container.errHandlers.push(errHandler);
  97. container.rawEmitters.push(rawEmitter);
  98. }
  99. var listenerIndex = container.listeners.length - 1;
  100. // removes this instance's listeners and closes the underlying fs.watch
  101. // instance if there are no more listeners left
  102. return function close() {
  103. delete container.listeners[listenerIndex];
  104. delete container.errHandlers[listenerIndex];
  105. delete container.rawEmitters[listenerIndex];
  106. if (!Object.keys(container.listeners).length) {
  107. container.watcher.close();
  108. delete FsWatchInstances[fullPath];
  109. }
  110. };
  111. }
  112. // fs.watchFile helpers
  113. // object to hold per-process fs.watchFile instances
  114. // (may be shared across chokidar FSWatcher instances)
  115. var FsWatchFileInstances = Object.create(null);
  116. // Private function: Instantiates the fs.watchFile interface or binds listeners
  117. // to an existing one covering the same file system entry
  118. // * path - string, path to be watched
  119. // * fullPath - string, absolute path
  120. // * options - object, options to be passed to fs.watchFile
  121. // * handlers - object, container for event listener functions
  122. // Returns close function
  123. function setFsWatchFileListener(path, fullPath, options, handlers) {
  124. var listener = handlers.listener;
  125. var rawEmitter = handlers.rawEmitter;
  126. var container = FsWatchFileInstances[fullPath];
  127. var listeners = [];
  128. var rawEmitters = [];
  129. if (
  130. container && (
  131. container.options.persistent < options.persistent ||
  132. container.options.interval > options.interval
  133. )
  134. ) {
  135. // "Upgrade" the watcher to persistence or a quicker interval.
  136. // This creates some unlikely edge case issues if the user mixes
  137. // settings in a very weird way, but solving for those cases
  138. // doesn't seem worthwhile for the added complexity.
  139. listeners = container.listeners;
  140. rawEmitters = container.rawEmitters;
  141. fs.unwatchFile(fullPath);
  142. container = false;
  143. }
  144. if (!container) {
  145. listeners.push(listener);
  146. rawEmitters.push(rawEmitter);
  147. container = FsWatchFileInstances[fullPath] = {
  148. listeners: listeners,
  149. rawEmitters: rawEmitters,
  150. options: options,
  151. watcher: fs.watchFile(fullPath, options, function(curr, prev) {
  152. container.rawEmitters.forEach(function(rawEmitter) {
  153. rawEmitter('change', fullPath, {curr: curr, prev: prev});
  154. });
  155. var currmtime = curr.mtime.getTime();
  156. if (curr.size !== prev.size || currmtime > prev.mtime.getTime() || currmtime === 0) {
  157. container.listeners.forEach(function(listener) {
  158. listener(path, curr);
  159. });
  160. }
  161. })
  162. };
  163. } else {
  164. container.listeners.push(listener);
  165. container.rawEmitters.push(rawEmitter);
  166. }
  167. var listenerIndex = container.listeners.length - 1;
  168. // removes this instance's listeners and closes the underlying fs.watchFile
  169. // instance if there are no more listeners left
  170. return function close() {
  171. delete container.listeners[listenerIndex];
  172. delete container.rawEmitters[listenerIndex];
  173. if (!Object.keys(container.listeners).length) {
  174. fs.unwatchFile(fullPath);
  175. delete FsWatchFileInstances[fullPath];
  176. }
  177. };
  178. }
  179. // fake constructor for attaching nodefs-specific prototype methods that
  180. // will be copied to FSWatcher's prototype
  181. function NodeFsHandler() {}
  182. // Private method: Watch file for changes with fs.watchFile or fs.watch.
  183. // * path - string, path to file or directory.
  184. // * listener - function, to be executed on fs change.
  185. // Returns close function for the watcher instance
  186. NodeFsHandler.prototype._watchWithNodeFs =
  187. function(path, listener) {
  188. var directory = sysPath.dirname(path);
  189. var basename = sysPath.basename(path);
  190. var parent = this._getWatchedDir(directory);
  191. parent.add(basename);
  192. var absolutePath = sysPath.resolve(path);
  193. var options = {persistent: this.options.persistent};
  194. if (!listener) listener = Function.prototype; // empty function
  195. var closer;
  196. if (this.options.usePolling) {
  197. options.interval = this.enableBinaryInterval && isBinaryPath(basename) ?
  198. this.options.binaryInterval : this.options.interval;
  199. closer = setFsWatchFileListener(path, absolutePath, options, {
  200. listener: listener,
  201. rawEmitter: this.emit.bind(this, 'raw')
  202. });
  203. } else {
  204. closer = setFsWatchListener(path, absolutePath, options, {
  205. listener: listener,
  206. errHandler: this._handleError.bind(this),
  207. rawEmitter: this.emit.bind(this, 'raw')
  208. });
  209. }
  210. return closer;
  211. };
  212. // Private method: Watch a file and emit add event if warranted
  213. // * file - string, the file's path
  214. // * stats - object, result of fs.stat
  215. // * initialAdd - boolean, was the file added at watch instantiation?
  216. // * callback - function, called when done processing as a newly seen file
  217. // Returns close function for the watcher instance
  218. NodeFsHandler.prototype._handleFile =
  219. function(file, stats, initialAdd, callback) {
  220. var dirname = sysPath.dirname(file);
  221. var basename = sysPath.basename(file);
  222. var parent = this._getWatchedDir(dirname);
  223. // if the file is already being watched, do nothing
  224. if (parent.has(basename)) return callback();
  225. // kick off the watcher
  226. var closer = this._watchWithNodeFs(file, function(path, newStats) {
  227. if (!this._throttle('watch', file, 5)) return;
  228. if (!newStats || newStats && newStats.mtime.getTime() === 0) {
  229. fs.stat(file, function(error, newStats) {
  230. // Fix issues where mtime is null but file is still present
  231. if (error) {
  232. this._remove(dirname, basename);
  233. } else {
  234. this._emit('change', file, newStats);
  235. }
  236. }.bind(this));
  237. // add is about to be emitted if file not already tracked in parent
  238. } else if (parent.has(basename)) {
  239. this._emit('change', file, newStats);
  240. }
  241. }.bind(this));
  242. // emit an add event if we're supposed to
  243. if (!(initialAdd && this.options.ignoreInitial)) {
  244. if (!this._throttle('add', file, 0)) return;
  245. this._emit('add', file, stats);
  246. }
  247. if (callback) callback();
  248. return closer;
  249. };
  250. // Private method: Handle symlinks encountered while reading a dir
  251. // * entry - object, entry object returned by readdirp
  252. // * directory - string, path of the directory being read
  253. // * path - string, path of this item
  254. // * item - string, basename of this item
  255. // Returns true if no more processing is needed for this entry.
  256. NodeFsHandler.prototype._handleSymlink =
  257. function(entry, directory, path, item) {
  258. var full = entry.fullPath;
  259. var dir = this._getWatchedDir(directory);
  260. if (!this.options.followSymlinks) {
  261. // watch symlink directly (don't follow) and detect changes
  262. this._readyCount++;
  263. fs.realpath(path, function(error, linkPath) {
  264. if (dir.has(item)) {
  265. if (this._symlinkPaths[full] !== linkPath) {
  266. this._symlinkPaths[full] = linkPath;
  267. this._emit('change', path, entry.stat);
  268. }
  269. } else {
  270. dir.add(item);
  271. this._symlinkPaths[full] = linkPath;
  272. this._emit('add', path, entry.stat);
  273. }
  274. this._emitReady();
  275. }.bind(this));
  276. return true;
  277. }
  278. // don't follow the same symlink more than once
  279. if (this._symlinkPaths[full]) return true;
  280. else this._symlinkPaths[full] = true;
  281. };
  282. // Private method: Read directory to add / remove files from `@watched` list
  283. // and re-read it on change.
  284. // * dir - string, fs path.
  285. // * stats - object, result of fs.stat
  286. // * initialAdd - boolean, was the file added at watch instantiation?
  287. // * depth - int, depth relative to user-supplied path
  288. // * target - string, child path actually targeted for watch
  289. // * wh - object, common watch helpers for this path
  290. // * callback - function, called when dir scan is complete
  291. // Returns close function for the watcher instance
  292. NodeFsHandler.prototype._handleDir =
  293. function(dir, stats, initialAdd, depth, target, wh, callback) {
  294. var parentDir = this._getWatchedDir(sysPath.dirname(dir));
  295. var tracked = parentDir.has(sysPath.basename(dir));
  296. if (!(initialAdd && this.options.ignoreInitial) && !target && !tracked) {
  297. if (!wh.hasGlob || wh.globFilter(dir)) this._emit('addDir', dir, stats);
  298. }
  299. // ensure dir is tracked (harmless if redundant)
  300. parentDir.add(sysPath.basename(dir));
  301. this._getWatchedDir(dir);
  302. var read = function(directory, initialAdd, done) {
  303. // Normalize the directory name on Windows
  304. directory = sysPath.join(directory, '');
  305. if (!wh.hasGlob) {
  306. var throttler = this._throttle('readdir', directory, 1000);
  307. if (!throttler) return;
  308. }
  309. var previous = this._getWatchedDir(wh.path);
  310. var current = [];
  311. readdirp({
  312. root: directory,
  313. entryType: 'all',
  314. fileFilter: wh.filterPath,
  315. directoryFilter: wh.filterDir,
  316. depth: 0,
  317. lstat: true
  318. }).on('data', function(entry) {
  319. var item = entry.path;
  320. var path = sysPath.join(directory, item);
  321. current.push(item);
  322. if (entry.stat.isSymbolicLink() &&
  323. this._handleSymlink(entry, directory, path, item)) return;
  324. // Files that present in current directory snapshot
  325. // but absent in previous are added to watch list and
  326. // emit `add` event.
  327. if (item === target || !target && !previous.has(item)) {
  328. this._readyCount++;
  329. // ensure relativeness of path is preserved in case of watcher reuse
  330. path = sysPath.join(dir, sysPath.relative(dir, path));
  331. this._addToNodeFs(path, initialAdd, wh, depth + 1);
  332. }
  333. }.bind(this)).on('end', function() {
  334. if (throttler) throttler.clear();
  335. if (done) done();
  336. // Files that absent in current directory snapshot
  337. // but present in previous emit `remove` event
  338. // and are removed from @watched[directory].
  339. previous.children().filter(function(item) {
  340. return item !== directory &&
  341. current.indexOf(item) === -1 &&
  342. // in case of intersecting globs;
  343. // a path may have been filtered out of this readdir, but
  344. // shouldn't be removed because it matches a different glob
  345. (!wh.hasGlob || wh.filterPath({
  346. fullPath: sysPath.resolve(directory, item)
  347. }));
  348. }).forEach(function(item) {
  349. this._remove(directory, item);
  350. }, this);
  351. }.bind(this)).on('error', this._handleError.bind(this));
  352. }.bind(this);
  353. var closer;
  354. if (this.options.depth == null || depth <= this.options.depth) {
  355. if (!target) read(dir, initialAdd, callback);
  356. closer = this._watchWithNodeFs(dir, function(dirPath, stats) {
  357. // if current directory is removed, do nothing
  358. if (stats && stats.mtime.getTime() === 0) return;
  359. read(dirPath, false);
  360. });
  361. } else {
  362. callback();
  363. }
  364. return closer;
  365. };
  366. // Private method: Handle added file, directory, or glob pattern.
  367. // Delegates call to _handleFile / _handleDir after checks.
  368. // * path - string, path to file or directory.
  369. // * initialAdd - boolean, was the file added at watch instantiation?
  370. // * depth - int, depth relative to user-supplied path
  371. // * target - string, child path actually targeted for watch
  372. // * callback - function, indicates whether the path was found or not
  373. // Returns nothing
  374. NodeFsHandler.prototype._addToNodeFs =
  375. function(path, initialAdd, priorWh, depth, target, callback) {
  376. if (!callback) callback = Function.prototype;
  377. var ready = this._emitReady;
  378. if (this._isIgnored(path) || this.closed) {
  379. ready();
  380. return callback(null, false);
  381. }
  382. var wh = this._getWatchHelpers(path, depth);
  383. if (!wh.hasGlob && priorWh) {
  384. wh.hasGlob = priorWh.hasGlob;
  385. wh.globFilter = priorWh.globFilter;
  386. wh.filterPath = priorWh.filterPath;
  387. wh.filterDir = priorWh.filterDir;
  388. }
  389. // evaluate what is at the path we're being asked to watch
  390. fs[wh.statMethod](wh.watchPath, function(error, stats) {
  391. if (this._handleError(error)) return callback(null, path);
  392. if (this._isIgnored(wh.watchPath, stats)) {
  393. ready();
  394. return callback(null, false);
  395. }
  396. var initDir = function(dir, target) {
  397. return this._handleDir(dir, stats, initialAdd, depth, target, wh, ready);
  398. }.bind(this);
  399. var closer;
  400. if (stats.isDirectory()) {
  401. closer = initDir(wh.watchPath, target);
  402. } else if (stats.isSymbolicLink()) {
  403. var parent = sysPath.dirname(wh.watchPath);
  404. this._getWatchedDir(parent).add(wh.watchPath);
  405. this._emit('add', wh.watchPath, stats);
  406. closer = initDir(parent, path);
  407. // preserve this symlink's target path
  408. fs.realpath(path, function(error, targetPath) {
  409. this._symlinkPaths[sysPath.resolve(path)] = targetPath;
  410. ready();
  411. }.bind(this));
  412. } else {
  413. closer = this._handleFile(wh.watchPath, stats, initialAdd, ready);
  414. }
  415. if (closer) this._closers[path] = closer;
  416. callback(null, false);
  417. }.bind(this));
  418. };
  419. module.exports = NodeFsHandler;