Skip to content

Commit

Permalink
fs: use throwIfNoEntry option on statSync calls
Browse files Browse the repository at this point in the history
PR-URL: #36975
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Feb 2, 2021
1 parent 4a1fc42 commit 175f6f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
10 changes: 4 additions & 6 deletions lib/fs.js
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions lib/internal/fs/rimraf.js
Expand Up @@ -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
Expand Down
17 changes: 5 additions & 12 deletions lib/internal/fs/utils.js
Expand Up @@ -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',
Expand All @@ -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;
Expand Down

0 comments on commit 175f6f0

Please sign in to comment.