1 /*
2  * Copyright (c) 2021-2022 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 "mkdtemp.h"
17 
18 #include "../../common/napi/n_async/n_async_work_callback.h"
19 #include "../../common/napi/n_async/n_async_work_promise.h"
20 #include "../../common/napi/n_func_arg.h"
21 namespace OHOS {
22 namespace DistributedFS {
23 namespace ModuleFileIO {
24 using namespace std;
Sync(napi_env env,napi_callback_info info)25 napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info)
26 {
27     NFuncArg funcArg(env, info);
28     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
29         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
30         return nullptr;
31     }
32 
33     auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
34     if (!resGetFirstArg) {
35         UniError(EINVAL).ThrowErr(env, "Invalid path");
36         return nullptr;
37     }
38 
39     string path = tmp.get();
40     if (mkdtemp(const_cast<char *>(path.c_str())) == nullptr) {
41         UniError(errno).ThrowErr(env);
42         return nullptr;
43     }
44 
45     return NVal::CreateUTF8String(env, path).val_;
46 }
47 
Async(napi_env env,napi_callback_info info)48 napi_value Mkdtemp::Async(napi_env env, napi_callback_info info)
49 {
50     NFuncArg funcArg(env, info);
51     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
52         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
53         return nullptr;
54     }
55 
56     auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
57     if (!resGetFirstArg) {
58         UniError(EINVAL).ThrowErr(env, "Invalid path");
59         return nullptr;
60     }
61 
62     string path = tmp.get();
63     auto arg = make_shared<string>();
64     auto cbExec = [path, arg](napi_env env) -> UniError {
65         if (mkdtemp(const_cast<char *>(path.c_str())) == nullptr) {
66             return UniError(errno);
67         } else {
68             *arg = path;
69             return UniError(ERRNO_NOERR);
70         }
71     };
72 
73     auto cbComplete = [arg](napi_env env, UniError err) -> NVal {
74         if (err) {
75             return { env, err.GetNapiErr(env) };
76         } else {
77             return NVal::CreateUTF8String(env, *arg);
78         }
79     };
80 
81     const string procedureName = "FileIOmkdtemp";
82     size_t argc = funcArg.GetArgc();
83     NVal thisVar(env, funcArg.GetThisVar());
84     if (argc == NARG_CNT::ONE) {
85         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
86     } else {
87         NVal cb(env, funcArg[NARG_POS::SECOND]);
88         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
89     }
90 }
91 } // namespace ModuleFileIO
92 } // namespace DistributedFS
93 } // namespace OHOS
94