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_info_collector.h" 17 18 #include <sys/statvfs.h> 19 20 #include "device_profile_log.h" 21 #include "nlohmann/json.hpp" 22 23 namespace OHOS { 24 namespace DeviceProfile { 25 namespace { 26 const std::string TAG = "StorageInfoCollector"; 27 28 const char* PATH_DATA = "/data"; 29 const std::string SERVICE_ID = "storage"; 30 const std::string SERVICE_TYPE = "storage"; 31 const std::string CAPACITY = "capacity"; 32 constexpr int64_t KILOBYTE = 1024; 33 } 34 ConvertToProfileData(ServiceCharacteristicProfile & profile)35bool StorageInfoCollector::ConvertToProfileData(ServiceCharacteristicProfile& profile) 36 { 37 profile.SetServiceId(SERVICE_ID); 38 profile.SetServiceType(SERVICE_TYPE); 39 nlohmann::json jsonData; 40 int64_t totalSize = GetTotalSize(); 41 if (totalSize == 0) { 42 return false; 43 } 44 jsonData[CAPACITY] = totalSize; 45 profile.SetCharacteristicProfileJson(jsonData.dump()); 46 return true; 47 } 48 GetTotalSize()49int64_t StorageInfoCollector::GetTotalSize() 50 { 51 int64_t totalSize = 0; 52 struct statvfs diskInfo; 53 int ret = statvfs(PATH_DATA, &diskInfo); 54 if (ret != 0) { 55 HILOGE("GetTotalSize failed"); 56 return totalSize; 57 } 58 totalSize = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks / KILOBYTE; 59 return totalSize; 60 } 61 } // namespace DeviceProfile 62 } // OHOS 63