Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Apply the code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
errendir committed Sep 8, 2014
1 parent 1ee4169 commit a33fa92
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
20 changes: 14 additions & 6 deletions doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ site, set the NODE_DEBUG environment variable:
at Object.<anonymous> (/path/to/script.js:5:1)
<etc.>

## fs.CreateConfiguredFSObject(options)

Creates and returns a new fs object with the specified options set. The valid options
are:
* `throwSafe` to prevent the error creation when the non-existent file is stat-ed


## fs.rename(oldPath, newPath, callback)

Expand Down Expand Up @@ -168,14 +174,15 @@ Only available on Mac OS X.

Synchronous lchmod(2).

## fs.stat(path, callback, [throwSafe])
## fs.stat(path, callback)

Asynchronous stat(2). The callback gets two arguments `(err, stats)` where
`stats` is a [fs.Stats](#fs_class_fs_stats) object. See the [fs.Stats](#fs_class_fs_stats)
section below for more information.

Setting the throwSafe parameter to true prevents the error creation. If the call
fails, the first parameter of the callback will be true.
Setting the throwSafe fs option to true (see [fs.CreateConfiguredFSObject](#fs_class_fs_stats))
prevents the error creation. If the call fails, the first parameter of the callback will be true.
This does not prevent the errors when the path contains the null character.

## fs.lstat(path, callback)

Expand All @@ -190,12 +197,13 @@ Asynchronous fstat(2). The callback gets two arguments `(err, stats)` where
`stats` is a `fs.Stats` object. `fstat()` is identical to `stat()`, except that
the file to be stat-ed is specified by the file descriptor `fd`.

## fs.statSync(path, [throwSafe])
## fs.statSync(path)

Synchronous stat(2). Returns an instance of `fs.Stats`.

Setting the throwSafe parameter to true prevents the error creation. If the call
fails `fs.statSync` returns `false`.
Setting the throwSafe fs option to true (see [fs.CreateConfiguredFSObject](#fs_class_fs_stats))
prevents the error creation. If the call fails `fs.statSync` returns `false`.
This does not prevent the errors when the path contains the null character.

## fs.lstatSync(path)

Expand Down
40 changes: 14 additions & 26 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ var CreateConfiguredFSObject = function(options) {
}
}

function nullCheck(path, callback, throwSafe) {
if (typeof throwSafe === 'undefined')
throwSafe = fs.options.throwSafe;
function simpleNullCheck(path) {
if (('' + path).indexOf('\u0000') !== -1) {
return false;
}
return true;
}

function nullCheck(path, callback) {
if (('' + path).indexOf('\u0000') !== -1) {
if (throwSafe)
return false;
var er = new Error('Path must be a string without null bytes.');
if (!callback)
throw er;
Expand Down Expand Up @@ -199,25 +201,20 @@ var CreateConfiguredFSObject = function(options) {
};

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

fs.existsSync = function(path) {
if (!nullCheck(path, undefined, true))
if (!simpleNullCheck(path))
return false;
if (!binding.stat(pathModule._makeLong(path), undefined, true))
return false;
Expand Down Expand Up @@ -732,14 +729,7 @@ var CreateConfiguredFSObject = function(options) {
var throwSafe = fs.options.throwSafe;

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

Expand All @@ -755,9 +745,7 @@ var CreateConfiguredFSObject = function(options) {
fs.statSync = function(path) {
var throwSafe = fs.options.throwSafe;

if (!nullCheck(path))
return false;

nullCheck(path);
return binding.stat(pathModule._makeLong(path), undefined, throwSafe);
};

Expand Down
6 changes: 5 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ var _fs = fs.CreateConfiguredFSObject({ throwSafe: true });
function statPath(path) {
var fs = NativeModule.require('fs');

return _fs.statSync(path, true);
try {
return _fs.statSync(path, true);
} catch(err) {
return false;
}
}

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

0 comments on commit a33fa92

Please sign in to comment.