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