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 "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 bool succ = false;
43 unique_ptr<char[]> path;
44 tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
45 if (!succ) {
46 UniError(EINVAL).ThrowErr(env, "Invalid path");
47 return nullptr;
48 }
49
50 std::unique_ptr<DIR, std::function<void(DIR *)>> dir = { opendir(path.get()), closedir };
51 if (!dir) {
52 UniError(errno).ThrowErr(env);
53 return nullptr;
54 }
55
56 napi_value objDir = NClass::InstantiateClass(env, DirNExporter::className_, {});
57 if (!objDir) {
58 UniError(EINVAL).ThrowErr(env, "Cannot instantiate class DirSync");
59 return nullptr;
60 }
61
62 auto dirEntity = NClass::GetEntityOf<DirEntity>(env, objDir);
63 if (!dirEntity) {
64 UniError(EINVAL).ThrowErr(env, "Cannot get the entity of objDir");
65 return nullptr;
66 }
67 dirEntity->dir_.swap(dir);
68 return objDir;
69 }
70
71 struct OpenDirArgs {
72 NRef dirPtr_;
73 DIR *dir = nullptr;
OpenDirArgsOHOS::DistributedFS::ModuleFileIO::OpenDirArgs74 explicit OpenDirArgs(NVal dirPtr) : dirPtr_(dirPtr) {}
75 ~OpenDirArgs() = default;
76 };
77
Async(napi_env env,napi_callback_info info)78 napi_value OpenDir::Async(napi_env env, napi_callback_info info)
79 {
80 NFuncArg funcArg(env, info);
81 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
82 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
83 return nullptr;
84 }
85 string path;
86 unique_ptr<char[]> tmp;
87 bool succ = false;
88 tie(succ, tmp, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
89 if (!succ) {
90 UniError(EINVAL).ThrowErr(env, "Invalid path");
91 return nullptr;
92 }
93
94 path = tmp.get();
95 auto arg = make_shared<OpenDirArgs>(NVal(env, funcArg.GetThisVar()));
96 auto cbExec = [arg, path](napi_env env) -> UniError {
97 DIR *dir = nullptr;
98 dir = opendir(path.c_str());
99 if (dir == nullptr) {
100 return UniError(errno);
101 }
102 arg->dir = dir;
103 return UniError(ERRNO_NOERR);
104 };
105 auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
106 if (err) {
107 return { env, err.GetNapiErr(env) };
108 }
109 std::unique_ptr<DIR, std::function<void(DIR *)>> dir = { arg->dir, closedir };
110 if (!dir) {
111 return { env, UniError(errno).GetNapiErr(env) };
112 }
113
114 napi_value objDir = NClass::InstantiateClass(env, DirNExporter::className_, {});
115 if (!objDir) {
116 return { env, UniError(EINVAL).GetNapiErr(env) };
117 }
118
119 auto dirEntity = NClass::GetEntityOf<DirEntity>(env, objDir);
120 if (!dirEntity) {
121 return { env, UniError(EINVAL).GetNapiErr(env) };
122 }
123 dirEntity->dir_.swap(dir);
124 return { env, objDir };
125 };
126
127 NVal thisVar(env, funcArg.GetThisVar());
128 if (funcArg.GetArgc() == NARG_CNT::ONE) {
129 return NAsyncWorkPromise(env, thisVar).Schedule("fileIOOpenDir", cbExec, cbCompl).val_;
130 } else {
131 NVal cb(env, funcArg[NARG_POS::SECOND]);
132 return NAsyncWorkCallback(env, thisVar, cb).Schedule("fileIOOpenDir", cbExec, cbCompl).val_;
133 }
134 }
135 } // namespace ModuleFileIO
136 } // namespace DistributedFS
137 } // namespace OHOS