• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool succ = false;
34     unique_ptr<char[]> src;
35     tie(succ, src, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
36     if (!succ) {
37         UniError(EINVAL).ThrowErr(env, "Invalid src");
38         return { false, "", "" };
39     }
40 
41     unique_ptr<char[]> dest;
42     tie(succ, dest, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
43     if (!succ) {
44         UniError(EINVAL).ThrowErr(env, "Invalid dest");
45         return { false, "", "" };
46     }
47     return { true, src.get(), dest.get() };
48 }
49 
Sync(napi_env env,napi_callback_info info)50 napi_value Link::Sync(napi_env env, napi_callback_info info)
51 {
52     NFuncArg funcArg(env, info);
53     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
54         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
55         return nullptr;
56     }
57 
58     bool succ = false;
59     string oldPath;
60     string newPath;
61     tie(succ, oldPath, newPath) = GetLinkArg(env, funcArg);
62     if (!succ) {
63         return nullptr;
64     }
65     if (link(oldPath.c_str(), newPath.c_str()) == -1) {
66         UniError(errno).ThrowErr(env);
67         return nullptr;
68     }
69     return NVal::CreateUndefined(env).val_;
70 }
71 
Async(napi_env env,napi_callback_info info)72 napi_value Link::Async(napi_env env, napi_callback_info info)
73 {
74     NFuncArg funcArg(env, info);
75 
76     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
77         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
78         return nullptr;
79     }
80 
81     bool succ = false;
82     string oldPath;
83     string newPath;
84     tie(succ, oldPath, newPath) = GetLinkArg(env, funcArg);
85     if (!succ) {
86         return nullptr;
87     }
88 
89     auto cbExec = [oldPath = move(oldPath), newPath = move(newPath)](napi_env env) -> UniError {
90         int ret = link(oldPath.c_str(), newPath.c_str());
91         if (ret == -1) {
92             return UniError(errno);
93         } else {
94             return UniError(ERRNO_NOERR);
95         }
96     };
97 
98     auto cbComplCallback = [](napi_env env, UniError err) -> NVal {
99         if (err) {
100             return { env, err.GetNapiErr(env) };
101         }
102         return { NVal::CreateUndefined(env) };
103     };
104 
105     string procedureName = "FileIOLink";
106     NVal thisVar(env, funcArg.GetThisVar());
107     size_t argc = funcArg.GetArgc();
108     if (argc == NARG_CNT::TWO) {
109         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
110     } else {
111         NVal cb(env, funcArg[NARG_POS::THIRD]);
112         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
113     }
114 }
115 } // namespace ModuleFileIO
116 } // namespace DistributedFS
117 } // namespace OHOS