From cb03f5ca925f9547358abe6dbcac8b7b31dfec73 Mon Sep 17 00:00:00 2001 From: Bartosz Sosnowski Date: Mon, 12 Feb 2018 13:03:05 +0100 Subject: [PATCH] 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 # Conflicts: # lib/fs.js --- lib/fs.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index da718f3f334413..6853bc4b1625a5 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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, { @@ -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;