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 #ifndef GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_CPP
17 #define GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_CPP
18
19 #include "hisysevent_adapter.h"
20
21 #include <string>
22 #include <sys/statfs.h>
23
24 #include "directory_ex.h"
25 #include "font_hilog.h"
26 #include "hisysevent.h"
27
28 namespace OHOS {
29 namespace Global {
30 namespace FontManager {
31 using HiSysEventNameSpace = OHOS::HiviewDFX::HiSysEvent;
32
33 static const std::string DATA_PARTITION_NAME = "/data";
34 static const std::string INSTALL_PATH = "/data/service/el1/public/for-all-app/fonts/";
35 static const std::string COMPONENT_NAME = "font_manager";
36
HisyseventAdapter()37 HisyseventAdapter::HisyseventAdapter() {}
~HisyseventAdapter()38 HisyseventAdapter::~HisyseventAdapter() {}
39
CollectUserDataSize()40 int HisyseventAdapter::CollectUserDataSize()
41 {
42 std::string componentName = COMPONENT_NAME;
43 std::string partitionName = DATA_PARTITION_NAME;
44 std::uint64_t remainPartitionSize = this->GetDataPartitionRemainSize();
45 std::vector<std::string> fileOrFolderPath = this->GetFileOrFolderPath();
46 std::vector<std::uint64_t> fileOrFolderSize = this->GetFileOrFolderSize();
47 int ret = HiSysEventWrite(HiSysEventNameSpace::Domain::FILEMANAGEMENT, "USER_DATA_SIZE",
48 HiSysEventNameSpace::EventType::STATISTIC, "COMPONENT_NAME", componentName, "PARTITION_NAME", partitionName,
49 "REMAIN_PARTITION_SIZE", remainPartitionSize, "FILE_OR_FOLDER_PATH", fileOrFolderPath, "FILE_OR_FOLDER_SIZE",
50 fileOrFolderSize);
51 return ret;
52 }
53
GetDataPartitionRemainSize()54 std::uint64_t HisyseventAdapter::GetDataPartitionRemainSize()
55 {
56 std::string partitionName = DATA_PARTITION_NAME;
57 struct statfs stat;
58 if (statfs(partitionName.c_str(), &stat) != 0) {
59 return -1;
60 }
61 std::uint64_t blockSize = stat.f_bsize;
62 std::uint64_t freeSize = stat.f_bfree * blockSize;
63 constexpr double units = 1024.0;
64 std::uint64_t freeSizeMb = freeSize / (units * units);
65 return freeSizeMb;
66 }
67
GetFileOrFolderPath()68 std::vector<std::string> HisyseventAdapter::GetFileOrFolderPath()
69 {
70 std::vector<std::string> vec;
71 vec.push_back(INSTALL_PATH);
72 return vec;
73 }
74
GetFileOrFolderSize()75 std::vector<std::uint64_t> HisyseventAdapter::GetFileOrFolderSize()
76 {
77 std::vector<std::uint64_t> vec;
78 vec.push_back(OHOS::GetFolderSize(INSTALL_PATH));
79 return vec;
80 }
81 } // namespace FontManager
82 } // namespace Global
83 } // namespace OHOS
84
85 #endif // GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_CPP