Skip to content

Commit 70a8931

Browse files
author
pluris
committed
fs: improve error performance for fdatasyncSync
1 parent 47b2883 commit 70a8931

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

lib/fs.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,10 +1303,7 @@ function fdatasync(fd, callback) {
13031303
* @returns {void}
13041304
*/
13051305
function fdatasyncSync(fd) {
1306-
fd = getValidatedFd(fd);
1307-
const ctx = {};
1308-
binding.fdatasync(fd, undefined, ctx);
1309-
handleErrorFromBinding(ctx);
1306+
syncFs.fdatasync(fd);
13101307
}
13111308

13121309
/**

src/node_file.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ inline int64_t GetOffset(Local<Value> value) {
116116
return IsSafeJsInt(value) ? value.As<Integer>()->Value() : -1;
117117
}
118118

119+
inline int GetValidatedFd(Environment* env, Local<Value> value) {
120+
if (!value->IsInt32()) {
121+
env->isolate()->ThrowException(ERR_INVALID_ARG_TYPE(
122+
env->isolate(), "Invalid argument. The fd must be int32."));
123+
return 1 << 30;
124+
}
125+
126+
const int fd = value.As<Int32>()->Value();
127+
128+
if (fd < 0 || fd > INT32_MAX) {
129+
env->isolate()->ThrowException(ERR_OUT_OF_RANGE(
130+
env->isolate(), "It must be >= 0 && <= INT32_MAX. Received %d", fd));
131+
return 1 << 30;
132+
}
133+
134+
return fd;
135+
}
136+
119137
static const char* get_fs_func_name_by_type(uv_fs_type req_type) {
120138
switch (req_type) {
121139
#define FS_TYPE_TO_NAME(type, name) \
@@ -1520,6 +1538,24 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
15201538
}
15211539
}
15221540

1541+
static void FdatasyncSync(const FunctionCallbackInfo<Value>& args) {
1542+
Environment* env = Environment::GetCurrent(args);
1543+
1544+
CHECK_EQ(args.Length(), 1);
1545+
1546+
const int fd = GetValidatedFd(env, args[0]);
1547+
if (fd == (1 << 30)) return;
1548+
1549+
uv_fs_t req;
1550+
auto make = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
1551+
FS_SYNC_TRACE_BEGIN(fdatasync);
1552+
int err = uv_fs_fdatasync(nullptr, &req, fd, nullptr);
1553+
FS_SYNC_TRACE_END(fdatasync);
1554+
if (err < 0) {
1555+
return env->ThrowUVException(err, "fdatasync");
1556+
}
1557+
}
1558+
15231559
static void Fsync(const FunctionCallbackInfo<Value>& args) {
15241560
Environment* env = Environment::GetCurrent(args);
15251561

@@ -3218,6 +3254,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
32183254
SetMethod(isolate, target, "readFileUtf8", ReadFileUtf8);
32193255
SetMethod(isolate, target, "readBuffers", ReadBuffers);
32203256
SetMethod(isolate, target, "fdatasync", Fdatasync);
3257+
SetMethod(isolate, target, "fdatasyncSync", FdatasyncSync);
32213258
SetMethod(isolate, target, "fsync", Fsync);
32223259
SetMethod(isolate, target, "rename", Rename);
32233260
SetMethod(isolate, target, "ftruncate", FTruncate);
@@ -3337,6 +3374,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
33373374
registry->Register(ReadFileUtf8);
33383375
registry->Register(ReadBuffers);
33393376
registry->Register(Fdatasync);
3377+
registry->Register(FdatasyncSync);
33403378
registry->Register(Fsync);
33413379
registry->Register(Rename);
33423380
registry->Register(FTruncate);

typings/internalBinding/fs.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ declare namespace InternalFSBinding {
8484
function fdatasync(fd: number, req: FSReqCallback): void;
8585
function fdatasync(fd: number, req: undefined, ctx: FSSyncContext): void;
8686
function fdatasync(fd: number, usePromises: typeof kUsePromises): Promise<void>;
87+
function fdatasyncSync(fd: number): void;
8788

8889
function fstat(fd: number, useBigint: boolean, req: FSReqCallback<Float64Array | BigUint64Array>): void;
8990
function fstat(fd: number, useBigint: true, req: FSReqCallback<BigUint64Array>): void;

0 commit comments

Comments
 (0)