From 642db4892cd579928e448d4c76b48f991abf00a5 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 29 Sep 2023 13:13:11 -0400 Subject: [PATCH] fs: improve error performance of `lchownSync` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/49962 Refs: https://github.com/nodejs/performance/issues/106 Reviewed-By: Matteo Collina Reviewed-By: Stephen Belanger Reviewed-By: Vinícius Lourenço Claro Cardoso --- lib/fs.js | 8 +++++--- src/node_file.cc | 12 +++++------- typings/internalBinding/fs.d.ts | 1 + 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index f04f2d69330c2eb..d4bf1d80fc5c7d3 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2012,9 +2012,11 @@ function lchownSync(path, uid, gid) { path = getValidatedPath(path); validateInteger(uid, 'uid', -1, kMaxUserId); validateInteger(gid, 'gid', -1, kMaxUserId); - const ctx = { path }; - binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx); - handleErrorFromBinding(ctx); + binding.lchown( + pathModule.toNamespacedPath(path), + uid, + gid, + ); } /** diff --git a/src/node_file.cc b/src/node_file.cc index 50a3fd295521384..e892b8a8659f9c8 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2633,18 +2633,16 @@ static void LChown(const FunctionCallbackInfo& args) { CHECK(IsSafeJsInt(args[2])); const uv_gid_t gid = static_cast(args[2].As()->Value()); - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - if (req_wrap_async != nullptr) { // lchown(path, uid, gid, req) + if (argc > 3) { // lchown(path, uid, gid, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 3); FS_ASYNC_TRACE_BEGIN1( UV_FS_LCHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, req_wrap_async, args, "lchown", UTF8, AfterNoArgs, uv_fs_lchown, *path, uid, gid); - } else { // lchown(path, uid, gid, undefined, ctx) - CHECK_EQ(argc, 5); - FSReqWrapSync req_wrap_sync; + } else { // lchown(path, uid, gid) + FSReqWrapSync req_wrap_sync("lchown", *path); FS_SYNC_TRACE_BEGIN(lchown); - SyncCall(env, args[4], &req_wrap_sync, "lchown", - uv_fs_lchown, *path, uid, gid); + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_lchown, *path, uid, gid); FS_SYNC_TRACE_END(lchown); } } diff --git a/typings/internalBinding/fs.d.ts b/typings/internalBinding/fs.d.ts index 8aab4a3ae8d2422..f746ca582327d09 100644 --- a/typings/internalBinding/fs.d.ts +++ b/typings/internalBinding/fs.d.ts @@ -117,6 +117,7 @@ declare namespace InternalFSBinding { function lchown(path: string, uid: number, gid: number, req: FSReqCallback): void; function lchown(path: string, uid: number, gid: number, req: undefined, ctx: FSSyncContext): void; function lchown(path: string, uid: number, gid: number, usePromises: typeof kUsePromises): Promise; + function lchown(path: string, uid: number, gid: number): void; function link(existingPath: string, newPath: string, req: FSReqCallback): void; function link(existingPath: string, newPath: string, req: undefined, ctx: FSSyncContext): void;