From b8300f2dc0b1b2970cab402ad086e3fd46e6f1dc Mon Sep 17 00:00:00 2001 From: zhenghui Date: Wed, 4 Jun 2025 19:55:06 +0800 Subject: [PATCH] =?UTF-8?q?data=E5=88=86=E5=8C=BA=E6=89=93=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhenghui --- services/common/BUILD.gn | 3 + .../common/utils/data_size_report_adapter.cpp | 81 +++++++++++++++++++ .../common/utils/data_size_report_adapter.h | 31 +++++++ services/sandbox_manager/BUILD.gn | 1 + .../cpp/src/service/policy_info_manager.cpp | 3 +- services/sandbox_manager/test/BUILD.gn | 3 + .../sandbox_manager_service_fuzz.gni | 3 + 7 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 services/common/utils/data_size_report_adapter.cpp create mode 100644 services/common/utils/data_size_report_adapter.h diff --git a/services/common/BUILD.gn b/services/common/BUILD.gn index 6a4dea0..c5e4c25 100644 --- a/services/common/BUILD.gn +++ b/services/common/BUILD.gn @@ -29,11 +29,13 @@ ohos_shared_library("sandbox_manager_service_common") { "${sandbox_manager_path}/frameworks/common/include", "${sandbox_manager_path}/interfaces/inner_api/sandbox_manager/include", "database/include", + "utils", ] sources = [ "database/src/generic_values.cpp", "database/src/variant_value.cpp", + "utils/data_size_report_adapter.cpp", ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -42,6 +44,7 @@ ohos_shared_library("sandbox_manager_service_common") { external_deps = [ "c_utils:utils", "hilog:libhilog", + "hisysevent:libhisysevent", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/common/utils/data_size_report_adapter.cpp b/services/common/utils/data_size_report_adapter.cpp new file mode 100644 index 0000000..04a5ed3 --- /dev/null +++ b/services/common/utils/data_size_report_adapter.cpp @@ -0,0 +1,81 @@ +/* + * 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 AccessControl { +namespace SandboxManager { +namespace { +using namespace OHOS::HiviewDFX; +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, ACCESSCONTROL_DOMAIN_SANDBOXMANAGER, "SandboxManagerDataSizeReportAdapter" +}; +static const std::string SANDBOX_MGR_NAME = "sandbox_manager"; +static const std::string SYS_EL1_SANDBOX_MGR_DIR = "/data/service/el1/public/sandbox_manager"; +static const std::string USER_DATA_DIR = "/data"; +static constexpr uint64_t INVALID_SIZE = 0; +static const double UNITS = 1024.0; +static const long ONE_DAY = 24 * 60 * 60; +static time_t g_lastTime = 0; +} + +double GetPartitionRemainSize(const std::string& path) +{ + struct statfs stat; + if (statfs(path.c_str(), &stat) != 0) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Failed to get %{public}s's remaining size.", path.c_str()); + return INVALID_SIZE; + } + + /* 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", SANDBOX_MGR_NAME, "PARTITION_NAME", + USER_DATA_DIR, "REMAIN_PARTITION_SIZE", GetPartitionRemainSize(USER_DATA_DIR), + "FILE_OR_FOLDER_PATH", SYS_EL1_SANDBOX_MGR_DIR, "FILE_OR_FOLDER_SIZE", GetFolderSize(SYS_EL1_SANDBOX_MGR_DIR)); + if (ret != 0) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Hisysevent report data size failed!"); + } +} + +bool GetInterval(time_t lastTime, time_t nowTime) +{ + return (nowTime - lastTime) > ONE_DAY; +} + +void ReportUserDataSize() +{ + time_t nowTime = time(nullptr); + if (GetInterval(g_lastTime, nowTime)) { + g_lastTime = nowTime; + std::thread task(ReportTask); + task.join(); + } +} +} // namespace SandboxManager +} // namespace AccessControl +} // OHOS \ No newline at end of file diff --git a/services/common/utils/data_size_report_adapter.h b/services/common/utils/data_size_report_adapter.h new file mode 100644 index 0000000..c52ac04 --- /dev/null +++ b/services/common/utils/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 "sandbox_manager_log.h" + +namespace OHOS { +namespace AccessControl { +namespace SandboxManager { + +void ReportUserDataSize(); + +} // namespace SandboxManager +} // namespace AccessControl +} // OHOS +#endif // DATA_SIZE_REPORT_ADAPTER_H diff --git a/services/sandbox_manager/BUILD.gn b/services/sandbox_manager/BUILD.gn index a33984d..3c21349 100644 --- a/services/sandbox_manager/BUILD.gn +++ b/services/sandbox_manager/BUILD.gn @@ -45,6 +45,7 @@ if (is_standard_system) { "${sandbox_manager_path}/frameworks/common/include", "${sandbox_manager_path}/interfaces/inner_api/sandbox_manager/include", "${sandbox_manager_path}/services/common/database/include", + "${sandbox_manager_path}/services/common/utils", "main/cpp/include/service", "main/cpp/include/sensitive", "main/cpp/include/database", diff --git a/services/sandbox_manager/main/cpp/src/service/policy_info_manager.cpp b/services/sandbox_manager/main/cpp/src/service/policy_info_manager.cpp index e6a0305..9d8b804 100644 --- a/services/sandbox_manager/main/cpp/src/service/policy_info_manager.cpp +++ b/services/sandbox_manager/main/cpp/src/service/policy_info_manager.cpp @@ -36,6 +36,7 @@ #include "sandbox_manager_log.h" #include "os_account_manager.h" #include "media_path_support.h" +#include "data_size_report_adapter.h" namespace OHOS { namespace AccessControl { @@ -1018,7 +1019,7 @@ int32_t PolicyInfoManager::AddPolicy(const uint32_t tokenId, const std::vector