• 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 "lchown.h"
17 
18 #include <cstring>
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 
GetLchownArg(napi_env env,const NFuncArg & funcArg)30 static tuple<bool, string, int, int> GetLchownArg(napi_env env, const NFuncArg &funcArg)
31 {
32     auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
33     if (!resGetFirstArg) {
34         UniError(EINVAL).ThrowErr(env, "Invalid path");
35         return { false, "", -1, -1 };
36     }
37 
38     auto [resGetSecondArg, owner] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
39     if (!resGetSecondArg) {
40         UniError(EINVAL).ThrowErr(env, "Invalid owner");
41         return { false, "", -1, -1 };
42     }
43 
44     auto [resGetThirdArg, group] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
45     if (!resGetThirdArg) {
46         UniError(EINVAL).ThrowErr(env, "Invalid group");
47         return { false, "", -1, -1 };
48     }
49 
50     return { true, path.get(), owner, group };
51 }
52 
Sync(napi_env env,napi_callback_info info)53 napi_value Lchown::Sync(napi_env env, napi_callback_info info)
54 {
55     NFuncArg funcArg(env, info);
56     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
57         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
58         return nullptr;
59     }
60 
61     auto [resGetLchownArg, path, owner, group] = GetLchownArg(env, funcArg);
62     if (!resGetLchownArg) {
63         return nullptr;
64     }
65 
66     if (lchown(path.c_str(), owner, group) == -1) {
67         UniError(errno).ThrowErr(env);
68         return nullptr;
69     }
70 
71     return NVal::CreateUndefined(env).val_;
72 }
73 
Async(napi_env env,napi_callback_info info)74 napi_value Lchown::Async(napi_env env, napi_callback_info info)
75 {
76     NFuncArg funcArg(env, info);
77     if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) {
78         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
79         return nullptr;
80     }
81 
82     auto [resGetLchownArg, path, owner, group] = GetLchownArg(env, funcArg);
83     if (!resGetLchownArg) {
84         return nullptr;
85     }
86 
87     auto cbExec = [path = path, owner = owner, group = group](napi_env env) -> UniError {
88         if (lchown(path.c_str(), owner, group) == -1) {
89             return UniError(errno);
90         } else {
91             return UniError(ERRNO_NOERR);
92         }
93     };
94 
95     auto cbCompl = [](napi_env env, UniError err) -> NVal {
96         if (err) {
97             return { env, err.GetNapiErr(env) };
98         }
99         return { NVal::CreateUndefined(env) };
100     };
101 
102     const string procedureName = "FileIOLchown";
103     NVal thisVar(env, funcArg.GetThisVar());
104     if (funcArg.GetArgc() == NARG_CNT::THREE) {
105         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
106     } else {
107         NVal cb(env, funcArg[NARG_POS::FOURTH]);
108         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
109     }
110 }
111 } // namespace ModuleFileIO
112 } // namespace DistributedFS
113 } // namespace OHOS
114