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
16 #include "create_stream.h"
17
18 #include <memory>
19 #include <tuple>
20
21 #include "class_stream/stream_entity.h"
22 #include "class_stream/stream_n_exporter.h"
23 #include "common_func.h"
24 #include "filemgmt_libhilog.h"
25
26 namespace OHOS {
27 namespace FileManagement {
28 namespace ModuleFileIO {
29 using namespace std;
30 using namespace OHOS::FileManagement::LibN;
31
GetCreateStreamArgs(napi_env env,const NFuncArg & funcArg)32 static tuple<bool, string, string> GetCreateStreamArgs(napi_env env, const NFuncArg &funcArg)
33 {
34 auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
35 if (!resGetFirstArg) {
36 return { false, "", "" };
37 }
38
39 auto [resGetSecondArg, mode, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
40 if (!resGetSecondArg) {
41 return { false, "", "" };
42 }
43
44 return { true, path.get(), mode.get() };
45 }
46
Sync(napi_env env,napi_callback_info info)47 napi_value CreateStream::Sync(napi_env env, napi_callback_info info)
48 {
49 NFuncArg funcArg(env, info);
50 if (!funcArg.InitArgs(NARG_CNT::TWO)) {
51 HILOGE("Number of arguments unmatched");
52 NError(EINVAL).ThrowErr(env);
53 return nullptr;
54 }
55
56 auto [resGetCreateStreamArgs, argPath, argMode] = GetCreateStreamArgs(env, funcArg);
57 if (!resGetCreateStreamArgs) {
58 HILOGE("Arg path and mode are required to be type of string");
59 NError(EINVAL).ThrowErr(env);
60 return nullptr;
61 }
62
63 unique_ptr<FILE, decltype(&fclose)> fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
64 if (!fp) {
65 HILOGE("Failed to fdopen file by path");
66 NError(errno).ThrowErr(env);
67 return nullptr;
68 }
69
70 return CommonFunc::InstantiateStream(env, move(fp)).val_;
71 }
72
Async(napi_env env,napi_callback_info info)73 napi_value CreateStream::Async(napi_env env, napi_callback_info info)
74 {
75 NFuncArg funcArg(env, info);
76 if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
77 HILOGE("Number of arguments unmatched");
78 NError(EINVAL).ThrowErr(env);
79 return nullptr;
80 }
81
82 auto [resGetCreateStreamArgs, argPath, argMode] = GetCreateStreamArgs(env, funcArg);
83 if (!resGetCreateStreamArgs) {
84 NError(EINVAL).ThrowErr(env);
85 return nullptr;
86 }
87
88 auto arg = make_shared<AsyncCreateStreamArg>();
89 if (!arg) {
90 HILOGE("Failed to request heap memory.");
91 NError(ENOMEM).ThrowErr(env);
92 return nullptr;
93 }
94 auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)]() -> NError {
95 arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
96 if (!arg->fp) {
97 HILOGE("Failed to fdopen file by path");
98 return NError(errno);
99 }
100 return NError(ERRNO_NOERR);
101 };
102
103 auto cbCompl = [arg](napi_env env, NError err) -> NVal {
104 if (err) {
105 return { env, err.GetNapiErr(env) };
106 }
107 return CommonFunc::InstantiateStream(env, move(arg->fp));
108 };
109
110 NVal thisVar(env, funcArg.GetThisVar());
111 if (funcArg.GetArgc() == NARG_CNT::TWO) {
112 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_CREATESTREAM_NAME, cbExec, cbCompl).val_;
113 } else {
114 NVal cb(env, funcArg[NARG_POS::THIRD]);
115 return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_CREATESTREAM_NAME, cbExec, cbCompl).val_;
116 }
117 }
118 } // namespace ModuleFileIO
119 } // namespace FileManagement
120 } // namespace OHOS