1 /* 2 * Copyright (c) 2025 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 "statvfs_core.h" 17 18 #include <sys/statvfs.h> 19 #include "filemgmt_libhilog.h" 20 21 namespace OHOS { 22 namespace FileManagement { 23 namespace ModuleStatvfs { 24 using namespace std; 25 DoGetFreeSize(const string & path)26FsResult<int64_t> StatvfsCore::DoGetFreeSize(const string &path) 27 { 28 struct statvfs diskInfo; 29 int ret = statvfs(path.c_str(), &diskInfo); 30 if (ret != 0) { 31 return FsResult<int64_t>::Error(errno); 32 } 33 unsigned long long freeSize = static_cast<unsigned long long>(diskInfo.f_bsize) * 34 static_cast<unsigned long long>(diskInfo.f_bfree); 35 return FsResult<int64_t>::Success(static_cast<int64_t>(freeSize)); 36 } 37 DoGetTotalSize(const string & path)38FsResult<int64_t> StatvfsCore::DoGetTotalSize(const string &path) 39 { 40 struct statvfs diskInfo; 41 int ret = statvfs(path.c_str(), &diskInfo); 42 if (ret != 0) { 43 return FsResult<int64_t>::Error(errno); 44 } 45 unsigned long long totalSize = static_cast<unsigned long long>(diskInfo.f_bsize) * 46 static_cast<unsigned long long>(diskInfo.f_blocks); 47 return FsResult<int64_t>::Success(static_cast<int64_t>(totalSize)); 48 } 49 50 } // namespace ModuleStatfs 51 } // namespace FileManagement 52 } // namespace OHOS