• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hitrace_meter.h"
19 #include <sys/statvfs.h>
20 
21 #include "storage_service_errno.h"
22 #include "storage_service_log.h"
23 #include "utils/storage_radar.h"
24 #include "utils/storage_utils.h"
25 
26 using namespace OHOS::StorageService;
27 namespace OHOS {
28 namespace StorageManager {
StorageTotalStatusService()29 StorageTotalStatusService::StorageTotalStatusService() {}
~StorageTotalStatusService()30 StorageTotalStatusService::~StorageTotalStatusService() {}
31 
GetSystemSize(int64_t & systemSize)32 int32_t StorageTotalStatusService::GetSystemSize(int64_t &systemSize)
33 {
34     int64_t roundSize = 0;
35     int32_t ret = GetTotalSize(roundSize);
36     if (ret != E_OK) {
37         LOGE("storage total status service GetTotalSize failed, please check");
38         StorageRadar::ReportGetStorageStatus("GetSystemSize::GetTotalSize", DEFAULT_USERID, ret, "setting");
39         return ret;
40     }
41     int64_t totalSize = 0;
42     ret = GetSizeOfPath(PATH_DATA, SizeType::TOTAL, totalSize);
43     if (ret != E_OK) {
44         LOGE("storage total status service GetSizeOfPath failed, please check");
45         StorageRadar::ReportGetStorageStatus("GetSystemSize::GetSizeOfPath", DEFAULT_USERID, ret, "setting");
46         return ret;
47     }
48     systemSize = roundSize - totalSize;
49     LOGE("StorageTotalStatusService::GetSystemSize success, roundSize=%{public}lld, (/data)totalSize=%{public}lld, "
50         "systemSize=%{public}lld",
51         static_cast<long long>(roundSize), static_cast<long long>(totalSize), static_cast<long long>(systemSize));
52     return E_OK;
53 }
54 
GetTotalSize(int64_t & totalSize)55 int32_t StorageTotalStatusService::GetTotalSize(int64_t &totalSize)
56 {
57     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
58 
59     int64_t dataSize = 0;
60     int32_t ret = GetSizeOfPath(PATH_DATA, SizeType::TOTAL, dataSize);
61     if (ret != E_OK) {
62         LOGE("GetSizeOfPath of data size failed, please check");
63         StorageRadar::ReportGetStorageStatus("GetTotalSize::GetSizeOfDataPath", DEFAULT_USERID, ret, "setting");
64         return ret;
65     }
66     int64_t rootSize = 0;
67     ret = GetSizeOfPath(PATH_ROOT, SizeType::TOTAL, rootSize);
68     if (ret != E_OK) {
69         LOGE("GetSizeOfPath of root size failed, please check");
70         StorageRadar::ReportGetStorageStatus("GetSystemSize::GetSizeOfRootPath", DEFAULT_USERID, ret, "setting");
71         return ret;
72     }
73     totalSize = GetRoundSize(dataSize + rootSize);
74     LOGE("StorageTotalStatusService::GetTotalSize success, roundSize=%{public}lld, (/data)totalDataSize=%{public}lld,"
75         " (/)totalRootSize=%{public}lld",
76         static_cast<long long>(totalSize), static_cast<long long>(dataSize), static_cast<long long>(rootSize));
77     return E_OK;
78 }
79 
GetFreeSize(int64_t & freeSize)80 int32_t StorageTotalStatusService::GetFreeSize(int64_t &freeSize)
81 {
82     int32_t ret = GetSizeOfPath(PATH_DATA, SizeType::FREE, freeSize);
83     if (ret != E_OK) {
84         LOGE("GetFreeSize failed, please check");
85         StorageRadar::ReportGetStorageStatus("GetFreeSize", DEFAULT_USERID, ret, "setting");
86     }
87     LOGE("StorageTotalStatusService::GetFreeSize success, (/data)freeSize=%{public}lld",
88         static_cast<long long>(freeSize));
89     return ret;
90 }
91 
GetSizeOfPath(const char * path,int32_t type,int64_t & size)92 int32_t StorageTotalStatusService::GetSizeOfPath(const char *path, int32_t type, int64_t &size)
93 {
94     struct statvfs diskInfo;
95     int ret = statvfs(path, &diskInfo);
96     if (ret != E_OK) {
97         return E_STATVFS;
98     }
99     std::string typeStr = "";
100     if (type == SizeType::TOTAL) {
101         size = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks;
102         typeStr = "total space";
103     } else if (type == SizeType::FREE) {
104         size = (int64_t)diskInfo.f_frsize * (int64_t)diskInfo.f_bavail;
105         typeStr = "free space";
106     } else {
107         size = (int64_t)diskInfo.f_bsize * ((int64_t)diskInfo.f_blocks - (int64_t)diskInfo.f_bfree);
108         typeStr = "used space";
109     }
110     LOGI("StorageStatusService::GetSizeOfPath path is %{public}s, type is %{public}s, size is %{public}lld.",
111         path, typeStr.c_str(), static_cast<long long>(size));
112     return E_OK;
113 }
114 } // StorageManager
115 } // OHOS
116