• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "common_func.h"
24 #include "file_utils.h"
25 #include "filemgmt_libhilog.h"
26 
27 namespace OHOS {
28 namespace FileManagement {
29 namespace ModuleFileIO {
30 using namespace std;
31 using namespace OHOS::FileManagement::LibN;
32 
GetCreateStreamArgs(napi_env env,const NFuncArg & funcArg)33 static tuple<bool, string, string> GetCreateStreamArgs(napi_env env, const NFuncArg &funcArg)
34 {
35     auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
36     if (!resGetFirstArg) {
37         return { false, "", "" };
38     }
39 
40     auto [resGetSecondArg, mode, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
41     if (!resGetSecondArg) {
42         return { false, "", "" };
43     }
44 
45     return { true, path.get(), mode.get() };
46 }
47 
Sync(napi_env env,napi_callback_info info)48 napi_value CreateStream::Sync(napi_env env, napi_callback_info info)
49 {
50     NFuncArg funcArg(env, info);
51     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
52         HILOGE("Number of arguments unmatched");
53         NError(EINVAL).ThrowErr(env);
54         return nullptr;
55     }
56 
57     auto [resGetCreateStreamArgs, argPath, argMode] = GetCreateStreamArgs(env, funcArg);
58     if (!resGetCreateStreamArgs) {
59         HILOGE("Arg path and mode are required to be type of string");
60         NError(EINVAL).ThrowErr(env);
61         return nullptr;
62     }
63 
64     unique_ptr<FILE, decltype(&fclose)> fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
65     if (!fp) {
66         HILOGE("Failed to fdopen file by path");
67         NError(errno).ThrowErr(env);
68         return nullptr;
69     }
70 
71     return CommonFunc::InstantiateStream(env, move(fp)).val_;
72 }
73 
Async(napi_env env,napi_callback_info info)74 napi_value CreateStream::Async(napi_env env, napi_callback_info info)
75 {
76     NFuncArg funcArg(env, info);
77     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
78         HILOGE("Number of arguments unmatched");
79         NError(EINVAL).ThrowErr(env);
80         return nullptr;
81     }
82 
83     auto [resGetCreateStreamArgs, argPath, argMode] = GetCreateStreamArgs(env, funcArg);
84     if (!resGetCreateStreamArgs) {
85         NError(EINVAL).ThrowErr(env);
86         return nullptr;
87     }
88 
89     auto arg = CreateSharedPtr<AsyncCreateStreamArg>();
90     if (arg == nullptr) {
91         HILOGE("Failed to request heap memory.");
92         NError(ENOMEM).ThrowErr(env);
93         return nullptr;
94     }
95     auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)]() -> NError {
96         arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
97         if (!arg->fp) {
98             HILOGE("Failed to fdopen file by path");
99             return NError(errno);
100         }
101         return NError(ERRNO_NOERR);
102     };
103 
104     auto cbCompl = [arg](napi_env env, NError err) -> NVal {
105         if (err) {
106             return { env, err.GetNapiErr(env) };
107         }
108         return CommonFunc::InstantiateStream(env, move(arg->fp));
109     };
110 
111     NVal thisVar(env, funcArg.GetThisVar());
112     if (funcArg.GetArgc() == NARG_CNT::TWO) {
113         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_CREATESTREAM_NAME, cbExec, cbCompl).val_;
114     } else {
115         NVal cb(env, funcArg[NARG_POS::THIRD]);
116         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_CREATESTREAM_NAME, cbExec, cbCompl).val_;
117     }
118 }
119 } // namespace ModuleFileIO
120 } // namespace FileManagement
121 } // namespace OHOS