Skip to content

Commit 34bd27c

Browse files
Devin Nakamurabrendanashworth
authored andcommitted
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: nodejs/node-v0.x-archive#25469 Fixes: #1745
1 parent b0990ef commit 34bd27c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ function StatWatcher() {
12781278

12791279
this._handle.onchange = function(current, previous, newStatus) {
12801280
if (oldStatus === -1 &&
1281-
newStatus === -1 &&
1281+
newStatus < 0 &&
12821282
current.nlink === previous.nlink) return;
12831283

12841284
oldStatus = newStatus;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
5+
var filename = '/path/to/file/that/does/not/exist';
6+
7+
fs.watchFile(filename, function() {
8+
throw new Error('callback should not be called for non-existent files');
9+
});
10+
11+
setTimeout(function() {
12+
fs.unwatchFile(filename);
13+
}, 10);

0 commit comments

Comments
 (0)