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