diff --git a/interfaces/inner_api/code_sign_utils/BUILD.gn b/interfaces/inner_api/code_sign_utils/BUILD.gn index 2388b49d18515b96f4cdb2c586a98d36b483f04d..1ec18b50206745015d872d9ae6b1f69dacf00c4b 100644 --- a/interfaces/inner_api/code_sign_utils/BUILD.gn +++ b/interfaces/inner_api/code_sign_utils/BUILD.gn @@ -27,6 +27,7 @@ ohos_shared_library("libcode_sign_utils") { branch_protector_ret = "pac_ret" sources = [ "${code_signature_root_dir}/utils/src/code_sign_block.cpp", + "${code_signature_root_dir}/utils/src/data_size_report_adapter.cpp", "${code_signature_root_dir}/utils/src/file_helper.cpp", "src/code_sign_enable_multi_task.cpp", "src/code_sign_helper.cpp", diff --git a/interfaces/inner_api/code_sign_utils/src/code_sign_utils.cpp b/interfaces/inner_api/code_sign_utils/src/code_sign_utils.cpp index a9fa7aa5814924e0794a8ac6b31cd7cede5d907e..45a318d44cf0319c232f0343b389fc974fd67865 100644 --- a/interfaces/inner_api/code_sign_utils/src/code_sign_utils.cpp +++ b/interfaces/inner_api/code_sign_utils/src/code_sign_utils.cpp @@ -40,6 +40,7 @@ #include "stat_utils.h" #include "signer_info.h" #include "rust_interface.h" +#include "data_size_report_adapter.h" namespace OHOS { namespace Security { @@ -259,6 +260,7 @@ int32_t CodeSignUtils::EnableKeyInProfile(const std::string &bundleName, const B { int ret = EnableKeyInProfileByRust(bundleName.c_str(), profileBuffer.GetBuffer(), profileBuffer.GetSize()); if (ret == CS_SUCCESS) { + ReportUserDataSize(); return ret; } LOG_ERROR("Enable key in profile failed. ret = %{public}d", ret); diff --git a/utils/include/data_size_report_adapter.h b/utils/include/data_size_report_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..67223a658e2ad57fe08753372d97af5e580d8025 --- /dev/null +++ b/utils/include/data_size_report_adapter.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DATA_SIZE_REPORT_ADAPTER_H +#define DATA_SIZE_REPORT_ADAPTER_H + +#include +#include "log.h" + +namespace OHOS { +namespace Security { +namespace CodeSign { + +void ReportUserDataSize(); + +} // namespace CodeSign +} // namespace Security +} // OHOS +#endif // DATA_SIZE_REPORT_ADAPTER_H diff --git a/utils/src/data_size_report_adapter.cpp b/utils/src/data_size_report_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c2bcbfca7d915b98c1505460983d3345dae1784 --- /dev/null +++ b/utils/src/data_size_report_adapter.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "data_size_report_adapter.h" +#include "hisysevent.h" + +#include +#include +#include +#include +#include +#include "directory_ex.h" + +namespace OHOS { +namespace Security { +namespace CodeSign { +namespace { +using namespace OHOS::HiviewDFX; + +static const std::string CODE_SIGN_NAME = "code_signature"; +static const std::string SYS_EL1_CODE_SIGN_DIR = "/data/service/el1/public/profiles"; +static const std::string USER_DATA_DIR = "/data"; +static const double UNITS = 1024.0; +} + +double GetPartitionRemainSize(const std::string& path) +{ + struct statfs stat; + if (statfs(path.c_str(), &stat) != 0) { + LOG_ERROR("Failed to get %{public}s's remaining size.", path.c_str()); + return 0; + } + + /* change B to MB */ + return (static_cast(stat.f_bfree) * static_cast(stat.f_bsize)) / (UNITS * UNITS); +} + +void ReportTask() +{ + int ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, "USER_DATA_SIZE", + HiviewDFX::HiSysEvent::EventType::STATISTIC, "COMPONENT_NAME", CODE_SIGN_NAME, "PARTITION_NAME", + USER_DATA_DIR, "REMAIN_PARTITION_SIZE", GetPartitionRemainSize(USER_DATA_DIR), + "FILE_OR_FOLDER_PATH", SYS_EL1_CODE_SIGN_DIR, "FILE_OR_FOLDER_SIZE", GetFolderSize(SYS_EL1_CODE_SIGN_DIR)); + if (ret != 0) { + LOG_ERROR("Hisysevent report data size failed!"); + } +} + +void ReportUserDataSize() +{ + std::thread task(ReportTask); + task.join(); +} +} // namespace CodeSign +} // namespace Security +} // OHOS \ No newline at end of file