fs: use fs.access in fs.exists

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: https://github.com/nodejs/node/issues/17921

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Bartosz Sosnowski 2018-02-12 13:03:05 +01:00
parent b1e52fe2ea
commit d3955d15ff

View File

@ -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, {
@ -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;