区块链项目,需要用到linux 磁盘的容量信息。
#include
#include
#include
#include
#include
#include
#include
using std::vector;
using namespace std;
bool disk_info(char * path, string & capacity, string & free_capacity)
{
struct statfs diskInfo;
statfs(path, &diskInfo);
unsigned long long blocksize = diskInfo.f_bsize;
unsigned long long totalsize = blocksize * diskInfo.f_blocks;
char buf[128] = {0,};
snprintf(buf, sizeof(buf), " %llu GB", totalsize >> 30);
capacity.assign(buf);
unsigned long long freeDisk = diskInfo.f_bfree * blocksize;
unsigned long long availableDisk = diskInfo.f_bavail * blocksize;
char buf_free[128] = {0,};
snprintf(buf_free, sizeof(buf_free), " %llu GB", availableDisk >> 30);
free_capacity.assign(buf_free);
return true;
}
bool all_disk_info(std::vector& paths, string & capacity, string & free_capacity)
{
struct statfs diskInfo;
unsigned long long blocksize = 0;
unsigned long long totalsize = 0;
unsigned long long freeDisk = 0;
unsigned long long availableDisk = 0;
capacity.clear();
free_capacity.clear();
for (auto & it : paths) {
statfs(it.c_str(), &diskInfo);
blocksize = diskInfo.f_bsize;
totalsize += blocksize * diskInfo.f_blocks;
freeDisk = diskInfo.f_bfree * blocksize;
availableDisk += diskInfo.f_bavail * blocksize;
}
char buf[128] = {0,};
snprintf(buf, sizeof(buf), " %llu GB", totalsize >> 30);
capacity.assign(buf);
char buf_free[128] = {0,};
snprintf(buf_free, sizeof(buf_free), " %llu GB", availableDisk >> 30);
free_capacity.assign(buf_free);
}
void Split(const std::string& str, std::vector& v, const char* delimiter/* = "|"*/)
{
if (delimiter == NULL || str.empty())
return;
std::string buf = str;
size_t pos = std::string::npos;
std::string substr;
int delimiterlength = strlen(delimiter);
while (true)
{
pos = buf.find(delimiter);
if (pos != std::string::npos)
{
substr = buf.substr(0, pos);
if (!substr.empty())
v.push_back(substr);
buf = buf.substr(pos + delimiterlength);
}
else
{
if (!buf.empty())
v.push_back(buf);
break;
}
}
}
/*
获取系统挂载点信息
#!/bin/python
# execute the external "mount" command
# and parse the output.
import commands
mount = commands.getoutput('mount -v')
lines = mount.split('\n')
points = map(lambda line: line.split()[2], lines)
print points
*/
bool get_mount_points(std::vector & points)
{
FILE * fp = popen("mount -v", "r");
if (fp) {
char disk[4096];
size_t res = fread(disk, 1, 4096, fp);
if (res > 0 && res < 4096) {
if (disk[res - 1] == '\n')
disk[res - 1] = 0;
else
disk[res] = 0;
string inbuf;
inbuf.append(disk, strlen(disk));
std::vector lines;
Split(inbuf, lines, "\n");
int i = 0;
for (auto & it : lines) {
string str;
stringstream ss(it);
i = 0;
while (ss >> str)
{
if (++i == 3)
{
points.push_back(str);
break;
}
}
}
}
else {
cout << "read 'mount -v' failed" << res << endl;
}
pclose(fp);
return true;
}
else {
cout << "linux: cannot get system name" << endl;
}
return false;
}
int main(int argc,char **argv)
{
string disk_capacity, disk_free_capaciry;
if (argc == 2) {
disk_info(argv[1], disk_capacity, disk_free_capaciry);
}
else {
disk_info((char *)"/", disk_capacity, disk_free_capaciry);
}
std::cout << "total disk size is : " << disk_capacity << "\n" << "free disk size is : " << disk_free_capaciry << std::endl;
cout << "---------------------------\r\n\r\n" << endl;
std::vector points;
if (get_mount_points(points)) {
all_disk_info(points, disk_capacity, disk_free_capaciry);
cout << "disk_capacity=[" << disk_capacity << "]" << endl;
cout << "disk_free_capaciry=[" << disk_free_capaciry << "]" << endl;
for (auto & it : points) {
cout << it << endl;
}
}
return 0;
}
#include
#include
#include
#include "locale.h"
using namespace std;
bool get_disk_info(LPCSTR disk, DWORD & dwSectPerClust, DWORD & dwBytesPerSect, DWORD & dwFreeClusters, DWORD & dwTotalClusters, DWORD64 & total, DWORD64 & free)
{
BOOL bResult = GetDiskFreeSpace(disk, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters);
if (bResult) {
total = dwTotalClusters * (DWORD64)dwSectPerClust * (DWORD64)dwBytesPerSect + 1;
free = dwFreeClusters * (DWORD64)dwSectPerClust * (DWORD64)dwBytesPerSect + 1;
}
return bResult;
}
int get_disk_capacity(DWORD64 & get_size, DWORD64& get_free_size, LPCSTR disk = nullptr) {
setlocale(0, "rus");
if (!disk) {
cout << "unspecified partion, get all disk capacity" << endl;
}
else {
cout << "specified partion, get target disk capacity only" << endl;
}
int n;
char dd[4];
DWORD dr = GetLogicalDrives();
int d;
bool Flag;
get_size = 0;
get_free_size = 0;
for (int i = 0; i < 26; i++)
{
n = ((dr >> i) & 0x00000001);
if (n == 1)
{
dd[0] = char(65 + i); dd[1] = ':'; dd[2] = '\\'; dd[3] = 0;
cout << "partion -->>: " << dd << endl;
d = GetDriveType(dd);
if (d == DRIVE_UNKNOWN) cout << "UNKNOWN" << endl;
if (d == DRIVE_NO_ROOT_DIR) cout << " DRIVE NO ROOT DIR" << endl;
if (d == DRIVE_REMOVABLE) cout << "A floppy drive" << endl;
if (d == DRIVE_FIXED) cout << "A fixed drive" << endl;
if (d == DRIVE_REMOTE) cout << "A remote (network) drive" << endl;
if (d == DRIVE_CDROM) cout << "A CD-ROM drive" << endl;
if (d == DRIVE_RAMDISK) cout << "A RAM disk " << endl;
if (d == DRIVE_FIXED) {
DWORD dwTotalClusters;
DWORD dwFreeClusters;
DWORD dwSectPerClust;
DWORD dwBytesPerSect;
DWORD64 total_size, free_size;
BOOL bResult = get_disk_info(dd, dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters, total_size, free_size);
if (bResult) {
cout << "disk attribution: " << endl;
cout << "total clusters cnt: " << dwTotalClusters << endl; //总簇数量
cout << "Avail clusters cnt: " << dwFreeClusters << endl; //可用的簇
cout << "sectors cnt per cluster: " << dwSectPerClust << endl; //每个簇有多少个扇区
cout << "bytes per sector: " << dwBytesPerSect << endl; //每个扇区有多少个字节
cout << "Total disk capacity: " << total_size << endl; //磁盘总容量
cout << "Disk free capacity: " << free_size << endl; //磁盘空闲容量
if (disk) {
if (!strcmp(disk, dd)) {
get_size += total_size;
get_free_size += free_size;
}
}
else {
get_size += total_size;
get_free_size += free_size;
}
}
char drive_label[30];
char drive_fat[30];
DWORD drive_sn;
DWORD drive_name_size = sizeof(drive_label);
Flag = GetVolumeInformation(dd,
drive_label,
sizeof(drive_label),
&drive_sn,
&drive_name_size,
NULL,
drive_fat,
sizeof(drive_fat)
);
cout << "serial number: " << drive_sn << "\n\n" << endl;
}
}
}
return 0;
}
int main(int argc, char **argv)
{
DWORD64 total_size, total_free_size;
if (argc == 2) {
get_disk_capacity(total_size, total_free_size, argv[1]);
cout << "partion" << argv[1] << "total capacity is : " << total_size << "free capacity is: " << total_free_size << "G" << endl;
}
else {
get_disk_capacity(total_size, total_free_size);
cout << "Win system total capacity is : " << total_size / 1024 / 1024 /1024 << "G " << "free capacity is: " << total_free_size / 1024 / 1024 / 1024 << "G" << endl;
}
get_disk_capacity(total_size, total_free_size, "D:\\");
cout << "partion D:\\ total capacity is : " << total_size / 1024 / 1024 / 1024 << "G " << "free capacity is: " << total_free_size / 1024 / 1024 / 1024 << "G" << endl;
return 0;
}
unspecified partion, get all disk capacity
partion -->>: C:\
A fixed drive
disk attribution:
total clusters cnt: 28836351
Avail clusters cnt: 17486305
sectors cnt per cluster: 8
bytes per sector: 512
Total disk capacity: 118113693696
Disk free capacity: 71623905280
Total disk capacity: 118113693697
Disk free capacity: 71623905281
serial number: 747761994
partion -->>: D:\
A fixed drive
disk attribution:
total clusters cnt: 102399999
Avail clusters cnt: 100960971
sectors cnt per cluster: 8
bytes per sector: 512
Total disk capacity: 419430395904
Disk free capacity: 413536137216
Total disk capacity: 419430395905
Disk free capacity: 413536137217
serial number: 3906419263
partion -->>: E:\
A fixed drive
disk attribution:
total clusters cnt: 29770239
Avail clusters cnt: 27881970
sectors cnt per cluster: 8
bytes per sector: 512
Total disk capacity: 121938898944
Disk free capacity: 114204549120
Total disk capacity: 121938898945
Disk free capacity: 114204549121
serial number: 3659066362
partion -->>: F:\
A fixed drive
disk attribution:
total clusters cnt: 141789695
Avail clusters cnt: 141576879
sectors cnt per cluster: 8
bytes per sector: 512
Total disk capacity: 580770590720
Disk free capacity: 579898896384
Total disk capacity: 580770590721
Disk free capacity: 579898896385
serial number: 584913794
Win system total capacity is : 1155G free capacity is: 1098G
partion D:\ total capacity is : 390G free capacity is: 385G
email:[email protected] 画笔