• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
18 #include <cstring>
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 
27 namespace OHOS {
28 namespace DistributedFS {
29 namespace ModuleFileIO {
30 using namespace std;
31 
Sync(napi_env env,napi_callback_info info)32 napi_value Chmod::Sync(napi_env env, napi_callback_info info)
33 {
34     NFuncArg funcArg(env, info);
35     if (!funcArg.InitArgs(NARG_CNT::TWO)) {
36         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
37         return nullptr;
38     }
39 
40     auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
41     if (!resGetFirstArg) {
42         UniError(EINVAL).ThrowErr(env, "Invalid path");
43         return nullptr;
44     }
45 
46     auto [resGetSecondArg, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
47     if (!resGetSecondArg) {
48         UniError(EINVAL).ThrowErr(env, "Invalid mode");
49         return nullptr;
50     }
51 
52     if (chmod(path.get(), mode) == -1) {
53         UniError(errno).ThrowErr(env);
54         return nullptr;
55     }
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     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
64         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
65         return nullptr;
66     }
67 
68     auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
69     if (!resGetFirstArg) {
70         UniError(EINVAL).ThrowErr(env, "Invalid path");
71         return nullptr;
72     }
73 
74     auto [resGetSecondArg, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
75     if (!resGetSecondArg) {
76         UniError(EINVAL).ThrowErr(env, "Invalid mode");
77     }
78 
79     string path = tmp.get();
80     auto cbExec = [path = path, mode = mode](napi_env env) -> UniError {
81         if (chmod(path.c_str(), mode) == -1) {
82             return UniError(errno);
83         } else {
84             return UniError(ERRNO_NOERR);
85         }
86     };
87 
88     auto cbComplete = [](napi_env env, UniError err) -> NVal {
89         if (err) {
90             return { env, err.GetNapiErr(env) };
91         }
92         return { NVal::CreateUndefined(env) };
93     };
94 
95     size_t argc = funcArg.GetArgc();
96     NVal thisVar(env, funcArg.GetThisVar());
97     const string procedureName = "FileIOChmod";
98     if (argc == NARG_CNT::TWO) {
99         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
100     } else {
101         NVal cb(env, funcArg[NARG_POS::THIRD]);
102         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
103     }
104 }
105 } // namespace ModuleFileIO
106 } // namespace DistributedFS
107 } // namespace OHOS