1 /*
2 * Copyright (c) 2020 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 "gt_bundle_manager_service.h"
17 #include "aafwk_event_error_id.h"
18 #include "aafwk_event_error_code.h"
19 #include "ability_info_utils.h"
20 #include "ability_message_id.h"
21 #include "appexecfwk_errors.h"
22 #include "bundle_common.h"
23 #include "bundle_message_id.h"
24 #include "bundle_util.h"
25 #include "bundlems_log.h"
26 #include "cmsis_os2.h"
27 #include "cstdio"
28 #include "dirent.h"
29 #include "fcntl.h"
30 #include "gt_bundle_extractor.h"
31 #include "gt_bundle_parser.h"
32 #include "gt_extractor_util.h"
33 #include "jerryscript_adapter.h"
34 #include "los_tick.h"
35 #include "sys/stat.h"
36 #include "unistd.h"
37 #include "utils.h"
38 #include "want.h"
39
40 using namespace OHOS::ACELite;
41
42 namespace OHOS {
43 const uint8_t OPERATION_DOING = 200;
44 const uint8_t BMS_INSTALLATION_START = 101;
45 const uint8_t BMS_UNINSTALLATION_START = 104;
46 const uint8_t BMS_INSTALLATION_COMPLETED = 100;
47
GtManagerService()48 GtManagerService::GtManagerService()
49 {
50 installer_ = new GtBundleInstaller();
51 bundleResList_ = new List<BundleRes *>();
52 bundleMap_ = BundleMap::GetInstance();
53 bundleInstallMsg_ = nullptr;
54 jsEngineVer_ = nullptr;
55 installedThirdBundleNum_ = 0;
56 preAppList_ = nullptr;
57 updateFlag_ = false;
58 oldVersionCode_ = -1;
59 }
60
~GtManagerService()61 GtManagerService::~GtManagerService()
62 {
63 delete installer_;
64 installer_ = nullptr;
65 delete bundleResList_;
66 bundleResList_ = nullptr;
67 }
68
Install(const char * hapPath,const InstallParam * installParam,InstallerCallback installerCallback)69 bool GtManagerService::Install(const char *hapPath, const InstallParam *installParam,
70 InstallerCallback installerCallback)
71 {
72 if (installer_ == nullptr) {
73 installer_ = new GtBundleInstaller();
74 }
75 if (hapPath == nullptr) {
76 return false;
77 }
78 if (installerCallback == nullptr) {
79 return false;
80 }
81 char *path = reinterpret_cast<char *>(AdapterMalloc(strlen(hapPath) + 1));
82 if (path == nullptr) {
83 return false;
84 }
85 if (strncpy_s(path, strlen(hapPath) + 1, hapPath, strlen(hapPath)) != EOK) {
86 AdapterFree(path);
87 return false;
88 }
89
90 // delete resource temp dir first
91 (void) BundleUtil::RemoveDir(TMP_RESOURCE_DIR);
92 // create new bundleInstallMsg
93 bundleInstallMsg_ = reinterpret_cast<BundleInstallMsg *>(AdapterMalloc(sizeof(BundleInstallMsg)));
94 if (bundleInstallMsg_ == nullptr) {
95 AdapterFree(path);
96 return false;
97 }
98 if (memset_s(bundleInstallMsg_, sizeof(BundleInstallMsg), 0, sizeof(BundleInstallMsg)) != EOK) {
99 AdapterFree(bundleInstallMsg_);
100 AdapterFree(path);
101 return false;
102 }
103 // set bundleName、label、smallIconPath、bigIconPath in bundleInstallMsg_
104 uint8_t ret = GtBundleExtractor::ExtractInstallMsg(path, &(bundleInstallMsg_->bundleName),
105 &(bundleInstallMsg_->label), &(bundleInstallMsg_->smallIconPath),
106 &(bundleInstallMsg_->bigIconPath));
107 if (ret != 0) {
108 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Install extract install msg failed, ret is %{public}d", ret);
109 char *name = strrchr(path, '/');
110 bundleInstallMsg_->bundleName = Utils::Strdup(name + 1);
111 (void) ReportInstallCallback(ret, BUNDLE_INSTALL_FAIL, BMS_INSTALLATION_COMPLETED, installerCallback);
112 ClearSystemBundleInstallMsg();
113 (void) BundleUtil::RemoveDir(TMP_RESOURCE_DIR);
114 AdapterFree(path);
115 return false;
116 }
117
118 updateFlag_ = false;
119 oldVersionCode_ = -1;
120 BundleInfo *installedInfo = bundleMap_->Get(bundleInstallMsg_->bundleName);
121 if (installedInfo != nullptr) {
122 updateFlag_ = true;
123 oldVersionCode_ = installedInfo->versionCode;
124 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] App is in the updated state");
125 }
126
127 SetCurrentBundle(bundleInstallMsg_->bundleName);
128 (void) ReportInstallCallback(OPERATION_DOING, 0, BMS_INSTALLATION_START, installerCallback);
129 #ifdef _MINI_BMS_PERMISSION_
130 DisableServiceWdg();
131 RefreshAllServiceTimeStamp();
132 #endif
133 ret = installer_->Install(path, installerCallback);
134 #ifdef _MINI_BMS_PERMISSION_
135 EnableServiceWdg();
136 RefreshAllServiceTimeStamp();
137 #endif
138 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] Install ret is %d", ret);
139 if (ret == 0) {
140 (void) ReportInstallCallback(ret, BUNDLE_INSTALL_OK, BMS_INSTALLATION_COMPLETED, installerCallback);
141 } else {
142 (void) ReportInstallCallback(ret, BUNDLE_INSTALL_FAIL, BMS_INSTALLATION_COMPLETED, installerCallback);
143 }
144 SetCurrentBundle(nullptr);
145 ClearSystemBundleInstallMsg();
146 (void) BundleUtil::RemoveDir(TMP_RESOURCE_DIR);
147 AdapterFree(path);
148 return true;
149 }
150
Uninstall(const char * bundleName,const InstallParam * installParam,InstallerCallback installerCallback)151 bool GtManagerService::Uninstall(const char *bundleName, const InstallParam *installParam,
152 InstallerCallback installerCallback)
153 {
154 if (installer_ == nullptr) {
155 installer_ = new GtBundleInstaller();
156 }
157 if (bundleName == nullptr) {
158 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Parsed bundleName to be uninstalled is null");
159 return false;
160 }
161 if (installerCallback == nullptr) {
162 return false;
163 }
164 char *innerBundleName = reinterpret_cast<char *>(AdapterMalloc(strlen(bundleName) + 1));
165 if (innerBundleName == nullptr) {
166 return false;
167 }
168 if (strncpy_s(innerBundleName, strlen(bundleName) + 1, bundleName, strlen(bundleName)) != EOK) {
169 AdapterFree(innerBundleName);
170 return false;
171 }
172 SetCurrentBundle(innerBundleName);
173
174 (void) ReportUninstallCallback(OPERATION_DOING, BUNDLE_UNINSTALL_DOING, innerBundleName,
175 BMS_UNINSTALLATION_START, installerCallback);
176 #ifdef _MINI_BMS_PERMISSION_
177 DisableServiceWdg();
178 RefreshAllServiceTimeStamp();
179 #endif
180 uint8_t ret = installer_->Uninstall(innerBundleName);
181 #ifdef _MINI_BMS_PERMISSION_
182 EnableServiceWdg();
183 RefreshAllServiceTimeStamp();
184 #endif
185 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] Uninstall ret is %d", ret);
186 if (ret == 0) {
187 (void) ReportUninstallCallback(ret, BUNDLE_UNINSTALL_OK, innerBundleName,
188 BMS_INSTALLATION_COMPLETED, installerCallback);
189 } else {
190 (void) ReportUninstallCallback(ret, BUNDLE_UNINSTALL_FAIL, innerBundleName,
191 BMS_INSTALLATION_COMPLETED, installerCallback);
192 }
193
194 SetCurrentBundle(nullptr);
195 AdapterFree(innerBundleName);
196 return true;
197 }
198
GetInstallState(const char * bundleName,InstallState * installState,uint8_t * installProcess)199 bool GtManagerService::GetInstallState(const char *bundleName, InstallState *installState, uint8_t *installProcess)
200 {
201 if (bundleName == nullptr) {
202 return false;
203 }
204 BundleInfo *installedInfo = bundleMap_->Get(bundleName);
205 if (installedInfo != nullptr) {
206 bool isUpdateSuccess = updateFlag_ && oldVersionCode_ < installedInfo->versionCode;
207 if (!updateFlag_ || isUpdateSuccess) {
208 *installState = BUNDLE_INSTALL_OK;
209 *installProcess = BMS_INSTALLATION_COMPLETED;
210 return true;
211 }
212 }
213 if (bundleInstallMsg_ == nullptr || bundleInstallMsg_->bundleName == nullptr) {
214 *installState = BUNDLE_INSTALL_FAIL;
215 *installProcess = 0;
216 return true;
217 }
218 if (strcmp(bundleName, bundleInstallMsg_->bundleName) == 0) {
219 *installState = bundleInstallMsg_->installState;
220 *installProcess = bundleInstallMsg_->installProcess;
221 return true;
222 }
223 *installState = BUNDLE_INSTALL_FAIL;
224 *installProcess = 0;
225 return true;
226 }
227
GetBundleSize(const char * bundleName)228 uint32_t GtManagerService::GetBundleSize(const char *bundleName)
229 {
230 if (bundleName == nullptr) {
231 return 0;
232 }
233 BundleInfo *installedInfo = bundleMap_->Get(bundleName);
234 if (installedInfo == nullptr) {
235 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] failed to get bundle size because the bundle does not exist!");
236 return 0;
237 }
238 char *codePath = installedInfo->codePath;
239 uint32_t codeBundleSize = BundleUtil::GetFileFolderSize(codePath);
240 if (codeBundleSize == 0) {
241 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] failed to get code bundle size!");
242 return 0;
243 }
244 char *dataPath = installedInfo->dataPath;
245 uint32_t dataBundleSize = BundleUtil::GetFileFolderSize(dataPath);
246 return codeBundleSize + dataBundleSize;
247 }
248
QueryAbilityInfo(const Want * want,AbilityInfo * abilityInfo)249 uint8_t GtManagerService::QueryAbilityInfo(const Want *want, AbilityInfo *abilityInfo)
250 {
251 if (want->element == nullptr) {
252 return 0;
253 }
254 const char *bundleName = want->element->bundleName;
255 BundleInfo *bundleInfo = OHOS::GtManagerService::GetInstance().QueryBundleInfo(bundleName);
256 if (bundleInfo == nullptr) {
257 return 0;
258 }
259 AbilityInfo *ability = bundleInfo->abilityInfo;
260 if (ability == nullptr) {
261 return 0;
262 }
263 OHOS::AbilityInfoUtils::SetAbilityInfoBundleName(abilityInfo, bundleName);
264 OHOS::AbilityInfoUtils::SetAbilityInfoSrcPath(abilityInfo, ability->srcPath);
265 return 1;
266 }
267
GetBundleInfo(const char * bundleName,int32_t flags,BundleInfo & bundleInfo)268 uint8_t GtManagerService::GetBundleInfo(const char *bundleName, int32_t flags, BundleInfo& bundleInfo)
269 {
270 if (bundleMap_ == nullptr) {
271 return ERR_APPEXECFWK_OBJECT_NULL;
272 }
273 return bundleMap_->GetBundleInfo(bundleName, flags, bundleInfo);
274 }
275
GetBundleInfos(const int flags,BundleInfo ** bundleInfos,int32_t * len)276 uint8_t GtManagerService::GetBundleInfos(const int flags, BundleInfo **bundleInfos, int32_t *len)
277 {
278 if (bundleMap_ == nullptr) {
279 return ERR_APPEXECFWK_OBJECT_NULL;
280 }
281 return bundleMap_->GetBundleInfos(flags, bundleInfos, len);
282 }
283
GetBundleInfosNoReplication(const int flags,BundleInfo ** bundleInfos,int32_t * len)284 uint8_t GtManagerService::GetBundleInfosNoReplication(const int flags, BundleInfo **bundleInfos, int32_t *len)
285 {
286 if (bundleMap_ == nullptr) {
287 return ERR_APPEXECFWK_OBJECT_NULL;
288 }
289 return bundleMap_->GetBundleInfosNoReplication(flags, bundleInfos, len);
290 }
291
RegisterInstallerCallback(InstallerCallback installerCallback)292 bool GtManagerService::RegisterInstallerCallback(InstallerCallback installerCallback)
293 {
294 if (installerCallback == nullptr) {
295 return false;
296 }
297 #ifdef BC_TRANS_ENABLE
298 ScanPackages();
299 #endif
300 InstallPreBundle(systemPathList_, installerCallback);
301 return true;
302 }
303
InstallPreBundle(List<ToBeInstalledApp * > systemPathList,InstallerCallback installerCallback)304 void GtManagerService::InstallPreBundle(List<ToBeInstalledApp *> systemPathList, InstallerCallback installerCallback)
305 {
306 if (!BundleUtil::IsDir(JSON_PATH_NO_SLASH_END)) {
307 BundleUtil::MkDirs(JSON_PATH_NO_SLASH_END);
308 InstallAllSystemBundle(installerCallback);
309 RemoveSystemAppPathList(&systemPathList);
310 return;
311 }
312 // get third system bundle uninstall record
313 cJSON *uninstallRecord = BundleUtil::GetJsonStream(UNINSTALL_THIRD_SYSTEM_BUNDLE_JSON);
314 if (uninstallRecord == nullptr) {
315 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] InstallPreBundle uninstallRecord is nullptr!");
316 (void) unlink(UNINSTALL_THIRD_SYSTEM_BUNDLE_JSON);
317 }
318
319 // scan system apps and third system apps
320 #ifdef _MINI_BMS_PERMISSION_
321 DisableServiceWdg();
322 RefreshAllServiceTimeStamp();
323 #endif
324 ScanSystemApp(uninstallRecord, &systemPathList_);
325 if (uninstallRecord != nullptr) {
326 cJSON_Delete(uninstallRecord);
327 }
328
329 // scan third apps
330 ScanThirdApp(INSTALL_PATH, &systemPathList_);
331 #ifdef _MINI_BMS_PERMISSION_
332 EnableServiceWdg();
333 RefreshAllServiceTimeStamp();
334 #endif
335 for (auto node = systemPathList.Begin(); node != systemPathList.End(); node = node->next_) {
336 ToBeInstalledApp *toBeInstalledApp = node->value_;
337 if (!BundleUtil::IsFile(toBeInstalledApp->path) ||
338 !BundleUtil::EndWith(toBeInstalledApp->path, INSTALL_FILE_SUFFIX)) {
339 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Pre install file path is invalid");
340 continue;
341 }
342 if (toBeInstalledApp->isUpdated) {
343 (void) ReloadBundleInfo(toBeInstalledApp->installedPath, toBeInstalledApp->appId,
344 toBeInstalledApp->isSystemApp);
345 }
346 (void) Install(toBeInstalledApp->path, nullptr, installerCallback);
347 RefreshAllServiceTimeStamp();
348 }
349 RemoveSystemAppPathList(&systemPathList);
350 }
351
InstallAllSystemBundle(InstallerCallback installerCallback)352 void GtManagerService::InstallAllSystemBundle(InstallerCallback installerCallback)
353 {
354 PreAppList *list = preAppList_;
355 if (list == nullptr) {
356 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] InstallAllSystemBundle InitAllAppInfo fail, list is nullptr");
357 return;
358 }
359
360 PreAppList *currentNode = nullptr;
361 PreAppList *nextNode = nullptr;
362 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(currentNode, nextNode, &list->appDoubleList, PreAppList, appDoubleList) {
363 if (currentNode == nullptr) {
364 return;
365 }
366 if ((strcmp(((PreAppList *)currentNode)->filePath, ".") == 0) ||
367 (strcmp(((PreAppList *)currentNode)->filePath, "..") == 0)) {
368 continue;
369 }
370
371 if (!BundleUtil::IsFile(((PreAppList *)currentNode)->filePath) ||
372 !BundleUtil::EndWith(((PreAppList *)currentNode)->filePath, INSTALL_FILE_SUFFIX)) {
373 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Install all system bundle file path is invalid");
374 return;
375 }
376 (void) Install(((PreAppList *)currentNode)->filePath, nullptr, installerCallback);
377 }
378 GtManagerService::FreePreAppInfo(list);
379 }
380
ClearSystemBundleInstallMsg()381 void GtManagerService::ClearSystemBundleInstallMsg()
382 {
383 if (bundleInstallMsg_ == nullptr) {
384 return;
385 }
386
387 if (bundleInstallMsg_->bundleName != nullptr) {
388 AdapterFree(bundleInstallMsg_->bundleName);
389 bundleInstallMsg_->bundleName = nullptr;
390 }
391
392 if (bundleInstallMsg_->label != nullptr) {
393 AdapterFree(bundleInstallMsg_->label);
394 bundleInstallMsg_->label = nullptr;
395 }
396
397 if (bundleInstallMsg_->smallIconPath != nullptr) {
398 AdapterFree(bundleInstallMsg_->smallIconPath);
399 bundleInstallMsg_->smallIconPath = nullptr;
400 }
401
402 if (bundleInstallMsg_->bigIconPath != nullptr) {
403 AdapterFree(bundleInstallMsg_->bigIconPath);
404 bundleInstallMsg_->bigIconPath = nullptr;
405 }
406
407 AdapterFree(bundleInstallMsg_);
408 bundleInstallMsg_ = nullptr;
409 }
410
ScanPackages()411 void GtManagerService::ScanPackages()
412 {
413 #ifdef BC_TRANS_ENABLE
414 JerryBmsPsRamMemInit();
415 bms_task_context_init();
416 jsEngineVer_ = get_jerry_version_no();
417 if (jsEngineVer_ == nullptr) {
418 HILOG_WARN(HILOG_MODULE_AAFWK, "[BMS] get jsEngine version fail when restart!");
419 }
420 #endif
421 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] get jsEngine version success!");
422 }
423
RemoveSystemAppPathList(List<ToBeInstalledApp * > * systemPathList)424 void GtManagerService::RemoveSystemAppPathList(List<ToBeInstalledApp *> *systemPathList)
425 {
426 if (systemPathList == nullptr) {
427 return;
428 }
429
430 for (auto node = systemPathList->Begin(); node != systemPathList->End(); node = node->next_) {
431 ToBeInstalledApp *toBeInstalledApp = node->value_;
432 if (toBeInstalledApp != nullptr) {
433 AdapterFree(toBeInstalledApp->installedPath);
434 AdapterFree(toBeInstalledApp->path);
435 AdapterFree(toBeInstalledApp->appId);
436 UI_Free(toBeInstalledApp);
437 }
438 }
439 }
440
ScanSystemApp(const cJSON * uninstallRecord,List<ToBeInstalledApp * > * systemPathList)441 void GtManagerService::ScanSystemApp(const cJSON *uninstallRecord, List<ToBeInstalledApp *> *systemPathList)
442 {
443 PreAppList *list = preAppList_;
444 if (list == nullptr) {
445 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] ScanSystemApp InitAllAppInfo fail, list is nullptr");
446 return;
447 }
448
449 uint8_t scanFlag = 0;
450 char *bundleName = nullptr;
451 int32_t versionCode = -1;
452 PreAppList *currentNode = nullptr;
453 PreAppList *nextNode = nullptr;
454
455 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(currentNode, nextNode, &list->appDoubleList, PreAppList, appDoubleList) {
456 if (currentNode == nullptr) {
457 return;
458 }
459 if ((strcmp(((PreAppList *)currentNode)->filePath, ".") == 0) ||
460 (strcmp(((PreAppList *)currentNode)->filePath, "..") == 0)) {
461 continue;
462 }
463
464 if (BundleUtil::StartWith(((PreAppList *)currentNode)->filePath, SYSTEM_BUNDLE_PATH)) {
465 scanFlag = SYSTEM_APP_FLAG;
466 } else if (BundleUtil::StartWith(((PreAppList *)currentNode)->filePath, THIRD_SYSTEM_BUNDLE_PATH)) {
467 scanFlag = THIRD_SYSTEM_APP_FLAG;
468 } else {
469 continue; // skip third app
470 }
471
472 // scan system app
473 bool res = CheckSystemBundleIsValid(((PreAppList *)currentNode)->filePath, &bundleName, versionCode);
474 if (!res) {
475 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] ScanSystemApp CheckSystemBundleIsValid failed!");
476 APP_ERRCODE_EXTRA(EXCE_ACE_APP_SCAN, EXCE_ACE_APP_SCAN_INVALID_SYSTEM_APP);
477 AdapterFree(bundleName);
478 continue;
479 }
480
481 // third system app cannot restore after uninstall
482 if (scanFlag == THIRD_SYSTEM_APP_FLAG &&
483 CheckThirdSystemBundleHasUninstalled(bundleName, uninstallRecord)) {
484 AdapterFree(bundleName);
485 continue;
486 }
487
488 ReloadEntireBundleInfo(((PreAppList *)currentNode)->filePath, bundleName,
489 systemPathList, versionCode, scanFlag);
490 AdapterFree(bundleName);
491 }
492 GtManagerService::FreePreAppInfo(list);
493 }
494
ScanThirdApp(const char * appDir,const List<ToBeInstalledApp * > * systemPathList)495 void GtManagerService::ScanThirdApp(const char *appDir, const List<ToBeInstalledApp *> *systemPathList)
496 {
497 dirent *ent = nullptr;
498
499 if (appDir == nullptr) {
500 return;
501 }
502
503 DIR *dir = opendir(appDir);
504 if (dir == nullptr) {
505 return;
506 }
507 char *bundleName = reinterpret_cast<char *>(AdapterMalloc(MAX_BUNDLE_NAME_LEN + 1));
508 if (bundleName == nullptr) {
509 closedir(dir);
510 return;
511 }
512 int32_t entLen = 0;
513 while ((ent = readdir(dir)) != nullptr) {
514 ++entLen;
515 if (memset_s(bundleName, MAX_BUNDLE_NAME_LEN + 1, 0, MAX_BUNDLE_NAME_LEN + 1) != EOK) {
516 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] memset fail when initialize bundle name!");
517 break;
518 }
519
520 if (strcpy_s(bundleName, MAX_BUNDLE_NAME_LEN + 1, ent->d_name) != 0) {
521 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] failed to copy bundle name!");
522 break;
523 }
524
525 if ((strcmp(bundleName, ".") == 0) || (strcmp(bundleName, "..")) == 0) {
526 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] strcmp fail when reload third app!");
527 continue;
528 }
529
530 int32_t len = strlen(appDir) + 1 + strlen(bundleName) + 1;
531 char *appPath = reinterpret_cast<char *>(UI_Malloc(len));
532 if (appPath == nullptr) {
533 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] malloc fail when reload third app!");
534 break;
535 }
536
537 if (sprintf_s(appPath, len, "%s/%s", appDir, bundleName) < 0) {
538 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] strcat fail when reload third app!");
539 UI_Free(appPath);
540 break;
541 }
542
543 if (!BundleUtil::IsDir(appPath)) {
544 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] app path is not dir when reload third app!");
545 UI_Free(appPath);
546 continue;
547 }
548
549 if (IsSystemBundleInstalledPath(appPath, systemPathList)) {
550 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] app path is not third bundle path!");
551 UI_Free(appPath);
552 continue;
553 }
554
555 if (installedThirdBundleNum_ >= MAX_THIRD_BUNDLE_NUMBER) {
556 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] third bundle reload number is %d!",
557 installedThirdBundleNum_);
558 UI_Free(appPath);
559 continue;
560 }
561
562 ReloadEntireBundleInfo(appPath, bundleName, nullptr, -1, THIRD_APP_FLAG);
563 UI_Free(appPath);
564 }
565
566 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] third app number is %{public}d", entLen);
567 AdapterFree(bundleName);
568 closedir(dir);
569 }
570
IsSystemBundleInstalledPath(const char * appPath,const List<ToBeInstalledApp * > * systemPathList)571 bool GtManagerService::IsSystemBundleInstalledPath(const char *appPath, const List<ToBeInstalledApp *> *systemPathList)
572 {
573 if (appPath == nullptr || systemPathList == nullptr) {
574 return false;
575 }
576
577 for (auto node = systemPathList->Begin(); node != systemPathList->End(); node = node->next_) {
578 ToBeInstalledApp *toBeInstalledApp = node->value_;
579 if (toBeInstalledApp->installedPath != nullptr &&
580 strcmp(appPath, toBeInstalledApp->installedPath) == 0) {
581 return true;
582 }
583 }
584 return false;
585 }
586
CheckSystemBundleIsValid(const char * appPath,char ** bundleName,int32_t & versionCode)587 bool GtManagerService::CheckSystemBundleIsValid(const char *appPath, char **bundleName, int32_t &versionCode)
588 {
589 if (appPath == nullptr || bundleName == nullptr) {
590 return false;
591 }
592
593 if (!BundleUtil::EndWith(appPath, INSTALL_FILE_SUFFIX)) {
594 return false;
595 }
596
597 if (!GtBundleParser::ParseBundleAttr(appPath, bundleName, versionCode)) {
598 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] parse bundle attr fail!");
599 return false;
600 }
601
602 if (*bundleName != nullptr && strlen(*bundleName) >= MAX_BUNDLE_NAME_LEN) {
603 return false;
604 }
605 return true;
606 }
607
ReloadEntireBundleInfo(const char * appPath,const char * bundleName,List<ToBeInstalledApp * > * systemPathList,int32_t versionCode,uint8_t scanFlag)608 void GtManagerService::ReloadEntireBundleInfo(const char *appPath, const char *bundleName,
609 List<ToBeInstalledApp *> *systemPathList, int32_t versionCode, uint8_t scanFlag)
610 {
611 char *codePath = nullptr;
612 char *appId = nullptr;
613 int32_t oldVersionCode = -1;
614
615 if (appPath == nullptr || bundleName == nullptr) {
616 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] ReloadEntireBundleInfo app path or bundle name is nullptr!");
617 APP_ERRCODE_EXTRA(EXCE_ACE_APP_SCAN, EXCE_ACE_APP_SCAN_UNKNOWN_BUNDLE_INFO);
618 return;
619 }
620
621 if (QueryBundleInfo(bundleName) != nullptr) {
622 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] bundle has been reloaded!");
623 return;
624 }
625
626 bool res = BundleUtil::CheckBundleJsonIsValid(bundleName, &codePath, &appId, oldVersionCode);
627 bool isSystemApp = (scanFlag == SYSTEM_APP_FLAG);
628 if (scanFlag != THIRD_APP_FLAG) {
629 if (!res) {
630 AddSystemAppPathList(nullptr, appPath, systemPathList, isSystemApp, false, appId);
631 AdapterFree(appId);
632 AdapterFree(codePath);
633 return;
634 }
635 if (oldVersionCode < versionCode) {
636 AddSystemAppPathList(codePath, appPath, systemPathList, isSystemApp, true, appId);
637 AdapterFree(appId);
638 AdapterFree(codePath);
639 return;
640 }
641 } else {
642 if (!res && !BundleUtil::CheckBundleJsonIsValid(bundleName, &codePath, &appId, oldVersionCode)) {
643 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] ReloadEntireBundleInfo CheckBundleJsonIsValid failed!");
644 RecordAbiityInfoEvt(bundleName);
645 APP_ERRCODE_EXTRA(EXCE_ACE_APP_SCAN, EXCE_ACE_APP_SCAN_PARSE_JSON_FALIED);
646 AdapterFree(appId);
647 AdapterFree(codePath);
648 BundleUtil::RemoveDir(appPath);
649 return;
650 }
651 }
652 #ifdef BC_TRANS_ENABLE
653 TransformJsToBcWhenRestart(codePath, bundleName);
654 #endif
655 bool ret = ReloadBundleInfo(codePath, appId, isSystemApp);
656 if (ret && (scanFlag == THIRD_APP_FLAG)) {
657 installedThirdBundleNum_++;
658 }
659 AdapterFree(appId);
660 AdapterFree(codePath);
661 }
662
AddSystemAppPathList(const char * installedPath,const char * path,List<ToBeInstalledApp * > * systemPathList,bool isSystemApp,bool isUpdated,const char * appId)663 void GtManagerService::AddSystemAppPathList(const char *installedPath, const char *path,
664 List<ToBeInstalledApp *> *systemPathList, bool isSystemApp, bool isUpdated, const char *appId)
665 {
666 if (path == nullptr || systemPathList == nullptr) {
667 return;
668 }
669
670 ToBeInstalledApp *toBeInstalledApp =
671 reinterpret_cast<ToBeInstalledApp *>(UI_Malloc(sizeof(ToBeInstalledApp)));
672 if (toBeInstalledApp == nullptr) {
673 return;
674 }
675 toBeInstalledApp->installedPath = Utils::Strdup(installedPath);
676 toBeInstalledApp->path = Utils::Strdup(path);
677 toBeInstalledApp->isSystemApp = isSystemApp;
678 toBeInstalledApp->isUpdated = isUpdated;
679 toBeInstalledApp->appId = Utils::Strdup(appId);
680 systemPathList->PushBack(toBeInstalledApp);
681 }
682
ReloadBundleInfo(const char * profileDir,const char * appId,bool isSystemApp)683 bool GtManagerService::ReloadBundleInfo(const char *profileDir, const char *appId, bool isSystemApp)
684 {
685 if (profileDir == nullptr) {
686 return false;
687 }
688 BundleRes *bundleRes = reinterpret_cast<BundleRes *>(AdapterMalloc(sizeof(BundleRes)));
689 if (bundleRes == nullptr) {
690 return false;
691 }
692 if (memset_s(bundleRes, sizeof(BundleRes), 0, sizeof(BundleRes)) != EOK) {
693 AdapterFree(bundleRes);
694 return false;
695 }
696 BundleInfo *bundleInfo = GtBundleParser::ParseHapProfile(profileDir, bundleRes);
697 if (bundleInfo != nullptr) {
698 bundleInfo->isSystemApp = isSystemApp;
699 bundleInfo->appId = Utils::Strdup(appId);
700 if (bundleInfo->appId == nullptr) {
701 BundleInfoUtils::FreeBundleInfo(bundleInfo);
702 AdapterFree(bundleRes->abilityRes);
703 AdapterFree(bundleRes);
704 return false;
705 }
706 bundleMap_->Add(bundleInfo);
707 if (bundleRes->abilityRes == nullptr ||
708 (bundleRes->abilityRes->labelId == 0 && bundleRes->abilityRes->iconId == 0)) {
709 AdapterFree(bundleRes->abilityRes);
710 AdapterFree(bundleRes);
711 } else {
712 bundleRes->bundleName = bundleInfo->bundleName;
713 AddBundleResList(bundleRes);
714 }
715 return true;
716 }
717 AdapterFree(bundleRes->abilityRes);
718 AdapterFree(bundleRes);
719 APP_ERRCODE_EXTRA(EXCE_ACE_APP_SCAN, EXCE_ACE_APP_SCAN_PARSE_PROFILE_FALIED);
720 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] reload bundle info fail!, isSystemApp is %d", isSystemApp);
721 return false;
722 }
723
AddBundleResList(const BundleRes * bundleRes)724 void GtManagerService::AddBundleResList(const BundleRes *bundleRes)
725 {
726 if (bundleRes == nullptr || bundleRes->bundleName == nullptr || bundleResList_ == nullptr) {
727 return;
728 }
729
730 for (auto node = bundleResList_->Begin(); node != bundleResList_->End(); node = node->next_) {
731 BundleRes *res = node->value_;
732 if (res != nullptr && res->bundleName != nullptr && strcmp(res->bundleName, bundleRes->bundleName) == 0) {
733 return;
734 }
735 }
736 bundleResList_->PushFront(const_cast<BundleRes *>(bundleRes));
737 }
738
RemoveBundleResList(const char * bundleName)739 void GtManagerService::RemoveBundleResList(const char *bundleName)
740 {
741 if (bundleName == nullptr || bundleResList_ == nullptr) {
742 return;
743 }
744
745 for (auto node = bundleResList_->Begin(); node != bundleResList_->End(); node = node->next_) {
746 BundleRes *res = node->value_;
747 if (res == nullptr) {
748 return;
749 }
750 if (res->bundleName != nullptr && strcmp(bundleName, res->bundleName) == 0) {
751 AdapterFree(res->abilityRes);
752 AdapterFree(res);
753 bundleResList_->Remove(node);
754 return;
755 }
756 }
757 }
758
UpdateBundleInfoList()759 void GtManagerService::UpdateBundleInfoList()
760 {
761 if (bundleResList_ == nullptr) {
762 return;
763 }
764
765 for (auto node = bundleResList_->Begin(); node != bundleResList_->End(); node = node->next_) {
766 BundleRes *res = node->value_;
767 if (res == nullptr || res->bundleName == nullptr || res->abilityRes == nullptr) {
768 continue;
769 }
770
771 BundleInfo *bundleInfo = bundleMap_->Get(res->bundleName);
772 if (bundleInfo == nullptr) {
773 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] get no bundleInfo when change bundle res!");
774 continue;
775 }
776
777 int32_t len = strlen(INSTALL_PATH) + 1 + strlen(res->bundleName);
778 char *path = reinterpret_cast<char *>(UI_Malloc(len + 1));
779 if (path == nullptr) {
780 continue;
781 }
782
783 if (sprintf_s(path, len + 1, "%s/%s", INSTALL_PATH, res->bundleName) < 0) {
784 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] change bundle res failed! because sprintf_s fail");
785 UI_Free(path);
786 continue;
787 }
788
789 uint8_t errorCode = GtBundleParser::ConvertResInfoToBundleInfo(path, res->abilityRes->labelId,
790 res->abilityRes->iconId, bundleInfo);
791 UI_Free(path);
792 if (errorCode != ERR_OK) {
793 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] change bundle res failed! errorCode is %d", errorCode);
794 return;
795 }
796 }
797 }
798
799 #ifdef BC_TRANS_ENABLE
TransformJsToBcWhenRestart(const char * codePath,const char * bundleName)800 void GtManagerService::TransformJsToBcWhenRestart(const char *codePath, const char *bundleName)
801 {
802 if (codePath == nullptr) {
803 return;
804 }
805
806 char *bundleJsonPathComp[] = {
807 const_cast<char *>(JSON_PATH), const_cast<char *>(bundleName), const_cast<char *>(JSON_SUFFIX)
808 };
809 char *bundleJsonPath = BundleUtil::Strscat(bundleJsonPathComp, sizeof(bundleJsonPathComp) / sizeof(char *));
810 if (bundleJsonPath == nullptr) {
811 return;
812 }
813
814 cJSON *installRecordJson = BundleUtil::GetJsonStream(bundleJsonPath);
815 if (installRecordJson == nullptr) {
816 AdapterFree(bundleJsonPath);
817 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] get installRecord fail when restart!");
818 return;
819 }
820
821 if (jsEngineVer_ == nullptr) {
822 cJSON_Delete(installRecordJson);
823 AdapterFree(bundleJsonPath);
824 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] TransformJsToBcWhenRestart jsEngineVer_ is nullptr!");
825 return;
826 }
827
828 cJSON *jsEngineVerObj = cJSON_CreateString(jsEngineVer_);
829 if (jsEngineVerObj == nullptr) {
830 cJSON_Delete(installRecordJson);
831 AdapterFree(bundleJsonPath);
832 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] create string object fail when restart!");
833 return;
834 }
835
836 cJSON *oldJsEngineVerObj = cJSON_GetObjectItem(installRecordJson, JSON_SUB_KEY_JSENGINE_VERSION);
837 if (cJSON_IsString(oldJsEngineVerObj) && strcmp(oldJsEngineVerObj->valuestring, jsEngineVer_) == 0) {
838 cJSON_Delete(jsEngineVerObj);
839 cJSON *transformResultObj = cJSON_GetObjectItem(installRecordJson, JSON_SUB_KEY_TRANSFORM_RESULT);
840 if (cJSON_IsNumber(transformResultObj) && transformResultObj->valueint == 0) {
841 cJSON_Delete(installRecordJson);
842 AdapterFree(bundleJsonPath);
843 return;
844 }
845 } else if (oldJsEngineVerObj == nullptr) {
846 if (!cJSON_AddItemToObject(installRecordJson, JSON_SUB_KEY_JSENGINE_VERSION, jsEngineVerObj)) {
847 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] add js engine version record fail when restart!");
848 cJSON_Delete(jsEngineVerObj);
849 cJSON_Delete(installRecordJson);
850 AdapterFree(bundleJsonPath);
851 return;
852 }
853 } else {
854 if (!cJSON_ReplaceItemInObject(installRecordJson, JSON_SUB_KEY_JSENGINE_VERSION, jsEngineVerObj)) {
855 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] refresh js engine version fail when restart!");
856 cJSON_Delete(jsEngineVerObj);
857 cJSON_Delete(installRecordJson);
858 AdapterFree(bundleJsonPath);
859 return;
860 }
861 }
862
863 TransformJsToBc(codePath, bundleJsonPath, installRecordJson);
864 cJSON_Delete(installRecordJson);
865 AdapterFree(bundleJsonPath);
866 }
867
TransformJsToBc(const char * codePath,const char * bundleJsonPath,cJSON * installRecordObj)868 void GtManagerService::TransformJsToBc(const char *codePath, const char *bundleJsonPath, cJSON *installRecordObj)
869 {
870 if (codePath == nullptr || installRecordObj == nullptr || bundleJsonPath == nullptr) {
871 return;
872 }
873
874 char *jsPathComp[] = {const_cast<char *>(codePath), const_cast<char *>(ASSET_JS_PATH)};
875 char *jsPath = BundleUtil::Strscat(jsPathComp, sizeof(jsPathComp) / sizeof(char *));
876 if (jsPath == nullptr) {
877 return;
878 }
879
880 EXECRES result = walk_directory(jsPath);
881 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] transform js to bc, result is %d", result);
882 if (result != EXCE_ACE_JERRY_EXEC_OK) {
883 result = walk_del_bytecode(jsPath);
884 HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] delete byte code, result is %d", result);
885 AdapterFree(jsPath);
886 return;
887 }
888 AdapterFree(jsPath);
889
890 cJSON *resultObj = cJSON_CreateNumber(0);
891 if (resultObj == nullptr) {
892 return;
893 }
894 cJSON *oldResultObj = cJSON_GetObjectItem(installRecordObj, JSON_SUB_KEY_TRANSFORM_RESULT);
895 if (oldResultObj == nullptr) {
896 if (!cJSON_AddItemToObject(installRecordObj, JSON_SUB_KEY_TRANSFORM_RESULT, resultObj)) {
897 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] add transform result record fail when restart!");
898 cJSON_Delete(resultObj);
899 return;
900 }
901 } else {
902 if (!cJSON_ReplaceItemInObject(installRecordObj, JSON_SUB_KEY_TRANSFORM_RESULT, resultObj)) {
903 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] refresh transform result record fail when restart!");
904 cJSON_Delete(resultObj);
905 return;
906 }
907 }
908 (void)BundleUtil::StoreJsonContentToFile(bundleJsonPath, installRecordObj);
909 }
910 #endif
911
CheckThirdSystemBundleHasUninstalled(const char * bundleName,const cJSON * object)912 bool GtManagerService::CheckThirdSystemBundleHasUninstalled(const char *bundleName, const cJSON *object)
913 {
914 if (object == nullptr || bundleName == nullptr) {
915 return false;
916 }
917
918 cJSON *array = cJSON_GetObjectItem(object, JSON_MAIN_KEY);
919 if (!cJSON_IsArray(array)) {
920 return false;
921 }
922
923 int32_t size = cJSON_GetArraySize(array);
924 for (int32_t i = 0; i < size; i++) {
925 cJSON *item = cJSON_GetArrayItem(array, i);
926 if (!cJSON_IsString(item)) {
927 return false;
928 }
929 if ((item->valuestring != nullptr) && strcmp(bundleName, item->valuestring) == 0) {
930 return true;
931 }
932 }
933 return false;
934 }
935
QueryBundleInfo(const char * bundleName)936 BundleInfo *GtManagerService::QueryBundleInfo(const char *bundleName)
937 {
938 if (bundleName == nullptr || bundleMap_ == nullptr) {
939 return nullptr;
940 }
941 return bundleMap_->Get(bundleName);
942 }
943
RemoveBundleInfo(const char * bundleName)944 void GtManagerService::RemoveBundleInfo(const char *bundleName)
945 {
946 if (bundleName == nullptr || bundleMap_ == nullptr) {
947 return;
948 }
949 bundleMap_->Erase(bundleName);
950 }
951
AddBundleInfo(BundleInfo * info)952 void GtManagerService::AddBundleInfo(BundleInfo *info)
953 {
954 if (info == nullptr || info->bundleName == nullptr || bundleMap_ == nullptr) {
955 return;
956 }
957 bundleMap_->Add(info);
958 }
959
UpdateBundleInfo(BundleInfo * info)960 bool GtManagerService::UpdateBundleInfo(BundleInfo *info)
961 {
962 if (info == nullptr) {
963 return false;
964 }
965 return bundleMap_->Update(info);
966 }
967
GetNumOfThirdBundles()968 uint32_t GtManagerService::GetNumOfThirdBundles()
969 {
970 return installedThirdBundleNum_;
971 }
972
AddNumOfThirdBundles()973 void GtManagerService::AddNumOfThirdBundles()
974 {
975 installedThirdBundleNum_++;
976 }
977
ReduceNumOfThirdBundles()978 void GtManagerService::ReduceNumOfThirdBundles()
979 {
980 installedThirdBundleNum_--;
981 }
982
ReportInstallCallback(uint8_t errCode,uint8_t installState,uint8_t process,InstallerCallback installerCallback)983 int32_t GtManagerService::ReportInstallCallback(uint8_t errCode, uint8_t installState,
984 uint8_t process, InstallerCallback installerCallback)
985 {
986 if (bundleInstallMsg_ == nullptr) {
987 return -1;
988 }
989 if (installerCallback == nullptr) {
990 return -1;
991 }
992 BundleInstallMsg *bundleInstallMsg = reinterpret_cast<BundleInstallMsg *>(AdapterMalloc(sizeof(BundleInstallMsg)));
993 if (bundleInstallMsg == nullptr) {
994 return -1;
995 }
996 bundleInstallMsg_->installState = static_cast<InstallState>(installState);
997 bundleInstallMsg_->installProcess = process;
998 bundleInstallMsg->installState = bundleInstallMsg_->installState;
999 bundleInstallMsg->installProcess = bundleInstallMsg_->installProcess;
1000 bundleInstallMsg->label = bundleInstallMsg_->label;
1001 bundleInstallMsg->bundleName = bundleInstallMsg_->bundleName;
1002 bundleInstallMsg->smallIconPath = bundleInstallMsg_->smallIconPath;
1003 bundleInstallMsg->bigIconPath = bundleInstallMsg_->bigIconPath;
1004 (*installerCallback)(errCode, bundleInstallMsg);
1005 return 0;
1006 }
1007
ReportUninstallCallback(uint8_t errCode,uint8_t installState,char * bundleName,uint8_t process,InstallerCallback installerCallback)1008 int32_t GtManagerService::ReportUninstallCallback(uint8_t errCode, uint8_t installState, char *bundleName,
1009 uint8_t process, InstallerCallback installerCallback)
1010 {
1011 if (installerCallback == nullptr) {
1012 return -1;
1013 }
1014 BundleInstallMsg *bundleInstallMsg = reinterpret_cast<BundleInstallMsg *>(AdapterMalloc(sizeof(BundleInstallMsg)));
1015 if (bundleInstallMsg == nullptr) {
1016 return -1;
1017 }
1018 bundleInstallMsg->installState = static_cast<InstallState>(installState);
1019 bundleInstallMsg->bundleName = bundleName;
1020 bundleInstallMsg->installProcess = process;
1021 (*installerCallback)(errCode, bundleInstallMsg);
1022 return 0;
1023 }
1024
InitPreAppInfo()1025 PreAppList *GtManagerService::InitPreAppInfo()
1026 {
1027 PreAppList *list = (PreAppList *)AdapterMalloc(sizeof(PreAppList));
1028 if (list == nullptr) {
1029 return nullptr;
1030 }
1031
1032 if (memset_s(list, sizeof(PreAppList), 0, sizeof(PreAppList)) != EOK) {
1033 AdapterFree(list);
1034 return nullptr;
1035 }
1036
1037 LOS_ListInit(&list->appDoubleList);
1038 return list;
1039 }
1040
QueryPreAppInfo(const char * appDir,PreAppList * list)1041 void GtManagerService::QueryPreAppInfo(const char *appDir, PreAppList *list)
1042 {
1043 struct dirent *ent = nullptr;
1044 if (appDir == nullptr) {
1045 return;
1046 }
1047
1048 DIR *dir = opendir(appDir);
1049 if (dir == nullptr) {
1050 return;
1051 }
1052 char *fileName = reinterpret_cast<char *>(AdapterMalloc(MAX_NAME_LEN + 1));
1053 if (fileName == nullptr) {
1054 closedir(dir);
1055 return;
1056 }
1057 while ((ent = readdir(dir)) != nullptr) {
1058 if (memset_s(fileName, MAX_NAME_LEN + 1, 0, MAX_NAME_LEN + 1) != EOK) {
1059 break;
1060 }
1061
1062 if (strcpy_s(fileName, MAX_NAME_LEN + 1, ent->d_name) != 0) {
1063 break;
1064 }
1065
1066 if ((strcmp(fileName, ".") == 0) || (strcmp(fileName, "..")) == 0) {
1067 continue;
1068 }
1069
1070 int32_t len = strlen(appDir) + 1 + strlen(fileName) + 1;
1071 char *appPath = reinterpret_cast<char *>(AdapterMalloc(len));
1072 if (appPath == nullptr) {
1073 break;
1074 }
1075
1076 if (sprintf_s(appPath, len, "%s/%s", appDir, fileName) < 0) {
1077 AdapterFree(appPath);
1078 break;
1079 }
1080
1081 if (!BundleUtil::IsFile(appPath)) {
1082 AdapterFree(appPath);
1083 continue;
1084 }
1085
1086 InsertPreAppInfo(appPath, (PreAppList *)&list->appDoubleList);
1087 AdapterFree(appPath);
1088 }
1089 AdapterFree(fileName);
1090 closedir(dir);
1091 }
1092
InsertPreAppInfo(const char * filePath,PreAppList * list)1093 void GtManagerService::InsertPreAppInfo(const char *filePath, PreAppList *list)
1094 {
1095 if ((filePath == nullptr) || (list == nullptr)) {
1096 return;
1097 }
1098
1099 PreAppList *app = (PreAppList *)AdapterMalloc(sizeof(PreAppList));
1100 if (app == nullptr) {
1101 return;
1102 }
1103
1104 if (memset_s(app, sizeof(PreAppList), 0, sizeof(PreAppList)) != 0) {
1105 AdapterFree(app);
1106 return;
1107 }
1108
1109 if (memcpy_s(app->filePath, sizeof(app->filePath), filePath, strnlen(filePath, MAX_APP_FILE_PATH_LEN)) != 0) {
1110 AdapterFree(app);
1111 return;
1112 }
1113
1114 LOS_ListTailInsert(&list->appDoubleList, &app->appDoubleList);
1115 return;
1116 }
1117
SetPreAppInfo(PreAppList * list)1118 void GtManagerService::SetPreAppInfo(PreAppList *list)
1119 {
1120 if (list == nullptr) {
1121 return;
1122 }
1123 preAppList_ = list;
1124 return;
1125 }
1126
FreePreAppInfo(const PreAppList * list)1127 void GtManagerService::FreePreAppInfo(const PreAppList *list)
1128 {
1129 if (list == nullptr) {
1130 return;
1131 }
1132
1133 PreAppList *currentNode = nullptr;
1134 PreAppList *nextNode = nullptr;
1135 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(currentNode, nextNode, &list->appDoubleList, PreAppList, appDoubleList) {
1136 if (currentNode != nullptr) {
1137 LOS_ListDelete(&(currentNode->appDoubleList));
1138 AdapterFree(currentNode);
1139 currentNode = nullptr;
1140 }
1141 }
1142
1143 if (list != nullptr) {
1144 AdapterFree(list);
1145 }
1146 return;
1147 }
1148 } // namespace OHOS
1149 extern "C" {
1150 static char *g_currentBundle = nullptr;
1151 const int32_t BUNDLENAME_MUTEX_TIMEOUT = 2000;
1152 static osMutexId_t g_currentBundleMutex;
1153
SetCurrentBundle(const char * name)1154 void SetCurrentBundle(const char *name)
1155 {
1156 MutexAcquire(&g_currentBundleMutex, BUNDLENAME_MUTEX_TIMEOUT);
1157 AdapterFree(g_currentBundle);
1158 if (name == nullptr) {
1159 MutexRelease(&g_currentBundleMutex);
1160 return;
1161 }
1162
1163 int len = strlen(name);
1164 g_currentBundle = (char *)AdapterMalloc(len + 1);
1165 if (g_currentBundle == nullptr || strncpy_s(g_currentBundle, len + 1, name, len) < 0) {
1166 AdapterFree(g_currentBundle);
1167 }
1168 MutexRelease(&g_currentBundleMutex);
1169 }
1170
GetCurrentBundle()1171 const char *GetCurrentBundle()
1172 {
1173 MutexAcquire(&g_currentBundleMutex, BUNDLENAME_MUTEX_TIMEOUT);
1174 if (g_currentBundle == nullptr) {
1175 MutexRelease(&g_currentBundleMutex);
1176 return nullptr;
1177 }
1178
1179 int len = strlen(g_currentBundle);
1180 char *bundleName = (char *)AdapterMalloc(len + 1);
1181 if (bundleName == nullptr || strncpy_s(bundleName, len + 1, g_currentBundle, len) < 0) {
1182 AdapterFree(bundleName);
1183 MutexRelease(&g_currentBundleMutex);
1184 return nullptr;
1185 }
1186
1187 MutexRelease(&g_currentBundleMutex);
1188 return bundleName;
1189 }
1190 }
1191