• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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     string path;
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     path = tmp.get();
42     if (mkdtemp(const_cast<char *>(path.c_str())) == nullptr) {
43         UniError(errno).ThrowErr(env);
44         return nullptr;
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 argments unmatched");
54         return nullptr;
55     }
56     unique_ptr<char[]> tmp;
57     bool succ = false;
58     tie(succ, tmp, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
59     if (!succ) {
60         UniError(EINVAL).ThrowErr(env, "Invalid path");
61         return nullptr;
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             string &res = *arg;
70             res = path;
71             return UniError(ERRNO_NOERR);
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     string procedureName = "FileIOmkdtemp";
82     int 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