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 "open_dir.h"
17 #include <memory>
18 #include <string>
19 #include <tuple>
20
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_class.h"
24 #include "../../common/napi/n_func_arg.h"
25 #include "../../common/napi/n_val.h"
26 #include "../../common/uni_error.h"
27 #include "../class_dir/dir_entity.h"
28 #include "../class_dir/dir_n_exporter.h"
29
30 namespace OHOS {
31 namespace DistributedFS {
32 namespace ModuleFileIO {
33 using namespace std;
34
Sync(napi_env env,napi_callback_info info)35 napi_value OpenDir::Sync(napi_env env, napi_callback_info info)
36 {
37 NFuncArg funcArg(env, info);
38 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
39 return nullptr;
40 }
41
42 auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
43 if (!resGetFirstArg) {
44 UniError(EINVAL).ThrowErr(env, "Invalid path");
45 return nullptr;
46 }
47
48 std::unique_ptr<DIR, std::function<void(DIR *)>> dir = { opendir(path.get()), closedir };
49 if (dir == nullptr) {
50 UniError(errno).ThrowErr(env);
51 return nullptr;
52 }
53
54 napi_value objDir = NClass::InstantiateClass(env, DirNExporter::className_, {});
55 if (objDir == nullptr) {
56 UniError(EINVAL).ThrowErr(env, "Cannot instantiate class DirSync");
57 return nullptr;
58 }
59
60 auto dirEntity = NClass::GetEntityOf<DirEntity>(env, objDir);
61 if (dirEntity == nullptr) {
62 UniError(EINVAL).ThrowErr(env, "Cannot get the entity of objDir");
63 return nullptr;
64 }
65
66 dirEntity->dir_.swap(dir);
67 return objDir;
68 }
69
70 struct OpenDirArgs {
71 NRef dirPtr_;
72 DIR *dir = nullptr;
OpenDirArgsOHOS::DistributedFS::ModuleFileIO::OpenDirArgs73 explicit OpenDirArgs(NVal dirPtr) : dirPtr_(dirPtr) {}
74 ~OpenDirArgs() = default;
75 };
76
Async(napi_env env,napi_callback_info info)77 napi_value OpenDir::Async(napi_env env, napi_callback_info info)
78 {
79 NFuncArg funcArg(env, info);
80 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
81 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
82 return nullptr;
83 }
84
85 auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
86 if (!resGetFirstArg) {
87 UniError(EINVAL).ThrowErr(env, "Invalid path");
88 return nullptr;
89 }
90
91 string path = tmp.get();
92 auto arg = make_shared<OpenDirArgs>(NVal(env, funcArg.GetThisVar()));
93 auto cbExec = [arg, path](napi_env env) -> UniError {
94 DIR *dir = nullptr;
95 dir = opendir(path.c_str());
96 if (dir == nullptr) {
97 return UniError(errno);
98 }
99 arg->dir = dir;
100 return UniError(ERRNO_NOERR);
101 };
102
103 auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
104 if (err) {
105 return { env, err.GetNapiErr(env) };
106 }
107 std::unique_ptr<DIR, std::function<void(DIR *)>> dir = { arg->dir, closedir };
108 if (!dir) {
109 return { env, UniError(errno).GetNapiErr(env) };
110 }
111
112 napi_value objDir = NClass::InstantiateClass(env, DirNExporter::className_, {});
113 if (!objDir) {
114 return { env, UniError(EINVAL).GetNapiErr(env) };
115 }
116
117 auto dirEntity = NClass::GetEntityOf<DirEntity>(env, objDir);
118 if (!dirEntity) {
119 return { env, UniError(EINVAL).GetNapiErr(env) };
120 }
121 dirEntity->dir_.swap(dir);
122 return { env, objDir };
123 };
124
125 NVal thisVar(env, funcArg.GetThisVar());
126 if (funcArg.GetArgc() == NARG_CNT::ONE) {
127 return NAsyncWorkPromise(env, thisVar).Schedule("fileIOOpenDir", cbExec, cbCompl).val_;
128 } else {
129 NVal cb(env, funcArg[NARG_POS::SECOND]);
130 return NAsyncWorkCallback(env, thisVar, cb).Schedule("fileIOOpenDir", cbExec, cbCompl).val_;
131 }
132 }
133 } // namespace ModuleFileIO
134 } // namespace DistributedFS
135 } // namespace OHOS