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 "rmdir.h"
17
18 #include <cstring>
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 Rmdir::Sync(napi_env env, napi_callback_info info)
30 {
31 NFuncArg funcArg(env, info);
32 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
33 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
34 return nullptr;
35 }
36
37 auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
38 if (!resGetFirstArg) {
39 UniError(EINVAL).ThrowErr(env, "Invalid path");
40 return nullptr;
41 }
42
43 if (rmdir(path.get()) == -1) {
44 UniError(errno).ThrowErr(env);
45 return nullptr;
46 }
47
48 return NVal::CreateUndefined(env).val_;
49 }
50
Async(napi_env env,napi_callback_info info)51 napi_value Rmdir::Async(napi_env env, napi_callback_info info)
52 {
53 NFuncArg funcArg(env, info);
54 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
55 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
56 return nullptr;
57 }
58
59 auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
60 if (!resGetFirstArg) {
61 UniError(EINVAL).ThrowErr(env, "Invalid path");
62 return nullptr;
63 }
64
65 auto cbExec = [path = string(path.get())](napi_env env) -> UniError {
66 int res = rmdir(path.c_str());
67 if (res == -1) {
68 return UniError(errno);
69 } else {
70 return UniError(ERRNO_NOERR);
71 }
72 };
73
74 auto cbCompl = [](napi_env env, UniError err) -> NVal {
75 if (err) {
76 return { env, err.GetNapiErr(env) };
77 } else {
78 return NVal::CreateUndefined(env);
79 }
80 };
81
82 const string procedureName = "FileIORmdir";
83 NVal thisVar(env, funcArg.GetThisVar());
84 size_t argc = funcArg.GetArgc();
85 if (argc == NARG_CNT::ONE) {
86 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
87 } else {
88 NVal cb(env, funcArg[NARG_POS::SECOND]);
89 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
90 }
91 }
92 } // namespace ModuleFileIO
93 } // namespace DistributedFS
94 } // namespace OHOS