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 if (!funcArg.InitArgs(NARG_CNT::TWO)) {
35 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
36 return nullptr;
37 }
38
39 auto [resGetFirstArg, src, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
40 if (!resGetFirstArg) {
41 UniError(EINVAL).ThrowErr(env, "Invalid src");
42 return nullptr;
43 }
44
45 auto [resGetSecondArg, dest, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8StringPath();
46 if (!resGetSecondArg) {
47 UniError(EINVAL).ThrowErr(env, "Invalid dest");
48 return nullptr;
49 }
50
51 if (rename(src.get(), dest.get()) == -1) {
52 UniError(errno).ThrowErr(env);
53 return nullptr;
54 }
55
56 return NVal::CreateUndefined(env).val_;
57 }
58
Async(napi_env env,napi_callback_info info)59 napi_value Rename::Async(napi_env env, napi_callback_info info)
60 {
61 NFuncArg funcArg(env, info);
62 if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
63 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
64 return nullptr;
65 }
66
67 auto [resGetFirstArg, src, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
68 if (!resGetFirstArg) {
69 UniError(EINVAL).ThrowErr(env, "Invalid src");
70 return nullptr;
71 }
72
73 auto [resGetSecondArg, dest, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8StringPath();
74 if (!resGetSecondArg) {
75 UniError(EINVAL).ThrowErr(env, "Invalid dest");
76 return nullptr;
77 }
78
79 auto cbExec = [opath = string(src.get()), npath = string(dest.get())](napi_env env) -> UniError {
80 int ret = rename(opath.c_str(), npath.c_str());
81 if (ret == -1) {
82 return UniError(errno);
83 } else {
84 return UniError(ERRNO_NOERR);
85 }
86 };
87
88 auto cbComplCallback = [](napi_env env, UniError err) -> NVal {
89 if (err) {
90 return { env, err.GetNapiErr(env) };
91 }
92 return { NVal::CreateUndefined(env) };
93 };
94
95 const string procedureName = "FileIORename";
96 NVal thisVar(env, funcArg.GetThisVar());
97 size_t argc = funcArg.GetArgc();
98 if (argc == NARG_CNT::TWO) {
99 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
100 } else {
101 NVal cb(env, funcArg[NARG_POS::THIRD]);
102 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
103 }
104 }
105 } // namespace ModuleFileIO
106 } // namespace DistributedFS
107 } // namespace OHOS