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     auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
55     if (!resGetFirstArg || fd < 0) {
56         UniError(EINVAL).ThrowErr(env, "Arg fd is required to be type integer");
57         return { false, -1, "" };
58     }
59 
60     auto [resGetSecondArg, mode, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
61     if (!resGetSecondArg) {
62         UniError(EINVAL).ThrowErr(env, "Arg mode is required to be type string");
63         return { false, -1, "" };
64     }
65 
66     return { true, fd, mode.get() };
67 }
68 
Sync(napi_env env,napi_callback_info info)69 napi_value FdopenStream::Sync(napi_env env, napi_callback_info info)
70 {
71     NFuncArg funcArg(env, info);
72     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
73         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
74         return nullptr;
75     }
76 
77     auto [resGetFdopenStreamArgs, fd, mode] = GetFdopenStreamArgs(env, funcArg);
78     if (!resGetFdopenStreamArgs) {
79         return nullptr;
80     }
81 
82     unique_ptr<FILE, decltype(&fclose)> fp = { fdopen(fd, mode.c_str()), fclose };
83     if (!fp) {
84         UniError(errno).ThrowErr(env);
85         return nullptr;
86     }
87 
88     return InstantiateStream(env, move(fp)).val_;
89 }
90 
91 struct AsyncFdopenStreamArg {
92     unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose };
93 };
94 
Async(napi_env env,napi_callback_info info)95 napi_value FdopenStream::Async(napi_env env, napi_callback_info info)
96 {
97     NFuncArg funcArg(env, info);
98     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
99         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
100         return nullptr;
101     }
102 
103     auto [resGetFdopenStreamArgs, fd, mode] = GetFdopenStreamArgs(env, funcArg);
104     if (!resGetFdopenStreamArgs) {
105         return nullptr;
106     }
107 
108     auto arg = make_shared<AsyncFdopenStreamArg>();
109     auto cbExec = [arg = arg, fd = fd, mode = mode](napi_env env) -> UniError {
110         arg->fp = { fdopen(fd, mode.c_str()), fclose };
111         if (!arg->fp) {
112             return UniError(errno);
113         } else {
114             return UniError(ERRNO_NOERR);
115         }
116     };
117 
118     auto cbCompl = [arg = arg](napi_env env, UniError err) -> NVal {
119         if (err) {
120             return { env, err.GetNapiErr(env) };
121         }
122         return InstantiateStream(env, move(arg->fp));
123     };
124 
125     const string procedureName = "FileIOFdopenStream";
126     NVal thisVar(env, funcArg.GetThisVar());
127     if (funcArg.GetArgc() == NARG_CNT::TWO) {
128         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
129     } else {
130         NVal cb(env, funcArg[NARG_POS::THIRD]);
131         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
132     }
133 }
134 } // namespace ModuleFileIO
135 } // namespace DistributedFS
136 } // namespace OHOS