1 /*
2 * Copyright (c) 2021 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 #include <cstring>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <tuple>
21 #include <unistd.h>
22
23 #include "../../common/napi/n_async/n_async_work_callback.h"
24 #include "../../common/napi/n_async/n_async_work_promise.h"
25 #include "../../common/napi/n_func_arg.h"
26 namespace OHOS {
27 namespace DistributedFS {
28 namespace ModuleFileIO {
29 using namespace std;
30
Sync(napi_env env,napi_callback_info info)31 napi_value Fdatasync::Sync(napi_env env, napi_callback_info info)
32 {
33 NFuncArg funcArg(env, info);
34 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
35 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
36 return nullptr;
37 }
38
39 auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
40 if (!resGetFirstArg) {
41 UniError(EINVAL).ThrowErr(env, "Invalid fd");
42 return nullptr;
43 }
44
45 int ret = fdatasync(fd);
46 if (ret == -1) {
47 UniError(errno).ThrowErr(env);
48 return nullptr;
49 }
50
51 return NVal::CreateUndefined(env).val_;
52 }
53
54
Async(napi_env env,napi_callback_info info)55 napi_value Fdatasync::Async(napi_env env, napi_callback_info info)
56 {
57 NFuncArg funcArg(env, info);
58 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
59 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
60 return nullptr;
61 }
62
63 auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
64 if (!resGetFirstArg) {
65 UniError(EINVAL).ThrowErr(env, "Invalid fd");
66 return nullptr;
67 }
68
69 auto cbExec = [fd = fd](napi_env env) -> UniError {
70 int ret = fdatasync(fd);
71 if (ret == -1) {
72 return UniError(errno);
73 } else {
74 return UniError(ERRNO_NOERR);
75 }
76 };
77
78 auto cbComplCallback = [](napi_env env, UniError err) -> NVal {
79 if (err) {
80 return { env, err.GetNapiErr(env) };
81 }
82 return { NVal::CreateUndefined(env) };
83 };
84
85 const string procedureName = "FileIOFdatasync";
86 NVal thisVar(env, funcArg.GetThisVar());
87 if (funcArg.GetArgc() == NARG_CNT::ONE) {
88 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
89 } else {
90 NVal cb(env, funcArg[NARG_POS::SECOND]);
91 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
92 }
93 }
94 } // namespace ModuleFileIO
95 } // namespace DistributedFS
96 } // namespace OHOS