• 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 "fdopen_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 "filemgmt_libhilog.h"
25 
26 namespace OHOS {
27 namespace FileManagement {
28 namespace ModuleFileIO {
29 using namespace std;
30 using namespace OHOS::FileManagement::LibN;
31 
GetFdopenStreamArgs(napi_env env,const NFuncArg & funcArg)32 static tuple<bool, int, string> GetFdopenStreamArgs(napi_env env, const NFuncArg &funcArg)
33 {
34     auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
35     if (!resGetFirstArg) {
36         return { false, -1, "" };
37     }
38 
39     auto [resGetSecondArg, mode, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
40     if (!resGetSecondArg) {
41         return { false, -1, "" };
42     }
43 
44     return { true, fd, mode.get() };
45 }
46 
Sync(napi_env env,napi_callback_info info)47 napi_value FdopenStream::Sync(napi_env env, napi_callback_info info)
48 {
49     NFuncArg funcArg(env, info);
50     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
51         HILOGE("Number of arguments unmatched");
52         NError(EINVAL).ThrowErr(env);
53         return nullptr;
54     }
55 
56     auto [resGetFdopenStreamArgs, fd, mode] = GetFdopenStreamArgs(env, funcArg);
57     if (!resGetFdopenStreamArgs || fd < 0) {
58         HILOGE("Invalid fd or mode from JS arugments");
59         NError(EINVAL).ThrowErr(env);
60         return nullptr;
61     }
62 
63     unique_ptr<FILE, decltype(&fclose)> fp = { fdopen(fd, mode.c_str()), fclose };
64     if (!fp) {
65         HILOGE("Failed to fdopen file by fd:%{public}d", fd);
66         NError(errno).ThrowErr(env);
67         return nullptr;
68     }
69 
70     return CommonFunc::InstantiateStream(env, move(fp)).val_;
71 }
72 
Async(napi_env env,napi_callback_info info)73 napi_value FdopenStream::Async(napi_env env, napi_callback_info info)
74 {
75     NFuncArg funcArg(env, info);
76     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
77         HILOGE("Number of arguments unmatched");
78         NError(EINVAL).ThrowErr(env);
79         return nullptr;
80     }
81 
82     auto [resGetFdopenStreamArgs, fd, mode] = GetFdopenStreamArgs(env, funcArg);
83     if (!resGetFdopenStreamArgs || fd < 0) {
84         NError(EINVAL).ThrowErr(env);
85         return nullptr;
86     }
87 
88     auto arg = make_shared<AsyncFdopenStreamArg>();
89     if (!arg) {
90         HILOGE("Failed to request heap memory.");
91         NError(ENOMEM).ThrowErr(env);
92         return nullptr;
93     }
94     auto cbExec = [arg, fd = fd, mode = mode]() -> NError {
95         arg->fp = { fdopen(fd, mode.c_str()), fclose };
96         if (!arg->fp) {
97             HILOGE("Failed to fdopen file by fd:%{public}d", fd);
98             return NError(errno);
99         }
100         return NError(ERRNO_NOERR);
101     };
102 
103     auto cbCompl = [arg](napi_env env, NError err) -> NVal {
104         if (err) {
105             return { env, err.GetNapiErr(env) };
106         }
107         return CommonFunc::InstantiateStream(env, move(arg->fp));
108     };
109 
110     NVal thisVar(env, funcArg.GetThisVar());
111     if (funcArg.GetArgc() == NARG_CNT::TWO) {
112         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_FDOPENSTREAM_NAME, cbExec, cbCompl).val_;
113     } else {
114         NVal cb(env, funcArg[NARG_POS::THIRD]);
115         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_FDOPENSTREAM_NAME, cbExec, cbCompl).val_;
116     }
117 }
118 } // namespace ModuleFileIO
119 } // namespace FileManagement
120 } // namespace OHOS