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