fs: handle result of access binding directly in fs.existsSync

Instead of throwing errors in fs.accessSync and then catching it,
handle the result from the binding directly in fs.existsSync.

Note that the argument validation errors still needs to be caught
until we properly deprecate the don't-thrown-on-invalid-arguments
behavior.

PR-URL: https://github.com/nodejs/node/pull/24015
Fixes: https://github.com/nodejs/node/issues/24008
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
Joyee Cheung 2018-11-01 18:04:57 +08:00 committed by Rich Trott
parent 616fac9169
commit bcc7b7a58c

View File

@ -228,11 +228,14 @@ Object.defineProperty(exists, internalUtil.promisify.custom, {
// TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior
function existsSync(path) {
try {
fs.accessSync(path, F_OK);
return true;
path = toPathIfFileURL(path);
validatePath(path);
} catch (e) {
return false;
}
const ctx = { path };
binding.access(pathModule.toNamespacedPath(path), F_OK, undefined, ctx);
return ctx.errno === undefined;
}
function readFileAfterOpen(err, fd) {