From 2a0b1988682c6b4b1dc7c945db17bb1a9c207f57 Mon Sep 17 00:00:00 2001 From: Bartosz Sosnowski Date: Wed, 28 Mar 2018 10:56:43 +0200 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 PR-URL: https://github.com/nodejs/node/pull/18618 Reviewed-By: Colin Ihrig Reviewed-By: Ben Noordhuis Reviewed-By: Richard Lau Reviewed-By: Weijia Wang Reviewed-By: Joyee Cheung Reviewed-By: Ruben Bridgewater Reviewed-By: Evan Lucas Reviewed-By: James M Snell --- lib/fs.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 231303ec42c620..1aa27ebe4e7a35 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -323,12 +323,7 @@ fs.accessSync = function(path, mode) { }; fs.exists = function(path, callback) { - if (handleError((path = getPathFromURL(path)), cb)) - return; - if (!nullCheck(path, cb)) return; - var req = new FSReqWrap(); - req.oncomplete = cb; - binding.stat(pathModule.toNamespacedPath(path), req); + fs.access(path, fs.F_OK, cb); function cb(err) { if (callback) callback(err ? false : true); } @@ -345,9 +340,7 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, { fs.existsSync = function(path) { try { - handleError((path = getPathFromURL(path))); - nullCheck(path); - binding.stat(pathModule.toNamespacedPath(path)); + fs.accessSync(path, fs.F_OK); return true; } catch (e) { return false;