From 34bd27c045f21c27ceadbd9f3d570524b426ebf4 Mon Sep 17 00:00:00 2001 From: Devin Nakamura Date: Mon, 1 Jun 2015 11:57:44 -0400 Subject: [PATCH] fs: fix StatWatcher to handle error codes Previously, fs.watchFile() would call the callback even if the file does not exist. This is erroneous and is fixed by correctly handling the error code provided. Ref: https://github.com/joyent/node/pull/25469 Fixes: https://github.com/nodejs/io.js/issues/1745 --- lib/fs.js | 2 +- test/parallel/test-fs-watch-file-nonexistent.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-fs-watch-file-nonexistent.js diff --git a/lib/fs.js b/lib/fs.js index 58704e529747b2..e0a6ad32fea9ca 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1278,7 +1278,7 @@ function StatWatcher() { this._handle.onchange = function(current, previous, newStatus) { if (oldStatus === -1 && - newStatus === -1 && + newStatus < 0 && current.nlink === previous.nlink) return; oldStatus = newStatus; diff --git a/test/parallel/test-fs-watch-file-nonexistent.js b/test/parallel/test-fs-watch-file-nonexistent.js new file mode 100644 index 00000000000000..cd45056c63fd57 --- /dev/null +++ b/test/parallel/test-fs-watch-file-nonexistent.js @@ -0,0 +1,13 @@ +'use strict'; + +var fs = require('fs'); + +var filename = '/path/to/file/that/does/not/exist'; + +fs.watchFile(filename, function() { + throw new Error('callback should not be called for non-existent files'); +}); + +setTimeout(function() { + fs.unwatchFile(filename); +}, 10);