如何以编程方式在Linux中获取安装点的源设备
How to get source device of mount point in Linux programmatically?
我想知道安装在某些目录上的哪个设备,例如:
auto device = get_device_of_mount_point("/path/to/some/dir");
std::cout << device << std::endl; // /dev/sda1
这是一个起点,假设有C 17可用:
#include <string_view>
#include <fstream>
#include <optional>
std::optional<std::string> get_device_of_mount_point(std::string_view path)
{
std::ifstream mounts{"/proc/mounts"};
std::string mountPoint;
std::string device;
while (mounts >> device >> mountPoint)
{
if (mountPoint == path)
{
return device;
}
}
return std::nullopt;
}
您可以按以下方式使用此功能。
if (const auto device = get_device_of_mount_point("/"))
std::cout << *device << "n";
else
std::cout << "Not foundn";
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 如何在何处获取 MSVS 2008(32 位)编译器工具链的编译器,而无需安装 MSVS2008
- 如何以编程方式在Linux中获取安装点的源设备
- 无法获取包含文件'excpt.h'的视觉工作室C++以进行安装
- 在Windows中获取安装前缀的最佳方法是什么
- 从已安装的窗口光栅(位图)字体直接获取位图
- 如何在 Debian 上安装所有 .debs 后获取 WxWidgets setup.h
- 如何获取在 Windows 7 中安装的主题列表(主题名称)
- 在进行安装时,构建应用程序获取 ISO c 不允许命名可变
- C++ WinAPI - 如何从可执行文件的名称中获取其安装路径
- 如何在安装驱动程序之前获取 C++ 中的 GPU 信息
- 我已经安装了 GPERFtools-2.0,但我无法获取 CPU 配置文件统计信息
- 使用qmake安装文件:如何获取可执行文件
- GCC规范文件:如何获取安装路径
- 从安装api获取SATA HBA的列表
- 获取已安装应用程序的列表
- 使用c++获取Windows 7中已安装程序的完整路径
- 如何使用c++语言以编程方式获取已安装的所有Java JRE(JVM)及其路径
- 获取给定应用程序的安装目录
- c++ Windows 7获取已安装驱动程序的符号名