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 %{public}s rawfile", fileName);
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 %{public}s", hapPath.c_str());
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) {
358 return false;
359 }
360 RState state = rawFile->resMgr->resManager->
361 GetRawFdNdkFromHap(rawFile->filePath, resMgrDescriptor);
362 if (state != SUCCESS) {
363 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "GetRawFileDescriptorFromHap failed");
364 return false;
365 }
366 descriptor.fd = resMgrDescriptor.fd;
367 descriptor.length = static_cast<long>(resMgrDescriptor.length);
368 descriptor.start = static_cast<long>(resMgrDescriptor.offset);
369 return true;
370 }
371
OH_ResourceManager_GetRawFileDescriptor(const RawFile * rawFile,RawFileDescriptor & descriptor)372 bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor)
373 {
374 return OH_ResourceManager_GetRawFileDescriptorData(rawFile, &descriptor);
375 }
376
OH_ResourceManager_GetRawFileDescriptorData(const RawFile * rawFile,RawFileDescriptor * descriptor)377 bool OH_ResourceManager_GetRawFileDescriptorData(const RawFile *rawFile, RawFileDescriptor *descriptor)
378 {
379 if (rawFile == nullptr || rawFile->actualOffset == nullptr) {
380 return false;
381 }
382 if (rawFile->resMgr != nullptr) {
383 return GetRawFileDescriptorFromHap(rawFile, *descriptor);
384 }
385 char paths[PATH_MAX] = {0};
386 #ifdef __WINNT__
387 if (!PathCanonicalizeA(paths, rawFile->filePath.c_str())) {
388 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to PathCanonicalizeA the rawFile path");
389 }
390 #else
391 if (realpath(rawFile->filePath.c_str(), paths) == nullptr) {
392 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to realpath the rawFile path");
393 }
394 #endif
395 int fd = open(paths, O_RDONLY);
396 if (fd > 0) {
397 descriptor->fd = fd;
398 descriptor->length = static_cast<long>(rawFile->length);
399 descriptor->start = static_cast<long>(rawFile->actualOffset->offset);
400 } else {
401 return false;
402 }
403 return true;
404 }
405
OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor & descriptor)406 bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor)
407 {
408 return OH_ResourceManager_ReleaseRawFileDescriptorData(&descriptor);
409 }
410
OH_ResourceManager_ReleaseRawFileDescriptorData(const RawFileDescriptor * descriptor)411 bool OH_ResourceManager_ReleaseRawFileDescriptorData(const RawFileDescriptor *descriptor)
412 {
413 if (descriptor->fd > 0) {
414 return close(descriptor->fd) == 0;
415 }
416 return true;
417 }
418
419 struct Raw {
420 const std::string filePath;
421 int64_t offset; // offset base on the rawfile
422 int64_t length;
423 int64_t start; // offset base on the Hap
424 FILE* pf;
425 const NativeResourceManager *resMgr;
426
RawRaw427 explicit Raw(const std::string &path) : filePath(path), offset(0), length(0), start(0),
428 pf(nullptr), resMgr(nullptr) {}
429
~RawRaw430 ~Raw()
431 {
432 if (pf != nullptr) {
433 fclose(pf);
434 pf = nullptr;
435 }
436 }
437
openRaw438 bool open()
439 {
440 pf = std::fopen(filePath.c_str(), "rb");
441 return pf != nullptr;
442 }
443 };
444
445 struct RawFile64 {
446 std::unique_ptr<Raw> raw;
RawFile64RawFile64447 explicit RawFile64(std::unique_ptr<Raw> raw) : raw(std::move(raw)) {}
448 };
449
LoadRawFileFromHap64(const NativeResourceManager * mgr,const char * fileName,const std::string hapPath)450 RawFile64 *LoadRawFileFromHap64(const NativeResourceManager *mgr, const char *fileName, const std::string hapPath)
451 {
452 ResourceManager::RawFileDescriptor resMgrDescriptor;
453 RState state = mgr->resManager->GetRawFdNdkFromHap(fileName, resMgrDescriptor);
454 if (state != SUCCESS) {
455 RESMGR_HILOGD(RESMGR_RAWFILE_TAG, "failed to get %{public}s rawfile descriptor", fileName);
456 return nullptr;
457 }
458 auto result = std::make_unique<Raw>(fileName);
459 result->pf = fdopen(resMgrDescriptor.fd, "rb");
460 if (result->pf == nullptr) {
461 if (resMgrDescriptor.fd > 0) {
462 close(resMgrDescriptor.fd);
463 resMgrDescriptor.fd = 0;
464 }
465 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to open %{public}s rawfile descriptor", fileName);
466 return nullptr;
467 }
468 result->length = resMgrDescriptor.length;
469 result->start = resMgrDescriptor.offset;
470 result->resMgr = mgr;
471 std::fseek(result->pf, result->start, SEEK_SET);
472 return new RawFile64(std::move(result));
473 }
474
OH_ResourceManager_OpenRawFile64(const NativeResourceManager * mgr,const char * fileName)475 RawFile64 *OH_ResourceManager_OpenRawFile64(const NativeResourceManager *mgr, const char *fileName)
476 {
477 std::lock_guard<std::mutex> lock(g_rawFile64Mutex);
478 if (mgr == nullptr || fileName == nullptr || mgr->resManager == nullptr) {
479 return nullptr;
480 }
481
482 std::string hapPath;
483 if (IsLoadHap(mgr, hapPath)) {
484 return LoadRawFileFromHap64(mgr, fileName, hapPath);
485 }
486 std::string filePath;
487 RState state = mgr->resManager->GetRawFilePathByName(fileName, filePath);
488 if (state != SUCCESS) {
489 return nullptr;
490 }
491 auto result = std::make_unique<Raw>(filePath);
492 if (!result->open()) {
493 return nullptr;
494 }
495
496 std::fseek(result->pf, 0, SEEK_END);
497 result->length = ftell(result->pf);
498 std::fseek(result->pf, 0, SEEK_SET);
499 return new RawFile64(std::move(result));
500 }
501
OH_ResourceManager_ReadRawFile64(const RawFile64 * rawFile,void * buf,int64_t length)502 int64_t OH_ResourceManager_ReadRawFile64(const RawFile64 *rawFile, void *buf, int64_t length)
503 {
504 if (rawFile == nullptr || rawFile->raw == nullptr || buf == nullptr || length == 0) {
505 return 0;
506 }
507 int64_t len = OH_ResourceManager_GetRawFileRemainingLength64(rawFile);
508 if (length > len) {
509 length = len;
510 }
511 size_t ret = std::fread(buf, 1, length, rawFile->raw->pf);
512 if (ret == 0) {
513 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to fread");
514 return 0;
515 }
516 rawFile->raw->offset += length;
517 return static_cast<int64_t>(ret);
518 }
519
OH_ResourceManager_SeekRawFile64(const RawFile64 * rawFile,int64_t offset,int whence)520 int OH_ResourceManager_SeekRawFile64(const RawFile64 *rawFile, int64_t offset, int whence)
521 {
522 if (rawFile == nullptr || rawFile->raw == nullptr || abs(offset) > rawFile->raw->length) {
523 return -1;
524 }
525
526 int origin = 0;
527 int64_t actualOffset = 0;
528 switch (whence) {
529 case SEEK_SET:
530 origin = SEEK_SET;
531 rawFile->raw->offset = offset;
532 actualOffset = rawFile->raw->start + offset;
533 break;
534 case SEEK_CUR:
535 origin = SEEK_CUR;
536 rawFile->raw->offset = rawFile->raw->offset + offset;
537 actualOffset = offset;
538 break;
539 case SEEK_END:
540 origin = SEEK_SET;
541 rawFile->raw->offset = rawFile->raw->length + offset;
542 actualOffset = rawFile->raw->start + rawFile->raw->length + offset;
543 break;
544 default:
545 return -1;
546 }
547
548 if (rawFile->raw->offset < 0 || rawFile->raw->offset > rawFile->raw->length) {
549 return -1;
550 }
551
552 return std::fseek(rawFile->raw->pf, actualOffset, origin);
553 }
554
OH_ResourceManager_GetRawFileSize64(RawFile64 * rawFile)555 int64_t OH_ResourceManager_GetRawFileSize64(RawFile64 *rawFile)
556 {
557 if (rawFile == nullptr || rawFile->raw == nullptr) {
558 return 0;
559 }
560
561 return rawFile->raw->length;
562 }
563
OH_ResourceManager_GetRawFileRemainingLength64(const RawFile64 * rawFile)564 int64_t OH_ResourceManager_GetRawFileRemainingLength64(const RawFile64 *rawFile)
565 {
566 if (rawFile == nullptr || rawFile->raw == nullptr ||
567 rawFile->raw->length < rawFile->raw->offset) {
568 return 0;
569 }
570
571 return rawFile->raw->length - rawFile->raw->offset;
572 }
573
OH_ResourceManager_CloseRawFile64(RawFile64 * rawFile)574 void OH_ResourceManager_CloseRawFile64(RawFile64 *rawFile)
575 {
576 std::lock_guard<std::mutex> lock(g_rawFile64Mutex);
577 if (rawFile != nullptr) {
578 delete rawFile;
579 rawFile = nullptr;
580 }
581 }
582
OH_ResourceManager_GetRawFileOffset64(const RawFile64 * rawFile)583 int64_t OH_ResourceManager_GetRawFileOffset64(const RawFile64 *rawFile)
584 {
585 if (rawFile == nullptr || rawFile->raw == nullptr) {
586 return 0;
587 }
588 return rawFile->raw->offset;
589 }
590
GetRawFileDescriptorFromHap64(const RawFile64 * rawFile,RawFileDescriptor64 * descriptor)591 static bool GetRawFileDescriptorFromHap64(const RawFile64 *rawFile, RawFileDescriptor64 *descriptor)
592 {
593 ResourceManager::RawFileDescriptor resMgrDescriptor;
594 if (rawFile->raw->resMgr->resManager == nullptr) {
595 return false;
596 }
597 RState state = rawFile->raw->resMgr->resManager->
598 GetRawFdNdkFromHap(rawFile->raw->filePath, resMgrDescriptor);
599 if (state != SUCCESS) {
600 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "GetRawFileDescriptorFromHap64 failed");
601 return false;
602 }
603 descriptor->fd = resMgrDescriptor.fd;
604 descriptor->length = resMgrDescriptor.length;
605 descriptor->start = resMgrDescriptor.offset;
606 return true;
607 }
608
OH_ResourceManager_GetRawFileDescriptor64(const RawFile64 * rawFile,RawFileDescriptor64 * descriptor)609 bool OH_ResourceManager_GetRawFileDescriptor64(const RawFile64 *rawFile, RawFileDescriptor64 *descriptor)
610 {
611 if (rawFile == nullptr || rawFile->raw == nullptr) {
612 return false;
613 }
614 if (rawFile->raw->resMgr != nullptr) {
615 return GetRawFileDescriptorFromHap64(rawFile, descriptor);
616 }
617 char paths[PATH_MAX] = {0};
618 #ifdef __WINNT__
619 if (!PathCanonicalizeA(paths, rawFile->raw->filePath.c_str())) {
620 RESMGR_HILOGE(RESMGR_RAWFILE_TAG, "failed to PathCanonicalizeA the rawFile path");
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 }
626 #endif
627 int fd = open(paths, O_RDONLY);
628 if (fd > 0) {
629 descriptor->fd = fd;
630 descriptor->length = rawFile->raw->length;
631 descriptor->start = rawFile->raw->offset;
632 } else {
633 return false;
634 }
635 return true;
636 }
637
OH_ResourceManager_ReleaseRawFileDescriptor64(const RawFileDescriptor64 * descriptor)638 bool OH_ResourceManager_ReleaseRawFileDescriptor64(const RawFileDescriptor64 *descriptor)
639 {
640 if (descriptor->fd > 0) {
641 return close(descriptor->fd) == 0;
642 }
643 return true;
644 }
645
OH_ResourceManager_IsRawDir(const NativeResourceManager * mgr,const char * path)646 bool OH_ResourceManager_IsRawDir(const NativeResourceManager *mgr, const char *path)
647 {
648 bool result = false;
649 if (mgr == nullptr || path == nullptr || mgr->resManager == nullptr) {
650 return result;
651 }
652 RState state = mgr->resManager->IsRawDirFromHap(path, result);
653 if (state != SUCCESS) {
654 RESMGR_HILOGD(RESMGR_RAWFILE_TAG, "failed to determine whether the path is a directory");
655 return result;
656 }
657 return result;
658 }