1 /*
2 * Copyright (c) 2023 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 "class_stat/stat_entity.h"
21 #include "class_stat/stat_n_exporter.h"
22 #include "filemgmt_libhilog.h"
23 #include "stream_entity.h"
24
25 namespace OHOS {
26 namespace FileManagement {
27 namespace ModuleFileIO {
28 using namespace std;
29 using namespace OHOS::FileManagement::LibN;
30
Sync(napi_env env,napi_callback_info info)31 napi_value Flush::Sync(napi_env env, napi_callback_info info)
32 {
33 NFuncArg funcArg(env, info);
34 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
35 HILOGE("Number of arguments unmatched");
36 NError(EINVAL).ThrowErr(env);
37 return nullptr;
38 }
39
40 auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
41 if (!streamEntity || !streamEntity->fp) {
42 HILOGE("Failed to get entity of Stream");
43 NError(EIO).ThrowErr(env);
44 return nullptr;
45 }
46
47 int ret = fflush(streamEntity->fp.get());
48 if (ret < 0) {
49 HILOGE("Failed to fflush file in the stream, ret: %{public}d", ret);
50 NError(errno).ThrowErr(env);
51 return nullptr;
52 }
53 return NVal::CreateUndefined(env).val_;
54 }
55
Async(napi_env env,napi_callback_info info)56 napi_value Flush::Async(napi_env env, napi_callback_info info)
57 {
58 NFuncArg funcArg(env, info);
59 if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
60 HILOGE("Number of arguments unmatched");
61 NError(EINVAL).ThrowErr(env);
62 return nullptr;
63 }
64
65 auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
66 if (!streamEntity || !streamEntity->fp) {
67 HILOGE("Failed to get entity of Stream");
68 NError(EIO).ThrowErr(env);
69 return nullptr;
70 }
71
72 auto cbExec = [streamEntity]() -> NError {
73 if (!streamEntity || !streamEntity->fp) {
74 HILOGE("Stream has been closed in flush cbExec possibly");
75 return NError(EIO);
76 }
77 int ret = fflush(streamEntity->fp.get());
78 if (ret < 0) {
79 HILOGE("Failed to fflush file in the stream");
80 return NError(errno);
81 } else {
82 return NError(ERRNO_NOERR);
83 }
84 };
85 auto cbCompl = [](napi_env env, NError err) -> NVal {
86 if (err) {
87 return { env, err.GetNapiErr(env) };
88 }
89 return { NVal::CreateUndefined(env) };
90 };
91
92 NVal thisVar(env, funcArg.GetThisVar());
93 if (funcArg.GetArgc() == NARG_CNT::ZERO) {
94 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STREAM_FLUSH_NAME, cbExec, cbCompl).val_;
95 } else {
96 NVal cb(env, funcArg[NARG_POS::FIRST]);
97 return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STREAM_FLUSH_NAME, cbExec, cbCompl).val_;
98 }
99 }
100 } // namespace ModuleFileIO
101 } // namespace FileManagement
102 } // namespace OHOS