@@ -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+
119137static 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 ) \
@@ -1618,6 +1636,24 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
16181636 }
16191637}
16201638
1639+ static void FdatasyncSync (const FunctionCallbackInfo<Value>& args) {
1640+ Environment* env = Environment::GetCurrent (args);
1641+
1642+ CHECK_EQ (args.Length (), 1 );
1643+
1644+ const int fd = GetValidatedFd (env, args[0 ]);
1645+ if (fd == (1 << 30 )) return ;
1646+
1647+ uv_fs_t req;
1648+ auto make = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
1649+ FS_SYNC_TRACE_BEGIN (fdatasync);
1650+ int err = uv_fs_fdatasync (nullptr , &req, fd, nullptr );
1651+ FS_SYNC_TRACE_END (fdatasync);
1652+ if (err < 0 ) {
1653+ return env->ThrowUVException (err, " fdatasync" );
1654+ }
1655+ }
1656+
16211657static void Fsync (const FunctionCallbackInfo<Value>& args) {
16221658 Environment* env = Environment::GetCurrent (args);
16231659
@@ -3393,6 +3429,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
33933429 SetMethod (isolate, target, " readFileUtf8" , ReadFileUtf8);
33943430 SetMethod (isolate, target, " readBuffers" , ReadBuffers);
33953431 SetMethod (isolate, target, " fdatasync" , Fdatasync);
3432+ SetMethod (isolate, target, " fdatasyncSync" , FdatasyncSync);
33963433 SetMethod (isolate, target, " fsync" , Fsync);
33973434 SetMethod (isolate, target, " rename" , Rename);
33983435 SetMethod (isolate, target, " ftruncate" , FTruncate);
@@ -3519,6 +3556,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
35193556 registry->Register (ReadFileUtf8);
35203557 registry->Register (ReadBuffers);
35213558 registry->Register (Fdatasync);
3559+ registry->Register (FdatasyncSync);
35223560 registry->Register (Fsync);
35233561 registry->Register (Rename);
35243562 registry->Register (FTruncate);
0 commit comments