Skip to content

Commit d3955d1

Browse files
committed
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: #17921 PR-URL: #18618 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b1e52fe commit d3955d1

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed

lib/fs.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,10 @@ fs.exists = function(path, callback) {
234234
}
235235

236236
try {
237-
path = getPathFromURL(path);
238-
validatePath(path);
237+
fs.access(path, fs.FS_OK, suppressedCallback);
239238
} catch (err) {
240239
return callback(false);
241240
}
242-
var req = new FSReqWrap();
243-
req.oncomplete = suppressedCallback;
244-
binding.stat(pathModule.toNamespacedPath(path), req);
245241
};
246242

247243
Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
@@ -260,13 +256,7 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
260256
// TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior
261257
fs.existsSync = function(path) {
262258
try {
263-
path = getPathFromURL(path);
264-
validatePath(path);
265-
const ctx = { path };
266-
binding.stat(pathModule.toNamespacedPath(path), undefined, ctx);
267-
if (ctx.errno !== undefined) {
268-
return false;
269-
}
259+
fs.accessSync(path, fs.FS_OK);
270260
return true;
271261
} catch (e) {
272262
return false;

0 commit comments

Comments
 (0)