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 "mkdtemp.h"
17 
18 #include "common_func.h"
19 #include "file_utils.h"
20 #include "filemgmt_libhilog.h"
21 
22 namespace OHOS {
23 namespace FileManagement {
24 namespace ModuleFileIO {
25 using namespace std;
26 using namespace OHOS::FileManagement::LibN;
27 
Sync(napi_env env,napi_callback_info info)28 napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info)
29 {
30     NFuncArg funcArg(env, info);
31     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
32         HILOGE("Number of arguments unmatched");
33         NError(EINVAL).ThrowErr(env);
34         return nullptr;
35     }
36 
37     auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
38     if (!resGetFirstArg) {
39         HILOGE("Invalid path");
40         NError(EINVAL).ThrowErr(env);
41         return nullptr;
42     }
43 
44     string path = tmp.get();
45     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> mkdtemp_req = {
46         new uv_fs_t, CommonFunc::fs_req_cleanup };
47     if (!mkdtemp_req) {
48         HILOGE("Failed to request heap memory.");
49         NError(ENOMEM).ThrowErr(env);
50         return nullptr;
51     }
52     int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast<char *>(path.c_str()), nullptr);
53     if (ret < 0) {
54         HILOGE("Failed to create a temporary directory with path");
55         NError(ret).ThrowErr(env);
56         return nullptr;
57     }
58 
59     return NVal::CreateUTF8String(env, mkdtemp_req->path).val_;
60 }
61 
MkdTempExec(shared_ptr<string> arg,const string & path)62 static NError MkdTempExec(shared_ptr<string> arg, const string &path)
63 {
64     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> mkdtemp_req = {
65         new uv_fs_t, CommonFunc::fs_req_cleanup };
66     if (!mkdtemp_req) {
67         HILOGE("Failed to request heap memory.");
68         return NError(ENOMEM);
69     }
70     int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), path.c_str(), nullptr);
71     if (ret < 0) {
72         HILOGE("Failed to create a temporary directory with path");
73         return NError(ret);
74     } else {
75         *arg = mkdtemp_req->path;
76         return NError(ERRNO_NOERR);
77     }
78 }
79 
Async(napi_env env,napi_callback_info info)80 napi_value Mkdtemp::Async(napi_env env, napi_callback_info info)
81 {
82     NFuncArg funcArg(env, info);
83     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
84         HILOGE("Number of arguments unmatched");
85         NError(EINVAL).ThrowErr(env);
86         return nullptr;
87     }
88 
89     auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
90     if (!resGetFirstArg) {
91         HILOGE("Invalid path");
92         NError(EINVAL).ThrowErr(env);
93         return nullptr;
94     }
95 
96     auto arg = CreateSharedPtr<string>();
97     if (arg == nullptr) {
98         HILOGE("Failed to request heap memory.");
99         NError(ENOMEM).ThrowErr(env);
100         return nullptr;
101     }
102     auto cbExec = [path = string(tmp.get()), arg]() -> NError {
103         return MkdTempExec(arg, path);
104     };
105     auto cbComplete = [arg](napi_env env, NError err) -> NVal {
106         if (err) {
107             return { env, err.GetNapiErr(env) };
108         } else {
109             return NVal::CreateUTF8String(env, *arg);
110         }
111     };
112 
113     size_t argc = funcArg.GetArgc();
114     NVal thisVar(env, funcArg.GetThisVar());
115     if (argc == NARG_CNT::ONE) {
116         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MKDTEMP_NAME, cbExec, cbComplete).val_;
117     } else {
118         NVal cb(env, funcArg[NARG_POS::SECOND]);
119         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MKDTEMP_NAME, cbExec, cbComplete).val_;
120     }
121 }
122 } // namespace ModuleFileIO
123 } // namespace FileManagement
124 } // namespace OHOS
125