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 "create_stream.h"
17
18 #include "../../common/napi/n_class.h"
19 #include "../../common/napi/n_func_arg.h"
20 #include "../../common/uni_error.h"
21
22 #include "../class_stream/stream_entity.h"
23 #include "../class_stream/stream_n_exporter.h"
24
25 namespace OHOS {
26 namespace DistributedFS {
27 namespace ModuleFileIO {
28 using namespace std;
29
InstantiateStream(napi_env env,unique_ptr<FILE,decltype(& fclose) > fp)30 static NVal InstantiateStream(napi_env env, unique_ptr<FILE, decltype(&fclose)> fp)
31 {
32 napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {});
33 if (!objStream) {
34 UniError(EIO).ThrowErr(env, "INNER BUG. Cannot instantiate stream");
35 return NVal();
36 }
37
38 auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, objStream);
39 if (!streamEntity) {
40 UniError(EIO).ThrowErr(env, "Cannot instantiate stream because of void entity");
41 return NVal();
42 }
43
44 streamEntity->fp.swap(fp);
45 return {env, objStream};
46 }
47
GetCreateStreamArgs(napi_env env,const NFuncArg & funcArg)48 static tuple<bool, string, string> GetCreateStreamArgs(napi_env env, const NFuncArg& funcArg)
49 {
50 bool succ = false;
51 unique_ptr<char[]> path;
52 tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
53 if (!succ) {
54 UniError(EINVAL).ThrowErr(env, "Invalid path");
55 return {false, "", ""};
56 }
57
58 unique_ptr<char[]> mode;
59 tie(succ, mode, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
60 if (!succ) {
61 UniError(EINVAL).ThrowErr(env, "Invalid mode");
62 return {false, "", ""};
63 }
64
65 return {true, path.get(), mode.get()};
66 }
67
Sync(napi_env env,napi_callback_info info)68 napi_value CreateStream::Sync(napi_env env, napi_callback_info info)
69 {
70 NFuncArg funcArg(env, info);
71 if (!funcArg.InitArgs(NARG_CNT::TWO)) {
72 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
73 return nullptr;
74 }
75
76 bool succ = false;
77 string argPath;
78 string argMode;
79 tie(succ, argPath, argMode) = GetCreateStreamArgs(env, funcArg);
80 if (!succ) {
81 return nullptr;
82 }
83
84 unique_ptr<FILE, decltype(&fclose)> fp = {fopen(argPath.c_str(), argMode.c_str()), fclose};
85 if (!fp) {
86 UniError(errno).ThrowErr(env);
87 return nullptr;
88 }
89 return InstantiateStream(env, move(fp)).val_;
90 }
91 } // namespace ModuleFileIO
92 } // namespace DistributedFS
93 } // namespace OHOS