• 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 
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     bool succ = false;
33     unique_ptr<char[]> src;
34     tie(succ, src, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
35     if (!succ) {
36         UniError(EINVAL).ThrowErr(env, "Invalid src");
37         return { false, "", "" };
38     }
39 
40     unique_ptr<char[]> dest;
41     tie(succ, dest, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
42     if (!succ) {
43         UniError(EINVAL).ThrowErr(env, "Invalid dest");
44         return { false, "", "" };
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     bool succ = false;
58     string oldPath;
59     string newPath;
60     tie(succ, oldPath, newPath) = GetSymlinkArg(env, funcArg);
61     if (!succ) {
62         return nullptr;
63     }
64 
65     if (symlink(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 Symlink::Async(napi_env env, napi_callback_info info)
73 {
74     NFuncArg funcArg(env, info);
75     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
76         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
77         return nullptr;
78     }
79 
80     bool succ = false;
81     string oldPath;
82     string newPath;
83     tie(succ, oldPath, newPath) = GetSymlinkArg(env, funcArg);
84     if (!succ) {
85         return nullptr;
86     }
87 
88     auto cbExec = [oldPath = move(oldPath), newPath = move(newPath)](napi_env env) -> UniError {
89         int ret = symlink(oldPath.c_str(), newPath.c_str());
90         if (ret == -1) {
91             return UniError(errno);
92         } else {
93             return UniError(ERRNO_NOERR);
94         }
95     };
96 
97     auto cbComplCallback = [](napi_env env, UniError err) -> NVal {
98         if (err) {
99             return { env, err.GetNapiErr(env) };
100         }
101         return { NVal::CreateUndefined(env) };
102     };
103 
104     string procedureName = "FileIOsymLink";
105     NVal thisVar(env, funcArg.GetThisVar());
106     size_t argc = funcArg.GetArgc();
107     if (argc == NARG_CNT::TWO) {
108         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
109     } else {
110         NVal cb(env, funcArg[NARG_POS::THIRD]);
111         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
112     }
113 }
114 } // namespace ModuleFileIO
115 } // namespace DistributedFS
116 } // namespace OHOS