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