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