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