1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hks_report_data_size.h"
17
18 #include <cstdint>
19 #include <string>
20 #include "hks_event_info.h"
21 #include "hks_param.h"
22 #include "hks_template.h"
23 #include "hks_type.h"
24 #include "hks_type_enum.h"
25 #include "hks_type_inner.h"
26 #include "hks_report_common.h"
27 #include "hks_file_operator.h"
28 #include "hks_ha_event_report.h"
29 #include "hks_mem.h"
30 #include "hks_util.h"
31 #include "securec.h"
32 #include "directory_ex.h"
33
34 constexpr static char COMPONENT_NAME[] = "HUKS";
35 constexpr static char PARTITION_NAME[] = "/data";
36 constexpr static char FUNCTION_NAME[] = "ReportDataSizeEvent";
37
AddDataSizeParam(const std::string & foldSize,const std::string & foldPath,struct HksParamSet ** reportParamSet)38 static int32_t AddDataSizeParam(
39 const std::string &foldSize, const std::string &foldPath, struct HksParamSet **reportParamSet)
40 {
41 const struct HksParam params[] = {
42 {
43 .tag = HKS_TAG_PARAM0_UINT32,
44 .uint32Param = HKS_EVENT_DATA_SIZE_STATISTICS
45 },
46 {
47 .tag = HKS_TAG_COMPONENT_NAME,
48 .blob = { strlen(COMPONENT_NAME) + 1, (uint8_t *)COMPONENT_NAME }
49 },
50 {
51 .tag = HKS_TAG_PARTITION_NAME,
52 .blob = { strlen(PARTITION_NAME) + 1, (uint8_t *)PARTITION_NAME }
53 },
54 {
55 .tag = HKS_TAG_REMAIN_PARTITION_SIZE,
56 .uint64Param = static_cast<uint64_t>(GetDeviceValidSize(PARTITION_NAME))
57 },
58 {
59 .tag = HKS_TAG_FILE_OF_FOLDER_PATH,
60 .blob = { foldPath.size() + 1, (uint8_t *)(foldPath.c_str()) }
61 },
62 {
63 .tag = HKS_TAG_FILE_OF_FOLDER_SIZE,
64 .blob = { foldSize.size() + 1, (uint8_t *)(foldSize.c_str()) }
65 },
66 {
67 .tag = HKS_TAG_PARAM2_BUFFER,
68 .blob = { strlen(COMPONENT_NAME) + 1, (uint8_t *)COMPONENT_NAME }
69 },
70 {
71 .tag = HKS_TAG_PARAM0_BUFFER,
72 .blob = { strlen(FUNCTION_NAME) + 1, (uint8_t *)FUNCTION_NAME }
73 }
74 };
75 int32_t ret = HksAddParams(*reportParamSet, params, HKS_ARRAY_SIZE(params));
76 HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "add params failed");
77 return HKS_SUCCESS;
78 }
79
PreConstructDataSizeReportParamSet(int userId,struct HksParamSet ** reportParamSet)80 int32_t PreConstructDataSizeReportParamSet(int userId, struct HksParamSet **reportParamSet)
81 {
82 int32_t ret = HksInitParamSet(reportParamSet);
83 HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "init report paramset fail");
84 std::string userStr = "/" + std::to_string(userId);
85 std::string el1Path = HKS_EL1_DATA_PATH;
86 std::string el2Path = HKS_EL2_DATA_PATH + userStr + HKS_DIRECTOREY_NAME;
87 std::string el4Path = HKS_EL4_DATA_PATH + userStr + HKS_DIRECTOREY_NAME;
88 uint64_t startTime = 0;
89 (void)HksElapsedRealTime(&startTime);
90 std::string foldSize = "[" +
91 std::to_string(OHOS::GetFolderSize(el1Path)) + ", " +
92 std::to_string(OHOS::GetFolderSize(el2Path)) + ", " +
93 std::to_string(OHOS::GetFolderSize(el4Path)) + "]";
94 std::string foldPath = "[\"" + el1Path + "\", \"" + el2Path + "\", \"" + el4Path + "\"]";
95 do {
96 ret = AddTimeCost(*reportParamSet, startTime);
97 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "add time cost to reportParamSet failed!")
98
99 ret = AddDataSizeParam(foldSize, foldPath, reportParamSet);
100 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "AddDataSizeParam failed");
101
102 ret = HksBuildParamSet(reportParamSet);
103 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "build paramset failed");
104 } while (0);
105 if (ret != HKS_SUCCESS) {
106 HksFreeParamSet(reportParamSet);
107 }
108 return ret;
109 }
110
HksParamSetToEventInfoForDataSize(const struct HksParamSet * paramSetIn,struct HksEventInfo * eventInfo)111 int32_t HksParamSetToEventInfoForDataSize(const struct HksParamSet *paramSetIn, struct HksEventInfo* eventInfo)
112 {
113 HKS_IF_TRUE_LOGI_RETURN(paramSetIn == nullptr || eventInfo == nullptr, HKS_ERROR_NULL_POINTER,
114 "HksParamSetToEventInfoForRename params is null")
115 int32_t ret = HKS_SUCCESS;
116 do {
117 ret = GetCommonEventInfo(paramSetIn, eventInfo);
118 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "report GetCommonEventInfo failed! ret = %" LOG_PUBLIC "d", ret);
119
120 struct HksParam *paramToEventInfo = nullptr;
121 if (HksGetParam(paramSetIn, HKS_TAG_COMPONENT_NAME, ¶mToEventInfo) == HKS_SUCCESS) {
122 CopyParamBlobData(&eventInfo->dataSizeInfo.component, paramToEventInfo);
123 }
124
125 if (HksGetParam(paramSetIn, HKS_TAG_PARTITION_NAME, ¶mToEventInfo) == HKS_SUCCESS) {
126 CopyParamBlobData(&eventInfo->dataSizeInfo.partition, paramToEventInfo);
127 }
128
129 if (HksGetParam(paramSetIn, HKS_TAG_FILE_OF_FOLDER_PATH, ¶mToEventInfo) == HKS_SUCCESS) {
130 CopyParamBlobData(&eventInfo->dataSizeInfo.foldPath, paramToEventInfo);
131 }
132
133 if (HksGetParam(paramSetIn, HKS_TAG_FILE_OF_FOLDER_SIZE, ¶mToEventInfo) == HKS_SUCCESS) {
134 CopyParamBlobData(&eventInfo->dataSizeInfo.foldSize, paramToEventInfo);
135 }
136
137 if (HksGetParam(paramSetIn, HKS_TAG_REMAIN_PARTITION_SIZE, ¶mToEventInfo) == HKS_SUCCESS) {
138 eventInfo->dataSizeInfo.partitionRemain = paramToEventInfo->uint64Param;
139 }
140 } while (0);
141 if (ret != HKS_SUCCESS) {
142 HKS_LOG_I("report ParamSetToEventInfo failed! ret = %" LOG_PUBLIC "d", ret);
143 FreeEventInfoSpecificPtr(eventInfo);
144 }
145 return ret;
146 }
147
HksEventInfoIsNeedReportForDataSize(const struct HksEventInfo * eventInfo)148 bool HksEventInfoIsNeedReportForDataSize(const struct HksEventInfo *eventInfo)
149 {
150 return true;
151 }
152
HksEventInfoIsEqualForDataSize(const struct HksEventInfo * eventInfo1,const struct HksEventInfo * eventInfo2)153 bool HksEventInfoIsEqualForDataSize(const struct HksEventInfo *eventInfo1, const struct HksEventInfo *eventInfo2)
154 {
155 /* data size event is not a statistic event */
156 return false;
157 }
158
HksEventInfoAddForDataSize(struct HksEventInfo * dstEventInfo,const struct HksEventInfo * srcEventInfo)159 void HksEventInfoAddForDataSize(struct HksEventInfo *dstEventInfo, const struct HksEventInfo *srcEventInfo)
160 {
161 if (HksEventInfoIsEqualForDataSize(dstEventInfo, srcEventInfo)) {
162 dstEventInfo->common.count++;
163 }
164 }
165
HksEventInfoToMapForDataSize(const struct HksEventInfo * eventInfo,std::unordered_map<std::string,std::string> & reportData)166 int32_t HksEventInfoToMapForDataSize(const struct HksEventInfo *eventInfo,
167 std::unordered_map<std::string, std::string> &reportData)
168 {
169 HKS_IF_NULL_LOGI_RETURN(eventInfo, HKS_ERROR_NULL_POINTER, "HksEventInfoToMapForDataSize evenInfo is null")
170
171 const char *component = (eventInfo->dataSizeInfo.component != nullptr) ?
172 eventInfo->dataSizeInfo.component : "unknown";
173 auto ret = reportData.insert_or_assign("componentName", std::string(component));
174 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert component failed!");
175
176 const char *partition = (eventInfo->dataSizeInfo.partition != nullptr) ?
177 eventInfo->dataSizeInfo.partition : "unknown";
178 ret = reportData.insert_or_assign("partitionName", std::string(partition));
179 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert partition failed!");
180
181 const char *foldPath = (eventInfo->dataSizeInfo.foldPath != nullptr) ?
182 eventInfo->dataSizeInfo.foldPath : "unknown";
183 ret = reportData.insert_or_assign("filepath", std::string(foldPath));
184 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert foldPath failed!");
185
186 const char *foldSize = (eventInfo->dataSizeInfo.foldSize != nullptr) ?
187 eventInfo->dataSizeInfo.foldSize : "unknown";
188 ret = reportData.insert_or_assign("filesize", std::string(foldSize));
189 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert foldSize failed!");
190
191 ret = reportData.insert_or_assign("remainPartitionSize", std::to_string(eventInfo->dataSizeInfo.partitionRemain));
192 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert partitionRemain failed!");
193
194 HKS_IF_NOT_TRUE_LOGI_RETURN(ret.second, HKS_ERROR_BUFFER_TOO_SMALL,
195 "HksEventInfoToMapForDataSize failed! reportData insert failed!")
196 return HKS_SUCCESS;
197 }
198
ReportDataSizeEvent(int userId)199 void ReportDataSizeEvent(int userId)
200 {
201 HksParamSet *reportParamSet = { nullptr };
202 int32_t ret = PreConstructDataSizeReportParamSet(userId, &reportParamSet);
203 if (ret != HKS_SUCCESS) {
204 HKS_LOG_I("PreConstructDataSizeReportParamSet failed");
205 HksFreeParamSet(&reportParamSet);
206 return;
207 }
208 HksEventReport(__func__, nullptr, nullptr, reportParamSet, HKS_SUCCESS);
209 HksFreeParamSet(&reportParamSet);
210 }