From 96940e4ec0062ec061cfa07d64d6ef3469706c8b Mon Sep 17 00:00:00 2001 From: wangdengjia Date: Tue, 3 Aug 2021 16:24:41 +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 --- frameworks/bundle_lite/src/bundle_manager.cpp | 36 +++++++++ .../bundlemgr_lite/bundle_inner_interface.h | 2 + interfaces/kits/bundle_lite/bundle_manager.h | 2 + .../include/bundle_manager_service.h | 1 + .../include/bundle_ms_feature.h | 2 + services/bundlemgr_lite/include/bundle_util.h | 2 + .../src/bundle_manager_service.cpp | 28 +++++++ .../bundlemgr_lite/src/bundle_ms_feature.cpp | 28 +++++++ services/bundlemgr_lite/src/bundle_util.cpp | 74 +++++++++++++++++++ 9 files changed, 175 insertions(+) diff --git a/frameworks/bundle_lite/src/bundle_manager.cpp b/frameworks/bundle_lite/src/bundle_manager.cpp index 6386271..6a0be53 100755 --- a/frameworks/bundle_lite/src/bundle_manager.cpp +++ b/frameworks/bundle_lite/src/bundle_manager.cpp @@ -605,6 +605,42 @@ 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); + // IpcIoPushInt32(&ipcIo, flags); + if (!IpcIoAvailable(&ipcIo)) { + HILOG_ERROR(HILOG_MODULE_APP, "BundleManager GetBundleSize ipc failed"); + return 0; + } + uint32_t bundleSize = 0; + int32_t ret = bmsClient->Invoke(bmsClient, GET_BUNDLE_SIZE, &ipcIo, &bundleSize, Notify); + if (ret != OHOS_SUCCESS) { + HILOG_ERROR(HILOG_MODULE_APP, "BundleManager GetBundleInfo invoke failed: %{public}d", ret); + return 0; + } + 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..c67140e 100755 --- a/interfaces/innerkits/bundlemgr_lite/bundle_inner_interface.h +++ b/interfaces/innerkits/bundlemgr_lite/bundle_inner_interface.h @@ -41,6 +41,7 @@ enum BmsCmd { GET_BUNDLE_INFOS_BY_METADATA, CHECK_SYS_CAP, GET_SYS_CAP, + GET_BUNDLE_SIZE, BMS_INNER_BEGIN, INSTALL = BMS_INNER_BEGIN, // bms install application UNINSTALL, @@ -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..4d02ac0 100755 --- a/interfaces/kits/bundle_lite/bundle_manager.h +++ b/interfaces/kits/bundle_lite/bundle_manager.h @@ -246,6 +246,8 @@ SystemCapability *GetSystemAvailableCapabilities(); * @version 4 */ void FreeSystemAvailableCapabilitiesInfo(SystemCapability *sysCap); + +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/include/bundle_util.h b/services/bundlemgr_lite/include/bundle_util.h index 890c763..d7547dd 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 GetCurrentFileFolderSize(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/src/bundle_manager_service.cpp b/services/bundlemgr_lite/src/bundle_manager_service.cpp index 169fb03..e3634cf 100755 --- a/services/bundlemgr_lite/src/bundle_manager_service.cpp +++ b/services/bundlemgr_lite/src/bundle_manager_service.cpp @@ -302,6 +302,9 @@ void ManagerService::InstallThirdBundle(const char *path, const SvcIdentity &svc InstallParam installParam = {.installLocation = installLocation, .keepData = false}; uint8_t bResult = installer_->Install(path, installParam); HILOG_DEBUG(HILOG_MODULE_APP, "BundleMS InstallThirdBundle Install : %{public}d\n", bResult); + uint32_t bundleSize = GetBundleSize(bundleName); + HILOG_ERROR("wdj", "bundle name is %{public}s\n", bundleName); + HILOG_ERROR("wdj", "bundle size is %{public}d\n", bundleSize); InnerSelfTransact(INSTALL_CALLBACK, bResult, svc); InnerTransact(INSTALL_CALLBACK, bResult, bundleName); } @@ -625,6 +628,31 @@ uint8_t ManagerService::GetBundleInfos(int32_t flags, BundleInfo **bundleInfos, return bundleMap_->GetBundleInfos(flags, bundleInfos, len); } +uint32_t ManagerService::GetBundleSize(const char *bundleName) +{ + HILOG_ERROR("wdj", "get bundle size1\n"); + if (bundleName == nullptr) { + return 0; + } + HILOG_ERROR("wdj", "get bundle size2\n"); + BundleInfo *installedInfo = bundleMap_->Get(bundleName); + if (installedInfo == nullptr) { + return 0; + } + HILOG_ERROR("wdj", "get bundle size3\n"); + char *codePath = installedInfo->codePath; + uint32_t codeBundleSize = BundleUtil::GetFileFolderSize(codePath); + if (codeBundleSize == 0) { + return 0; + } + HILOG_ERROR("wdj", "get bundle size4 codePath is %{public}s\n", codePath); + HILOG_ERROR("wdj", "get bundle size4 codeBundleSize is %{public}d\n", codeBundleSize); + char *dataPath = installedInfo->dataPath; + uint32_t dataBundleSize = BundleUtil::GetFileFolderSize(dataPath); + HILOG_ERROR("wdj", "get bundle size5 dataPath is %{public}s\n", dataPath); + HILOG_ERROR("wdj", "get bundle size5 dataBundleSize is %{public}d\n", 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..4c9cfbd 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 }; @@ -55,6 +56,7 @@ BundleInvokeType BundleMsFeature::BundleMsInvokeFuc[BMS_INNER_BEGIN] { HandleGetBundleInfos, HasSystemCapability, GetSystemAvailableCapabilities, + GetInnerBundleSize, }; IUnknown *GetBmsFeatureApi(Feature *feature) @@ -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) { + HILOG_ERROR(HILOG_MODULE_APP, "BundleMS GET_BUNDLE_SIZE error!\n"); + return ERR_APPEXECFWK_OBJECT_NULL; + } + IpcIoPushUint32(reply, bundleSize); + return OHOS_SUCCESS; +} + uint8_t BundleMsFeature::HandleGetBundleInfos(const uint8_t funcId, IpcIo *req, IpcIo *reply) { if ((req == nullptr) || (reply == nullptr)) { @@ -367,6 +388,8 @@ int32 BundleMsFeature::Invoke(IServerProxy *iProxy, int funcId, void *origin, Ip ret = BundleMsInvokeFuc[funcId](funcId, req, reply); } else if (funcId >= CHECK_SYS_CAP && funcId <= GET_SYS_CAP) { ret = BundleMsInvokeFuc[funcId](funcId, req, reply); + } else if (funcId == GET_BUNDLE_SIZE) { + ret = BundleMsInvokeFuc[funcId](funcId, req, reply); } else { ret = ERR_APPEXECFWK_COMMAND_ERROR; } @@ -427,6 +450,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..27813a6 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 (IsFile(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) { -- Gitee