1 /*
2 * Copyright (c) 2021-2022 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 "storage/storage_total_status_service.h"
17
18 #include <cstdlib>
19 #include <cstring>
20 #include <cinttypes>
21 #include "hitrace_meter.h"
22 #include <mntent.h>
23 #include <singleton.h>
24 #include <sys/statvfs.h>
25 #include <unordered_set>
26
27 #include "storage_service_errno.h"
28 #include "storage_service_log.h"
29 #include "utils/storage_radar.h"
30 #include "utils/storage_utils.h"
31
32 using namespace OHOS::StorageService;
33 namespace OHOS {
34 namespace StorageManager {
StorageTotalStatusService()35 StorageTotalStatusService::StorageTotalStatusService() {}
~StorageTotalStatusService()36 StorageTotalStatusService::~StorageTotalStatusService() {}
37
GetSystemSize(int64_t & systemSize)38 int32_t StorageTotalStatusService::GetSystemSize(int64_t &systemSize)
39 {
40 int64_t roundSize = 0;
41 int32_t ret = GetTotalSize(roundSize);
42 if (ret != E_OK) {
43 LOGE("storage total status service GetTotalSize failed, please check");
44 StorageService::StorageRadar::GetInstance().RecordFuctionResult(
45 "GetTotalSize", BizScene::SPACE_STATISTICS, BizStage::BIZ_STAGE_GET_SYSTEM_SIZE, "EL1", ret);
46 return ret;
47 }
48 int64_t totalSize = 0;
49 ret = GetSizeOfPath(PATH_DATA, SizeType::TOTAL, totalSize);
50 if (ret != E_OK) {
51 LOGE("storage total status service GetSizeOfPath failed, please check");
52 StorageService::StorageRadar::GetInstance().RecordFuctionResult(
53 "GetSizeOfPath", BizScene::SPACE_STATISTICS, BizStage::BIZ_STAGE_GET_SYSTEM_SIZE, "EL1", ret);
54 return ret;
55 }
56 systemSize = roundSize - totalSize;
57 LOGE("StorageTotalStatusService::GetSystemSize success, roundSize=%{public}lld, (/data)totalSize=%{public}lld, "
58 "systemSize=%{public}lld",
59 static_cast<long long>(roundSize), static_cast<long long>(totalSize), static_cast<long long>(systemSize));
60 return E_OK;
61 }
62
GetTotalSize(int64_t & totalSize)63 int32_t StorageTotalStatusService::GetTotalSize(int64_t &totalSize)
64 {
65 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
66
67 int64_t dataSize = 0;
68 int32_t ret = GetSizeOfPath(PATH_DATA, SizeType::TOTAL, dataSize);
69 if (ret != E_OK) {
70 LOGE("GetSizeOfPath of data size failed, please check");
71 StorageService::StorageRadar::GetInstance().RecordFuctionResult("GetTotalSize GetSizeOfPath for dataSize",
72 BizScene::SPACE_STATISTICS,
73 BizStage::BIZ_STAGE_GET_TOTAL_SIZE, "EL1", ret);
74 return ret;
75 }
76 int64_t rootSize = 0;
77 ret = GetSizeOfPath(PATH_ROOT, SizeType::TOTAL, rootSize);
78 if (ret != E_OK) {
79 LOGE("GetSizeOfPath of root size failed, please check");
80 StorageService::StorageRadar::GetInstance().RecordFuctionResult("GetTotalSize GetSizeOfPath for rootSize",
81 BizScene::SPACE_STATISTICS,
82 BizStage::BIZ_STAGE_GET_TOTAL_SIZE, "EL1", ret);
83 return ret;
84 }
85 totalSize = GetRoundSize(dataSize + rootSize);
86 LOGE("StorageTotalStatusService::GetTotalSize success, roundSize=%{public}lld, (/data)totalDataSize=%{public}lld,"
87 " (/)totalRootSize=%{public}lld",
88 static_cast<long long>(totalSize), static_cast<long long>(dataSize), static_cast<long long>(rootSize));
89 return E_OK;
90 }
91
GetFreeSize(int64_t & freeSize)92 int32_t StorageTotalStatusService::GetFreeSize(int64_t &freeSize)
93 {
94 int32_t ret = GetSizeOfPath(PATH_DATA, SizeType::FREE, freeSize);
95 if (ret != E_OK) {
96 LOGE("GetFreeSize failed, please check");
97 StorageService::StorageRadar::GetInstance().RecordFuctionResult("GetFreeSize",
98 BizScene::SPACE_STATISTICS,
99 BizStage::BIZ_STAGE_GET_FREE_SIZE, "EL1", ret);
100 }
101 LOGE("StorageTotalStatusService::GetFreeSize success, (/data)freeSize=%{public}lld",
102 static_cast<long long>(freeSize));
103 return ret;
104 }
105
GetSizeOfPath(const char * path,int32_t type,int64_t & size)106 int32_t StorageTotalStatusService::GetSizeOfPath(const char *path, int32_t type, int64_t &size)
107 {
108 struct statvfs diskInfo;
109 int ret = statvfs(path, &diskInfo);
110 if (ret != E_OK) {
111 return E_ERR;
112 }
113 std::string typeStr = "";
114 if (type == SizeType::TOTAL) {
115 size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks;
116 typeStr = "total space";
117 } else if (type == SizeType::FREE) {
118 size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_bfree;
119 typeStr = "free space";
120 } else {
121 size = (int64_t)diskInfo.f_bsize * ((int64_t)diskInfo.f_blocks - (int64_t)diskInfo.f_bfree);
122 typeStr = "used space";
123 }
124 LOGI("StorageStatusService::GetSizeOfPath path is %{public}s, type is %{public}s, size is %{public}lld.",
125 path, typeStr.c_str(), static_cast<long long>(size));
126 return E_OK;
127 }
128 } // StorageManager
129 } // OHOS
130