Skip to content

Commit

Permalink
fs: use fs.access in fs.exists
Browse files Browse the repository at this point in the history
Uses fs.access to implement fs.exists functionality. Fixes a issue,
when a file exists but user does not have privileges to do stat on the
file.

Fixes: nodejs#17921

# Conflicts:
#	lib/fs.js
  • Loading branch information
bzoz committed Feb 12, 2018
1 parent bd4773a commit cb03f5c
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,10 @@ fs.exists = function(path, callback) {
}

try {
path = getPathFromURL(path);
validatePath(path);
fs.access(path, fs.FS_OK, suppressedCallback);
} catch (err) {
return callback(false);
}
var req = new FSReqWrap();
req.oncomplete = suppressedCallback;
binding.stat(pathModule.toNamespacedPath(path), req);
};

Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
Expand All @@ -453,13 +449,7 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
// TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior
fs.existsSync = function(path) {
try {
path = getPathFromURL(path);
validatePath(path);
const ctx = { path };
binding.stat(pathModule.toNamespacedPath(path), undefined, ctx);
if (ctx.errno !== undefined) {
return false;
}
fs.accessSync(path, fs.FS_OK);
return true;
} catch (e) {
return false;
Expand Down

0 comments on commit cb03f5c

Please sign in to comment.