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 StorageRadar::ReportGetStorageStatus("GetSystemSize::GetTotalSize", DEFAULT_USERID, ret, "setting");
45 return ret;
46 }
47 int64_t totalSize = 0;
48 ret = GetSizeOfPath(PATH_DATA, SizeType::TOTAL, totalSize);
49 if (ret != E_OK) {
50 LOGE("storage total status service GetSizeOfPath failed, please check");
51 StorageRadar::ReportGetStorageStatus("GetSystemSize::GetSizeOfPath", DEFAULT_USERID, ret, "setting");
52 return ret;
53 }
54 systemSize = roundSize - totalSize;
55 LOGE("StorageTotalStatusService::GetSystemSize success, roundSize=%{public}lld, (/data)totalSize=%{public}lld, "
56 "systemSize=%{public}lld",
57 static_cast<long long>(roundSize), static_cast<long long>(totalSize), static_cast<long long>(systemSize));
58 return E_OK;
59 }
60
GetTotalSize(int64_t & totalSize)61 int32_t StorageTotalStatusService::GetTotalSize(int64_t &totalSize)
62 {
63 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
64
65 int64_t dataSize = 0;
66 int32_t ret = GetSizeOfPath(PATH_DATA, SizeType::TOTAL, dataSize);
67 if (ret != E_OK) {
68 LOGE("GetSizeOfPath of data size failed, please check");
69 StorageRadar::ReportGetStorageStatus("GetTotalSize::GetSizeOfDataPath", DEFAULT_USERID, ret, "setting");
70 return ret;
71 }
72 int64_t rootSize = 0;
73 ret = GetSizeOfPath(PATH_ROOT, SizeType::TOTAL, rootSize);
74 if (ret != E_OK) {
75 LOGE("GetSizeOfPath of root size failed, please check");
76 StorageRadar::ReportGetStorageStatus("GetTotalSize::GetSizeOfRootPath", DEFAULT_USERID, ret, "setting");
77 return ret;
78 }
79 totalSize = GetRoundSize(dataSize + rootSize);
80 LOGE("StorageTotalStatusService::GetTotalSize success, roundSize=%{public}lld, (/data)totalDataSize=%{public}lld,"
81 " (/)totalRootSize=%{public}lld",
82 static_cast<long long>(totalSize), static_cast<long long>(dataSize), static_cast<long long>(rootSize));
83 return E_OK;
84 }
85
GetFreeSize(int64_t & freeSize)86 int32_t StorageTotalStatusService::GetFreeSize(int64_t &freeSize)
87 {
88 int32_t ret = GetSizeOfPath(PATH_DATA, SizeType::FREE, freeSize);
89 if (ret != E_OK) {
90 LOGE("GetFreeSize failed, please check");
91 StorageRadar::ReportGetStorageStatus("GetFreeSize", DEFAULT_USERID, ret, "setting");
92 }
93 LOGE("StorageTotalStatusService::GetFreeSize success, (/data)freeSize=%{public}lld",
94 static_cast<long long>(freeSize));
95 return ret;
96 }
97
GetSizeOfPath(const char * path,int32_t type,int64_t & size)98 int32_t StorageTotalStatusService::GetSizeOfPath(const char *path, int32_t type, int64_t &size)
99 {
100 struct statvfs diskInfo;
101 int ret = statvfs(path, &diskInfo);
102 if (ret != E_OK) {
103 return E_ERR;
104 }
105 std::string typeStr = "";
106 if (type == SizeType::TOTAL) {
107 size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks;
108 typeStr = "total space";
109 } else if (type == SizeType::FREE) {
110 size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_bfree;
111 typeStr = "free space";
112 } else {
113 size = (int64_t)diskInfo.f_bsize * ((int64_t)diskInfo.f_blocks - (int64_t)diskInfo.f_bfree);
114 typeStr = "used space";
115 }
116 LOGI("StorageStatusService::GetSizeOfPath path is %{public}s, type is %{public}s, size is %{public}lld.",
117 path, typeStr.c_str(), static_cast<long long>(size));
118 return E_OK;
119 }
120 } // StorageManager
121 } // OHOS
122