• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/statvfs.h>
19 
20 #include "storage_service_errno.h"
21 #include "storage_service_log.h"
22 #include "utils/storage_utils.h"
23 #ifdef EXTERNAL_STORAGE_MANAGER
24 #include "volume/volume_manager_service.h"
25 #endif
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 #ifdef EXTERNAL_STORAGE_MANAGER
36     auto volumePtr = VolumeManagerService::GetInstance().GetVolumeByUuid(volumeUuid);
37     if (volumePtr == nullptr) {
38         LOGE("VolumeStorageStatusService::GetVolumePath fail.");
39         return "";
40     }
41     return volumePtr->GetPath();
42 #else
43     return "";
44 #endif
45 }
46 
GetFreeSizeOfVolume(std::string volumeUuid,int64_t & freeSize)47 int32_t VolumeStorageStatusService::GetFreeSizeOfVolume(std::string volumeUuid, int64_t &freeSize)
48 {
49     std::string path = GetVolumePath(volumeUuid);
50     LOGI("VolumeStorageStatusService::GetFreeSizeOfVolume path is %{public}s", GetAnonyString(path).c_str());
51     if (path == "") {
52         return E_NON_EXIST;
53     }
54     struct statvfs diskInfo;
55     int ret = statvfs(path.c_str(), &diskInfo);
56     if (ret != E_OK) {
57         return E_STATVFS;
58     }
59     freeSize = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_bfree;
60     return E_OK;
61 }
62 
GetTotalSizeOfVolume(std::string volumeUuid,int64_t & totalSize)63 int32_t VolumeStorageStatusService::GetTotalSizeOfVolume(std::string volumeUuid, int64_t &totalSize)
64 {
65     std::string path = GetVolumePath(volumeUuid);
66     if (path == "") {
67         return E_NON_EXIST;
68     }
69     struct statvfs diskInfo;
70     int ret = statvfs(path.c_str(), &diskInfo);
71     if (ret != E_OK) {
72         return E_STATVFS;
73     }
74     totalSize =  (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks;
75     return E_OK;
76 }
77 } // StorageManager
78 } // OHOS
79