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