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