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 <mntent.h>
21 #include <singleton.h>
22 #include <sys/statvfs.h>
23 #include <unordered_set>
24
25 #include "storage_service_errno.h"
26 #include "storage_service_log.h"
27 #include "utils/storage_utils.h"
28
29 namespace OHOS {
30 namespace StorageManager {
StorageTotalStatusService()31 StorageTotalStatusService::StorageTotalStatusService() {}
~StorageTotalStatusService()32 StorageTotalStatusService::~StorageTotalStatusService() {}
33
GetSystemSize(int64_t & systemSize)34 int32_t StorageTotalStatusService::GetSystemSize(int64_t &systemSize)
35 {
36 int64_t roundSize = 0;
37 int32_t ret = GetTotalSize(roundSize);
38 if (ret != E_OK) {
39 return ret;
40 }
41 int64_t totalSize = 0;
42 ret = GetSizeOfPath(PATH_DATA, SizeType::TOTAL, totalSize);
43 if (ret != E_OK) {
44 return ret;
45 }
46 systemSize = roundSize - totalSize;
47 return E_OK;
48 }
49
GetTotalSize(int64_t & totalSize)50 int32_t StorageTotalStatusService::GetTotalSize(int64_t &totalSize)
51 {
52 int64_t dataSize = 0;
53 int32_t ret = GetSizeOfPath(PATH_DATA, SizeType::TOTAL, dataSize);
54 if (ret != E_OK) {
55 return ret;
56 }
57 int64_t rootSize = 0;
58 ret = GetSizeOfPath(PATH_ROOT, SizeType::TOTAL, rootSize);
59 if (ret != E_OK) {
60 return ret;
61 }
62 totalSize = GetRoundSize(dataSize + rootSize);
63 return E_OK;
64 }
65
GetFreeSize(int64_t & freeSize)66 int32_t StorageTotalStatusService::GetFreeSize(int64_t &freeSize)
67 {
68 return GetSizeOfPath(PATH_DATA, SizeType::FREE, freeSize);
69 }
70
GetSizeOfPath(const char * path,int32_t type,int64_t & size)71 int32_t StorageTotalStatusService::GetSizeOfPath(const char *path, int32_t type, int64_t &size)
72 {
73 struct statvfs diskInfo;
74 int ret = statvfs(path, &diskInfo);
75 if (ret != E_OK) {
76 return E_ERR;
77 }
78 if (type == SizeType::TOTAL) {
79 size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks;
80 } else if (type == SizeType::FREE) {
81 size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_bfree;
82 } else {
83 size = (int64_t)diskInfo.f_bsize * ((int64_t)diskInfo.f_blocks - (int64_t)diskInfo.f_bfree);
84 }
85 return E_OK;
86 }
87 } // StorageManager
88 } // OHOS
89