string linux_path = "/";
//不管linux_path 写"/"还是"/home"或者其他,获取到的信息都一样,不知道怎么回事
void GetSysInfo::GetDiskInfo(vector<tgDiskInfo> &chDriveInfo)
{
//TODO: 获取Linux下各个盘符的使用情况
//通过系统函数计算磁盘使用情况
struct statfs diskInfo;
//statfs("/", &diskInfo);
statfs(linux_path.c_str(), &diskInfo);
unsigned long long totalBlocks = diskInfo.f_bsize;
unsigned long long totalSize = totalBlocks * diskInfo.f_blocks;
size_t mbTotalsize = totalSize >> 20;
unsigned long long freeDisk = diskInfo.f_bfree*totalBlocks;
size_t mbFreedisk = freeDisk >> 20;
unsigned long long availableDisk = diskInfo.f_bavail*totalBlocks;
size_t mbAvailableDisk = availableDisk >> 20;
tgDiskInfo DiskInfo = { 0 };
DiskInfo.FreeBytes = mbFreedisk;
DiskInfo.TotalBytes = mbTotalsize;
strncpy(DiskInfo.Name, linux_path.c_str(), 49);
string sName = linux_path;
if (sName == "/")
{
DiskInfo.sys = true;
}
std::cout << "test_GetDiskInfo:" << " FreeBytes = " << DiskInfo.FreeBytes
<< " TotalBytes = " << DiskInfo.TotalBytes
<< " Name = " << DiskInfo.Name
<< std::endl;
chDriveInfo.push_back(DiskInfo);
}
07-17
3483
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)