From 19748b832fa30e4ddb0ca55b855d2bf9cbc84eaf Mon Sep 17 00:00:00 2001 From: pluris Date: Wed, 27 Sep 2023 01:00:26 +0900 Subject: [PATCH] fixup! fs: improve error performance for `fsyncSync` --- benchmark/fs/bench-fsyncSync.js | 6 +++--- lib/internal/fs/sync.js | 1 - src/node_file.cc | 4 +++- typings/internalBinding/fs.d.ts | 8 +++++--- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/benchmark/fs/bench-fsyncSync.js b/benchmark/fs/bench-fsyncSync.js index 0346230b72a605..fc7ef29266790b 100644 --- a/benchmark/fs/bench-fsyncSync.js +++ b/benchmark/fs/bench-fsyncSync.js @@ -21,7 +21,7 @@ function main({ n, type }) { fd = fs.openSync(tmpfile, 'r', 0o666); break; case 'non-existing': - fd = 0; + fd = 1 << 30; break; default: new Error('Invalid type'); @@ -36,7 +36,7 @@ function main({ n, type }) { } } - if (fd !== 0) fs.closeSync(fd); - bench.end(n); + + if (type === 'existing') fs.closeSync(fd); } diff --git a/lib/internal/fs/sync.js b/lib/internal/fs/sync.js index cc3ee002abbc12..56f82fd48426c6 100644 --- a/lib/internal/fs/sync.js +++ b/lib/internal/fs/sync.js @@ -89,7 +89,6 @@ function close(fd) { } function fsync(fd) { - fd = getValidatedFd(fd); return binding.fsyncSync(fd); } diff --git a/src/node_file.cc b/src/node_file.cc index 238ab53dea8f9e..3c1ab254e23258 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1648,14 +1648,16 @@ static void FsyncSync(const FunctionCallbackInfo& args) { CHECK_GE(argc, 1); CHECK(args[0]->IsInt32()); + const int fd = args[0].As()->Value(); + CHECK_GE(fd, 0); uv_fs_t req; FS_SYNC_TRACE_BEGIN(fsync); int err = uv_fs_fsync(nullptr, &req, fd, nullptr); FS_SYNC_TRACE_END(fsync); if (err < 0) { - return env->ThrowUVException(err, "fsync", nullptr); + return env->ThrowUVException(err, "fsync"); } } diff --git a/typings/internalBinding/fs.d.ts b/typings/internalBinding/fs.d.ts index d87b326e42efc8..a4ea9987cd8198 100644 --- a/typings/internalBinding/fs.d.ts +++ b/typings/internalBinding/fs.d.ts @@ -94,10 +94,11 @@ declare namespace InternalFSBinding { function fstat(fd: number, useBigint: boolean, usePromises: typeof kUsePromises): Promise; function fstat(fd: number, useBigint: true, usePromises: typeof kUsePromises): Promise; function fstat(fd: number, useBigint: false, usePromises: typeof kUsePromises): Promise; + function fsync(fd: number): void; - function fsync(fd: number, req: FSReqCallback): void; - function fsync(fd: number, req: undefined, ctx: FSSyncContext): void; - function fsync(fd: number, usePromises: typeof kUsePromises): Promise; + function fsyncSync(fd: number, req: FSReqCallback): void; + function fsyncSync(fd: number, req: undefined, ctx: FSSyncContext): void; + function fsyncSync(fd: number, usePromises: typeof kUsePromises): Promise; function ftruncate(fd: number, len: number, req: FSReqCallback): void; function ftruncate(fd: number, len: number, req: undefined, ctx: FSSyncContext): void; @@ -243,6 +244,7 @@ export interface FsBinding { fdatasync: typeof InternalFSBinding.fdatasync; fstat: typeof InternalFSBinding.fstat; fsync: typeof InternalFSBinding.fsync; + fsyncSync: typeof InternalFSBinding.fsyncSync; ftruncate: typeof InternalFSBinding.ftruncate; futimes: typeof InternalFSBinding.futimes; internalModuleReadJSON: typeof InternalFSBinding.internalModuleReadJSON;