• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fsync.h"
17 
18 #include <cstring>
19 #include <tuple>
20 #include <unistd.h>
21 
22 #include "../../common/napi/n_async/n_async_work_callback.h"
23 #include "../../common/napi/n_async/n_async_work_promise.h"
24 #include "../../common/napi/n_func_arg.h"
25 namespace OHOS {
26 namespace DistributedFS {
27 namespace ModuleFileIO {
28 using namespace std;
29 
Sync(napi_env env,napi_callback_info info)30 napi_value Fsync::Sync(napi_env env, napi_callback_info info)
31 {
32     NFuncArg funcArg(env, info);
33 
34     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
35         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
36         return nullptr;
37     }
38 
39     bool succ = false;
40     int fd;
41     tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
42     if (!succ) {
43         UniError(EINVAL).ThrowErr(env, "Invalid fd");
44         return nullptr;
45     }
46 
47     if (fsync(fd) == -1) {
48         UniError(errno).ThrowErr(env);
49         return nullptr;
50     }
51     return NVal::CreateUndefined(env).val_;
52 }
53 
Async(napi_env env,napi_callback_info info)54 napi_value Fsync::Async(napi_env env, napi_callback_info info)
55 {
56     NFuncArg funcArg(env, info);
57 
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     bool succ = false;
64     int fd;
65     tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
66     if (!succ) {
67         UniError(EINVAL).ThrowErr(env, "Invalid fd");
68         return nullptr;
69     }
70 
71     auto cbExec = [fd](napi_env env) -> UniError {
72         if (fsync(fd) == -1) {
73             return UniError(errno);
74         } else {
75             return UniError(ERRNO_NOERR);
76         }
77     };
78     auto cbComplete = [](napi_env env, UniError err) -> NVal {
79         if (err) {
80             return { env, err.GetNapiErr(env) };
81         } else {
82             return NVal::CreateUndefined(env);
83         }
84     };
85     string procedureName = "FileIOFsync";
86     size_t argc = funcArg.GetArgc();
87     NVal thisVar(env, funcArg.GetThisVar());
88     if (argc == NARG_CNT::ONE) {
89         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
90     } else {
91         NVal cb(env, funcArg[NARG_POS::SECOND]);
92         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
93     }
94 }
95 } // namespace ModuleFileIO
96 } // namespace DistributedFS
97 } // namespace OHOS
98