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