From 722ffe425e86e87c6fee10ddbde70d76853e75f2 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Wed, 20 Mar 2024 19:50:30 +0800 Subject: [PATCH] src: use S_ISDIR to check if the file is dir --- src/node_file.cc | 4 ++-- src/permission/fs_permission.cc | 2 +- test/parallel/test-fs-readdir-recursive.js | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-fs-readdir-recursive.js diff --git a/src/node_file.cc b/src/node_file.cc index 70c37d7f7ddad7..8b9a3b481d9a97 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1049,7 +1049,7 @@ static void InternalModuleStat(const FunctionCallbackInfo& args) { int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr); if (rc == 0) { const uv_stat_t* const s = static_cast(req.ptr); - rc = !!(s->st_mode & S_IFDIR); + rc = S_ISDIR(s->st_mode); } uv_fs_req_cleanup(&req); @@ -2854,7 +2854,7 @@ BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile( if (rc == 0) { const uv_stat_t* const s = static_cast(req.ptr); - rc = !!(s->st_mode & S_IFDIR); + rc = S_ISDIR(s->st_mode); } uv_fs_req_cleanup(&req); diff --git a/src/permission/fs_permission.cc b/src/permission/fs_permission.cc index 0c881fa266d23d..745ddd96c089ed 100644 --- a/src/permission/fs_permission.cc +++ b/src/permission/fs_permission.cc @@ -20,7 +20,7 @@ std::string WildcardIfDir(const std::string& res) noexcept { int rc = uv_fs_stat(nullptr, &req, res.c_str(), nullptr); if (rc == 0) { const uv_stat_t* const s = static_cast(req.ptr); - if (s->st_mode & S_IFDIR) { + if ((s->st_mode & S_IFMT) == S_IFDIR) { // add wildcard when directory if (res.back() == node::kPathSeparator) { return res + "*"; diff --git a/test/parallel/test-fs-readdir-recursive.js b/test/parallel/test-fs-readdir-recursive.js new file mode 100644 index 00000000000000..c6e3c8f9443d8d --- /dev/null +++ b/test/parallel/test-fs-readdir-recursive.js @@ -0,0 +1,13 @@ +'use strict'; +const common = require('../common'); +const fs = require('fs'); +const net = require('net'); + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const server = net.createServer().listen(common.PIPE, common.mustCall(() => { + // See https://github.com/nodejs/node/issues/52159 + fs.readdirSync(tmpdir.path, { recursive: true }); + server.close(); +}));