• 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 "fchmod.h"
17 #include <cstring>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <tuple>
21 #include <unistd.h>
22 
23 #include "../../common/napi/n_async/n_async_work_callback.h"
24 #include "../../common/napi/n_async/n_async_work_promise.h"
25 #include "../../common/napi/n_func_arg.h"
26 namespace OHOS {
27 namespace DistributedFS {
28 namespace ModuleFileIO {
29 using namespace std;
30 
Sync(napi_env env,napi_callback_info info)31 napi_value Fchmod::Sync(napi_env env, napi_callback_info info)
32 {
33     NFuncArg funcArg(env, info);
34     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
35         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
36         return nullptr;
37     }
38 
39     auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
40     if (!resGetFirstArg) {
41         UniError(EINVAL).ThrowErr(env, "Invalid fd");
42         return nullptr;
43     }
44 
45     auto [resGetSecondArg, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
46     if (!resGetSecondArg) {
47         UniError(EINVAL).ThrowErr(env, "Invalid mode");
48         return nullptr;
49     }
50 
51     int ret = fchmod(fd, mode);
52     if (ret == -1) {
53         UniError(errno).ThrowErr(env);
54         return nullptr;
55     }
56 
57     return NVal::CreateUndefined(env).val_;
58 }
59 
60 
Async(napi_env env,napi_callback_info info)61 napi_value Fchmod::Async(napi_env env, napi_callback_info info)
62 {
63     NFuncArg funcArg(env, info);
64     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
65         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
66         return nullptr;
67     }
68 
69     auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
70     if (!resGetFirstArg) {
71         UniError(EINVAL).ThrowErr(env, "Invalid fd");
72         return nullptr;
73     }
74 
75     auto [resGetSecondArg, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
76     if (!resGetSecondArg) {
77         UniError(EINVAL).ThrowErr(env, "Invalid owner");
78         return nullptr;
79     }
80 
81     auto cbExec = [fd = fd, mode = mode](napi_env env) -> UniError {
82         int ret = fchmod(fd, mode);
83         if (ret == -1) {
84             return UniError(errno);
85         } else {
86             return UniError(ERRNO_NOERR);
87         }
88     };
89 
90     auto cbComplCallback = [](napi_env env, UniError err) -> NVal {
91         if (err) {
92             return { env, err.GetNapiErr(env) };
93         }
94         return { NVal::CreateUndefined(env) };
95     };
96 
97     const string procedureName = "FileIOFchmod";
98     NVal thisVar(env, funcArg.GetThisVar());
99     size_t argc = funcArg.GetArgc();
100     if (argc == NARG_CNT::TWO) {
101         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
102     } else {
103         NVal cb(env, funcArg[NARG_POS::THIRD]);
104         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
105     }
106 }
107 } // namespace ModuleFileIO
108 } // namespace DistributedFS
109 } // namespace OHOS