现代c++获取linux系统磁盘大小
现代c++获取linux系统磁盘大小
- 前言
- 一、命令获取系统磁盘大小
- 二、使用c++获取系统磁盘大小
- 三、总结
前言
本文介绍一种使用c++
获取linux
系统磁盘大小的方法
一、命令获取系统磁盘大小
在linux
系统中可以使用lsblk
命令显示当前系统磁盘大小,如下图所示
lsblk
二、使用c++获取系统磁盘大小
-
在使用
c++
获取磁盘大小之前我们先了解一下linux
下的/sys/block
目录,有兴趣的可以去详细了解下,我这里就不卖关子了,首先呢/sys/block
目录存放的是所有的块设备文件,也就是系统磁盘分区。如下图所示
-
上图中的每一个子目录如
loop0
、mmcblk1
、zram0
等都是系统上的磁盘分区文件,也就是说每一个子目录都是一个磁盘分区设备,可以理解为当前系统有子目录这么多的块设备,如果说我们把所有块设备的大小加起来不就是我们磁盘大小了吗,没错,我们就是要这样干。 -
首先,每个块设备都是一个目录,这个目录下存放的有该块设备的一些信息,如下图所示
-
我们重点关注的是这个
size
文件,这个文件中存放的是这个块设备所占有的磁盘扇区数(注意不是字节数,是扇区数量)
如下图所示
-
接下来我们要知道一个扇区多少个字节呢,这个是存放在每个块设备目录下的
queue/hw_sector_size
里的
-
接下来我们要知道这些块设备哪些是虚拟的块设备,哪些是实际的块设备,有一个典型的区分方法,就是如果是虚拟的块设备,它的子目录下通常没有
device
目录,而实际设备是有device
目录的
-
接下来我们整理一下思路
- 收集
/sys/block
下的所有子目录名称(块设备) - 根据块设备目录下是否有
device
目录来判断它是否是虚拟设备,排除所有的虚拟设备,得到实际块设备 - 读取实际块设备目录下的
size
文件获取该块设备占用的扇区数。 - 读取实际块设备目录下的
queue/hw_sector_size
获取一个扇区占用多少字节。 - 得到一个(实际块设备占用的字节数) = (该块设备占用扇区数) * (该块设备扇区占用字节数)。
- 遍历所有的实际块设备得到他们占用的字节数,再相加,就是系统磁盘大小。
- 收集
-
先写一些辅助方法,如判断文件或者文件夹是否存在的方法, 如果文件中只有一个数字,获取该数字的方法
#include <string>
#include <filesystem> // 需要c++17支持
#include <fstream>
bool directIsExists(const std::string &path) {return std::filesystem::exists(path);
}std::size_t getFileNumber(const std::string &path) {std::size_t number = 0;std::ifstream ifs(path);if (!ifs.is_open()) {return number;}ifs >> number;return number;
}
如果不支持c++17,可以使用下面方法平替
#include <string>
#include <unistd.h>
#include <fstream>
bool directIsExists(const std::string &path) {return access(path.c_str(), F_OK) == 0;
}std::size_t getFileNumber(const std::string &path) {std::size_t number = 0;std::ifstream ifs(path);if (!ifs.is_open()) {return number;}ifs >> number;return number;
}
- 收集
/sys/block
目录下的所有实际块设备
#include <string>
#include <iostream>
#include <filesystem>
#include <vector>
std::vector<std::string> getBlockDevices(void) {std::vector<std::string> subdirs;try {for (const auto &entry : std::filesystem::directory_iterator("/sys/block")) {if (std::filesystem::is_directory(entry.status())) {if (directIsExists("/sys/block/" + entry.path().filename().string() + "/device")) {subdirs.push_back(entry.path().filename().string());}}}}catch (const std::filesystem::filesystem_error &e) {std::cerr << "Error accessing : " << e.what() << std::endl;}return subdirs;
}
如果不支持c++17,可以使用下面方法平替
#include <vector>
#include <string>
#include <iostream>
#include <dirent.h>
std::vector<std::string> getBlockDevices() {std::vector<std::string> subdirs;DIR *dir = opendir("/sys/block");if (dir == nullptr) {perror("opendir");return subdirs;}struct dirent *entry;while ((entry = readdir(dir)) != nullptr) {if (std::string(entry->d_name) != "." && std::string(entry->d_name) != "..") {if (directIsExists("/sys/block/" + std::string(entry->d_name) + "/device")) {subdirs.push_back(entry->d_name);}}}closedir(dir);return subdirs;
}
- 获取指定块设备的扇区数
std::size_t getSectorNumber(const std::string &path) {return getFileNumber(path);
}
- 获取指定块设备的扇区的大小
std::size_t getSectorSize(const std::string &path) {return getFileNumber(path);
}
- 获取总大小
std::size_t getDiskSize(void) {std::size_t number = 0;for (const auto &it : getBlockDevices()) {std::cout << it << std::endl;std::cout << getSectorNumber("/sys/block/" + it + "/size") << std::endl;std::cout << getSectorSize("/sys/block/" + it + "/queue/hw_sector_size") << std::endl;number += getSectorNumber("/sys/block/" + it + "/size") * getSectorSize("/sys/block/" + it + "/queue/hw_sector_size");}return number;
}
完整代码如下
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <unistd.h>
#include <vector>
bool directIsExists(const std::string &path) {return std::filesystem::exists(path);
}std::vector<std::string> getBlockDevices(void) {std::vector<std::string> subdirs;try {for (const auto &entry : std::filesystem::directory_iterator("/sys/block")) {if (std::filesystem::is_directory(entry.status())) {if (directIsExists("/sys/block/" + entry.path().filename().string() + "/device")) {subdirs.push_back(entry.path().filename().string());}}}}catch (const std::filesystem::filesystem_error &e) {std::cerr << "Error accessing : " << e.what() << std::endl;}return subdirs;
}// #include <dirent.h>// bool directIsExists(const std::string &path) {
// return access(path.c_str(), F_OK) == 0;
// }
// std::vector<std::string> getBlockDevices() {
// std::vector<std::string> subdirs;// DIR *dir = opendir("/sys/block");
// if (dir == nullptr) {
// perror("opendir");
// return subdirs;
// }// struct dirent *entry;
// while ((entry = readdir(dir)) != nullptr) {
// if (std::string(entry->d_name) != "." && std::string(entry->d_name) != "..") {
// if (directIsExists("/sys/block/" + std::string(entry->d_name) + "/device")) {
// subdirs.push_back(entry->d_name);
// }
// }
// }// closedir(dir);
// return subdirs;
// }std::size_t getFileNumber(const std::string &path) {std::size_t number = 0;std::ifstream ifs(path);if (!ifs.is_open()) {return number;}ifs >> number;return number;
}std::size_t getSectorNumber(const std::string &path) {return getFileNumber(path);
}std::size_t getSectorSize(const std::string &path) {return getFileNumber(path);
}std::size_t getDiskSize(void) {std::size_t number = 0;for (const auto &it : getBlockDevices()) {number += getSectorNumber("/sys/block/" + it + "/size") * getSectorSize("/sys/block/" + it + "/queue/hw_sector_size");}return number;
}int main(int argc, char **argv) {auto num = getDiskSize();std::cout << "bytes --> " << num << std::endl;std::cout << num / 1024.0 << "kb" << std::endl;std::cout << num / 1024.0 / 1024.0 << "Mb" << std::endl;std::cout << num / 1024.0 / 1024.0 / 1024.0 << "Gb" << std::endl;return 0;
}
编译并执行, g++ main.cpp -o main -std=c++17 && ./main
, 执行结果如下
三、总结
通过上诉方法确实可以获取磁盘容量,亲测可用!!!