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 "create_stream.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_stream/stream_entity.h"
28 #include "../class_stream/stream_n_exporter.h"
29
30 namespace OHOS {
31 namespace DistributedFS {
32 namespace ModuleFileIO {
33 using namespace std;
34
InstantiateStream(napi_env env,unique_ptr<FILE,decltype(& fclose) > fp)35 static NVal InstantiateStream(napi_env env, unique_ptr<FILE, decltype(&fclose)> fp)
36 {
37 napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {});
38 if (!objStream) {
39 UniError(EIO).ThrowErr(env, "INNER BUG. Cannot instantiate stream");
40 return NVal();
41 }
42
43 auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, objStream);
44 if (!streamEntity) {
45 UniError(EIO).ThrowErr(env, "Cannot instantiate stream because of void entity");
46 return NVal();
47 }
48
49 streamEntity->fp.swap(fp);
50 return { env, objStream };
51 }
52
GetCreateStreamArgs(napi_env env,const NFuncArg & funcArg)53 static tuple<bool, string, string> GetCreateStreamArgs(napi_env env, const NFuncArg &funcArg)
54 {
55 bool succ = false;
56 unique_ptr<char[]> path;
57 tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
58 if (!succ) {
59 UniError(EINVAL).ThrowErr(env, "Invalid path");
60 return { false, "", "" };
61 }
62
63 unique_ptr<char[]> mode;
64 tie(succ, mode, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
65 if (!succ) {
66 UniError(EINVAL).ThrowErr(env, "Invalid mode");
67 return { false, "", "" };
68 }
69
70 return { true, path.get(), mode.get() };
71 }
72
Sync(napi_env env,napi_callback_info info)73 napi_value CreateStream::Sync(napi_env env, napi_callback_info info)
74 {
75 NFuncArg funcArg(env, info);
76 if (!funcArg.InitArgs(NARG_CNT::TWO)) {
77 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
78 return nullptr;
79 }
80
81 bool succ = false;
82 string argPath;
83 string argMode;
84 tie(succ, argPath, argMode) = GetCreateStreamArgs(env, funcArg);
85 if (!succ) {
86 return nullptr;
87 }
88
89 unique_ptr<FILE, decltype(&fclose)> fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
90 if (!fp) {
91 UniError(errno).ThrowErr(env);
92 return nullptr;
93 }
94 return InstantiateStream(env, move(fp)).val_;
95 }
96
97 struct AsyncCreateStreamArg {
98 unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose };
99 };
100
Async(napi_env env,napi_callback_info info)101 napi_value CreateStream::Async(napi_env env, napi_callback_info info)
102 {
103 NFuncArg funcArg(env, info);
104 if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
105 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
106 return nullptr;
107 }
108
109 bool succ = false;
110 string argPath;
111 string argMode;
112 tie(succ, argPath, argMode) = GetCreateStreamArgs(env, funcArg);
113 if (!succ) {
114 return nullptr;
115 }
116
117 auto arg = make_shared<AsyncCreateStreamArg>();
118 auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)](napi_env env) -> UniError {
119 arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
120 if (!arg->fp) {
121 return UniError(errno);
122 } else {
123 return UniError(ERRNO_NOERR);
124 }
125 };
126 auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
127 if (err) {
128 return { env, err.GetNapiErr(env) };
129 }
130 return InstantiateStream(env, move(arg->fp));
131 };
132
133 string procedureName = "FileIOCreateStream";
134 NVal thisVar(env, funcArg.GetThisVar());
135 if (funcArg.GetArgc() == NARG_CNT::TWO) {
136 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
137 } else {
138 NVal cb(env, funcArg[NARG_POS::THIRD]);
139 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
140 }
141 }
142 } // namespace ModuleFileIO
143 } // namespace DistributedFS
144 } // namespace OHOS