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