Skip to content

Commit

Permalink
fs.stat third parameter controlling error creation
Browse files Browse the repository at this point in the history
In some environment (especially when coffescript is involved) module.js
will spend a lot of time looking up nonexisting files. This is caused by the
lengthy error creation. Those errors are then immediately caught.

This change makes the lookup much faster, by avoiding the unnecessary error
creation.

module::statPath now uses the new second parameter of fs.statSync function of the
fs.js. There is a similar change for the fs.stat, adding the third parameter.
  • Loading branch information
errendir authored and Albert Siddhartha Slawinski committed Aug 19, 2014
1 parent 6a11bfe commit 92bdc6a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 86 deletions.
1 change: 1 addition & 0 deletions deps/uv/include/uv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,7 @@ struct uv_fs_s {
uv_fs_type fs_type;
uv_loop_t* loop;
uv_fs_cb cb;
int throw_safe;
ssize_t result;
void* ptr;
const char* path;
Expand Down
63 changes: 39 additions & 24 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ function assertEncoding(encoding) {
}
}

function nullCheck(path, callback) {
function nullCheck(path, callback, throw_safe) {
if (('' + path).indexOf('\u0000') !== -1) {
var er = new Error('Path must be a string without null bytes.');
if (!callback)
throw er;
process.nextTick(function() {
callback(er);
});
if (!throw_safe) {
var er = new Error('Path must be a string without null bytes.');
if (!callback)
throw er;
process.nextTick(function() {
callback(er);
});
}
return false;
}
return true;
Expand Down Expand Up @@ -162,21 +164,25 @@ fs.Stats.prototype.isSocket = function() {
};

fs.exists = function(path, callback) {
if (!nullCheck(path, cb)) return;
binding.stat(pathModule._makeLong(path), cb);
function cb(err, stats) {
if (callback) callback(err ? false : true);
if (!nullCheck(path, undefined, true)) {
cb(true, false);
return;
}
binding.stat(pathModule._makeLong(path), cb, true);
function cb(err, result) {
if (callback) {
if (err) {
callback(false);
} else {
callback(!!result);
}
}
}
};

fs.existsSync = function(path) {
try {
nullCheck(path);
binding.stat(pathModule._makeLong(path));
return true;
} catch (e) {
return false;
}
return (nullCheck(path, undefined, true)) &&
(!!binding.stat(pathModule._makeLong(path), undefined, true));
};

fs.readFile = function(path, options, callback_) {
Expand Down Expand Up @@ -675,10 +681,17 @@ fs.lstat = function(path, callback) {
binding.lstat(pathModule._makeLong(path), callback);
};

fs.stat = function(path, callback) {
fs.stat = function(path, callback, throw_safe) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
binding.stat(pathModule._makeLong(path), callback);
if (!nullCheck(path, callback, throw_safe)) {
if (throw_safe) {
process.nextTick(function() {
callback(true, false);
});
}
return;
}
binding.stat(pathModule._makeLong(path), callback, throw_safe);
};

fs.fstatSync = function(fd) {
Expand All @@ -690,9 +703,11 @@ fs.lstatSync = function(path) {
return binding.lstat(pathModule._makeLong(path));
};

fs.statSync = function(path) {
nullCheck(path);
return binding.stat(pathModule._makeLong(path));
fs.statSync = function(path, throw_safe) {
if (!nullCheck(path, undefined, throw_safe)) {
return false;
}
return binding.stat(pathModule._makeLong(path), undefined, throw_safe);
};

fs.readlink = function(path, callback) {
Expand Down
6 changes: 2 additions & 4 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ var debug = Module._debug;

function statPath(path) {
var fs = NativeModule.require('fs');
try {
return fs.statSync(path);
} catch (ex) {}
return false;

return fs.statSync(path, true);
}

// check if the directory is a package.json dir
Expand Down
Loading

0 comments on commit 92bdc6a

Please sign in to comment.