1 /*
2 * Copyright (C) 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 #ifndef NAPI_FILEACCESS_HELPER_H
17 #define NAPI_FILEACCESS_HELPER_H
18
19 #include "napi_root_info_exporter.h"
20
21 #include "file_access_framework_errno.h"
22 #include "file_filter.h"
23 #include "file_iterator_entity.h"
24 #include "hilog_wrapper.h"
25 #include "napi_file_iterator_exporter.h"
26 #include "napi_utils.h"
27 #include "root_info_entity.h"
28
29 namespace OHOS {
30 namespace FileAccessFwk {
Export()31 bool NapiRootInfoExporter::Export()
32 {
33 std::vector<napi_property_descriptor> props = {
34 NVal::DeclareNapiFunction("listFile", ListFile),
35 NVal::DeclareNapiFunction("scanFile", ScanFile),
36 NVal::DeclareNapiGetter("deviceType", GetDeviceType),
37 NVal::DeclareNapiGetter("uri", GetUri),
38 NVal::DeclareNapiGetter("displayName", GetDisplayName),
39 NVal::DeclareNapiGetter("deviceFlags", GetDeviceFlags)
40 };
41
42 std::string className = GetClassName();
43 bool succ = false;
44 napi_value classValue = nullptr;
45 std::tie(succ, classValue) = NClass::DefineClass(exports_.env_, className,
46 NapiRootInfoExporter::Constructor, std::move(props));
47 if (!succ) {
48 NError(E_GETRESULT).ThrowErr(exports_.env_);
49 return false;
50 }
51
52 succ = NClass::SaveClass(exports_.env_, className, classValue);
53 if (!succ) {
54 NError(E_GETRESULT).ThrowErr(exports_.env_);
55 return false;
56 }
57
58 return exports_.AddProp(className, classValue);
59 }
60
Constructor(napi_env env,napi_callback_info info)61 napi_value NapiRootInfoExporter::Constructor(napi_env env, napi_callback_info info)
62 {
63 NFuncArg funcArg(env, info);
64 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
65 NError(EINVAL).ThrowErr(env);
66 return nullptr;
67 }
68
69 auto rootInfoEntity = std::make_unique<RootInfoEntity>();
70 if (rootInfoEntity == nullptr) {
71 NError(E_GETRESULT).ThrowErr(env);
72 return nullptr;
73 }
74
75 if (!NClass::SetEntityFor<RootInfoEntity>(env, funcArg.GetThisVar(), std::move(rootInfoEntity))) {
76 NError(E_GETRESULT).ThrowErr(env);
77 return nullptr;
78 }
79
80 return funcArg.GetThisVar();
81 }
82
ListFile(napi_env env,napi_callback_info info)83 napi_value NapiRootInfoExporter::ListFile(napi_env env, napi_callback_info info)
84 {
85 NFuncArg funcArg(env, info);
86 if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
87 NError(EINVAL).ThrowErr(env);
88 return nullptr;
89 }
90
91 FileFilter filter({}, {}, {}, 0, 0, false, false);
92 if (funcArg.GetArgc() == NARG_CNT::ONE) {
93 auto ret = GetFileFilterParam(NVal(env, funcArg.GetArg(NARG_POS::FIRST)), filter);
94 if (ret != ERR_OK) {
95 NError(ret).ThrowErr(env);
96 return nullptr;
97 }
98 }
99
100 auto rootEntity = NClass::GetEntityOf<RootInfoEntity>(env, funcArg.GetThisVar());
101 if (rootEntity == nullptr) {
102 NError(E_GETRESULT).ThrowErr(env);
103 return nullptr;
104 }
105
106 if (rootEntity->fileAccessHelper == nullptr) {
107 NError(E_GETRESULT).ThrowErr(env);
108 return nullptr;
109 }
110
111 napi_value objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {});
112 if (objFileIteratorExporter == nullptr) {
113 NError(E_GETRESULT).ThrowErr(env);
114 return nullptr;
115 }
116
117 auto fileIteratorEntity = NClass::GetEntityOf<FileIteratorEntity>(env, objFileIteratorExporter);
118 if (fileIteratorEntity == nullptr) {
119 NError(E_GETRESULT).ThrowErr(env);
120 return nullptr;
121 }
122
123 FileInfo fileInfo;
124 fileInfo.uri = rootEntity->rootInfo.uri;
125 fileInfo.mode = DOCUMENT_FLAG_REPRESENTS_DIR | DOCUMENT_FLAG_SUPPORTS_READ | DOCUMENT_FLAG_SUPPORTS_WRITE;
126 {
127 std::lock_guard<std::mutex> lock(fileIteratorEntity->entityOperateMutex);
128 fileIteratorEntity->fileAccessHelper = rootEntity->fileAccessHelper;
129 fileIteratorEntity->fileInfo = fileInfo;
130 fileIteratorEntity->fileInfoVec.clear();
131 fileIteratorEntity->offset = 0;
132 fileIteratorEntity->pos = 0;
133 fileIteratorEntity->filter = std::move(filter);
134 fileIteratorEntity->flag = 0;
135 auto ret = rootEntity->fileAccessHelper->ListFile(fileInfo, fileIteratorEntity->offset,
136 MAX_COUNT, filter, fileIteratorEntity->fileInfoVec);
137 if (ret != ERR_OK) {
138 NError(ret).ThrowErr(env);
139 return nullptr;
140 }
141 }
142
143 return NVal(env, objFileIteratorExporter).val_;
144 }
145
ScanFile(napi_env env,napi_callback_info info)146 napi_value NapiRootInfoExporter::ScanFile(napi_env env, napi_callback_info info)
147 {
148 NFuncArg funcArg(env, info);
149 if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
150 NError(EINVAL).ThrowErr(env);
151 return nullptr;
152 }
153
154 FileFilter filter({}, {}, {}, 0, 0, false, false);
155 if (funcArg.GetArgc() == NARG_CNT::ONE) {
156 auto ret = GetFileFilterParam(NVal(env, funcArg.GetArg(NARG_POS::FIRST)), filter);
157 if (ret != ERR_OK) {
158 NError(ret).ThrowErr(env);
159 return nullptr;
160 }
161 }
162
163 auto rootEntity = NClass::GetEntityOf<RootInfoEntity>(env, funcArg.GetThisVar());
164 if (rootEntity == nullptr) {
165 NError(E_GETRESULT).ThrowErr(env);
166 return nullptr;
167 }
168
169 if (rootEntity->fileAccessHelper == nullptr) {
170 NError(E_GETRESULT).ThrowErr(env);
171 return nullptr;
172 }
173
174 napi_value objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {});
175 if (objFileIteratorExporter == nullptr) {
176 NError(E_GETRESULT).ThrowErr(env);
177 return nullptr;
178 }
179
180 auto fileIteratorEntity = NClass::GetEntityOf<FileIteratorEntity>(env, objFileIteratorExporter);
181 if (fileIteratorEntity == nullptr) {
182 NError(E_GETRESULT).ThrowErr(env);
183 return nullptr;
184 }
185
186 FileInfo fileInfo;
187 fileInfo.uri = rootEntity->rootInfo.uri;
188 fileInfo.mode = DOCUMENT_FLAG_REPRESENTS_DIR | DOCUMENT_FLAG_SUPPORTS_READ | DOCUMENT_FLAG_SUPPORTS_WRITE;
189 {
190 std::lock_guard<std::mutex> lock(fileIteratorEntity->entityOperateMutex);
191 fileIteratorEntity->fileAccessHelper = rootEntity->fileAccessHelper;
192 fileIteratorEntity->fileInfo = fileInfo;
193 fileIteratorEntity->fileInfoVec.clear();
194 fileIteratorEntity->offset = 0;
195 fileIteratorEntity->pos = 0;
196 fileIteratorEntity->filter = std::move(filter);
197 fileIteratorEntity->flag = 1;
198 auto ret = rootEntity->fileAccessHelper->ScanFile(fileInfo, fileIteratorEntity->offset,
199 MAX_COUNT, filter, fileIteratorEntity->fileInfoVec);
200 if (ret != ERR_OK) {
201 NError(ret).ThrowErr(env);
202 return nullptr;
203 }
204 }
205
206 return NVal(env, objFileIteratorExporter).val_;
207 }
208
GetDeviceType(napi_env env,napi_callback_info info)209 napi_value NapiRootInfoExporter::GetDeviceType(napi_env env, napi_callback_info info)
210 {
211 NFuncArg funcArg(env, info);
212 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
213 NError(EINVAL).ThrowErr(env);
214 return nullptr;
215 }
216
217 auto rootInfoEntity = NClass::GetEntityOf<RootInfoEntity>(env, funcArg.GetThisVar());
218 if (rootInfoEntity == nullptr) {
219 NError(E_GETRESULT).ThrowErr(env);
220 return nullptr;
221 }
222
223 return NVal::CreateInt64(env, (int64_t)(rootInfoEntity->rootInfo.deviceType)).val_;
224 }
225
GetUri(napi_env env,napi_callback_info info)226 napi_value NapiRootInfoExporter::GetUri(napi_env env, napi_callback_info info)
227 {
228 NFuncArg funcArg(env, info);
229 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
230 NError(EINVAL).ThrowErr(env);
231 return nullptr;
232 }
233
234 auto rootInfoEntity = NClass::GetEntityOf<RootInfoEntity>(env, funcArg.GetThisVar());
235 if (rootInfoEntity == nullptr) {
236 NError(E_GETRESULT).ThrowErr(env);
237 return nullptr;
238 }
239
240 return NVal::CreateUTF8String(env, rootInfoEntity->rootInfo.uri).val_;
241 }
242
GetDisplayName(napi_env env,napi_callback_info info)243 napi_value NapiRootInfoExporter::GetDisplayName(napi_env env, napi_callback_info info)
244 {
245 NFuncArg funcArg(env, info);
246 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
247 NError(EINVAL).ThrowErr(env);
248 return nullptr;
249 }
250
251 auto rootInfoEntity = NClass::GetEntityOf<RootInfoEntity>(env, funcArg.GetThisVar());
252 if (rootInfoEntity == nullptr) {
253 NError(E_GETRESULT).ThrowErr(env);
254 return nullptr;
255 }
256
257 return NVal::CreateUTF8String(env, rootInfoEntity->rootInfo.displayName).val_;
258 }
259
GetDeviceFlags(napi_env env,napi_callback_info info)260 napi_value NapiRootInfoExporter::GetDeviceFlags(napi_env env, napi_callback_info info)
261 {
262 NFuncArg funcArg(env, info);
263 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
264 NError(EINVAL).ThrowErr(env);
265 return nullptr;
266 }
267
268 auto rootInfoEntity = NClass::GetEntityOf<RootInfoEntity>(env, funcArg.GetThisVar());
269 if (rootInfoEntity == nullptr) {
270 NError(E_GETRESULT).ThrowErr(env);
271 return nullptr;
272 }
273
274 return NVal::CreateInt64(env, rootInfoEntity->rootInfo.deviceFlags).val_;
275 }
276
GetClassName()277 std::string NapiRootInfoExporter::GetClassName()
278 {
279 return NapiRootInfoExporter::className_;
280 }
281 } // namespace FileAccessFwk
282 } // namespace OHOS
283 #endif // NAPI_FILEACCESS_HELPER_H