Skip to content

Commit 175f6f0

Browse files
aduh95targos
authored andcommitted
fs: use throwIfNoEntry option on statSync calls
PR-URL: #36975 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 4a1fc42 commit 175f6f0

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

lib/fs.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1215,12 +1215,10 @@ function symlink(target, path, type_, callback_) {
12151215
function symlinkSync(target, path, type) {
12161216
type = (typeof type === 'string' ? type : null);
12171217
if (isWindows && type === null) {
1218-
try {
1219-
const absoluteTarget = pathModule.resolve(path, '..', target);
1220-
if (statSync(absoluteTarget).isDirectory()) {
1221-
type = 'dir';
1222-
}
1223-
} catch { }
1218+
const absoluteTarget = pathModule.resolve(`${path}`, '..', `${target}`);
1219+
if (statSync(absoluteTarget, { throwIfNoEntry: false })?.isDirectory()) {
1220+
type = 'dir';
1221+
}
12241222
}
12251223
target = getValidatedPath(target, 'target');
12261224
path = getValidatedPath(path);

lib/internal/fs/rimraf.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,13 @@ function fixWinEPERMSync(path, options, originalErr) {
280280
let stats;
281281

282282
try {
283-
stats = statSync(path);
284-
} catch (err) {
285-
if (err.code === 'ENOENT')
286-
return;
287-
283+
stats = statSync(path, { throwIfNoEntry: false });
284+
} catch {
288285
throw originalErr;
289286
}
290287

288+
if (stats === undefined) return;
289+
291290
if (stats.isDirectory())
292291
_rmdirSync(path, options, originalErr);
293292
else

lib/internal/fs/utils.js

+5-12
Original file line numberDiff line numberDiff line change
@@ -720,14 +720,15 @@ const validateRmOptionsSync = hideStackFrames((path, options, warn) => {
720720
options = validateRmdirOptions(options, defaultRmOptions);
721721
validateBoolean(options.force, 'options.force');
722722

723-
try {
724-
const stats = lazyLoadFs().statSync(path);
723+
if (!options.force || warn || !options.recursive) {
724+
const isDirectory = lazyLoadFs()
725+
.statSync(path, { throwIfNoEntry: !options.force })?.isDirectory();
725726

726-
if (warn && !stats.isDirectory()) {
727+
if (warn && !isDirectory) {
727728
emitPermissiveRmdirWarning();
728729
}
729730

730-
if (stats.isDirectory() && !options.recursive) {
731+
if (isDirectory && !options.recursive) {
731732
throw new ERR_FS_EISDIR({
732733
code: 'EISDIR',
733734
message: 'is a directory',
@@ -736,14 +737,6 @@ const validateRmOptionsSync = hideStackFrames((path, options, warn) => {
736737
errno: EISDIR
737738
});
738739
}
739-
} catch (err) {
740-
if (err.code !== 'ENOENT') {
741-
throw err;
742-
} else if (err.code === 'ENOENT' && !options.force) {
743-
throw err;
744-
} else if (warn && err.code === 'ENOENT') {
745-
emitPermissiveRmdirWarning();
746-
}
747740
}
748741

749742
return options;

0 commit comments

Comments
 (0)