• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundle_info_creator.h"
17 
18 #include "ability_info_utils.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_info_utils.h"
21 #include "bundle_manager_service.h"
22 #include "bundle_res_transform.h"
23 #include "bundle_util.h"
24 #include "bundle_log.h"
25 #include "module_info_utils.h"
26 #include "securec.h"
27 #include "utils.h"
28 
29 namespace OHOS {
SaveBundleInfo(const BundleProfile & bundleProfile,BundleInfo ** bundleInfo)30 uint8_t BundleInfoCreator::SaveBundleInfo(const BundleProfile &bundleProfile, BundleInfo **bundleInfo)
31 {
32     *bundleInfo = reinterpret_cast<BundleInfo *>(AdapterMalloc(sizeof(BundleInfo)));
33     if (*bundleInfo == nullptr) {
34         HILOG_ERROR(HILOG_MODULE_APP, "malloc bundleInfo fail!");
35         return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
36     }
37 
38     if (memset_s(*bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo)) != EOK) {
39         HILOG_ERROR(HILOG_MODULE_APP, "memset bundleInfo fail!");
40         AdapterFree(*bundleInfo);
41         *bundleInfo = nullptr;
42         return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
43     }
44     std::string installDirPath;
45     std::string dataDirPath;
46     BundleInfo *info = ManagerService::GetInstance().QueryBundleInfo(bundleProfile.bundleName);
47     if (info != nullptr) {
48         size_t index = std::string(info->codePath).find_last_of(PATH_SEPARATOR);
49         if (index == std::string::npos) {
50             HILOG_ERROR(HILOG_MODULE_APP, "codePath is invalid!");
51             BundleInfoUtils::FreeBundleInfo(*bundleInfo);
52             *bundleInfo = nullptr;
53             return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
54         }
55         installDirPath = std::string(info->codePath).substr(0, index);
56         index = std::string(info->dataPath).find_last_of(PATH_SEPARATOR);
57         if (index == std::string::npos) {
58             HILOG_ERROR(HILOG_MODULE_APP, "dataPath is invalid!");
59             BundleInfoUtils::FreeBundleInfo(*bundleInfo);
60             *bundleInfo = nullptr;
61             return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
62         }
63         dataDirPath = std::string(info->dataPath).substr(0, index);
64     } else {
65         installDirPath = ManagerService::GetInstance().GetCodeDirPath();
66         dataDirPath = ManagerService::GetInstance().GetDataDirPath();
67     }
68 
69     if (!SetBundleInfo(bundleProfile, installDirPath, dataDirPath, *bundleInfo)) {
70         BundleInfoUtils::FreeBundleInfo(*bundleInfo);
71         *bundleInfo = nullptr;
72         return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
73     }
74     return ERR_OK;
75 }
76 
CreateBundleInfo(const BundleProfile & bundleProfile,const std::string & installDirPath,const std::string & dataDirPath,const BundleRes & bundleRes)77 BundleInfo *BundleInfoCreator::CreateBundleInfo(const BundleProfile &bundleProfile, const std::string &installDirPath,
78     const std::string &dataDirPath, const BundleRes &bundleRes)
79 {
80     BundleInfo *bundleInfo = reinterpret_cast<BundleInfo *>(AdapterMalloc(sizeof(BundleInfo)));
81     if (bundleInfo == nullptr) {
82         HILOG_ERROR(HILOG_MODULE_APP, "malloc bundleInfo fail when create bundleInfo!");
83         return nullptr;
84     }
85 
86     int32_t ret = memset_s(bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo));
87     if (ret != EOK) {
88         HILOG_ERROR(HILOG_MODULE_APP, "memset bundleInfo fail when create bundleInfo!");
89         AdapterFree(bundleInfo);
90         return nullptr;
91     }
92 
93     if (!SetBundleInfo(bundleProfile, installDirPath, dataDirPath, bundleInfo)) {
94         HILOG_ERROR(HILOG_MODULE_APP, "set bundleInfo fail when create bundleInfo!");
95         BundleInfoUtils::FreeBundleInfo(bundleInfo);
96         return nullptr;
97     }
98 
99     uint8_t errorCode = BundleResTransform::ConvertResInfoToBundleInfo(bundleInfo->codePath, bundleRes, bundleInfo);
100     if (errorCode != ERR_OK) {
101         HILOG_ERROR(HILOG_MODULE_APP, "convert resInfo to bundleInfo fail when create bundleInfo!");
102         BundleInfoUtils::FreeBundleInfo(bundleInfo);
103         return nullptr;
104     }
105     return bundleInfo;
106 }
107 
SetBundleInfo(const BundleProfile & bundleProfile,const std::string & installDirPath,const std::string & dataDirPath,BundleInfo * bundleInfo)108 bool BundleInfoCreator::SetBundleInfo(const BundleProfile &bundleProfile, const std::string &installDirPath,
109     const std::string &dataDirPath, BundleInfo *bundleInfo)
110 {
111     if (bundleInfo == nullptr || installDirPath.empty() || dataDirPath.empty()) {
112         return false;
113     }
114     bundleInfo->isKeepAlive = bundleProfile.isKeepAlive;
115     bundleInfo->isNativeApp = bundleProfile.isNativeApp;
116     bundleInfo->versionCode = bundleProfile.profileVersion.versionCode;
117     bundleInfo->compatibleApi = bundleProfile.profileApiVersion.minApiVersion;
118     if (bundleProfile.profileApiVersion.maxApiVersion == 0) {
119         bundleInfo->targetApi = bundleInfo->compatibleApi;
120     } else {
121         bundleInfo->targetApi = bundleProfile.profileApiVersion.maxApiVersion;
122     }
123     // set codePath and dataPath
124     std::string codePath = installDirPath + PATH_SEPARATOR + bundleProfile.bundleName;
125     std::string dataPath = dataDirPath + PATH_SEPARATOR + bundleProfile.bundleName;
126     if (codePath.size() > PATH_LENGTH || dataPath.size() > PATH_LENGTH) {
127         HILOG_ERROR(HILOG_MODULE_APP, "codePath or dataPath length exceed max value!");
128         return false;
129     }
130 
131     if (bundleProfile.iconPath != nullptr && !BundleUtil::StartWith(bundleProfile.iconPath,
132         DEFAULT_ICON_SETTING_BEGIN)) {
133         std::string iconPath = codePath + PATH_SEPARATOR + bundleProfile.moduleInfo.moduleName + PATH_SEPARATOR +
134             bundleProfile.iconPath;
135         if (iconPath.size() > PATH_LENGTH) {
136             HILOG_ERROR(HILOG_MODULE_APP, "iconPath length exceed max value!");
137             return false;
138         }
139         if (!BundleInfoUtils::SetBundleInfoBigIconPath(bundleInfo, iconPath.c_str())) {
140             HILOG_ERROR(HILOG_MODULE_APP, "set iconPath in bundleInfo fail!");
141             return false;
142         }
143     }
144 
145     if ((bundleProfile.label != nullptr) && !BundleUtil::StartWith(bundleProfile.label, DEFAULT_LABEL_SETTING)) {
146         if (!BundleInfoUtils::SetBundleInfoLabel(bundleInfo, bundleProfile.label)) {
147             HILOG_ERROR(HILOG_MODULE_APP, "set label in bundleInfo fail!");
148             return false;
149         }
150     }
151 
152     if (!BundleInfoUtils::SetBundleInfoBundleName(bundleInfo, bundleProfile.bundleName) ||
153         !BundleInfoUtils::SetBundleInfoVersionName(bundleInfo, bundleProfile.profileVersion.versionName) ||
154         !BundleInfoUtils::SetBundleInfoCodePath(bundleInfo, codePath.c_str()) ||
155         !BundleInfoUtils::SetBundleInfoDataPath(bundleInfo, dataPath.c_str()) ||
156         (bundleProfile.vendor != nullptr && !BundleInfoUtils::SetBundleInfoVendor(bundleInfo, bundleProfile.vendor)) ||
157         !SetModuleInfos(bundleProfile, bundleInfo) ||
158         (bundleProfile.numOfAbility != 0 && !SetAbilityInfos(bundleProfile, codePath, bundleInfo))) {
159         HILOG_ERROR(HILOG_MODULE_APP, "SetBundleInfo fail!");
160         return false;
161     }
162     return true;
163 }
164 
SetModuleInfos(const BundleProfile & bundleProfile,BundleInfo * bundleInfo)165 bool BundleInfoCreator::SetModuleInfos(const BundleProfile &bundleProfile, BundleInfo *bundleInfo)
166 {
167     if (bundleInfo == nullptr) {
168         return false;
169     }
170     bundleInfo->moduleInfos = reinterpret_cast<ModuleInfo *>(AdapterMalloc(sizeof(ModuleInfo)));
171     if (bundleInfo->moduleInfos == nullptr) {
172         return false;
173     }
174 
175     if (memset_s(bundleInfo->moduleInfos, sizeof(ModuleInfo), 0, sizeof(ModuleInfo)) != EOK) {
176         HILOG_ERROR(HILOG_MODULE_APP, "memset ModuleInfo fail!");
177         AdapterFree(bundleInfo->moduleInfos);
178         bundleInfo->moduleInfos = nullptr;
179         return false;
180     }
181 
182     bundleInfo->numOfModule = 1;
183     if (!ModuleInfoUtils::SetModuleInfoDeviceType(bundleInfo->moduleInfos,
184         const_cast<char **>(bundleProfile.moduleInfo.deviceType), DEVICE_TYPE_SIZE)) {
185         HILOG_ERROR(HILOG_MODULE_APP, "SetModuleInfoDeviceType fail!");
186         return false;
187     }
188     if (!ModuleInfoUtils::SetModuleInfoMetaData(bundleInfo->moduleInfos,
189         const_cast<MetaData **>(bundleProfile.moduleInfo.metaData), METADATA_SIZE)) {
190         HILOG_ERROR(HILOG_MODULE_APP, "SetModuleInfoMetaData fail!");
191         return false;
192     }
193 
194     if (bundleProfile.moduleInfo.description != nullptr &&
195         !BundleUtil::StartWith(bundleProfile.moduleInfo.description, DEFAULT_DESC_SETTING)) {
196         if (!ModuleInfoUtils::SetModuleInfoDescription(bundleInfo->moduleInfos,
197             bundleProfile.moduleInfo.description)) {
198             HILOG_ERROR(HILOG_MODULE_APP, "SetModuleInfo description fail!");
199             return false;
200         }
201     }
202     if ((bundleProfile.moduleInfo.name != nullptr &&
203         !ModuleInfoUtils::SetModuleInfoName(bundleInfo->moduleInfos, bundleProfile.moduleInfo.name)) ||
204         !ModuleInfoUtils::SetModuleInfoModuleName(bundleInfo->moduleInfos, bundleProfile.moduleInfo.moduleName) ||
205         !ModuleInfoUtils::SetModuleInfoModuleType(bundleInfo->moduleInfos, bundleProfile.moduleInfo.moduleType)) {
206         HILOG_ERROR(HILOG_MODULE_APP, "Set name or moduleName or moduleType or description in ModuleInfo fail!");
207         return false;
208     }
209     bundleInfo->moduleInfos->isDeliveryInstall = bundleProfile.moduleInfo.isDeliveryInstall;
210     return true;
211 }
212 
SetAbilityInfos(const BundleProfile & bundleProfile,const std::string & codePath,BundleInfo * bundleInfo)213 bool BundleInfoCreator::SetAbilityInfos(const BundleProfile &bundleProfile, const std::string &codePath,
214     BundleInfo *bundleInfo)
215 {
216     if (bundleInfo == nullptr || bundleProfile.numOfAbility == 0) {
217         return false;
218     }
219     bundleInfo->numOfAbility = bundleProfile.numOfAbility;
220     bundleInfo->abilityInfos = reinterpret_cast<AbilityInfo *>(AdapterMalloc(sizeof(AbilityInfo) *
221         bundleInfo->numOfAbility));
222     if (bundleInfo->abilityInfos == nullptr) {
223         return false;
224     }
225     int32_t ret = memset_s(bundleInfo->abilityInfos, sizeof(AbilityInfo) * bundleInfo->numOfAbility, 0,
226         sizeof(AbilityInfo) * bundleInfo->numOfAbility);
227     if (ret != EOK) {
228         HILOG_ERROR(HILOG_MODULE_APP, "memset AbilityInfo fail!");
229         return false;
230     }
231 
232     for (int32_t i = 0; i < bundleInfo->numOfAbility; i++) {
233         if (!SetAbilityInfo(bundleProfile, codePath, bundleInfo, i)) {
234             return false;
235         }
236     }
237     return true;
238 }
239 
SetAbilityInfo(const BundleProfile & bundleProfile,const std::string & codePath,BundleInfo * bundleInfo,uint32_t index)240 bool BundleInfoCreator::SetAbilityInfo(const BundleProfile &bundleProfile, const std::string &codePath,
241     BundleInfo *bundleInfo, uint32_t index)
242 {
243     if (bundleInfo == nullptr) {
244         return false;
245     }
246 
247     AbilityInfo *abilityInfo = bundleInfo->abilityInfos + index;
248     if (abilityInfo == nullptr) {
249         return false;
250     }
251 
252     if (bundleProfile.abilityInfos[index].iconPath != nullptr &&
253         !BundleUtil::StartWith(bundleProfile.abilityInfos[index].iconPath, DEFAULT_ICON_SETTING_BEGIN)) {
254         std::string iconPath = codePath + PATH_SEPARATOR + bundleProfile.moduleInfo.moduleName + PATH_SEPARATOR +
255             bundleProfile.abilityInfos[index].iconPath;
256         if (iconPath.size() > PATH_LENGTH) {
257             HILOG_ERROR(HILOG_MODULE_APP, "ability iconPath length exceed max value!");
258             return false;
259         }
260         if (!AbilityInfoUtils::SetAbilityInfoIconPath(abilityInfo, iconPath.c_str())) {
261             HILOG_ERROR(HILOG_MODULE_APP, "Set iconPath in AbilityInfos fail!");
262             return false;
263         }
264     }
265 
266     if (bundleProfile.abilityInfos[index].description != nullptr &&
267         !BundleUtil::StartWith(bundleProfile.abilityInfos[index].description, DEFAULT_DESC_SETTING)) {
268         if (!AbilityInfoUtils::SetAbilityInfoDescription(abilityInfo, bundleProfile.abilityInfos[index].description)) {
269             HILOG_ERROR(HILOG_MODULE_APP, "Set description in AbilityInfos fail!");
270             return false;
271         }
272     }
273 
274     if (bundleProfile.abilityInfos[index].label != nullptr &&
275         !BundleUtil::StartWith(bundleProfile.abilityInfos[index].label, DEFAULT_LABEL_SETTING)) {
276         if (!AbilityInfoUtils::SetAbilityInfoLabel(abilityInfo, bundleProfile.abilityInfos[index].label)) {
277             HILOG_ERROR(HILOG_MODULE_APP, "Set label in AbilityInfos fail!");
278             return false;
279         }
280     }
281 
282     abilityInfo->isVisible = bundleProfile.abilityInfos[index].isVisible;
283     abilityInfo->abilityType = bundleProfile.abilityInfos[index].abilityType;
284     abilityInfo->launchMode = bundleProfile.abilityInfos[index].launchMode;
285     if (!AbilityInfoUtils::SetAbilityInfoBundleName(abilityInfo, bundleProfile.bundleName) ||
286         !AbilityInfoUtils::SetAbilityInfoModuleName(abilityInfo, bundleProfile.moduleInfo.moduleName) ||
287         !AbilityInfoUtils::SetAbilityInfoName(abilityInfo, bundleProfile.abilityInfos[index].name)) {
288         HILOG_ERROR(HILOG_MODULE_APP, "Set other value in AbilityInfos fail!");
289         return false;
290     }
291     return true;
292 }
293 } // namespace OHOS