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 #include "flush.h"
16
17 #include <memory>
18 #include <tuple>
19
20 #include "../../common/napi/n_async/n_async_work_callback.h"
21 #include "../../common/napi/n_async/n_async_work_promise.h"
22 #include "../../common/napi/n_class.h"
23 #include "../../common/napi/n_func_arg.h"
24 #include "../../common/napi/n_val.h"
25 #include "../../common/uni_error.h"
26
27 #include "../class_stat/stat_entity.h"
28 #include "../class_stat/stat_n_exporter.h"
29
30 namespace OHOS {
31 namespace DistributedFS {
32 namespace ModuleFileIO {
33 using namespace std;
34
35 struct StreamEntity {
36 std::unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose };
37 };
38
Sync(napi_env env,napi_callback_info info)39 napi_value Flush::Sync(napi_env env, napi_callback_info info)
40 {
41 NFuncArg funcArg(env, info);
42 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
43 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
44 return nullptr;
45 }
46
47 auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
48 if (!streamEntity || !streamEntity->fp) {
49 UniError(EBADF).ThrowErr(env, "Stream may has been closed");
50 return nullptr;
51 }
52
53 int ret = fflush(streamEntity->fp.get());
54 if (ret == -1) {
55 UniError(errno).ThrowErr(env);
56 return nullptr;
57 }
58 return NVal::CreateUndefined(env).val_;
59 }
60
Async(napi_env env,napi_callback_info info)61 napi_value Flush::Async(napi_env env, napi_callback_info info)
62 {
63 NFuncArg funcArg(env, info);
64 if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
65 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
66 return nullptr;
67 }
68
69 auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
70 if (!streamEntity || !streamEntity->fp) {
71 UniError(EBADF).ThrowErr(env, "Stream may has been closed");
72 return nullptr;
73 }
74
75 auto cbExec = [streamEntity](napi_env env) -> UniError {
76 int ret = fflush(streamEntity->fp.get());
77 if (ret == -1) {
78 return UniError(errno);
79 } else {
80 return UniError(ERRNO_NOERR);
81 }
82 };
83 auto cbCompl = [](napi_env env, UniError err) -> NVal {
84 if (err) {
85 return { env, err.GetNapiErr(env) };
86 }
87 return { NVal::CreateUndefined(env) };
88 };
89
90 NVal thisVar(env, funcArg.GetThisVar());
91 string procedureName = "fileIOFlush";
92 if (funcArg.GetArgc() == NARG_CNT::ZERO) {
93 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
94 } else {
95 NVal cb(env, funcArg[NARG_POS::FIRST]);
96 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
97 }
98 }
99 } // namespace ModuleFileIO
100 } // namespace DistributedFS
101 } // namespace OHOS