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("Stream may has been closed");
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("Stream may has been closed");
68 NError(EIO).ThrowErr(env);
69 return nullptr;
70 }
71
72 auto cbExec = [streamEntity]() -> NError {
73 int ret = fflush(streamEntity->fp.get());
74 if (ret < 0) {
75 HILOGE("Failed to fflush file in the stream");
76 return NError(errno);
77 } else {
78 return NError(ERRNO_NOERR);
79 }
80 };
81 auto cbCompl = [](napi_env env, NError err) -> NVal {
82 if (err) {
83 return { env, err.GetNapiErr(env) };
84 }
85 return { NVal::CreateUndefined(env) };
86 };
87
88 NVal thisVar(env, funcArg.GetThisVar());
89 if (funcArg.GetArgc() == NARG_CNT::ZERO) {
90 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STREAM_FLUSH_NAME, cbExec, cbCompl).val_;
91 } else {
92 NVal cb(env, funcArg[NARG_POS::FIRST]);
93 return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STREAM_FLUSH_NAME, cbExec, cbCompl).val_;
94 }
95 }
96 } // namespace ModuleFileIO
97 } // namespace FileManagement
98 } // namespace OHOS