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

PR-URL: nodejs#18618
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bzoz authored and MayaLekova committed May 8, 2018
1 parent 5426143 commit 89dc53d
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 @@ -234,14 +234,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 @@ -260,13 +256,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 89dc53d

Please sign in to comment.