• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "fdatasync.h"
17 
18 #include <cstring>
19 #include <fcntl.h>
20 #include <tuple>
21 #include <unistd.h>
22 
23 #include <sys/stat.h>
24 
25 #include "n_async_work_callback.h"
26 #include "n_async_work_promise.h"
27 #include "n_func_arg.h"
28 
29 namespace OHOS {
30 namespace DistributedFS {
31 namespace ModuleFileIO {
32 using namespace std;
33 
Sync(napi_env env,napi_callback_info info)34 napi_value Fdatasync::Sync(napi_env env, napi_callback_info info)
35 {
36     NFuncArg funcArg(env, info);
37     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
38         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
39         return nullptr;
40     }
41 
42     auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
43     if (!resGetFirstArg) {
44         UniError(EINVAL).ThrowErr(env, "Invalid fd");
45         return nullptr;
46     }
47 
48     int ret = fdatasync(fd);
49     if (ret == -1) {
50         UniError(errno).ThrowErr(env);
51         return nullptr;
52     }
53 
54     return NVal::CreateUndefined(env).val_;
55 }
56 
57 
Async(napi_env env,napi_callback_info info)58 napi_value Fdatasync::Async(napi_env env, napi_callback_info info)
59 {
60     NFuncArg funcArg(env, info);
61     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
62         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
63         return nullptr;
64     }
65 
66     auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
67     if (!resGetFirstArg) {
68         UniError(EINVAL).ThrowErr(env, "Invalid fd");
69         return nullptr;
70     }
71 
72     auto cbExec = [fd = fd](napi_env env) -> UniError {
73         int ret = fdatasync(fd);
74         if (ret == -1) {
75             return UniError(errno);
76         } else {
77             return UniError(ERRNO_NOERR);
78         }
79     };
80 
81     auto cbComplCallback = [](napi_env env, UniError err) -> NVal {
82         if (err) {
83             return { env, err.GetNapiErr(env) };
84         }
85         return { NVal::CreateUndefined(env) };
86     };
87 
88     const string procedureName = "FileIOFdatasync";
89     NVal thisVar(env, funcArg.GetThisVar());
90     if (funcArg.GetArgc() == NARG_CNT::ONE) {
91         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
92     } else {
93         NVal cb(env, funcArg[NARG_POS::SECOND]);
94         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
95     }
96 }
97 } // namespace ModuleFileIO
98 } // namespace DistributedFS
99 } // namespace OHOS