fs: handle UV_ENOTDIR in fs.statSync with throwIfNoEntry provided

Fixes: https://github.com/nodejs/node/issues/56993
Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/56996
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Juan José Arboleda 2025-02-10 12:14:36 -05:00 committed by James M Snell
parent 61a57f7761
commit 409e28d5aa
2 changed files with 14 additions and 2 deletions

View File

@ -1088,6 +1088,10 @@ constexpr bool is_uv_error_except_no_entry(int result) {
return result < 0 && result != UV_ENOENT;
}
constexpr bool is_uv_error_except_no_entry_dir(int result) {
return result < 0 && !(result == UV_ENOENT || result == UV_ENOTDIR);
}
static void Stat(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();
@ -1121,8 +1125,11 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
FS_SYNC_TRACE_BEGIN(stat);
int result;
if (do_not_throw_if_no_entry) {
result = SyncCallAndThrowIf(
is_uv_error_except_no_entry, env, &req_wrap_sync, uv_fs_stat, *path);
result = SyncCallAndThrowIf(is_uv_error_except_no_entry_dir,
env,
&req_wrap_sync,
uv_fs_stat,
*path);
} else {
result = SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_stat, *path);
}

View File

@ -221,3 +221,8 @@ fs.lstat(__filename, undefined, common.mustCall());
},
);
}
{
// Test that the throwIfNoEntry option works and returns undefined
assert.ok(!(fs.statSync('./wont_exists', { throwIfNoEntry: false })));
}