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