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 "data_usage_dfx.h"
17
18 #include <sys/statfs.h>
19 #include "accesstoken_common_log.h"
20 #include "directory_ex.h"
21 #include "hisysevent.h"
22
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 constexpr const char* ACCESSTOKEN_NAME = "access_token";
28 constexpr const char* NATIVE_CFG_FILE_PATH = "/data/service/el0/access_token/nativetoken.json";
29 constexpr const char* DATABASE_DIR_PATH = "/data/service/el1/public/access_token/";
30 constexpr const char* DATA_FOLDER = "/data";
31 constexpr const char* ACCESSTOKEN_DATABASE_NAME = "access_token.db";
32 constexpr const char* ACCESSTOKEN_DATABASE_NAME_BACK = "access_token_slave.db";
33 constexpr const char* PRIVACY_DATABASE_NAME = "permission_used_record.db";
34 static constexpr uint64_t INVALID_SIZE = 0;
35 }
36
GetUserDataRemainSize()37 uint64_t GetUserDataRemainSize()
38 {
39 struct statfs stat;
40 if (statfs(DATA_FOLDER, &stat) != 0) {
41 LOGE(ATM_DOMAIN, ATM_TAG, "Failed to get data remain size.");
42 return INVALID_SIZE;
43 }
44 return static_cast<uint64_t>(stat.f_bfree) * stat.f_bsize;
45 }
46
GetFileSize(const char * filePath)47 uint64_t GetFileSize(const char* filePath)
48 {
49 struct stat fileInfo;
50 if (stat(filePath, &fileInfo) != 0) {
51 LOGE(ATM_DOMAIN, ATM_TAG, "Failed to get file stat, path=%{public}s.", filePath);
52 return INVALID_SIZE;
53 }
54 return fileInfo.st_size;
55 }
56
GetDatabaseFileSize(const std::string & name,std::vector<std::string> & filePath,std::vector<uint64_t> & fileSize)57 void GetDatabaseFileSize(const std::string& name, std::vector<std::string>& filePath, std::vector<uint64_t>& fileSize)
58 {
59 std::vector<std::string> files;
60 GetDirFiles(DATABASE_DIR_PATH, files);
61 for (const std::string& file : files) {
62 if (file.find(name) != std::string::npos) {
63 fileSize.emplace_back(GetFileSize(file.c_str()));
64 filePath.emplace_back(file);
65 }
66 }
67 }
68
ReportAccessTokenUserData()69 void ReportAccessTokenUserData()
70 {
71 std::vector<std::string> filePath = { NATIVE_CFG_FILE_PATH };
72 std::vector<uint64_t> fileSize = { GetFileSize(NATIVE_CFG_FILE_PATH) };
73 GetDatabaseFileSize(ACCESSTOKEN_DATABASE_NAME, filePath, fileSize);
74 GetDatabaseFileSize(ACCESSTOKEN_DATABASE_NAME_BACK, filePath, fileSize);
75 (void)HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, "USER_DATA_SIZE",
76 HiviewDFX::HiSysEvent::EventType::STATISTIC, "COMPONENT_NAME", ACCESSTOKEN_NAME, "PARTITION_NAME", DATA_FOLDER,
77 "REMAIN_PARTITION_SIZE", GetUserDataRemainSize(),
78 "FILE_OR_FOLDER_PATH", filePath, "FILE_OR_FOLDER_SIZE", fileSize);
79 }
80
ReportPrivacyUserData()81 void ReportPrivacyUserData()
82 {
83 std::vector<std::string> filePath;
84 std::vector<uint64_t> fileSize;
85 GetDatabaseFileSize(PRIVACY_DATABASE_NAME, filePath, fileSize);
86 (void)HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, "USER_DATA_SIZE",
87 HiviewDFX::HiSysEvent::EventType::STATISTIC, "COMPONENT_NAME", ACCESSTOKEN_NAME, "PARTITION_NAME", DATA_FOLDER,
88 "REMAIN_PARTITION_SIZE", GetUserDataRemainSize(),
89 "FILE_OR_FOLDER_PATH", filePath, "FILE_OR_FOLDER_SIZE", fileSize);
90 }
91 } // namespace AccessToken
92 } // namespace Security
93 } // namespace OHOS