• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "n_async_work_callback.h"
24 #include "n_async_work_promise.h"
25 #include "n_class.h"
26 #include "n_func_arg.h"
27 #include "n_val.h"
28 #include "uni_error.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     auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
56     if (!resGetFirstArg) {
57         UniError(EINVAL).ThrowErr(env, "Invalid path");
58         return { false, "", "" };
59     }
60 
61     auto [resGetSecondArg, mode, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
62     if (!resGetSecondArg) {
63         UniError(EINVAL).ThrowErr(env, "Invalid mode");
64         return { false, "", "" };
65     }
66 
67     return { true, path.get(), mode.get() };
68 }
69 
Sync(napi_env env,napi_callback_info info)70 napi_value CreateStream::Sync(napi_env env, napi_callback_info info)
71 {
72     NFuncArg funcArg(env, info);
73     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
74         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
75         return nullptr;
76     }
77 
78     auto [resGetCreateStreamArgs, argPath, argMode] = GetCreateStreamArgs(env, funcArg);
79     if (!resGetCreateStreamArgs) {
80         return nullptr;
81     }
82 
83     unique_ptr<FILE, decltype(&fclose)> fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
84     if (!fp) {
85         UniError(errno).ThrowErr(env);
86         return nullptr;
87     }
88 
89     return InstantiateStream(env, move(fp)).val_;
90 }
91 
92 struct AsyncCreateStreamArg {
93     unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose };
94 };
95 
Async(napi_env env,napi_callback_info info)96 napi_value CreateStream::Async(napi_env env, napi_callback_info info)
97 {
98     NFuncArg funcArg(env, info);
99     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
100         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
101         return nullptr;
102     }
103 
104     auto [resGetCreateStreamArgs, argPath, argMode] = GetCreateStreamArgs(env, funcArg);
105     if (!resGetCreateStreamArgs) {
106         return nullptr;
107     }
108 
109     auto arg = make_shared<AsyncCreateStreamArg>();
110     auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)](napi_env env) -> UniError {
111         arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
112         if (!arg->fp) {
113             return UniError(errno);
114         } else {
115             return UniError(ERRNO_NOERR);
116         }
117     };
118 
119     auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
120         if (err) {
121             return { env, err.GetNapiErr(env) };
122         }
123         return InstantiateStream(env, move(arg->fp));
124     };
125 
126     const string procedureName = "FileIOCreateStream";
127     NVal thisVar(env, funcArg.GetThisVar());
128     if (funcArg.GetArgc() == NARG_CNT::TWO) {
129         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
130     } else {
131         NVal cb(env, funcArg[NARG_POS::THIRD]);
132         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
133     }
134 }
135 } // namespace ModuleFileIO
136 } // namespace DistributedFS
137 } // namespace OHOS