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