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 "rename.h"
17
18 #include <cstring>
19 #include <tuple>
20 #include <unistd.h>
21
22 #include "../../common/napi/n_async/n_async_work_callback.h"
23 #include "../../common/napi/n_async/n_async_work_promise.h"
24 #include "../../common/napi/n_func_arg.h"
25
26 namespace OHOS {
27 namespace DistributedFS {
28 namespace ModuleFileIO {
29 using namespace std;
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
35 if (!funcArg.InitArgs(NARG_CNT::TWO)) {
36 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
37 return nullptr;
38 }
39
40 bool succ = false;
41 unique_ptr<char[]> src;
42 tie(succ, src, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
43 if (!succ) {
44 UniError(EINVAL).ThrowErr(env, "Invalid src");
45 return nullptr;
46 }
47
48 unique_ptr<char[]> dest;
49 tie(succ, dest, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
50 if (!succ) {
51 UniError(EINVAL).ThrowErr(env, "Invalid dest");
52 return nullptr;
53 }
54
55 if (rename(src.get(), dest.get()) == -1) {
56 UniError(errno).ThrowErr(env);
57 return nullptr;
58 }
59
60 return NVal::CreateUndefined(env).val_;
61 }
62
Async(napi_env env,napi_callback_info info)63 napi_value Rename::Async(napi_env env, napi_callback_info info)
64 {
65 NFuncArg funcArg(env, info);
66 if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
67 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
68 return nullptr;
69 }
70
71 string path;
72 bool succ = false;
73 unique_ptr<char[]> src;
74 tie(succ, src, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
75 if (!succ) {
76 UniError(EINVAL).ThrowErr(env, "Invalid src");
77 return nullptr;
78 }
79
80 unique_ptr<char[]> dest;
81 tie(succ, dest, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
82 if (!succ) {
83 UniError(EINVAL).ThrowErr(env, "Invalid dest");
84 return nullptr;
85 }
86
87 auto cbExec = [opath = string(src.get()), npath = string(dest.get())](napi_env env) -> UniError {
88 int ret = rename(opath.c_str(), npath.c_str());
89 if (ret == -1) {
90 return UniError(errno);
91 } else {
92 return UniError(ERRNO_NOERR);
93 }
94 };
95
96 auto cbComplCallback = [](napi_env env, UniError err) -> NVal {
97 if (err) {
98 return { env, err.GetNapiErr(env) };
99 }
100 return { NVal::CreateUndefined(env) };
101 };
102
103 string procedureName = "FileIORename";
104 NVal thisVar(env, funcArg.GetThisVar());
105 size_t argc = funcArg.GetArgc();
106 if (argc == NARG_CNT::TWO) {
107 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
108 } else {
109 NVal cb(env, funcArg[NARG_POS::THIRD]);
110 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
111 }
112 }
113 } // namespace ModuleFileIO
114 } // namespace DistributedFS
115 } // namespace OHOS