From 48e2bb586cc7d714116a0c6b90d60f63a389e833 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 10 Aug 2021 21:29:48 +0800 Subject: [PATCH] IssueNo:#I3XCB5 Description:add get file folder size interface Sig:appexecfwk Feature or Bugfix:Feature Binary Source:No Signed-off-by: root --- .../include/bundle_callback_utils.h | 5 ++ frameworks/bundle_lite/src/bundle_manager.cpp | 56 +++++++++++++++++++ .../bundlemgr_lite/bundle_inner_interface.h | 2 + interfaces/kits/bundle_lite/bundle_manager.h | 11 ++++ .../include/bundle_manager_service.h | 1 + .../include/bundle_ms_feature.h | 2 + .../src/bundle_manager_service.cpp | 19 +++++++ .../bundlemgr_lite/src/bundle_ms_feature.cpp | 26 +++++++++ services/bundlemgr_lite/src/bundle_util.cpp | 2 +- 9 files changed, 123 insertions(+), 1 deletion(-) 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/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/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/kits/bundle_lite/bundle_manager.h b/interfaces/kits/bundle_lite/bundle_manager.h index c55791c..93c8c3a 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 4 + * @version 4 + */ +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_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/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_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 27813a6..6fb9877 100755 --- a/services/bundlemgr_lite/src/bundle_util.cpp +++ b/services/bundlemgr_lite/src/bundle_util.cpp @@ -242,7 +242,7 @@ uint32_t BundleUtil::GetCurrentFolderSize(const char *dirPath, List* lis continue; } - if (IsFile(filePath)) { + if (!IsDir(filePath)) { if (stat(filePath, &buf) != 0 || buf.st_size <= 0) { fileSize = 0; break; -- Gitee