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 #include "data_size_report_adapter.h"
16 #include "hisysevent.h"
17
18 #include <sstream>
19 #include <thread>
20 #include <vector>
21 #include <sys/statfs.h>
22 #include <sys/stat.h>
23 #include "directory_ex.h"
24
25 namespace OHOS {
26 namespace Security {
27 namespace CodeSign {
28 namespace {
29 using namespace OHOS::HiviewDFX;
30
31 static const std::string CODE_SIGN_NAME = "code_signature";
32 static const std::string SYS_EL1_CODE_SIGN_DIR = "/data/service/el1/public/profiles";
33 static const std::string USER_DATA_DIR = "/data";
34 static const double UNITS = 1024.0;
35 }
36
GetPartitionRemainSize(const std::string & path)37 double GetPartitionRemainSize(const std::string& path)
38 {
39 struct statfs stat;
40 if (statfs(path.c_str(), &stat) != 0) {
41 LOG_ERROR("Failed to get %{public}s's remaining size.", path.c_str());
42 return 0;
43 }
44
45 /* change B to MB */
46 return (static_cast<double>(stat.f_bfree) * static_cast<double>(stat.f_bsize)) / (UNITS * UNITS);
47 }
48
ReportTask()49 void ReportTask()
50 {
51 int ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, "USER_DATA_SIZE",
52 HiviewDFX::HiSysEvent::EventType::STATISTIC, "COMPONENT_NAME", CODE_SIGN_NAME, "PARTITION_NAME",
53 USER_DATA_DIR, "REMAIN_PARTITION_SIZE", GetPartitionRemainSize(USER_DATA_DIR),
54 "FILE_OR_FOLDER_PATH", SYS_EL1_CODE_SIGN_DIR, "FILE_OR_FOLDER_SIZE", GetFolderSize(SYS_EL1_CODE_SIGN_DIR));
55 if (ret != 0) {
56 LOG_ERROR("Hisysevent report data size failed!");
57 }
58 }
59
ReportUserDataSize()60 void ReportUserDataSize()
61 {
62 std::thread task(ReportTask);
63 task.join();
64 }
65 } // namespace CodeSign
66 } // namespace Security
67 } // OHOS