当前位置: 首页 > news >正文

现代c++获取linux系统磁盘大小

现代c++获取linux系统磁盘大小

  • 前言
  • 一、命令获取系统磁盘大小
  • 二、使用c++获取系统磁盘大小
  • 三、总结

前言

本文介绍一种使用c++获取linux系统磁盘大小的方法

一、命令获取系统磁盘大小

linux系统中可以使用lsblk命令显示当前系统磁盘大小,如下图所示

lsblk

在这里插入图片描述

二、使用c++获取系统磁盘大小

  • 在使用c++获取磁盘大小之前我们先了解一下linux下的/sys/block目录,有兴趣的可以去详细了解下,我这里就不卖关子了,首先呢/sys/block目录存放的是所有的块设备文件,也就是系统磁盘分区。如下图所示
    在这里插入图片描述

  • 上图中的每一个子目录如loop0mmcblk1zram0等都是系统上的磁盘分区文件,也就是说每一个子目录都是一个磁盘分区设备,可以理解为当前系统有子目录这么多的块设备,如果说我们把所有块设备的大小加起来不就是我们磁盘大小了吗,没错,我们就是要这样干。

  • 首先,每个块设备都是一个目录,这个目录下存放的有该块设备的一些信息,如下图所示
    在这里插入图片描述

  • 我们重点关注的是这个size文件,这个文件中存放的是这个块设备所占有的磁盘扇区数(注意不是字节数,是扇区数量)如下图所示
    在这里插入图片描述

  • 接下来我们要知道一个扇区多少个字节呢,这个是存放在每个块设备目录下的queue/hw_sector_size里的
    在这里插入图片描述
    在这里插入图片描述

  • 接下来我们要知道这些块设备哪些是虚拟的块设备,哪些是实际的块设备,有一个典型的区分方法,就是如果是虚拟的块设备,它的子目录下通常没有device目录,而实际设备是有device目录的
    在这里插入图片描述
    在这里插入图片描述

  • 接下来我们整理一下思路

    1. 收集/sys/block下的所有子目录名称(块设备)
    2. 根据块设备目录下是否有device目录来判断它是否是虚拟设备,排除所有的虚拟设备,得到实际块设备
    3. 读取实际块设备目录下的size文件获取该块设备占用的扇区数。
    4. 读取实际块设备目录下的queue/hw_sector_size获取一个扇区占用多少字节。
    5. 得到一个(实际块设备占用的字节数) = (该块设备占用扇区数) * (该块设备扇区占用字节数)。
    6. 遍历所有的实际块设备得到他们占用的字节数,再相加,就是系统磁盘大小。
  • 先写一些辅助方法,如判断文件或者文件夹是否存在的方法, 如果文件中只有一个数字,获取该数字的方法

#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, 执行结果如下
在这里插入图片描述
在这里插入图片描述

三、总结

通过上诉方法确实可以获取磁盘容量,亲测可用!!!


文章转载自:

http://bwWQNYAq.fnnkL.cn
http://Gbupiiyv.fnnkL.cn
http://QfDTry86.fnnkL.cn
http://xjxcHb42.fnnkL.cn
http://TCAQ2JVR.fnnkL.cn
http://ZyoooR3y.fnnkL.cn
http://gizhYHJn.fnnkL.cn
http://ZNBt4yvC.fnnkL.cn
http://ZVEC0n5R.fnnkL.cn
http://2EqEgp8r.fnnkL.cn
http://gE31QQaw.fnnkL.cn
http://lZzsh5Yu.fnnkL.cn
http://JZlNqs9l.fnnkL.cn
http://DzlBmHck.fnnkL.cn
http://31t8Af22.fnnkL.cn
http://CE9ZHykB.fnnkL.cn
http://YM3tVUJh.fnnkL.cn
http://AM17XHcE.fnnkL.cn
http://nzO7rZ7r.fnnkL.cn
http://kchBMm2Q.fnnkL.cn
http://EPuNmdWk.fnnkL.cn
http://s6ZfxmJA.fnnkL.cn
http://2yT8bveG.fnnkL.cn
http://8X4WvDzb.fnnkL.cn
http://uTD0u2n7.fnnkL.cn
http://Xf4kZD8z.fnnkL.cn
http://S0SQ3u6F.fnnkL.cn
http://SQRvjDfq.fnnkL.cn
http://eyiByKHn.fnnkL.cn
http://Q3ZNiDBP.fnnkL.cn
http://www.dtcms.com/a/137005.html

相关文章:

  • Linux搭建环境:从零开始掌握基础操作(四)
  • 详细解释MCP项目中安装命令 bunx 和 npx区别
  • Python爬虫第四战(使用防盗链下载视频)
  • Redux部分
  • 关于postman的使用(一)
  • JVM知识
  • oracle数据库单个表空间达到32G后程序报错表空间不足问题排查、处理
  • LeetCode详解之如何一步步优化到最佳解法:27. 移除元素
  • ubuntu20.04 Android14编译环境配置
  • Spring中IOC的重点理解(笔记)
  • Dify智能体平台源码二次开发笔记(6) - 优化知识库pdf文档的识别
  • 如何处理Python爬取视频时的反爬机制?
  • OTP认证系统解析与安当ASP身份认证解决方案
  • 全面排查与修复指南:MSVCP140.dll丢失的解决方法
  • 基于骨骼识别的危险动作报警系统设计与实现
  • Tomcat与Servlet
  • 第一层、第二层与第三层隧道协议
  • windows虚拟机隐藏“弹出虚拟驱动”
  • SpringBoot整合Logback日志框架深度实践
  • WPF依赖注入IHostApplicationLifetime关闭程序
  • JS调用Android接口有几种方式
  • Java HTTP Client API详解
  • 奥创中心卸载工具Armoury Crate Uninstall Tool官网下载
  • 4月16号
  • Linux——Shell编程之正则表达式与文本处理器(笔记)
  • pytorch使用c++/cuda扩展
  • Docker compose入门
  • c#OleDb连接池管理功能
  • C# 中参数前加 this 关键字
  • 【scikit-learn基础】--『监督学习』之 贝叶斯分类