1 /*
2 * Copyright (c) 2021-2022 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 "symlink.h"
17 #include <cstring>
18 #include <fcntl.h>
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 namespace OHOS {
26 namespace DistributedFS {
27 namespace ModuleFileIO {
28 using namespace std;
29
GetSymlinkArg(napi_env env,const NFuncArg & funcArg)30 static tuple<bool, string, string> GetSymlinkArg(napi_env env, const NFuncArg &funcArg)
31 {
32 auto [resGetFirstArg, src, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
33 if (!resGetFirstArg) {
34 UniError(EINVAL).ThrowErr(env, "Invalid src");
35 return { false, "", "" };
36 }
37
38 auto [resGetSecondArg, dest, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8StringPath();
39 if (!resGetSecondArg) {
40 UniError(EINVAL).ThrowErr(env, "Invalid dest");
41 return { false, "", "" };
42 }
43
44 return { true, src.get(), dest.get() };
45 }
46
Sync(napi_env env,napi_callback_info info)47 napi_value Symlink::Sync(napi_env env, napi_callback_info info)
48 {
49 NFuncArg funcArg(env, info);
50 if (!funcArg.InitArgs(NARG_CNT::TWO)) {
51 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
52 return nullptr;
53 }
54
55 auto [resGetSymlinkArg, oldPath, newPath] = GetSymlinkArg(env, funcArg);
56 if (!resGetSymlinkArg) {
57 return nullptr;
58 }
59
60 if (symlink(oldPath.c_str(), newPath.c_str()) == -1) {
61 UniError(errno).ThrowErr(env);
62 return nullptr;
63 }
64
65 return NVal::CreateUndefined(env).val_;
66 }
67
Async(napi_env env,napi_callback_info info)68 napi_value Symlink::Async(napi_env env, napi_callback_info info)
69 {
70 NFuncArg funcArg(env, info);
71 if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
72 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
73 return nullptr;
74 }
75
76 auto [resGetSymlinkArg, oldPath, newPath] = GetSymlinkArg(env, funcArg);
77 if (!resGetSymlinkArg) {
78 return nullptr;
79 }
80
81 auto cbExec = [oldPath = move(oldPath), newPath = move(newPath)](napi_env env) -> UniError {
82 int ret = symlink(oldPath.c_str(), newPath.c_str());
83 if (ret == -1) {
84 return UniError(errno);
85 } else {
86 return UniError(ERRNO_NOERR);
87 }
88 };
89
90 auto cbComplCallback = [](napi_env env, UniError err) -> NVal {
91 if (err) {
92 return { env, err.GetNapiErr(env) };
93 }
94 return { NVal::CreateUndefined(env) };
95 };
96
97 const string procedureName = "FileIOsymLink";
98 NVal thisVar(env, funcArg.GetThisVar());
99 size_t argc = funcArg.GetArgc();
100 if (argc == NARG_CNT::TWO) {
101 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
102 } else {
103 NVal cb(env, funcArg[NARG_POS::THIRD]);
104 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
105 }
106 }
107 } // namespace ModuleFileIO
108 } // namespace DistributedFS
109 } // namespace OHOS
110