diff --git a/common/utils/include/bundle_file_util.h b/common/utils/include/bundle_file_util.h index 468bbde7537d861eef63cb4300c65bf4f116cb76..86a0dee9f1a8da8ebc9c2e165b2cea66a85b0886 100644 --- a/common/utils/include/bundle_file_util.h +++ b/common/utils/include/bundle_file_util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -32,6 +32,10 @@ public: static bool DeleteDir(const std::string &path); static bool IsExistFile(const std::string &filePath); static bool IsExistDir(const std::string &dirPath); + static uint64_t GetFileSize(const std::string &filePath); + static uint64_t GetFreeSpaceInBytes(const std::string &partitionName); + static uint64_t GetFolderSizeInBytes(const std::string &pathName); + static bool IsReportDataPartitionUsageEvent(const std::string &path); }; } // AppExecFwk } // OHOS diff --git a/common/utils/src/bundle_file_util.cpp b/common/utils/src/bundle_file_util.cpp index 20bcf83085b47ad2e8e6f784039077ffc12587ee..34d29429e35845dda5dc177b3fa91ad9cf22d8ee 100644 --- a/common/utils/src/bundle_file_util.cpp +++ b/common/utils/src/bundle_file_util.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -42,6 +42,7 @@ constexpr int64_t MAX_HAP_SIZE = ONE_GB * 4; // 4GB constexpr int PATH_MAX_SIZE = 256; const char FILE_SEPARATOR_CHAR = '/'; constexpr uint8_t MAX_HAP_NUMBER = 128; +constexpr double LOW_PARTITION_SPACE_THRESHOLD = 0.1; } // namespace namespace OHOS { @@ -257,5 +258,65 @@ bool BundleFileUtil::IsExistDir(const std::string &dirPath) return S_ISDIR(result.st_mode); } + +uint64_t BundleFileUtil::GetFileSize(const std::string &filePath) +{ + if (filePath.empty()) { + APP_LOGE("input file path is empty."); + return UINT64_MAX; + } + + struct stat fileInfo = { 0 }; + if (stat(filePath.c_str(), &fileInfo) != 0) { + APP_LOGE("call stat error: %{public}d %{public}s", errno, filePath.c_str()); + return UINT64_MAX; + } + + if (!S_ISREG(fileInfo.st_mode)) { + APP_LOGE("path is not a regular file: %{public}s", filePath.c_str()); + return UINT64_MAX; + } + + return fileInfo.st_size; +} + +uint64_t BundleFileUtil::GetFreeSpaceInBytes(const std::string &partitionName) +{ + struct statvfs stst; + if (statvfs(partitionName.c_str(), &stst) != 0) { + APP_LOGE("failed to get free space for partition %{public}s", partitionName.c_str()); + return UINT64_MAX; + } + + return static_cast(stst.f_bavail) * stst.f_frsize; +} + +uint64_t BundleFileUtil::GetFolderSizeInBytes(const std::string &pathName) +{ + struct statvfs stst; + if (statvfs(pathName.c_str(), &stst) != 0) { + APP_LOGE("failed to get space info for partition %{public}s", + pathName.c_str()); + return UINT64_MAX; + } + + uint64_t usedBlocks = static_cast(stst.f_blocks) - stst.f_bfree; + return usedBlocks * stst.f_frsize; +} + +bool BundleFileUtil::IsReportDataPartitionUsageEvent(const std::string &path) +{ + int64_t remainPartitionSize = BundleFileUtil::GetFreeSpaceInBytes(path); + int64_t fileOrFolderSize = BundleFileUtil::GetFolderSizeInBytes(path); + if (fileOrFolderSize == 0) { + return false; + } + double ratio = static_cast(remainPartitionSize) / fileOrFolderSize; + if (ratio < LOW_PARTITION_SPACE_THRESHOLD) { + APP_LOGW("start hisysevent and partitionsize is smaller then 10%%"); + return true; + } + return false; +} } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/hisysevent.yaml b/hisysevent.yaml index 03ef0c2025015e57b9235ad46d672c19cd9bb9b9..005ebb4fb47b42168de90230b978e22970d94d0c 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -1,4 +1,4 @@ -# Copyright (c) 2022-2024 Huawei Device Co., Ltd. +# Copyright (c) 2022-2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -273,4 +273,12 @@ APP_CONTROL_RULE: OPERATION_TYPE: {type: INT32, desc: operation type} ACTION_TYPE: {type: INT32, desc: action type} RULE: {type: STRING, desc: rule} - APP_INDEX: {type: INT32, desc: app index} \ No newline at end of file + APP_INDEX: {type: INT32, desc: app index} + +USER_DATA_SIZE: + __BASE: {type: STATISTIC, level: MINOR , desc: data partition management} + COMPONENT_NAME: {type: STRING, desc: name of component} + PARTITION_NAME: {type: STRING, desc: name of partition} + REMAIN_PARTITION_SIZE: {type: UINT64, desc: Remaining size of the partition} + FILE_OR_FOLDER_PATH: {type: STRING, arrsize: 100, desc: File or folder path} + FILE_OR_FOLDER_SIZE: {type: UINT64, arrsize: 100, desc: File or folder size} \ No newline at end of file diff --git a/services/bundlemgr/BUILD.gn b/services/bundlemgr/BUILD.gn index 06bfcd3977cd10a7b73553e08883604f0f76ee5b..d269c2935843df21afffbcb69b891715b5c37aee 100644 --- a/services/bundlemgr/BUILD.gn +++ b/services/bundlemgr/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2024 Huawei Device Co., Ltd. +# Copyright (c) 2021-2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -428,6 +428,10 @@ ohos_shared_library("libbms") { defines += [ "HISYSEVENT_ENABLE" ] } + if (current_cpu == "arm64") { + defines += [ "USE_EXTENSION_DATA" ] + } + if (use_pre_bundle_profile) { defines += [ "USE_PRE_BUNDLE_PROFILE" ] } diff --git a/services/bundlemgr/include/bundle_installer_manager.h b/services/bundlemgr/include/bundle_installer_manager.h index 04fd35e7810d555586b755e4941395ab12ae4c69..e23d3e3c2388ba283a3326badb6de0d7adf03992 100644 --- a/services/bundlemgr/include/bundle_installer_manager.h +++ b/services/bundlemgr/include/bundle_installer_manager.h @@ -117,6 +117,10 @@ private: void DelayStopThreadPool(); + static void ReportReportDataPartitionUsageEvent(); + + static void GetDataPartitionUsageDirs(std::vector &dataDirs); + DISALLOW_COPY_AND_MOVE(BundleInstallerManager); int32_t threadNum_ = std::thread::hardware_concurrency(); diff --git a/services/bundlemgr/include/event_report.h b/services/bundlemgr/include/event_report.h index 1d1151c74cc38e7f6656b8574c72d2c496650bca..aa9b01270d86820b57b927ea8b00272b49a1953b 100644 --- a/services/bundlemgr/include/event_report.h +++ b/services/bundlemgr/include/event_report.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -51,7 +51,8 @@ enum class BMSEventType : uint8_t { FREE_INSTALL_EVENT, BMS_DISK_SPACE, APP_CONTROL_RULE, - DB_ERROR + DB_ERROR, + DATA_PARTITION_USAGE_EVENT }; enum class BundleEventType : uint8_t { @@ -155,6 +156,7 @@ struct EventInfo { // only used in fault event ErrCode errCode = ERR_OK; int64_t costTimeSeconds = 0; + uint64_t partitionSize = 0; int64_t timeStamp = 0; int64_t freeSize = 0; int32_t errorCode = 0; @@ -183,6 +185,7 @@ struct EventInfo { std::vector hashValue; std::vector totalBundleNames; std::vector appIds; + std::vector fileSize; void Reset() { @@ -205,6 +208,7 @@ struct EventInfo { callingAppId.clear(); callingBundleName.clear(); filePath.clear(); + fileSize.clear(); hashValue.clear(); fingerprint.clear(); hideDesktopIcon = false; @@ -216,6 +220,7 @@ struct EventInfo { compileResult = false; failureReason.clear(); costTimeSeconds = 0; + partitionSize = 0; continueType.clear(); sceneId = 0; processName.clear(); diff --git a/services/bundlemgr/include/inner_event_report.h b/services/bundlemgr/include/inner_event_report.h index 7f22bce8ee36311cd6803e4a72a917f74d4110a0..1cbf1782f2b146b9fa324957f15e4a529b67db02 100644 --- a/services/bundlemgr/include/inner_event_report.h +++ b/services/bundlemgr/include/inner_event_report.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -61,6 +61,7 @@ private: static void InnerSendBmsDiskSpaceEvent(const EventInfo& eventInfo); static void InnerSendAppControlRule(const EventInfo& eventInfo); static void InnerSendDbErrorEvent(const EventInfo& eventInfo); + static void InnerSendDataPartitionUsageEvent(const EventInfo& eventInfo); template static void InnerEventWrite(const std::string &eventName, diff --git a/services/bundlemgr/src/bundle_data_storage_rdb.cpp b/services/bundlemgr/src/bundle_data_storage_rdb.cpp index be6564eb5ae545c6222f7f772618bcbfed35c30a..dfda94055af0bee12033f381438e667ca3dc7cb9 100644 --- a/services/bundlemgr/src/bundle_data_storage_rdb.cpp +++ b/services/bundlemgr/src/bundle_data_storage_rdb.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,11 +16,14 @@ #include "bundle_data_storage_rdb.h" #include "bundle_exception_handler.h" +#include "bundle_file_util.h" +#include "event_report.h" namespace OHOS { namespace AppExecFwk { namespace { constexpr const char* BUNDLE_RDB_TABLE_NAME = "installed_bundle"; +constexpr const char* PARTITION_NAME = "/data"; } BundleDataStorageRdb::BundleDataStorageRdb() { @@ -104,9 +107,37 @@ void BundleDataStorageRdb::TransformStrToInfo( } } +void BundleDataStorageRdb::ReportReportRdbPartitionUsageEvent() +{ + if (!BundleFileUtil::IsReportDataPartitionUsageEvent(PARTITION_NAME)) { + APP_LOGE("fialed obtained the timing for HisEvent"); + return; + } + const std::vector paths = { + "/data/service/el1/public/bms/bundle_manager_service/bmsdb.db" + }; + EventInfo eventInfo; + eventInfo.partitionSize = BundleFileUtil::GetFreeSpaceInBytes(PARTITION_NAME); + if (eventInfo.partitionSize == UINT64_MAX) { + APP_LOGE("fialed get free space for Hisevent condition"); + return; + } + for (auto item : paths) { + auto useSize = BundleFileUtil::GetFileSize(item); + if (useSize == UINT64_MAX) { + APP_LOGE("fialed get the size of file or folder"); + continue; + } + eventInfo.filePath.push_back(item); + eventInfo.fileSize.push_back(useSize); + } + EventReport::SendSystemEvent(BMSEventType::DATA_PARTITION_USAGE_EVENT, eventInfo); +} + void BundleDataStorageRdb::UpdateDataBase(std::map &infos) { APP_LOGD("Begin to update database"); + ReportReportRdbPartitionUsageEvent(); if (rdbDataManager_ == nullptr) { APP_LOGE("rdbDataManager is null"); return; @@ -122,6 +153,7 @@ void BundleDataStorageRdb::UpdateDataBase(std::map bool BundleDataStorageRdb::SaveStorageBundleInfo(const InnerBundleInfo &innerBundleInfo) { + ReportReportRdbPartitionUsageEvent(); if (rdbDataManager_ == nullptr) { APP_LOGE("rdbDataManager is null"); return false; diff --git a/services/bundlemgr/src/bundle_installer_manager.cpp b/services/bundlemgr/src/bundle_installer_manager.cpp index 824132091f29eddd2752a2e1847ef911d640c695..99cbdb6dc87f88af88b74ec206fb68c2916a1dad 100644 --- a/services/bundlemgr/src/bundle_installer_manager.cpp +++ b/services/bundlemgr/src/bundle_installer_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,6 +22,7 @@ #include "ipc_skeleton.h" #include "parameters.h" #include "xcollie_helper.h" +#include "bundle_file_util.h" namespace OHOS { namespace AppExecFwk { @@ -30,11 +31,35 @@ constexpr const char* INSTALL_TASK = "Install_Task"; constexpr const char* UNINSTALL_TASK = "Uninstall_Task"; constexpr const char* RECOVER_TASK = "Recover_Task"; constexpr const char* THREAD_POOL_NAME = "InstallerThreadPool"; +constexpr const char* RETAIL_MODE_KEY = "const.dfx.enable_retail"; +constexpr const char* PARTITION_NAME = "/data"; +constexpr const char* DIR_DATA_SERVICE = "/data/service"; +constexpr const char* DIR_ELS[] = {"/el1/", "/el2/", "/el3/", "/el4/", "/el5/"}; +constexpr const char* DIR_BACKUP = "/backup/bundles"; +constexpr const char* DIR_SHARE = "/share"; +constexpr const char* DIR_ACCOUNT_DATA = "/hmdfs/account/data"; +constexpr const char* DIR_NON_ACCOUNT = "/hmdfs/non_account/data"; +constexpr const char* DIR_CLOUD_DATA = "/hmdfs/cloud/data"; +constexpr const char* DIR_LOCAL = "/data/local/shader_cache/local"; +constexpr const char* DIR_DATA_APP = "/data/app"; +constexpr const char* DIR_APP_BUNDLE = "/bundle/public"; +constexpr const char* DIR_BASE = "/base"; +constexpr const char* DIR_DATA_BASE = "/database"; +constexpr const char* DIR_ARK_PRO = "/ark-profile"; +constexpr const char* DIR_SHADER_CACHE = "/shader_cache"; +constexpr const char* DIR_LOG = "/log"; +constexpr const char* DIR_SHARE_FILES = "/sharefiles"; +constexpr const char* DIR_GROUP = "/group"; +constexpr const char* DIR_APP_LOCAL = "/app/local"; +constexpr const char* DIR_CLOUD = "/cloud"; +constexpr const char* DIR_CLOUD_COMMON = "/cloud/common"; +constexpr const char* DIR_DATA_LOCAL_ARK = "/data/local/ark-cache"; constexpr unsigned int TIME_OUT_SECONDS = 60 * 25; constexpr int8_t MAX_TASK_NUMBER = 10; constexpr int8_t RETAIL_MODE_THREAD_NUMBER = 1; constexpr int8_t DELAY_INTERVAL_SECONDS = 60; static std::atomic g_taskCounter = 0; +constexpr int32_t DATA_USER_ID = 0; } BundleInstallerManager::BundleInstallerManager() @@ -51,6 +76,77 @@ BundleInstallerManager::~BundleInstallerManager() LOG_NOFUNC_I(BMS_TAG_INSTALLER, "destroy bundle installer manager instance"); } +void BundleInstallerManager::GetDataPartitionUsageDirs(std::vector &dataDirs) +{ + std::set userIds; + userIds.insert(DATA_USER_ID); + AccountHelper::QueryAllCreatedOsAccounts(userIds); + dataDirs.push_back(DIR_LOCAL); + dataDirs.push_back(DIR_DATA_LOCAL_ARK); + for (auto userId : userIds) { + for (auto eli : DIR_ELS) { + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_BASE)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_DATA_BASE)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_GROUP)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(DIR_APP_BUNDLE)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_LOG)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_SHARE_FILES)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_BACKUP)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_ACCOUNT_DATA)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_ACCOUNT_DATA)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_NON_ACCOUNT)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_SHARE)); + dataDirs.push_back( + std::string(DIR_APP_LOCAL).append(DIR_SHADER_CACHE).append(DIR_CLOUD)); + dataDirs.push_back( + std::string(DIR_APP_LOCAL).append(DIR_SHADER_CACHE).append(DIR_CLOUD_COMMON)); + dataDirs.push_back( + std::string(DIR_APP_LOCAL).append(DIR_ARK_PRO).append(std::to_string(userId))); + } + } + APP_LOGD("get data dirs count is %{public}zu", dataDirs.size()); +} + +void BundleInstallerManager::ReportReportDataPartitionUsageEvent() +{ + if (!BundleFileUtil::IsReportDataPartitionUsageEvent(PARTITION_NAME)) { + APP_LOGE("get the timming of hisysevent fialed"); + return; + } + + EventInfo eventInfo; + eventInfo.partitionSize = BundleFileUtil::GetFreeSpaceInBytes(PARTITION_NAME); + if (eventInfo.partitionSize == UINT64_MAX) { + APP_LOGE("get the RemainPartitionSize of hisysevent fialed"); + return; + } + std::vector dataDirs; + GetDataPartitionUsageDirs(dataDirs); + for (auto &item : dataDirs) { + auto useSize = BundleFileUtil::GetFolderSizeInBytes(item); + if (useSize == UINT64_MAX) { + APP_LOGE("get the FileOrFolderSize of hisysevent fialed"); + continue; + } + + eventInfo.filePath.push_back(item); + eventInfo.fileSize.push_back(useSize); + } + + EventReport::SendSystemEvent(BMSEventType::DATA_PARTITION_USAGE_EVENT, eventInfo); +} + void BundleInstallerManager::CreateInstallTask( const std::string &bundleFilePath, const InstallParam &installParam, const sptr &statusReceiver) { @@ -62,6 +158,7 @@ void BundleInstallerManager::CreateInstallTask( auto task = [installer, bundleFilePath, installParam] { BundleMemoryGuard memoryGuard; int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr); + ReportReportDataPartitionUsageEvent(); installer->Install(bundleFilePath, installParam); g_taskCounter--; XCollieHelper::CancelTimer(timerId); @@ -98,6 +195,7 @@ void BundleInstallerManager::CreateInstallTask(const std::vector &b auto task = [installer, bundleFilePaths, installParam] { BundleMemoryGuard memoryGuard; int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr); + ReportReportDataPartitionUsageEvent(); installer->Install(bundleFilePaths, installParam); g_taskCounter--; XCollieHelper::CancelTimer(timerId); diff --git a/services/bundlemgr/src/inner_event_report.cpp b/services/bundlemgr/src/inner_event_report.cpp index ffa9b65d4ba75d3bd70158e87694cd367d5540c7..6432fe726f8d2dfa07514e13ada4b3ca095d8856 100644 --- a/services/bundlemgr/src/inner_event_report.cpp +++ b/services/bundlemgr/src/inner_event_report.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -86,6 +86,12 @@ const char* EVENT_PARAM_ACTION_TYPE = "ACTION_TYPE"; const char* EVENT_PARAM_RULE = "ACTION_RULE"; const char* EVENT_PARAM_APP_INDEX = "APP_INDEX"; const char* EVENT_PARAM_IS_PATCH = "IS_PATCH"; +const char* FILE_OR_FOLDER_PATH = "FILE_OR_FOLDER_PATH"; +const char* FILE_OR_FOLDER_SIZE = "FILE_OR_FOLDER_SIZE"; +const char* COMPONENT_NAME_KEY = "COMPONENT_NAME"; +const char* PARTITION_NAME_KEY = "PARTITION_NAME"; +const char* REMAIN_PARTITION_SIZE_KEY = "REMAIN_PARTITION_SIZE"; +const char* USER_DATA_SIZE = "USER_DATA_SIZE"; const char* FREE_INSTALL_TYPE = "FreeInstall"; const char* PRE_BUNDLE_INSTALL_TYPE = "PreBundleInstall"; @@ -123,6 +129,8 @@ const char* OPERATION_TYPE = "operationType"; const char* DB_NAME = "dbName"; const char* ERROR_CODE = "errorCode"; const char* REBUILD_TYPE = "rebuildType"; +const char* COMPONENT_NAME = "hisevent"; +const char* PARTITION_NAME = "/data"; const InstallScene INSTALL_SCENE_STR_MAP_KEY[] = { InstallScene::NORMAL, @@ -291,6 +299,10 @@ std::unordered_map [](const EventInfo& eventInfo) { InnerSendAppControlRule(eventInfo); } }, + { BMSEventType::DATA_PARTITION_USAGE_EVENT, + [](const EventInfo& eventInfo) { + InnerSendDataPartitionUsageEvent(eventInfo); + } }, { BMSEventType::DB_ERROR, [](const EventInfo& eventInfo) { InnerSendDbErrorEvent(eventInfo); @@ -663,6 +675,23 @@ void InnerEventReport::InnerSendDbErrorEvent(const EventInfo& eventInfo) ERROR_CODE, eventInfo.errorCode); } +void InnerEventReport::InnerSendDataPartitionUsageEvent(const EventInfo& eventInfo) +{ + HiSysEventWrite( +#ifdef USE_EXTENSION_DATA + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, +#else + OHOS::HiviewDFX::HiSysEvent::Domain::BUNDLEMANAGER_UE, +#endif + USER_DATA_SIZE, + HiviewDFX::HiSysEvent::EventType::STATISTIC, + COMPONENT_NAME_KEY, COMPONENT_NAME, + PARTITION_NAME_KEY, PARTITION_NAME, + REMAIN_PARTITION_SIZE_KEY, eventInfo.partitionSize, + FILE_OR_FOLDER_PATH, eventInfo.filePath, + FILE_OR_FOLDER_SIZE, eventInfo.fileSize); +} + template void InnerEventReport::InnerEventWrite( const std::string &eventName, diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_manager_test/bms_bundle_installer_manager_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_manager_test/bms_bundle_installer_manager_test.cpp index 8a8d2a122ff4c9ecd6b76bd3835c9b9b59fdc2c5..b6e6f6ac5a3b77a86959029715a09c0fd39f23f0 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_manager_test/bms_bundle_installer_manager_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_manager_test/bms_bundle_installer_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include "mock_status_receiver.h" #define private public @@ -25,6 +27,7 @@ #include "bundle_mgr_service.h" #include "event_runner.h" #include "inner_event.h" +#include "bundle_file_util.h" #undef public #undef protected using namespace testing::ext; @@ -206,5 +209,38 @@ HWTEST_F(BundleInstallerManagerTest, BundleInstallerManagerTest_020, TestSize.Le ErrCode result = receiver->GetResultCode(); EXPECT_NE(ERR_OK, result); } + +/** + * @tc.number: BundleInstallerManagerTest_021 + * @tc.name: test ReportReportDataPartitionUsageEvent + * @tc.desc: Verify function ReportReportDataPartitionUsageEvent is called, + * receiver->GetResultCode() return value is ERR_OK + */ +HWTEST_F(BundleInstallerManagerTest, BundleInstallerManagerTest_021, TestSize.Level1) +{ + const std::string partitionName = "DataErrorPath"; + BundleInstallerManager::ReportReportDataPartitionUsageEvent(); + bool result = BundleFileUtil::IsReportDataPartitionUsageEvent(partitionName); + EXPECT_EQ(result, false); + uint64_t bytes = BundleFileUtil::GetFreeSpaceInBytes(partitionName); + EXPECT_EQ(bytes, UINT64_MAX); + const std::string path = "datatest"; + uint64_t size = BundleFileUtil::GetFolderSizeInBytes(path); + EXPECT_EQ(size, UINT64_MAX); +} + +/** + * @tc.number: BundleInstallerManagerTest_022 + * @tc.name: test GetDataPartitionUsageDirs + * @tc.desc: Verify function GetDataPartitionUsageDirs is called, + * receiver->GetResultCode() return value is ERR_OK + */ +HWTEST_F(BundleInstallerManagerTest, BundleInstallerManagerTest_022, TestSize.Level1) +{ + const std::string partitionName = "DataErrorPath"; + std::vector dataDirs; + BundleInstallerManager::GetDataPartitionUsageDirs(dataDirs); + EXPECT_FALSE(dataDirs.empty()); +} } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/services/bundlemgr/test/unittest/bms_hap_module_info_test/bms_hap_module_info_test.cpp b/services/bundlemgr/test/unittest/bms_hap_module_info_test/bms_hap_module_info_test.cpp index d210bd12ac8b6c258536e70bcad139c8319eb673..69b7116e35eef6605a7ca7636ebdffd78dc92105 100644 --- a/services/bundlemgr/test/unittest/bms_hap_module_info_test/bms_hap_module_info_test.cpp +++ b/services/bundlemgr/test/unittest/bms_hap_module_info_test/bms_hap_module_info_test.cpp @@ -14,6 +14,8 @@ */ #define private public #define protected public +#include +#include #include #include #include @@ -87,6 +89,8 @@ const std::string BUNDLE_NAME2 = "testBundleName"; const std::string MODULE_NAME2 = "testModuleName"; const std::string ABILITY_NAME = "testAbilityName"; const char* DEVICE_ID = "deviceId"; +const std::string FILEPATH = "/data/test/bundle/test.hap"; +const std::string PARTITION_NAME = "/data"; void to_json(nlohmann::json &jsonObject, const PreloadItem &preloadItem); void from_json(const nlohmann::json &jsonObject, PreloadItem &preloadItem); @@ -756,5 +760,68 @@ HWTEST_F(BmsHapModuleInfoTest, DeleteDir_0200, Function | SmallTest | Level0) auto ret1 = BundleFileUtil::DeleteDir(dirPath); EXPECT_TRUE(ret1); } + +/** + * Feature: BmsHapModuleInfoTest + * Function: GetFileSize + * SubFunction: NA + * FunctionPoints: FileSize + * EnvConditions: NA + */ +HWTEST_F(BmsHapModuleInfoTest, GetFileSize_001, TestSize.Level1) +{ + uint64_t fileSize = BundleFileUtil::GetFileSize(FILEPATH); + ASSERT_NE(fileSize, UINT64_MAX); + struct stat fileInfo = { 0 }; + uint64_t fileInfoSize = stat(FILEPATH.c_str(), &fileInfo); + EXPECT_EQ(fileSize, fileInfoSize); + uint64_t fileModeSize = (S_ISREG(fileInfo.st_mode)); + ASSERT_NE(fileModeSize, UINT64_MAX); + EXPECT_EQ(fileSize, fileInfo.st_size); +} + +/** + * Feature: BmsHapModuleInfoTest + * Function: GetFreeSpaceInBytes + * SubFunction: NA + * FunctionPoints: FreeSpaceInBytes + * EnvConditions: NA + */ +HWTEST_F(BmsHapModuleInfoTest, GetFreeSpaceInBytes_002, TestSize.Level1) +{ + struct statvfs stat; + uint64_t freeSize = statvfs(PARTITION_NAME.c_str(), &stat); + ASSERT_NE(freeSize, UINT64_MAX); + uint64_t freeSpaceInBytes = BundleFileUtil::GetFreeSpaceInBytes(PARTITION_NAME); + EXPECT_EQ(freeSpaceInBytes, static_cast(stat.f_bavail) * stat.f_frsize); +} + +/** + * Feature: BmsHapModuleInfoTest + * Function: GetFolderSizeInBytes + * SubFunction: NA + * FunctionPoints: FolderSizeInBytes + * EnvConditions: NA + */ +HWTEST_F(BmsHapModuleInfoTest, GetFolderSizeInBytes_003, TestSize.Level1) +{ + struct statvfs stat; + uint64_t folderSize = statvfs(PARTITION_NAME.c_str(), &stat); + ASSERT_NE(folderSize, UINT64_MAX); + uint64_t folderSpaceInBytes = BundleFileUtil::GetFolderSizeInBytes(PARTITION_NAME); + EXPECT_EQ(folderSpaceInBytes, (static_cast(stat.f_blocks) - stat.f_bfree) * stat.f_frsize); +} + +/** + * Feature: BmsHapModuleInfoTest + * Function: IsReportDataPartitionUsageEvent + * SubFunction: NA + * FunctionPoints: ReportDataPartitionUsageEvent + * EnvConditions: NA + */ +HWTEST_F(BmsHapModuleInfoTest, IsReportDataPartitionUsageEvent_005, TestSize.Level1) +{ + EXPECT_FALSE(BundleFileUtil::IsReportDataPartitionUsageEvent(FILEPATH)); +} } } \ No newline at end of file diff --git a/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp b/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp index 694d47aa282cbfe834919be4226f91968dfc36e5..f81cc3ae922bd05f092261a6ac4d7df12d9c8a7f 100755 --- a/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp +++ b/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,6 +15,8 @@ #define private public #include +#include +#include #include "bundle_constants.h" #include "bundle_data_storage_rdb.h" @@ -25,6 +27,7 @@ #include "first_install_data_mgr_storage_rdb.h" #include "preinstall_data_storage_rdb.h" #include "rdb_data_manager.h" +#include "bundle_file_util.h" using namespace testing::ext; using namespace OHOS::AppExecFwk; @@ -46,6 +49,7 @@ const uint32_t TEST_VERSION = 1; const std::string TEST_BUNDLE_NAME_TWO = "com.test.rdbtwo"; const std::string TEST_NAME_TWO = "NameTwo"; const uint32_t TEST_VERSION_TWO = 2; +constexpr const char* PARTITION_NAME = "/data"; #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP const int32_t TEST_USERID = 500; const std::string TEST_DEFAULT_APP_TYPE = "IMAGE"; @@ -692,6 +696,25 @@ HWTEST_F(BmsRdbDataManagerTest, RdbDataManager_1200, Function | SmallTest | Leve ASSERT_NE(rdbStore, nullptr); } +/** + * @tc.number: RdbDataManager_1300 + * @tc.name: test ReportReportRdbPartitionUsageEvent + * @tc.desc: 1.test ReportReportRdbPartitionUsageEvent + */ +HWTEST_F(BmsRdbDataManagerTest, RdbDataManager_1300, Function | SmallTest | Level1) +{ + std::unique_ptr dataStorage = std::make_unique(); + dataStorage->ReportReportRdbPartitionUsageEvent(); + bool result = BundleFileUtil::IsReportDataPartitionUsageEvent(PARTITION_NAME); + EXPECT_EQ(result, false); + uint64_t bytes = BundleFileUtil::GetFreeSpaceInBytes(PARTITION_NAME); + struct statvfs stat; + EXPECT_EQ (bytes, static_cast(stat.f_bavail) * stat.f_frsize); + uint64_t size = BundleFileUtil::GetFileSize(PARTITION_NAME); + struct stat fileInfo = { 0 }; + EXPECT_EQ(size, fileInfo.st_size); +} + #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP /** * @tc.number: DefaultAppRdb_0100