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
28 namespace OHOS {
29 namespace StorageManager {
30 constexpr int32_t DEV_BLOCK_DIR_LEN = 10;
31 constexpr int32_t MNT_DIR_LEN = 4;
32 constexpr int32_t DATA_DIR_LEN = 5;
33
StorageTotalStatusService()34 StorageTotalStatusService::StorageTotalStatusService() {}
~StorageTotalStatusService()35 StorageTotalStatusService::~StorageTotalStatusService() {}
36
GetSystemSize(int64_t & systemSize)37 int32_t StorageTotalStatusService::GetSystemSize(int64_t &systemSize)
38 {
39 struct mntent *mntent = nullptr;
40 std::unordered_set<std::string> mntSet;
41 std::unique_ptr<FILE, decltype(&endmntent)> mntFile(setmntent("/proc/mounts", "re"), endmntent);
42 if (mntFile.get() == nullptr) {
43 LOGE("enter setmntent is error");
44 return E_ERR;
45 }
46 while ((mntent = getmntent(mntFile.get())) != nullptr) {
47 if (!mntent) {
48 LOGE("enter getmntent is error");
49 break;
50 }
51 char *strName = mntent->mnt_fsname;
52 char *strDir = mntent->mnt_dir;
53 if (!mntSet.insert(strName).second) {
54 continue;
55 }
56 if (strncmp(strName, PATH_DEV_BLOCK, DEV_BLOCK_DIR_LEN) == 0 && strncmp(strDir, PATH_MNT, MNT_DIR_LEN) != 0 &&
57 strncmp(strDir, PATH_DATA, DATA_DIR_LEN) != 0) {
58 LOGI("mntent::strName is %{public}s, mntent::strDir is %{public}s", strName, strDir);
59 int64_t blkSize;
60 int32_t err = GetSizeOfPath(strDir, SizeType::USED, blkSize);
61 if (err != E_OK) {
62 LOGE("get path %s size err", strDir);
63 }
64 systemSize += blkSize;
65 }
66 }
67 return E_OK;
68 }
69
GetTotalSize(int64_t & totalSize)70 int32_t StorageTotalStatusService::GetTotalSize(int64_t &totalSize)
71 {
72 return GetSizeOfPath(PATH_DATA, SizeType::TOTAL, totalSize);
73 }
74
GetFreeSize(int64_t & freeSize)75 int32_t StorageTotalStatusService::GetFreeSize(int64_t &freeSize)
76 {
77 return GetSizeOfPath(PATH_DATA, SizeType::FREE, freeSize);
78 }
79
GetSizeOfPath(const char * path,int32_t type,int64_t & size)80 int32_t StorageTotalStatusService::GetSizeOfPath(const char *path, int32_t type, int64_t &size)
81 {
82 struct statvfs diskInfo;
83 int ret = statvfs(path, &diskInfo);
84 if (ret != E_OK) {
85 return E_ERR;
86 }
87 if (type == SizeType::TOTAL) {
88 size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks;
89 } else if (type == SizeType::FREE) {
90 size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_bfree;
91 } else {
92 size = (int64_t)diskInfo.f_bsize * ((int64_t)diskInfo.f_blocks - (int64_t)diskInfo.f_bfree);
93 }
94 return E_OK;
95 }
96 } // StorageManager
97 } // OHOS
98