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