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