From 3c8ef845540d750148fc23a9a500aa185c6053c3 Mon Sep 17 00:00:00 2001 From: wangdengjia Date: Wed, 11 Aug 2021 09:51:04 +0800 Subject: [PATCH] IssueNo:#I3XCB5 Description:add get file folder size interface Sig:appexecfwk Feature or Bugfix:Feature Binary Source:No Signed-off-by: wangdengjia --- .../include/bundle_callback_utils.h | 5 ++ .../include/bundlems_slite_client.h | 2 +- frameworks/bundle_lite/src/bundle_manager.cpp | 56 +++++++++++++ .../src/slite/bundle_manager_inner.cpp | 4 +- .../src/slite/bundlems_slite_client.cpp | 6 +- .../bundlemgr_lite/bundle_inner_interface.h | 2 + .../bundlemgr_lite/bundle_service_interface.h | 2 +- .../bundlemgr_lite/slite/bundle_install_msg.h | 18 ++--- interfaces/kits/bundle_lite/bundle_manager.h | 11 +++ .../include/bundle_manager_service.h | 1 + .../include/bundle_mgr_slite_feature.h | 2 +- .../include/bundle_ms_feature.h | 2 + services/bundlemgr_lite/include/bundle_util.h | 2 + .../include/gt_bundle_extractor.h | 2 +- .../include/gt_bundle_manager_service.h | 10 +-- .../src/bundle_manager_service.cpp | 19 +++++ .../src/bundle_mgr_slite_feature.cpp | 6 +- .../bundlemgr_lite/src/bundle_ms_feature.cpp | 26 +++++++ services/bundlemgr_lite/src/bundle_util.cpp | 74 ++++++++++++++++++ .../src/gt_bundle_extractor.cpp | 20 ++--- .../src/gt_bundle_installer.cpp | 11 ++- .../src/gt_bundle_manager_service.cpp | 78 +++++++++---------- 22 files changed, 278 insertions(+), 81 deletions(-) diff --git a/frameworks/bundle_lite/include/bundle_callback_utils.h b/frameworks/bundle_lite/include/bundle_callback_utils.h index 376c90e..ec1031d 100755 --- a/frameworks/bundle_lite/include/bundle_callback_utils.h +++ b/frameworks/bundle_lite/include/bundle_callback_utils.h @@ -67,4 +67,9 @@ struct BundleCallbackInfo { BundleStateCallback bundleStateCallback; void *data; }; + +struct ResultOfGetBundleSize { + uint8_t resultCode; + uint32_t bundleSize; +}; #endif // OHOS_BUNDLE_CALLBACK_UTILS_H \ No newline at end of file diff --git a/frameworks/bundle_lite/include/bundlems_slite_client.h b/frameworks/bundle_lite/include/bundlems_slite_client.h index 8e5d9ec..a96f37d 100755 --- a/frameworks/bundle_lite/include/bundlems_slite_client.h +++ b/frameworks/bundle_lite/include/bundlems_slite_client.h @@ -47,7 +47,7 @@ public: bool GetInstallState(const char *bundleName, InstallState *installState, uint8_t *installProcess) const; - bool GetUninstallState(const char *bundleName, UninstallState *uninstallState) const; + uint32_t GetBundleSize(const char *bundleName) const; private: BundleMsClient() = default; diff --git a/frameworks/bundle_lite/src/bundle_manager.cpp b/frameworks/bundle_lite/src/bundle_manager.cpp index 6386271..f52f02a 100755 --- a/frameworks/bundle_lite/src/bundle_manager.cpp +++ b/frameworks/bundle_lite/src/bundle_manager.cpp @@ -224,6 +224,22 @@ static uint8_t DeserializeInnerBundleName(IOwner owner, IpcIo *reply) return resultCode; } +static uint8_t DeserializeBundleSize(IOwner owner, IpcIo *reply) +{ + if ((reply == nullptr) || (owner == nullptr)) { + return OHOS_FAILURE; + } + uint8_t resultCode = IpcIoPopUint8(reply); + ResultOfGetBundleSize *info = reinterpret_cast(owner); + if (resultCode != ERR_OK) { + info->resultCode = resultCode; + return resultCode; + } + info->bundleSize = IpcIoPopUint32(reply); + info->resultCode = resultCode; + return resultCode; +} + static uint8_t DeserializeSystemCapabilities(IOwner owner, IpcIo *reply) { if ((reply == nullptr) || (owner == nullptr)) { @@ -300,6 +316,9 @@ static int Notify(IOwner owner, int code, IpcIo *reply) HILOG_INFO(HILOG_MODULE_APP, "BundleManager HasSystemCapability invoke return: %{public}d", *ret); break; } + case GET_BUNDLE_SIZE: { + return DeserializeBundleSize(owner, reply); + } case GET_SYS_CAP: { return DeserializeSystemCapabilities(owner, reply); } @@ -605,6 +624,43 @@ uint8_t GetBundleInfos(const int flags, BundleInfo **bundleInfos, int32_t *len) return ObtainInnerBundleInfos(flags, bundleInfos, len, GET_BUNDLE_INFOS, &ipcIo); } +uint32_t GetBundleSize(const char *bundleName) +{ + if (bundleName == nullptr) { + return 0; + } + if (strlen(bundleName) >= MAX_BUNDLE_NAME) { + return 0; + } + if (CheckSelfPermission(static_cast(PERMISSION_GET_BUNDLE_INFO)) != GRANTED) { + HILOG_ERROR(HILOG_MODULE_APP, "BundleManager get bundle size failed due to permission denied"); + return 0; + } + auto bmsClient = GetBmsClient(); + if (bmsClient == nullptr) { + HILOG_ERROR(HILOG_MODULE_APP, "BundleManager get bundle size failed due to nullptr bms client"); + return 0; + } + + IpcIo ipcIo; + char data[IPC_IO_DATA_MAX]; + IpcIoInit(&ipcIo, data, IPC_IO_DATA_MAX, 0); + IpcIoPushString(&ipcIo, bundleName); + if (!IpcIoAvailable(&ipcIo)) { + HILOG_ERROR(HILOG_MODULE_APP, "BundleManager GetBundleSize ipc failed"); + return 0; + } + + ResultOfGetBundleSize resultOfGetBundleSize; + int32_t ret = bmsClient->Invoke(bmsClient, GET_BUNDLE_SIZE, &ipcIo, &resultOfGetBundleSize, Notify); + if (ret != OHOS_SUCCESS) { + HILOG_ERROR(HILOG_MODULE_APP, "BundleManager GetBundleSize invoke failed: %{public}d", ret); + return 0; + } + uint32_t bundleSize = resultOfGetBundleSize.bundleSize; + return bundleSize; +} + uint8_t QueryKeepAliveBundleInfos(BundleInfo **bundleInfos, int32_t *len) { if ((bundleInfos == nullptr) || (len == nullptr)) { diff --git a/frameworks/bundle_lite/src/slite/bundle_manager_inner.cpp b/frameworks/bundle_lite/src/slite/bundle_manager_inner.cpp index 853b04e..04c4e1b 100755 --- a/frameworks/bundle_lite/src/slite/bundle_manager_inner.cpp +++ b/frameworks/bundle_lite/src/slite/bundle_manager_inner.cpp @@ -24,8 +24,8 @@ bool GetInstallState(const char *bundleName, InstallState *installState, uint8_t return OHOS::BundleMsClient::GetInstance().GetInstallState(bundleName, installState, installProcess); } -bool GetUninstallState(const char *bundleName, UninstallState *uninstallState) +uint32_t GetBundleSize(const char *bundleName) { - return OHOS::BundleMsClient::GetInstance().GetUninstallState(bundleName, uninstallState); + return OHOS::BundleMsClient::GetInstance().GetBundleSize(bundleName); } } diff --git a/frameworks/bundle_lite/src/slite/bundlems_slite_client.cpp b/frameworks/bundle_lite/src/slite/bundlems_slite_client.cpp index 60a7185..ba326bc 100755 --- a/frameworks/bundle_lite/src/slite/bundlems_slite_client.cpp +++ b/frameworks/bundle_lite/src/slite/bundlems_slite_client.cpp @@ -158,11 +158,11 @@ bool BundleMsClient::GetInstallState (const char *bundleName, InstallState *inst return bmsProxy_->GetInstallState(bundleName, installState, installProcess); } -bool BundleMsClient::GetUninstallState (const char *bundleName, UninstallState *uninstallState) const +uint32_t BundleMsClient::GetBundleSize (const char *bundleName) const { if (!Initialize()) { - return false; + return -1; } - return bmsProxy_->GetUninstallState(bundleName, uninstallState); + return bmsProxy_->GetBundleSize(bundleName); } } // namespace OHOS diff --git a/interfaces/innerkits/bundlemgr_lite/bundle_inner_interface.h b/interfaces/innerkits/bundlemgr_lite/bundle_inner_interface.h index a79850d..c5b679c 100755 --- a/interfaces/innerkits/bundlemgr_lite/bundle_inner_interface.h +++ b/interfaces/innerkits/bundlemgr_lite/bundle_inner_interface.h @@ -40,6 +40,7 @@ enum BmsCmd { QUERY_KEEPALIVE_BUNDLE_INFOS, GET_BUNDLE_INFOS_BY_METADATA, CHECK_SYS_CAP, + GET_BUNDLE_SIZE, GET_SYS_CAP, BMS_INNER_BEGIN, INSTALL = BMS_INNER_BEGIN, // bms install application @@ -59,6 +60,7 @@ struct BmsServerProxy { uint8_t (*GetBundleInfos)(int flags, BundleInfo **bundleInfos, int32_t *len); uint8_t (*QueryKeepAliveBundleInfos)(BundleInfo **bundleInfos, int32_t *len); uint8_t (*GetBundleNameForUid)(int32_t uid, char **bundleName); + uint32_t (*GetBundleSize)(const char *bundleName); }; struct BmsInnerServerProxy { diff --git a/interfaces/innerkits/bundlemgr_lite/bundle_service_interface.h b/interfaces/innerkits/bundlemgr_lite/bundle_service_interface.h index 271375a..fec629b 100755 --- a/interfaces/innerkits/bundlemgr_lite/bundle_service_interface.h +++ b/interfaces/innerkits/bundlemgr_lite/bundle_service_interface.h @@ -50,7 +50,7 @@ struct BmsSliteInterface { uint8_t (*GetBundleInfo)(const char *bundleName, int32_t flags, BundleInfo *bundleInfo); uint8_t (*GetBundleInfos)(int32_t flags, BundleInfo **bundleInfos, int32_t *len); bool (*GetInstallState)(const char *bundleName, InstallState *installState, uint8_t *installProcess); - bool (*GetUninstallState)(const char *bundleName, UninstallState *uninstallState); + uint32_t (*GetBundleSize)(const char *bundleName); }; #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/innerkits/bundlemgr_lite/slite/bundle_install_msg.h b/interfaces/innerkits/bundlemgr_lite/slite/bundle_install_msg.h index 552a04a..77523d5 100644 --- a/interfaces/innerkits/bundlemgr_lite/slite/bundle_install_msg.h +++ b/interfaces/innerkits/bundlemgr_lite/slite/bundle_install_msg.h @@ -27,7 +27,10 @@ extern "C" { typedef enum { BUNDLE_INSTALL_DOING = 0, BUNDLE_INSTALL_OK = 1, - BUNDLE_INSTALL_FAIL = 2 + BUNDLE_INSTALL_FAIL = 2, + BUNDLE_UNINSTALL_DOING = 3, + BUNDLE_UNINSTALL_OK = 4, + BUNDLE_UNINSTALL_FAIL = 5 } InstallState; typedef struct { @@ -39,12 +42,6 @@ typedef struct { uint8_t installProcess; } BundleInstallMsg; -typedef enum { - BUNDLE_UNINSTALL_DOING = 0, - BUNDLE_UNINSTALL_OK = 1, - BUNDLE_UNINSTALL_FAIL = 2 -} UninstallState; - /** * @brief Get the install state and install process of the bundle. * @@ -59,14 +56,13 @@ typedef enum { bool GetInstallState(const char *bundleName, InstallState *installState, uint8_t *installProcess); /** - * @brief Get the uninstall state and uninstall state of the bundle. + * @brief Get the bundle size. * * @param bundleName Indicates the name of the bundle. - * @param uninstallState Obtains uninstall state. - * @return Returns success or not. + * @return Returns bundle size or returns 0 if get bundle size failed. * */ -bool GetUninstallState(const char *bundleName, UninstallState *uninstallState); +uint32_t GetBundleSize(const char *bundleName); #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/kits/bundle_lite/bundle_manager.h b/interfaces/kits/bundle_lite/bundle_manager.h index c55791c..e11f23e 100755 --- a/interfaces/kits/bundle_lite/bundle_manager.h +++ b/interfaces/kits/bundle_lite/bundle_manager.h @@ -246,6 +246,17 @@ SystemCapability *GetSystemAvailableCapabilities(); * @version 4 */ void FreeSystemAvailableCapabilitiesInfo(SystemCapability *sysCap); + +/** + * @brief Get bundle size + * + * @param bundleName Indicates the bundle name. + * @return Returns the bundle size if this function is successfully called; returns 0 otherwise. + * + * @since 7 + * @version 7 + */ +uint32_t GetBundleSize(const char *bundleName); #ifdef __cplusplus #if __cplusplus } diff --git a/services/bundlemgr_lite/include/bundle_manager_service.h b/services/bundlemgr_lite/include/bundle_manager_service.h index 472762f..ec6bb6f 100755 --- a/services/bundlemgr_lite/include/bundle_manager_service.h +++ b/services/bundlemgr_lite/include/bundle_manager_service.h @@ -44,6 +44,7 @@ public: bool UpdateBundleInfo(BundleInfo *info); uint8_t GetBundleInfo(const char *bundleName, int32_t flags, BundleInfo& bundleInfo); uint8_t GetBundleInfos(int32_t flags, BundleInfo **bundleInfos, int32_t *len); + uint32_t GetBundleSize(const char *bundleName); std::vector GetServiceId() const; int32_t GenerateUid(const char *bundleName, int8_t bundleStyle); void RecycleUid(const char *bundleName); diff --git a/services/bundlemgr_lite/include/bundle_mgr_slite_feature.h b/services/bundlemgr_lite/include/bundle_mgr_slite_feature.h index 6bdfe1b..b81bdf4 100755 --- a/services/bundlemgr_lite/include/bundle_mgr_slite_feature.h +++ b/services/bundlemgr_lite/include/bundle_mgr_slite_feature.h @@ -36,7 +36,7 @@ public: static uint8_t GetBundleInfo(const char *bundleName, int32_t flags, BundleInfo *bundleInfo); static uint8_t GetBundleInfos(int32_t flags, BundleInfo **bundleInfos, int32_t *len); static bool GetInstallState(const char *bundleName, InstallState *installState, uint8_t *installProcess); - static bool GetUninstallState(const char *bundleName, UninstallState *uninstallState); + static uint32_t GetBundleSize (const char *bundleName); static BundleMgrSliteFeature *GetInstance() { diff --git a/services/bundlemgr_lite/include/bundle_ms_feature.h b/services/bundlemgr_lite/include/bundle_ms_feature.h index 8a872f3..f33cfdb 100755 --- a/services/bundlemgr_lite/include/bundle_ms_feature.h +++ b/services/bundlemgr_lite/include/bundle_ms_feature.h @@ -44,6 +44,7 @@ public: static uint8_t QueryKeepAliveBundleInfos(BundleInfo **bundleInfos, int32_t *len); static uint8_t GetBundleInfosByMetaData(const char *metaDataKey, BundleInfo **bundleInfos, int32_t *len); static uint8_t GetBundleNameForUid(int32_t uid, char **bundleName); + static uint32_t GetBundleSize(const char *bundleName); private: BundleMsFeature(); @@ -60,6 +61,7 @@ private: static uint8_t ChangeInnerCallbackServiceId(const uint8_t funcId, IpcIo *req, IpcIo *reply); static uint8_t HasSystemCapability(const uint8_t funcId, IpcIo *req, IpcIo *reply); static uint8_t GetSystemAvailableCapabilities(const uint8_t funcId, IpcIo *req, IpcIo *reply); + static uint8_t GetInnerBundleSize(const uint8_t funcId, IpcIo *req, IpcIo *reply); Identity identity_; static BundleInvokeType BundleMsInvokeFuc[BMS_INNER_BEGIN]; diff --git a/services/bundlemgr_lite/include/bundle_util.h b/services/bundlemgr_lite/include/bundle_util.h index 890c763..4a2e11c 100755 --- a/services/bundlemgr_lite/include/bundle_util.h +++ b/services/bundlemgr_lite/include/bundle_util.h @@ -36,6 +36,8 @@ public: static bool StartWith(const char *str, const char *subStr); static cJSON *GetJsonStream(const char *path); static uint32_t GetFileSize(const char *filePath); + static uint32_t GetFileFolderSize(const char *filePath); + static uint32_t GetCurrentFolderSize(const char *dirPath, List* list); static cJSON *ConvertInstallRecordToJson(const InstallRecord &installRecord); static char *GetValueFromBundleJson(const char *bundleName, const char *key); static int32_t GetValueFromBundleJson(const char *bundleName, const char *key, int32_t defaultValue); diff --git a/services/bundlemgr_lite/include/gt_bundle_extractor.h b/services/bundlemgr_lite/include/gt_bundle_extractor.h index abc321b..037a156 100644 --- a/services/bundlemgr_lite/include/gt_bundle_extractor.h +++ b/services/bundlemgr_lite/include/gt_bundle_extractor.h @@ -26,7 +26,7 @@ public: uint8_t bundleStyle); static char *ExtractHapProfile(int32_t fp, uint32_t totalFileSize); static uint8_t ExtractBundleParam(const char *path, int32_t &fpStart, char **bundleName); - static bool ExtractInstallMsg(const char *path, char **bundleName, char **label, char **smallIconPath, + static uint8_t ExtractInstallMsg(const char *path, char **bundleName, char **label, char **smallIconPath, char **bigIconPath); private: static uint8_t ExtractFileDataPos(int32_t fp, uint64_t &filePos); diff --git a/services/bundlemgr_lite/include/gt_bundle_manager_service.h b/services/bundlemgr_lite/include/gt_bundle_manager_service.h index 48b7afc..4f0564b 100755 --- a/services/bundlemgr_lite/include/gt_bundle_manager_service.h +++ b/services/bundlemgr_lite/include/gt_bundle_manager_service.h @@ -49,11 +49,6 @@ typedef enum { BUNDLE_UPDATE, } BundleState; -typedef struct { - char *bundleName; - UninstallState uninstallState; -} BundleUninstallMsg; - class GtManagerService { public: static GtManagerService &GetInstance() @@ -79,10 +74,10 @@ public: void ReduceNumOfThirdBundles(); int32_t ReportInstallCallback(uint8_t errCode, uint8_t installState, uint8_t process, InstallerCallback installerCallback); - int32_t ReportUninstallCallback(uint8_t errCode, char *bundleName, + int32_t ReportUninstallCallback(uint8_t errCode, uint8_t installState, char *bundleName, uint8_t process, InstallerCallback installerCallback); bool GetInstallState(const char *bundleName, InstallState *installState, uint8_t *installProcess); - bool GetUninstallState(const char *bundleName, UninstallState *uninstallState); + uint32_t GetBundleSize(const char *bundleName); private: GtManagerService(); @@ -112,7 +107,6 @@ private: BundleMap *bundleMap_; List *bundleResList_; BundleInstallMsg *bundleInstallMsg_; - BundleUninstallMsg *bundleUninstallMsg_; char *jsEngineVer_; uint32_t installedThirdBundleNum_; }; diff --git a/services/bundlemgr_lite/src/bundle_manager_service.cpp b/services/bundlemgr_lite/src/bundle_manager_service.cpp index 169fb03..b4e28ab 100755 --- a/services/bundlemgr_lite/src/bundle_manager_service.cpp +++ b/services/bundlemgr_lite/src/bundle_manager_service.cpp @@ -625,6 +625,25 @@ uint8_t ManagerService::GetBundleInfos(int32_t flags, BundleInfo **bundleInfos, return bundleMap_->GetBundleInfos(flags, bundleInfos, len); } +uint32_t ManagerService::GetBundleSize(const char *bundleName) +{ + if (bundleName == nullptr) { + return 0; + } + BundleInfo *installedInfo = bundleMap_->Get(bundleName); + if (installedInfo == nullptr) { + return 0; + } + char *codePath = installedInfo->codePath; + uint32_t codeBundleSize = BundleUtil::GetFileFolderSize(codePath); + if (codeBundleSize == 0) { + return 0; + } + char *dataPath = installedInfo->dataPath; + uint32_t dataBundleSize = BundleUtil::GetFileFolderSize(dataPath); + HILOG_INFO(HILOG_MODULE_APP, "bundle size is %{public}d\n", codeBundleSize + dataBundleSize); + return codeBundleSize + dataBundleSize; +} std::string ManagerService::GetCodeDirPath() const { diff --git a/services/bundlemgr_lite/src/bundle_mgr_slite_feature.cpp b/services/bundlemgr_lite/src/bundle_mgr_slite_feature.cpp index 4a019ee..5b12cff 100755 --- a/services/bundlemgr_lite/src/bundle_mgr_slite_feature.cpp +++ b/services/bundlemgr_lite/src/bundle_mgr_slite_feature.cpp @@ -33,7 +33,7 @@ BundleMgrSliteFeatureImpl g_bmsSliteImpl = { .GetBundleInfo = BundleMgrSliteFeature::GetBundleInfo, .GetBundleInfos = BundleMgrSliteFeature::GetBundleInfos, .GetInstallState = BundleMgrSliteFeature::GetInstallState, - .GetUninstallState = BundleMgrSliteFeature::GetUninstallState, + .GetBundleSize = BundleMgrSliteFeature::GetBundleSize, DEFAULT_IUNKNOWN_ENTRY_END }; @@ -128,8 +128,8 @@ bool BundleMgrSliteFeature::GetInstallState(const char *bundleName, InstallState return OHOS::GtManagerService::GetInstance().GetInstallState(bundleName, installState, installProcess); } -bool BundleMgrSliteFeature::GetUninstallState(const char *bundleName, UninstallState *uninstallState) +uint32_t BundleMgrSliteFeature::GetBundleSize(const char *bundleName) { - return OHOS::GtManagerService::GetInstance().GetUninstallState(bundleName, uninstallState); + return OHOS::GtManagerService::GetInstance().GetBundleSize(bundleName); } } // namespace OHOS diff --git a/services/bundlemgr_lite/src/bundle_ms_feature.cpp b/services/bundlemgr_lite/src/bundle_ms_feature.cpp index 6edbe6c..9a8c4a2 100755 --- a/services/bundlemgr_lite/src/bundle_ms_feature.cpp +++ b/services/bundlemgr_lite/src/bundle_ms_feature.cpp @@ -42,6 +42,7 @@ static BmsImpl g_bmsImpl = { .GetBundleInfos = BundleMsFeature::GetBundleInfos, .QueryKeepAliveBundleInfos = BundleMsFeature::QueryKeepAliveBundleInfos, .GetBundleNameForUid = BundleMsFeature::GetBundleNameForUid, + .GetBundleSize = BundleMsFeature::GetBundleSize, IPROXY_END }; @@ -54,6 +55,7 @@ BundleInvokeType BundleMsFeature::BundleMsInvokeFuc[BMS_INNER_BEGIN] { HandleGetBundleInfos, HandleGetBundleInfos, HasSystemCapability, + GetInnerBundleSize, GetSystemAvailableCapabilities, }; @@ -248,6 +250,25 @@ uint8_t BundleMsFeature::GetInnerBundleInfo(const uint8_t funcId, IpcIo *req, Ip return OHOS_SUCCESS; } +uint8_t BundleMsFeature::GetInnerBundleSize(const uint8_t funcId, IpcIo *req, IpcIo *reply) +{ + if ((req == nullptr) || (reply == nullptr)) { + return ERR_APPEXECFWK_OBJECT_NULL; + } + size_t size = 0; + char *bundleName = reinterpret_cast(IpcIoPopString(req, &size)); + if (bundleName == nullptr) { + return ERR_APPEXECFWK_DESERIALIZATION_FAILED; + } + uint32_t bundleSize = GetBundleSize(bundleName); + if (bundleSize == 0) { + return ERR_APPEXECFWK_OBJECT_NULL; + } + IpcIoPushUint8(reply, static_cast(OHOS_SUCCESS)); + IpcIoPushUint32(reply, bundleSize); + return OHOS_SUCCESS; +} + uint8_t BundleMsFeature::HandleGetBundleInfos(const uint8_t funcId, IpcIo *req, IpcIo *reply) { if ((req == nullptr) || (reply == nullptr)) { @@ -427,6 +448,11 @@ uint8_t BundleMsFeature::GetBundleInfos(const int flags, BundleInfo **bundleInfo return OHOS::ManagerService::GetInstance().GetBundleInfos(flags, bundleInfos, len); } +uint32_t BundleMsFeature::GetBundleSize(const char *bundleName) +{ + return OHOS::ManagerService::GetInstance().GetBundleSize(bundleName); +} + uint8_t BundleMsFeature::QueryKeepAliveBundleInfos(BundleInfo **bundleInfos, int32_t *len) { if ((bundleInfos == nullptr) || (len == nullptr)) { diff --git a/services/bundlemgr_lite/src/bundle_util.cpp b/services/bundlemgr_lite/src/bundle_util.cpp index 7458161..6fb9877 100755 --- a/services/bundlemgr_lite/src/bundle_util.cpp +++ b/services/bundlemgr_lite/src/bundle_util.cpp @@ -182,6 +182,80 @@ uint32_t BundleUtil::GetFileSize(const char *filePath) return fileInfo.st_size; } +uint32_t BundleUtil::GetFileFolderSize(const char *filePath) +{ + if (!CheckRealPath(filePath)) { + return 0; + } + List* list = new (std::nothrow)List(); + if (list == nullptr) { +#ifdef APP_PLATFORM_WATCHGT + HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] GetFolderSize failed, list is null"); +#endif + return 0; + } + + list->PushFront(Utils::Strdup(filePath)); + uint32_t fileFolderSize = 0; + while(!list->IsEmpty()) { + char *curPath = list->Front(); + if (curPath == nullptr) { + break; + } + fileFolderSize += GetCurrentFolderSize(curPath, list); + list->PopFront(); + AdapterFree(curPath); + } + + if (!list->IsEmpty()) { +#ifdef APP_PLATFORM_WATCHGT + HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] After get folder size, list is still not empty"); +#endif + for (auto node = list->Begin(); node != list->End(); node = node->next_) { + AdapterFree(node->value_); + } + } + delete list; + return fileFolderSize; +} + +uint32_t BundleUtil::GetCurrentFolderSize(const char *dirPath, List* list) +{ + DIR *dir = nullptr; + if ((dir = opendir(dirPath)) == nullptr) { + return 0; + } + dirent *dp = nullptr; + char filePath[PATH_LENGTH] = { 0 }; + struct stat buf = { 0 }; + uint32_t fileSize = 0; + while ((dp = readdir(dir)) != nullptr) { + if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..")) == 0) { + continue; + } + + if (memset_s(filePath, PATH_LENGTH, 0, PATH_LENGTH) != EOK) { + continue; + } + + if (sprintf_s(filePath, PATH_LENGTH, "%s/%s", dirPath, dp->d_name) < 0) { + continue; + } + + if (!IsDir(filePath)) { + if (stat(filePath, &buf) != 0 || buf.st_size <= 0) { + fileSize = 0; + break; + } + fileSize += buf.st_size; + } else { + list->PushBack(Utils::Strdup(filePath)); + } + } + closedir(dir); + return fileSize; +} + void BundleUtil::DeleteJsonFile(const char *bundleName, const char *randStr) { if (bundleName == nullptr || randStr == nullptr) { diff --git a/services/bundlemgr_lite/src/gt_bundle_extractor.cpp b/services/bundlemgr_lite/src/gt_bundle_extractor.cpp index 35280c2..1750d77 100755 --- a/services/bundlemgr_lite/src/gt_bundle_extractor.cpp +++ b/services/bundlemgr_lite/src/gt_bundle_extractor.cpp @@ -179,45 +179,45 @@ bool GtBundleExtractor::ExtractResourceFile(const char *path, int32_t fp, uint32 return true; } -bool GtBundleExtractor::ExtractInstallMsg(const char *path, char **bundleName, char **label, char **smallIconPath, +uint8_t GtBundleExtractor::ExtractInstallMsg(const char *path, char **bundleName, char **label, char **smallIconPath, char **bigIconPath) { if (!BundleUtil::CheckRealPath(path)) { - return false; + return ERR_APPEXECFWK_INSTALL_FAILED_PARAM_ERROR; } int32_t totalFileSize = APPVERI_GetUnsignedFileLength(path); if (totalFileSize == V_ERR) { - return false; + return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR; } char *emptyJsPathComp[] = {const_cast(TMP_RESOURCE_DIR), const_cast(ASSET_JS_PATH)}; char *emptyJsPath = BundleUtil::Strscat(emptyJsPathComp, sizeof(emptyJsPathComp) / sizeof(char *)); if (emptyJsPath == nullptr) { - return false; + return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR; } if (!BundleUtil::MkDirs(emptyJsPath)) { AdapterFree(emptyJsPath); - return false; + return ERR_APPEXECFWK_INSTALL_FAILED_CREATE_DATA_DIR_ERROR; } AdapterFree(emptyJsPath); int32_t fp = open(path, O_RDONLY, S_IREAD | S_IWRITE); if (fp < 0) { - return false; + return ERR_APPEXECFWK_INSTALL_FAILED_FILE_NOT_EXISTS; } // extractor config.json、 resources dir and resources.index in TMP_RESOURCE_DIR if (!ExtractResourceFile(TMP_RESOURCE_DIR, fp, static_cast(totalFileSize))) { close(fp); - return false; + return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_PROFILE_ERROR; } close(fp); // get bundleName、 label、 smallIconPath and bigIconPath from bundleInfo BundleRes bundleRes = { 0 }; BundleInfo *bundleInfo = GtBundleParser::ParseHapProfile(TMP_RESOURCE_DIR, &bundleRes); if (bundleInfo == nullptr) { - return false; + return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_PROFILE_ERROR; } if (bundleRes.abilityRes != nullptr) { AdapterFree(bundleRes.abilityRes); @@ -228,10 +228,10 @@ bool GtBundleExtractor::ExtractInstallMsg(const char *path, char **bundleName, c *bigIconPath = Utils::Strdup(bundleInfo->bigIconPath); if (*bundleName == nullptr || *label == nullptr || *smallIconPath == nullptr || *bigIconPath == nullptr) { BundleInfoUtils::FreeBundleInfo(bundleInfo); - return false; + return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_ABILITIES_ERROR; } BundleInfoUtils::FreeBundleInfo(bundleInfo); - return true; + return ERR_OK; } uint8_t GtBundleExtractor::ExtractBundleParam(const char *path, int32_t &fpStart, char **bundleName) diff --git a/services/bundlemgr_lite/src/gt_bundle_installer.cpp b/services/bundlemgr_lite/src/gt_bundle_installer.cpp index 69eeefc..05e5de3 100755 --- a/services/bundlemgr_lite/src/gt_bundle_installer.cpp +++ b/services/bundlemgr_lite/src/gt_bundle_installer.cpp @@ -234,8 +234,8 @@ uint8_t GtBundleInstaller::ProcessBundleInstall(const char *path, const char *ra BundleInfo *bundleInfo = nullptr; (void) GtManagerService::GetInstance().ReportInstallCallback(OPERATION_DOING, 0, BMS_FIRST_FINISHED_PROCESS, installerCallback); uint8_t errorCode = PreCheckBundle(path, fp, signatureInfo, fileSize, bundleStyle); - (void) GtManagerService::GetInstance().ReportInstallCallback(OPERATION_DOING, 0, BMS_SECOND_FINISHED_PROCESS, installerCallback); CHECK_PRO_RESULT(errorCode, fp, permissions, bundleInfo, signatureInfo); + (void) GtManagerService::GetInstance().ReportInstallCallback(OPERATION_DOING, 0, BMS_SECOND_FINISHED_PROCESS, installerCallback); // parse HarmoyProfile.json, get permissions and bundleInfo errorCode = GtBundleParser::ParseHapProfile(fp, fileSize, permissions, bundleRes, &bundleInfo); CHECK_PRO_RESULT(errorCode, fp, permissions, bundleInfo, signatureInfo); @@ -285,6 +285,11 @@ uint8_t GtBundleInstaller::ProcessBundleInstall(const char *path, const char *ra errorCode = MoveRawFileToDataPath(bundleInfo); CHECK_PRO_ROLLBACK(errorCode, permissions, bundleInfo, signatureInfo, randStr); (void) GtManagerService::GetInstance().ReportInstallCallback(OPERATION_DOING, 0, BMS_FIFTH_FINISHED_PROCESS, installerCallback); + // store permissions + errorCode = StorePermissions(installRecord.bundleName, permissions.permissionTrans, permissions.permNum, + isUpdate); + CHECK_PRO_ROLLBACK(errorCode, permissions, bundleInfo, signatureInfo, randStr); + // update bundle Info errorCode = UpdateBundleInfo(bundleStyle, labelId, iconId, bundleInfo, isUpdate); CHECK_PRO_ROLLBACK(errorCode, permissions, bundleInfo, signatureInfo, randStr); // free memory @@ -572,6 +577,10 @@ uint8_t GtBundleInstaller::Uninstall(const char *bundleName) return ERR_APPEXECFWK_UNINSTALL_FAILED_INTERNAL_ERROR; } + if (DeletePermissions(const_cast(bundleName)) < 0) { + return ERR_APPEXECFWK_UNINSTALL_FAILED_DELETE_PERMISSIONS_ERROR; + } + bool res = CheckIsThirdSystemBundle(bundleName); if (!(BundleUtil::RemoveDir(bundleInfo->codePath) && BundleUtil::RemoveDir(bundleInfo->dataPath))) { GtManagerService::GetInstance().RemoveBundleInfo(bundleName); diff --git a/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp b/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp index 4d9e534..3b0a010 100755 --- a/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp +++ b/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp @@ -49,7 +49,6 @@ GtManagerService::GtManagerService() bundleResList_ = new List(); bundleMap_ = BundleMap::GetInstance(); bundleInstallMsg_ = nullptr; - bundleUninstallMsg_ = nullptr; jsEngineVer_ = nullptr; installedThirdBundleNum_ = 0; } @@ -95,20 +94,25 @@ bool GtManagerService::Install(const char *hapPath, const InstallParam *installP return false; } // set bundleName、label、smallIconPath、bigIconPath in bundleInstallMsg_ - bool isSuccess = GtBundleExtractor::ExtractInstallMsg(path, &(bundleInstallMsg_->bundleName), + uint8_t ret = GtBundleExtractor::ExtractInstallMsg(path, &(bundleInstallMsg_->bundleName), &(bundleInstallMsg_->label), &(bundleInstallMsg_->smallIconPath), &(bundleInstallMsg_->bigIconPath)); - if (!isSuccess) { + + if (ret != 0) { + char *name = strchr(path, '/'); + bundleInstallMsg_->bundleName = Utils::Strdup(name + 1); + (void) ReportInstallCallback(ret, BUNDLE_INSTALL_FAIL, BMS_INSTALLATION_COMPLETED, installerCallback); // delete resource temp dir (void) BundleUtil::RemoveDir(TMP_RESOURCE_DIR); ClearSystemBundleInstallMsg(); + AdapterFree(path); return false; } SetCurrentBundle(bundleInstallMsg_->bundleName); (void) ReportInstallCallback(OPERATION_DOING, 0, BMS_INSTALLATION_START, installerCallback); DisableServiceWdg(); - uint8_t ret = installer_->Install(path, installerCallback); + ret = installer_->Install(path, installerCallback); EnableServiceWdg(); if (ret == 0) { (void) ReportInstallCallback(ret, BUNDLE_INSTALL_OK, BMS_INSTALLATION_COMPLETED, installerCallback); @@ -145,30 +149,19 @@ bool GtManagerService::Uninstall(const char *bundleName, const InstallParam *ins } SetCurrentBundle(innerBundleName); - bundleUninstallMsg_ = reinterpret_cast(AdapterMalloc(sizeof(BundleUninstallMsg))); - if (bundleUninstallMsg_ == nullptr) { - return false; - } - if (memset_s(bundleUninstallMsg_, sizeof(BundleUninstallMsg), 0, sizeof(BundleUninstallMsg)) != EOK) { - AdapterFree(bundleUninstallMsg_); - return false; - } - bundleUninstallMsg_->bundleName = innerBundleName; - bundleUninstallMsg_->uninstallState = BUNDLE_UNINSTALL_DOING; - - (void) ReportUninstallCallback(OPERATION_DOING, innerBundleName, BMS_UNINSTALLATION_START, installerCallback); + (void) ReportUninstallCallback(OPERATION_DOING, BUNDLE_UNINSTALL_DOING, innerBundleName, + BMS_UNINSTALLATION_START, installerCallback); uint8_t ret = installer_->Uninstall(innerBundleName); if (ret == 0) { - bundleUninstallMsg_->uninstallState = BUNDLE_UNINSTALL_OK; + (void) ReportUninstallCallback(ret, BUNDLE_UNINSTALL_OK, innerBundleName, + BMS_INSTALLATION_COMPLETED, installerCallback); } else { - bundleUninstallMsg_->uninstallState = BUNDLE_UNINSTALL_FAIL; + (void) ReportUninstallCallback(ret, BUNDLE_UNINSTALL_FAIL, innerBundleName, + BMS_INSTALLATION_COMPLETED, installerCallback); } - (void) ReportUninstallCallback(ret, innerBundleName, BMS_INSTALLATION_COMPLETED, installerCallback); SetCurrentBundle(nullptr); AdapterFree(innerBundleName); - AdapterFree(bundleUninstallMsg_); - bundleUninstallMsg_ = nullptr; return true; } @@ -198,19 +191,25 @@ bool GtManagerService::GetInstallState(const char *bundleName, InstallState *ins return true; } -bool GtManagerService::GetUninstallState(const char *bundleName, UninstallState *uninstallState) +uint32_t GtManagerService::GetBundleSize(const char *bundleName) { - BundleInfo *uninstalledInfo = bundleMap_->Get(bundleName); - if (uninstalledInfo == nullptr) { - *uninstallState = BUNDLE_UNINSTALL_OK; - return true; + if (bundleName == nullptr) { + return 0; } - if (strcmp(bundleName, bundleUninstallMsg_->bundleName) == 0) { - *uninstallState = bundleUninstallMsg_->uninstallState; - return true; + BundleInfo *installedInfo = bundleMap_->Get(bundleName); + if (installedInfo == nullptr) { + HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] failed to get bundle size because the bundle does not exist!"); + return 0; } - *uninstallState = BUNDLE_UNINSTALL_DOING; - return true; + char *codePath = installedInfo->codePath; + uint32_t codeBundleSize = BundleUtil::GetFileFolderSize(codePath); + if (codeBundleSize == 0) { + HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] failed to get code bundle size!"); + return 0; + } + char *dataPath = installedInfo->dataPath; + uint32_t dataBundleSize = BundleUtil::GetFileFolderSize(dataPath); + return codeBundleSize + dataBundleSize; } uint8_t GtManagerService::QueryAbilityInfo(const Want *want, AbilityInfo *abilityInfo) @@ -290,10 +289,10 @@ void GtManagerService::InstallSystemBundle(const char *systemAppPath) return; } // set bundleName、label、smallIconPath、bigIconPath in bundleInstallMsg_ - bool ret = GtBundleExtractor::ExtractInstallMsg(systemAppPath, &(bundleInstallMsg_->bundleName), + uint8_t ret = GtBundleExtractor::ExtractInstallMsg(systemAppPath, &(bundleInstallMsg_->bundleName), &(bundleInstallMsg_->label), &(bundleInstallMsg_->smallIconPath), &(bundleInstallMsg_->bigIconPath)); - if (!ret) { + if (ret != 0) { // delete resource temp dir (void) BundleUtil::RemoveDir(TMP_RESOURCE_DIR); ClearSystemBundleInstallMsg(); @@ -883,19 +882,20 @@ int32_t GtManagerService::ReportInstallCallback(uint8_t errCode, uint8_t install return 0; } -int32_t GtManagerService::ReportUninstallCallback(uint8_t errCode, char *bundleName, +int32_t GtManagerService::ReportUninstallCallback(uint8_t errCode, uint8_t installState, char *bundleName, uint8_t process, InstallerCallback installerCallback) { if (installerCallback == nullptr) { return -1; } - BundleInstallMsg *bundleUninstallMsg = reinterpret_cast(AdapterMalloc(sizeof(BundleInstallMsg))); - if (bundleUninstallMsg == nullptr) { + BundleInstallMsg *bundleInstallMsg = reinterpret_cast(AdapterMalloc(sizeof(BundleInstallMsg))); + if (bundleInstallMsg == nullptr) { return -1; } - bundleUninstallMsg->bundleName = bundleName; - bundleUninstallMsg->installProcess = process; - (*installerCallback)(errCode, bundleUninstallMsg); + bundleInstallMsg->installState = static_cast(installState); + bundleInstallMsg->bundleName = bundleName; + bundleInstallMsg->installProcess = process; + (*installerCallback)(errCode, bundleInstallMsg); return 0; } -- Gitee