• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "report_data_partition_usage_manager.h"
17 
18 #include <sys/stat.h>
19 #include <sys/statfs.h>
20 
21 #include "directory_ex.h"
22 #include "ffrt.h"
23 #include "global_constant.h"
24 #include "hilog_tag_wrapper.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 static const std::string COMPONENT_NAME = "ability_runtime";
29 static const std::string USER_DATA_DIR = "/data";
30 static const uint64_t UNITS = 1024;
31 static const uint64_t INVALID_SIZE = 0;
32 static const uint64_t SECONDS_PER_DAY = 24 * 60 * 60;
33 static const uint64_t ONE_DAY_US = SECONDS_PER_DAY * 1000 * 1000;
34 static std::vector<std::string> pathList_ = {
35     "/data/service/el1/public/database/auto_startup_service",
36     "/data/service/el1/public/database/app_exit_reason",
37     "/data/service/el1/public/database/keep_alive_service",
38     "/data/service/el1/public/database/ability_manager_service",
39     "/data/service/el1/public/database/app_config_data"
40 };
41 
SendReportDataPartitionUsageEvent()42 void ReportDataPartitionUsageManager::SendReportDataPartitionUsageEvent()
43 {
44     ffrt::submit(HandleSendReportDataPartitionUsageEvent,
45         ffrt::task_attr().delay(ONE_DAY_US).name("SendReportDataPartitionUsageTask")
46         .timeout(AbilityRuntime::GlobalConstant::DEFAULT_FFRT_TASK_TIMEOUT));
47 }
48 
HandleSendReportDataPartitionUsageEvent()49 void ReportDataPartitionUsageManager::HandleSendReportDataPartitionUsageEvent()
50 {
51     EventInfo eventInfo;
52     GenerateEventInfo(eventInfo);
53     EventReport::SendReportDataPartitionUsageEvent(EventName::USER_DATA_SIZE,
54         HiSysEventType::STATISTIC, eventInfo);
55 
56     ffrt::submit(HandleSendReportDataPartitionUsageEvent,
57         ffrt::task_attr().delay(ONE_DAY_US).name("SendReportDataPartitionUsageTask")
58         .timeout(AbilityRuntime::GlobalConstant::DEFAULT_FFRT_TASK_TIMEOUT));
59 }
60 
GenerateEventInfo(EventInfo & eventInfo)61 void ReportDataPartitionUsageManager::GenerateEventInfo(EventInfo &eventInfo)
62 {
63     std::vector<std::uint64_t> fileOrFolderSize;
64     for (auto &path : pathList_) {
65         fileOrFolderSize.emplace_back(GetFilePathSize(path));
66     }
67 
68     eventInfo.componentName = COMPONENT_NAME;
69     eventInfo.partitionName = USER_DATA_DIR;
70     eventInfo.remainPartitionSize = GetPartitionRemainSize(USER_DATA_DIR);
71     eventInfo.fileOfFolderPath = pathList_;
72     eventInfo.fileOfFolderSize = fileOrFolderSize;
73 }
74 
GetFilePathSize(const std::string & filePath)75 uint64_t ReportDataPartitionUsageManager::GetFilePathSize(const std::string &filePath)
76 {
77     return OHOS::GetFolderSize(filePath);
78 }
79 
GetPartitionRemainSize(const std::string & filePath)80 uint64_t ReportDataPartitionUsageManager::GetPartitionRemainSize(const std::string &filePath)
81 {
82     if (!IsExistPath(filePath)) {
83         TAG_LOGE(AAFwkTag::ABILITYMGR, "file path not exist");
84         return INVALID_SIZE;
85     }
86 
87     struct statvfs stst;
88     if (statvfs(filePath.c_str(), &stst) != 0) {
89         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to get space info for path %{public}s", filePath.c_str());
90         return INVALID_SIZE;
91     }
92     return (static_cast<uint64_t>(stst.f_bfree) / UNITS) * (static_cast<uint64_t>(stst.f_bsize) / UNITS);
93 }
94 
IsExistPath(const std::string & filePath)95 bool ReportDataPartitionUsageManager::IsExistPath(const std::string &filePath)
96 {
97     if (filePath.empty()) {
98         return false;
99     }
100 
101     struct stat result = {};
102     if (stat(filePath.c_str(), &result) != 0) {
103         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail stat error");
104         return false;
105     }
106 
107     return true;
108 }
109 } // namespace AbilityRuntime
110 } // namespace OHOS