1 /*
2 * Copyright (c) 2022-2023 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 "raw_file_manager.h"
17
18 #include <climits>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <securec.h>
24 #include <unistd.h>
25
26 #include "raw_dir.h"
27 #include "raw_file.h"
28 #include "resource_manager.h"
29 #include "resource_manager_addon.h"
30 #include "resource_manager_impl.h"
31 #include "hilog_wrapper.h"
32
33 #ifdef __WINNT__
34 #include <shlwapi.h>
35 #include <windows.h>
36 #endif
37
38 using namespace OHOS::Global::Resource;
39
40 std::mutex g_rawDirMutex;
41 std::mutex g_rawFileMutex;
42 std::mutex g_rawFile64Mutex;
43 struct NativeResourceManager {
44 std::shared_ptr<ResourceManager> resManager = nullptr;
45 };
46
47 struct FileNameCache {
48 std::vector<std::string> names;
49 };
50
51 struct RawDir {
52 std::shared_ptr<ResourceManager> resManager = nullptr;
53 struct FileNameCache fileNameCache;
54 };
55
56 struct ActualOffset {
57 int64_t offset;
ActualOffsetActualOffset58 explicit ActualOffset() : offset(0) {}
59 };
60
61 struct RawFile {
62 const std::string filePath;
63 int64_t offset;
64 int64_t length;
65 FILE* pf;
66 uint8_t* buffer;
67 const NativeResourceManager *resMgr;
68 std::unique_ptr<ActualOffset> actualOffset;
69
RawFileRawFile70 explicit RawFile(const std::string &path) : filePath(path), offset(0), length(0),
71 pf(nullptr), buffer(nullptr), resMgr(nullptr), actualOffset(std::make_unique<ActualOffset>()) {}
72
~RawFileRawFile73 ~RawFile()
74 {
75 if (buffer != nullptr) {
76 delete[] buffer;
77 buffer = nullptr;
78 }
79 if (pf != nullptr) {
80 fclose(pf);
81 pf = nullptr;
82 }
83 }
84
openRawFile85 bool open()
86 {
87 pf = std::fopen(filePath.c_str(), "rb");
88 return pf != nullptr;
89 }
90 };
91
OH_ResourceManager_InitNativeResourceManager(napi_env env,napi_value jsResMgr)92 NativeResourceManager *OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr)
93 {
94 napi_valuetype valueType;
95 napi_typeof(env, jsResMgr, &valueType);
96 if (valueType != napi_object) {
97 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "jsResMgr is not an object");
98 return nullptr;
99 }
100 std::shared_ptr<ResourceManagerAddon> *addonPtr = nullptr;
101 napi_status status = napi_unwrap(env, jsResMgr, reinterpret_cast<void **>(&addonPtr));
102 if (status != napi_ok) {
103 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "Failed to get native resourcemanager");
104 return nullptr;
105 }
106 if (addonPtr == nullptr) {
107 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "Failed to unwrap, addonPtr is null");
108 return nullptr;
109 }
110 std::unique_ptr<NativeResourceManager> result = std::make_unique<NativeResourceManager>();
111 result->resManager = (*addonPtr)->GetResMgr();
112 return result.release();
113 }
114
OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager * resMgr)115 void OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager *resMgr)
116 {
117 if (resMgr != nullptr) {
118 delete resMgr;
119 resMgr = nullptr;
120 }
121 }
122
IsLoadHap(const NativeResourceManager * mgr,std::string & hapPath)123 static bool IsLoadHap(const NativeResourceManager *mgr, std::string &hapPath)
124 {
125 return mgr->resManager->IsLoadHap(hapPath) == RState::SUCCESS ? true : false;
126 }
127
LoadRawDirFromHap(const NativeResourceManager * mgr,const std::string dirName)128 RawDir *LoadRawDirFromHap(const NativeResourceManager *mgr, const std::string dirName)
129 {
130 std::unique_ptr<RawDir> result = std::make_unique<RawDir>();
131 RState state = mgr->resManager->GetRawFileList(dirName, result->fileNameCache.names);
132 if (state != RState::SUCCESS) {
133 RESMGR_HILOGD(RESMGR_RAWFILE_TAG, "failed to get RawDir dirName, %{public}s", dirName.c_str());
134 return nullptr;
135 }
136 return result.release();
137 }
138
OH_ResourceManager_OpenRawDir(const NativeResourceManager * mgr,const char * dirName)139 RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName)
140 {
141 std::lock_guard<std::mutex> lock(g_rawDirMutex);
142 if (mgr == nullptr || dirName == nullptr || mgr->resManager == nullptr) {
143 return nullptr;
144 }
145 std::string hapPath;
146 if (IsLoadHap(mgr, hapPath)) {
147 return LoadRawDirFromHap(mgr, dirName);
148 }
149 ResourceManagerImpl* impl = static_cast<ResourceManagerImpl *>(mgr->resManager.get());
150 std::string tempName = dirName;
151 const std::string rawFileDirName = tempName.empty() ? "rawfile" : "rawfile/";
152 if (tempName.length() < rawFileDirName.length()
153 || (tempName.compare(0, rawFileDirName.length(), rawFileDirName) != 0)) {
154 tempName = rawFileDirName + tempName;
155 }
156 std::unique_ptr<RawDir> result = std::make_unique<RawDir>();
157 std::vector<std::string> resourcesPaths = impl->GetResourcePaths();
158 for (auto iter = resourcesPaths.begin(); iter != resourcesPaths.end(); iter++) {
159 std::string currentPath = *iter + tempName;
160 DIR* dir = opendir(currentPath.c_str());
161 if (dir == nullptr) {
162 continue;
163 }
164 struct dirent *dirp = readdir(dir);
165 while (dirp != nullptr) {
166 if (std::strcmp(dirp->d_name, ".") == 0 ||
167 std::strcmp(dirp->d_name, "..") == 0) {
168 dirp = readdir(dir);
169 continue;
170 }
171 if (dirp->d_type == DT_REG || dirp->d_type == DT_DIR) {
172 result->fileNameCache.names.push_back(tempName + "/" + dirp->d_name);
173 }
174
175 dirp = readdir(dir);
176 }
177 closedir(dir);
178 }
179 return result.release();
180 }
181
LoadRawFileFromHap(const NativeResourceManager * mgr,const char * fileName,const std::string hapPath)182 RawFile *LoadRawFileFromHap(const NativeResourceManager *mgr, const char *fileName, const std::string hapPath)
183 {
184 size_t len;
185 std::unique_ptr<uint8_t[]> tmpBuf;
186 RState state = mgr->resManager->GetRawFileFromHap(fileName, len, tmpBuf);
187 if (state != SUCCESS || tmpBuf == nullptr) {
188 RESMGR_HILOGD(RESMGR_RAWFILE_TAG, "failed to get rawfile");
189 return nullptr;
190 }
191 auto result = std::make_unique<RawFile>(fileName);
192 result->buffer = tmpBuf.release();
193 if (result->buffer == nullptr) {
194 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed get file buffer");
195 return nullptr;
196 }
197 FILE* zipFile = fopen(hapPath.c_str(), "r");
198 if (!zipFile) {
199 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed open file");
200 return nullptr;
201 }
202 result->pf = zipFile;
203 result->length = static_cast<long>(len);
204 result->resMgr = mgr;
205 return result.release();
206 }
207
OH_ResourceManager_OpenRawFile(const NativeResourceManager * mgr,const char * fileName)208 RawFile *OH_ResourceManager_OpenRawFile(const NativeResourceManager *mgr, const char *fileName)
209 {
210 std::lock_guard<std::mutex> lock(g_rawFileMutex);
211 if (mgr == nullptr || fileName == nullptr || mgr->resManager == nullptr) {
212 return nullptr;
213 }
214
215 std::string hapPath;
216 if (IsLoadHap(mgr, hapPath)) {
217 return LoadRawFileFromHap(mgr, fileName, hapPath);
218 }
219
220 std::string filePath;
221 RState state = mgr->resManager->GetRawFilePathByName(fileName, filePath);
222 if (state != SUCCESS) {
223 return nullptr;
224 }
225 auto result = std::make_unique<RawFile>(filePath);
226 if (!result->open()) {
227 return nullptr;
228 }
229
230 std::fseek(result->pf, 0, SEEK_END);
231 result->length = ftell(result->pf);
232 std::fseek(result->pf, 0, SEEK_SET);
233 return result.release();
234 }
235
OH_ResourceManager_GetRawFileCount(RawDir * rawDir)236 int OH_ResourceManager_GetRawFileCount(RawDir *rawDir)
237 {
238 if (rawDir == nullptr) {
239 return 0;
240 }
241 return rawDir->fileNameCache.names.size();
242 }
243
OH_ResourceManager_GetRawFileName(RawDir * rawDir,int index)244 const char *OH_ResourceManager_GetRawFileName(RawDir *rawDir, int index)
245 {
246 if (rawDir == nullptr || index < 0) {
247 return nullptr;
248 }
249 uint32_t rawFileCount = rawDir->fileNameCache.names.size();
250 if (rawFileCount == 0 || index >= static_cast<int>(rawFileCount)) {
251 return nullptr;
252 }
253 return rawDir->fileNameCache.names[index].c_str();
254 }
255
OH_ResourceManager_CloseRawDir(RawDir * rawDir)256 void OH_ResourceManager_CloseRawDir(RawDir *rawDir)
257 {
258 std::lock_guard<std::mutex> lock(g_rawDirMutex);
259 if (rawDir != nullptr) {
260 delete rawDir;
261 rawDir = nullptr;
262 }
263 }
264
OH_ResourceManager_ReadRawFile(const RawFile * rawFile,void * buf,size_t length)265 int OH_ResourceManager_ReadRawFile(const RawFile *rawFile, void *buf, size_t length)
266 {
267 if (rawFile == nullptr || rawFile->actualOffset == nullptr || buf == nullptr || length == 0) {
268 return 0;
269 }
270 if (rawFile->buffer != nullptr) {
271 size_t len = static_cast<size_t>(OH_ResourceManager_GetRawFileRemainingLength(rawFile));
272 if (length > len) {
273 length = len;
274 }
275 int ret = memcpy_s(buf, length, rawFile->buffer + rawFile->actualOffset->offset, length);
276 if (ret != 0) {
277 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to copy to buf");
278 return 0;
279 }
280 rawFile->actualOffset->offset += static_cast<int64_t>(length);
281 return static_cast<int>(length);
282 } else {
283 return std::fread(buf, 1, length, rawFile->pf);
284 }
285 }
286
OH_ResourceManager_SeekRawFile(const RawFile * rawFile,long offset,int whence)287 int OH_ResourceManager_SeekRawFile(const RawFile *rawFile, long offset, int whence)
288 {
289 if (rawFile == nullptr || rawFile->actualOffset == nullptr || abs(offset) > rawFile->length) {
290 return -1;
291 }
292
293 int origin = 0;
294 switch (whence) {
295 case SEEK_SET:
296 origin = SEEK_SET;
297 rawFile->actualOffset->offset = offset;
298 break;
299 case SEEK_CUR:
300 origin = SEEK_CUR;
301 rawFile->actualOffset->offset = rawFile->actualOffset->offset + offset;
302 break;
303 case SEEK_END:
304 origin = SEEK_END;
305 rawFile->actualOffset->offset = rawFile->length + offset;
306 break;
307 default:
308 return -1;
309 }
310
311 if (rawFile->actualOffset->offset < 0 || rawFile->actualOffset->offset > rawFile->length) {
312 return -1;
313 }
314
315 return std::fseek(rawFile->pf, rawFile->actualOffset->offset, origin);
316 }
317
OH_ResourceManager_GetRawFileSize(RawFile * rawFile)318 long OH_ResourceManager_GetRawFileSize(RawFile *rawFile)
319 {
320 if (rawFile == nullptr || rawFile->actualOffset == nullptr) {
321 return 0;
322 }
323
324 return static_cast<long>(rawFile->length);
325 }
326
OH_ResourceManager_GetRawFileRemainingLength(const RawFile * rawFile)327 long OH_ResourceManager_GetRawFileRemainingLength(const RawFile *rawFile)
328 {
329 if (rawFile == nullptr || rawFile->actualOffset == nullptr ||
330 rawFile->length < rawFile->actualOffset->offset) {
331 return 0;
332 }
333
334 return static_cast<long>(rawFile->length - rawFile->actualOffset->offset);
335 }
336
OH_ResourceManager_CloseRawFile(RawFile * rawFile)337 void OH_ResourceManager_CloseRawFile(RawFile *rawFile)
338 {
339 std::lock_guard<std::mutex> lock(g_rawFileMutex);
340 if (rawFile != nullptr) {
341 delete rawFile;
342 rawFile = nullptr;
343 }
344 }
345
OH_ResourceManager_GetRawFileOffset(const RawFile * rawFile)346 long OH_ResourceManager_GetRawFileOffset(const RawFile *rawFile)
347 {
348 if (rawFile == nullptr || rawFile->actualOffset == nullptr) {
349 return 0;
350 }
351 return static_cast<long>(rawFile->actualOffset->offset);
352 }
353
GetRawFileDescriptorFromHap(const RawFile * rawFile,RawFileDescriptor & descriptor)354 static bool GetRawFileDescriptorFromHap(const RawFile *rawFile, RawFileDescriptor &descriptor)
355 {
356 ResourceManager::RawFileDescriptor resMgrDescriptor;
357 if (rawFile->resMgr->resManager == nullptr || rawFile->filePath == "") {
358 return false;
359 }
360 RState state = rawFile->resMgr->resManager->GetRawFdNdkFromHap(rawFile->filePath, resMgrDescriptor);
361 if (state != SUCCESS) {
362 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "GetRawFileDescriptorFromHap failed");
363 return false;
364 }
365 descriptor.fd = resMgrDescriptor.fd;
366 descriptor.length = static_cast<long>(resMgrDescriptor.length);
367 descriptor.start = static_cast<long>(resMgrDescriptor.offset);
368 return true;
369 }
370
OH_ResourceManager_GetRawFileDescriptor(const RawFile * rawFile,RawFileDescriptor & descriptor)371 bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor)
372 {
373 return OH_ResourceManager_GetRawFileDescriptorData(rawFile, &descriptor);
374 }
375
OH_ResourceManager_GetRawFileDescriptorData(const RawFile * rawFile,RawFileDescriptor * descriptor)376 bool OH_ResourceManager_GetRawFileDescriptorData(const RawFile *rawFile, RawFileDescriptor *descriptor)
377 {
378 if (rawFile == nullptr || rawFile->actualOffset == nullptr) {
379 return false;
380 }
381 if (rawFile->resMgr != nullptr) {
382 return GetRawFileDescriptorFromHap(rawFile, *descriptor);
383 }
384 char paths[PATH_MAX] = {0};
385 #ifdef __WINNT__
386 if (!PathCanonicalizeA(paths, rawFile->filePath.c_str())) {
387 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to PathCanonicalizeA the rawFile path");
388 }
389 #else
390 if (realpath(rawFile->filePath.c_str(), paths) == nullptr) {
391 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to realpath the rawFile path");
392 }
393 #endif
394 int fd = open(paths, O_RDONLY);
395 if (fd > 0) {
396 descriptor->fd = fd;
397 descriptor->length = static_cast<long>(rawFile->length);
398 descriptor->start = static_cast<long>(rawFile->actualOffset->offset);
399 } else {
400 return false;
401 }
402 return true;
403 }
404
OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor & descriptor)405 bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor)
406 {
407 return OH_ResourceManager_ReleaseRawFileDescriptorData(&descriptor);
408 }
409
OH_ResourceManager_ReleaseRawFileDescriptorData(const RawFileDescriptor * descriptor)410 bool OH_ResourceManager_ReleaseRawFileDescriptorData(const RawFileDescriptor *descriptor)
411 {
412 if (descriptor->fd > 0) {
413 return close(descriptor->fd) == 0;
414 }
415 return true;
416 }
417
418 struct Raw {
419 const std::string filePath;
420 int64_t offset; // offset base on the rawfile
421 int64_t length;
422 int64_t start; // offset base on the Hap
423 FILE* pf;
424 const NativeResourceManager *resMgr;
425
RawRaw426 explicit Raw(const std::string &path) : filePath(path), offset(0), length(0), start(0),
427 pf(nullptr), resMgr(nullptr) {}
428
~RawRaw429 ~Raw()
430 {
431 if (pf != nullptr) {
432 fclose(pf);
433 pf = nullptr;
434 }
435 }
436
openRaw437 bool open()
438 {
439 pf = std::fopen(filePath.c_str(), "rb");
440 return pf != nullptr;
441 }
442 };
443
444 struct RawFile64 {
445 std::unique_ptr<Raw> raw;
RawFile64RawFile64446 explicit RawFile64(std::unique_ptr<Raw> raw) : raw(std::move(raw)) {}
447 };
448
LoadRawFileFromHap64(const NativeResourceManager * mgr,const char * fileName,const std::string hapPath)449 RawFile64 *LoadRawFileFromHap64(const NativeResourceManager *mgr, const char *fileName, const std::string hapPath)
450 {
451 ResourceManager::RawFileDescriptor resMgrDescriptor;
452 RState state = mgr->resManager->GetRawFdNdkFromHap(fileName, resMgrDescriptor);
453 if (state != SUCCESS) {
454 RESMGR_HILOGD(RESMGR_RAWFILE_TAG, "failed to get rawfile descriptor");
455 return nullptr;
456 }
457 auto result = std::make_unique<Raw>(fileName);
458 result->pf = fdopen(resMgrDescriptor.fd, "rb");
459 if (result->pf == nullptr) {
460 if (resMgrDescriptor.fd > 0) {
461 close(resMgrDescriptor.fd);
462 resMgrDescriptor.fd = 0;
463 }
464 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to open rawfile descriptor");
465 return nullptr;
466 }
467 result->length = resMgrDescriptor.length;
468 result->start = resMgrDescriptor.offset;
469 result->resMgr = mgr;
470 std::fseek(result->pf, result->start, SEEK_SET);
471 return new RawFile64(std::move(result));
472 }
473
OH_ResourceManager_OpenRawFile64(const NativeResourceManager * mgr,const char * fileName)474 RawFile64 *OH_ResourceManager_OpenRawFile64(const NativeResourceManager *mgr, const char *fileName)
475 {
476 std::lock_guard<std::mutex> lock(g_rawFile64Mutex);
477 if (mgr == nullptr || fileName == nullptr || mgr->resManager == nullptr) {
478 return nullptr;
479 }
480
481 std::string hapPath;
482 if (IsLoadHap(mgr, hapPath)) {
483 return LoadRawFileFromHap64(mgr, fileName, hapPath);
484 }
485 std::string filePath;
486 RState state = mgr->resManager->GetRawFilePathByName(fileName, filePath);
487 if (state != SUCCESS) {
488 return nullptr;
489 }
490 auto result = std::make_unique<Raw>(filePath);
491 if (!result->open()) {
492 return nullptr;
493 }
494
495 std::fseek(result->pf, 0, SEEK_END);
496 result->length = ftell(result->pf);
497 std::fseek(result->pf, 0, SEEK_SET);
498 return new RawFile64(std::move(result));
499 }
500
OH_ResourceManager_ReadRawFile64(const RawFile64 * rawFile,void * buf,int64_t length)501 int64_t OH_ResourceManager_ReadRawFile64(const RawFile64 *rawFile, void *buf, int64_t length)
502 {
503 if (rawFile == nullptr || rawFile->raw == nullptr || buf == nullptr || length == 0) {
504 return 0;
505 }
506 int64_t len = OH_ResourceManager_GetRawFileRemainingLength64(rawFile);
507 if (length > len) {
508 length = len;
509 }
510 size_t ret = std::fread(buf, 1, length, rawFile->raw->pf);
511 if (ret == 0) {
512 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to fread");
513 return 0;
514 }
515 rawFile->raw->offset += length;
516 return static_cast<int64_t>(ret);
517 }
518
OH_ResourceManager_SeekRawFile64(const RawFile64 * rawFile,int64_t offset,int whence)519 int OH_ResourceManager_SeekRawFile64(const RawFile64 *rawFile, int64_t offset, int whence)
520 {
521 if (rawFile == nullptr || rawFile->raw == nullptr || abs(offset) > rawFile->raw->length) {
522 return -1;
523 }
524
525 int origin = 0;
526 int64_t actualOffset = 0;
527 switch (whence) {
528 case SEEK_SET:
529 origin = SEEK_SET;
530 rawFile->raw->offset = offset;
531 actualOffset = rawFile->raw->start + offset;
532 break;
533 case SEEK_CUR:
534 origin = SEEK_CUR;
535 rawFile->raw->offset = rawFile->raw->offset + offset;
536 actualOffset = offset;
537 break;
538 case SEEK_END:
539 origin = SEEK_SET;
540 rawFile->raw->offset = rawFile->raw->length + offset;
541 actualOffset = rawFile->raw->start + rawFile->raw->length + offset;
542 break;
543 default:
544 return -1;
545 }
546
547 if (rawFile->raw->offset < 0 || rawFile->raw->offset > rawFile->raw->length) {
548 return -1;
549 }
550
551 return std::fseek(rawFile->raw->pf, actualOffset, origin);
552 }
553
OH_ResourceManager_GetRawFileSize64(RawFile64 * rawFile)554 int64_t OH_ResourceManager_GetRawFileSize64(RawFile64 *rawFile)
555 {
556 if (rawFile == nullptr || rawFile->raw == nullptr) {
557 return 0;
558 }
559
560 return rawFile->raw->length;
561 }
562
OH_ResourceManager_GetRawFileRemainingLength64(const RawFile64 * rawFile)563 int64_t OH_ResourceManager_GetRawFileRemainingLength64(const RawFile64 *rawFile)
564 {
565 if (rawFile == nullptr || rawFile->raw == nullptr ||
566 rawFile->raw->length < rawFile->raw->offset) {
567 return 0;
568 }
569
570 return rawFile->raw->length - rawFile->raw->offset;
571 }
572
OH_ResourceManager_CloseRawFile64(RawFile64 * rawFile)573 void OH_ResourceManager_CloseRawFile64(RawFile64 *rawFile)
574 {
575 std::lock_guard<std::mutex> lock(g_rawFile64Mutex);
576 if (rawFile != nullptr) {
577 delete rawFile;
578 rawFile = nullptr;
579 }
580 }
581
OH_ResourceManager_GetRawFileOffset64(const RawFile64 * rawFile)582 int64_t OH_ResourceManager_GetRawFileOffset64(const RawFile64 *rawFile)
583 {
584 if (rawFile == nullptr || rawFile->raw == nullptr) {
585 return 0;
586 }
587 return rawFile->raw->offset;
588 }
589
GetRawFileDescriptorFromHap64(const RawFile64 * rawFile,RawFileDescriptor64 * descriptor)590 static bool GetRawFileDescriptorFromHap64(const RawFile64 *rawFile, RawFileDescriptor64 *descriptor)
591 {
592 ResourceManager::RawFileDescriptor resMgrDescriptor;
593 if (rawFile->raw->resMgr->resManager == nullptr) {
594 return false;
595 }
596 RState state = rawFile->raw->resMgr->resManager->
597 GetRawFdNdkFromHap(rawFile->raw->filePath, resMgrDescriptor);
598 if (state != SUCCESS) {
599 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "GetRawFileDescriptorFromHap64 failed");
600 return false;
601 }
602 descriptor->fd = resMgrDescriptor.fd;
603 descriptor->length = resMgrDescriptor.length;
604 descriptor->start = resMgrDescriptor.offset;
605 return true;
606 }
607
OH_ResourceManager_GetRawFileDescriptor64(const RawFile64 * rawFile,RawFileDescriptor64 * descriptor)608 bool OH_ResourceManager_GetRawFileDescriptor64(const RawFile64 *rawFile, RawFileDescriptor64 *descriptor)
609 {
610 if (rawFile == nullptr || rawFile->raw == nullptr) {
611 return false;
612 }
613 if (rawFile->raw->resMgr != nullptr) {
614 return GetRawFileDescriptorFromHap64(rawFile, descriptor);
615 }
616 char paths[PATH_MAX] = {0};
617 #ifdef __WINNT__
618 if (!PathCanonicalizeA(paths, rawFile->raw->filePath.c_str())) {
619 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to PathCanonicalizeA the rawFile path");
620 return false;
621 }
622 #else
623 if (realpath(rawFile->raw->filePath.c_str(), paths) == nullptr) {
624 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to realpath the rawFile path");
625 return false;
626 }
627 #endif
628 int fd = open(paths, O_RDONLY);
629 if (fd > 0) {
630 descriptor->fd = fd;
631 descriptor->length = rawFile->raw->length;
632 descriptor->start = rawFile->raw->offset;
633 } else {
634 return false;
635 }
636 return true;
637 }
638
OH_ResourceManager_ReleaseRawFileDescriptor64(const RawFileDescriptor64 * descriptor)639 bool OH_ResourceManager_ReleaseRawFileDescriptor64(const RawFileDescriptor64 *descriptor)
640 {
641 if (descriptor->fd > 0) {
642 return close(descriptor->fd) == 0;
643 }
644 return true;
645 }
646
OH_ResourceManager_IsRawDir(const NativeResourceManager * mgr,const char * path)647 bool OH_ResourceManager_IsRawDir(const NativeResourceManager *mgr, const char *path)
648 {
649 bool result = false;
650 if (mgr == nullptr || path == nullptr || mgr->resManager == nullptr) {
651 return result;
652 }
653 RState state = mgr->resManager->IsRawDirFromHap(path, result);
654 if (state != SUCCESS) {
655 RESMGR_HILOGD(RESMGR_RAWFILE_TAG, "failed to determine whether the path is a directory");
656 return result;
657 }
658 return result;
659 }