From 175f6f0be387e8db5dd675d128d9e0385fbefc20 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 17 Jan 2021 12:48:58 +0100 Subject: [PATCH] fs: use throwIfNoEntry option on statSync calls PR-URL: https://github.com/nodejs/node/pull/36975 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/fs.js | 10 ++++------ lib/internal/fs/rimraf.js | 9 ++++----- lib/internal/fs/utils.js | 17 +++++------------ 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 1bde6c81de5404..096b54d0c11cc7 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1215,12 +1215,10 @@ function symlink(target, path, type_, callback_) { function symlinkSync(target, path, type) { type = (typeof type === 'string' ? type : null); if (isWindows && type === null) { - try { - const absoluteTarget = pathModule.resolve(path, '..', target); - if (statSync(absoluteTarget).isDirectory()) { - type = 'dir'; - } - } catch { } + const absoluteTarget = pathModule.resolve(`${path}`, '..', `${target}`); + if (statSync(absoluteTarget, { throwIfNoEntry: false })?.isDirectory()) { + type = 'dir'; + } } target = getValidatedPath(target, 'target'); path = getValidatedPath(path); diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index 758cdcb1aa3548..675c2448c4568c 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -280,14 +280,13 @@ function fixWinEPERMSync(path, options, originalErr) { let stats; try { - stats = statSync(path); - } catch (err) { - if (err.code === 'ENOENT') - return; - + stats = statSync(path, { throwIfNoEntry: false }); + } catch { throw originalErr; } + if (stats === undefined) return; + if (stats.isDirectory()) _rmdirSync(path, options, originalErr); else diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 2353fd8c3cbcf2..3b4ef8c423712e 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -720,14 +720,15 @@ const validateRmOptionsSync = hideStackFrames((path, options, warn) => { options = validateRmdirOptions(options, defaultRmOptions); validateBoolean(options.force, 'options.force'); - try { - const stats = lazyLoadFs().statSync(path); + if (!options.force || warn || !options.recursive) { + const isDirectory = lazyLoadFs() + .statSync(path, { throwIfNoEntry: !options.force })?.isDirectory(); - if (warn && !stats.isDirectory()) { + if (warn && !isDirectory) { emitPermissiveRmdirWarning(); } - if (stats.isDirectory() && !options.recursive) { + if (isDirectory && !options.recursive) { throw new ERR_FS_EISDIR({ code: 'EISDIR', message: 'is a directory', @@ -736,14 +737,6 @@ const validateRmOptionsSync = hideStackFrames((path, options, warn) => { errno: EISDIR }); } - } catch (err) { - if (err.code !== 'ENOENT') { - throw err; - } else if (err.code === 'ENOENT' && !options.force) { - throw err; - } else if (warn && err.code === 'ENOENT') { - emitPermissiveRmdirWarning(); - } } return options;