Skip to content

Commit

Permalink
fs: fix StatWatcher to handle error codes
Browse files Browse the repository at this point in the history
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: nodejs/node-v0.x-archive#25469
Fixes: nodejs#1745
  • Loading branch information
dnakamura authored and brendanashworth committed Jun 22, 2015
1 parent b0990ef commit 34bd27c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-fs-watch-file-nonexistent.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 34bd27c

Please sign in to comment.