linux获取磁盘信息

发布于:2023-09-14 ⋅ 阅读:(140) ⋅ 点赞:(0)

/// <summary>
/// 获取linux磁盘信息
/// </summary>
/// <returns></returns>
std::string ZmApiDeviceInfo::GetLinuxStorage()
{
    struct statvfs vfs;

    const char* path = "/"; // 要检查的文件系统路径
    if (statvfs(path, &vfs) != 0) {
        std::cerr << "Error getting file system status." << std::endl;
        return "";
    }

    unsigned long long totalSpace = vfs.f_frsize * vfs.f_blocks; // 总磁盘空间
    unsigned long long freeSpace = vfs.f_frsize * vfs.f_bfree;   // 空闲磁盘空间
    unsigned long long usedSpace = totalSpace - freeSpace;       // 已用磁盘空间

    double usagePercentage = (double)usedSpace / totalSpace * 100.0;

    /*std::cout << "Total Space: " << totalSpace << " bytes" << std::endl;
    std::cout << "Used Space: " << usedSpace << " bytes" << std::endl;
    std::cout << "Free Space: " << freeSpace << " bytes" << std::endl;
    std::cout << "Usage Percentage: " << usagePercentage << "%" << std::endl;*/

    char strPercent[100]{ 0 };
    sprintf(strPercent, "%.2f%", usagePercentage);

    return strPercent;
}


网站公告

今日签到

点亮在社区的每一天
去签到