1 /*
2 * Copyright (c) 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/volume_storage_status_service.h"
17
18 #include <singleton.h>
19 #include <sys/statvfs.h>
20
21 #include "storage_service_errno.h"
22 #include "storage_service_log.h"
23 #include "volume/volume_manager_service.h"
24
25 using namespace std;
26
27 namespace OHOS {
28 namespace StorageManager {
VolumeStorageStatusService()29 VolumeStorageStatusService::VolumeStorageStatusService() {}
~VolumeStorageStatusService()30 VolumeStorageStatusService::~VolumeStorageStatusService() {}
31
32
GetVolumePath(std::string volumeUuid)33 std::string VolumeStorageStatusService::GetVolumePath(std::string volumeUuid)
34 {
35 auto volumePtr = DelayedSingleton<VolumeManagerService>::GetInstance()->GetVolumeByUuid(volumeUuid);
36 if (volumePtr == nullptr) {
37 LOGE("VolumeStorageStatusService::GetVolumePath fail.");
38 return "";
39 }
40 return volumePtr->GetPath();
41 }
42
GetFreeSizeOfVolume(string volumeUuid,int64_t & freeSize)43 int32_t VolumeStorageStatusService::GetFreeSizeOfVolume(string volumeUuid, int64_t &freeSize)
44 {
45 string path = GetVolumePath(volumeUuid);
46 LOGI("VolumeStorageStatusService::GetFreeSizeOfVolume path is %{public}s", path.c_str());
47 if (path == "") {
48 return E_NON_EXIST;
49 }
50 struct statvfs diskInfo;
51 int ret = statvfs(path.c_str(), &diskInfo);
52 if (ret != E_OK) {
53 return E_ERR;
54 }
55 freeSize = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_bfree;
56 return E_OK;
57 }
58
GetTotalSizeOfVolume(string volumeUuid,int64_t & totalSize)59 int32_t VolumeStorageStatusService::GetTotalSizeOfVolume(string volumeUuid, int64_t &totalSize)
60 {
61 string path = GetVolumePath(volumeUuid);
62 if (path == "") {
63 return E_NON_EXIST;
64 }
65 struct statvfs diskInfo;
66 int ret = statvfs(path.c_str(), &diskInfo);
67 if (ret != E_OK) {
68 return E_ERR;
69 }
70 totalSize = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks;
71 return E_OK;
72 }
73 } // StorageManager
74 } // OHOS
75