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 "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 
GetFdopenStreamArgs(napi_env env,const NFuncArg & funcArg)33 static tuple<bool, int, string> GetFdopenStreamArgs(napi_env env, const NFuncArg &funcArg)
34 {
35     auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
36     if (!resGetFirstArg) {
37         return { false, -1, "" };
38     }
39 
40     auto [resGetSecondArg, mode, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
41     if (!resGetSecondArg) {
42         return { false, -1, "" };
43     }
44 
45     return { true, fd, mode.get() };
46 }
47 
Sync(napi_env env,napi_callback_info info)48 napi_value FdopenStream::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 [resGetFdopenStreamArgs, fd, mode] = GetFdopenStreamArgs(env, funcArg);
58     if (!resGetFdopenStreamArgs || fd < 0) {
59         HILOGE("Invalid fd or mode from JS arugments");
60         NError(EINVAL).ThrowErr(env);
61         return nullptr;
62     }
63     FILE *file = fdopen(fd, mode.c_str());
64     if (!file) {
65         HILOGE("Failed to fdopen file by path");
66         NError(errno).ThrowErr(env);
67         return nullptr;
68     }
69     std::shared_ptr<FILE> fp(file, fclose);
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     shared_ptr<AsyncFdopenStreamArg> arg = CreateSharedPtr<AsyncFdopenStreamArg>();
89     if (arg == nullptr) {
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         FILE *file = fdopen(fd, mode.c_str());
96         if (!file) {
97             HILOGE("Failed to fdopen file by path");
98             return NError(errno);
99         }
100         arg->fp = std::shared_ptr<FILE>(file, fclose);
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), true);
109     };
110 
111     NVal thisVar(env, funcArg.GetThisVar());
112     if (funcArg.GetArgc() == NARG_CNT::TWO) {
113         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_FDOPENSTREAM_NAME, cbExec, cbCompl).val_;
114     } else {
115         NVal cb(env, funcArg[NARG_POS::THIRD]);
116         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_FDOPENSTREAM_NAME, cbExec, cbCompl).val_;
117     }
118 }
119 } // namespace ModuleFileIO
120 } // namespace FileManagement
121 } // namespace OHOS