DBUS获取USB存储器的可用磁盘空间

DBUS获取USB存储器的可用磁盘空间

我正在尝试使用 DBUS 连接器 ( ) 确定 USB 存储设备上有多少可用磁盘空间sdbus-c++。为了接收 USB 存储器连接状态的信号,我使用该org.freedesktop.UDisks2服务。

这工作得很好,但我找不到任何带有“可用磁盘空间”信息的属性。

我是否需要使用其他服务来实现此功能,或者如何获取此信息?目前我使用 Debian 系统,但在第一次测试后我将切换到嵌入式 busybox 系统(以防万一此信息很重要)。

答案1

Udisks 真正致力于处理存储设备的硬件方面,直至安装分区/卷。这些分区上有哪些文件系统以及它们有多少可用空间,有点超出了 Udisks2 的范围。您可能使用 Udisk 挂载的某些文件系统甚至不理解“可用空间”的概念(例如 ISO9669)

从我的头脑中,经过对可用 DBUS 接口的一些研究,我认为 DBUS 不会暴露“自由空间”属性。好吧,虽然很老套,但有效的解决方法就是 execing df /path/to/mount。 (许多文件系统需要安装,即使只是只读的,以估计可用空间量。顺便说一句,对于许多文件系统来说,可用空间和已用空间在幕后是非常复杂的。)

现在,特别是如果您稍后将其缩小到基于 busybox 的最小系统,您可能不想使用 GNU coreutildf执行来读取您需要从文本中解析的内容,只是为了获取您自己可以获得的信息。看到你正在做 C++(我的想法,甚至没有尝试编译它),并假设你没有 C++17std::filesystem::space,这当然比使用 DBUS 开始要少得多的麻烦:

#include <sys/statvfs.h>

std::string mountpoint;
/*
 * Get the mount point using org.freedesktop.UDisks2.Filesystem's "MountPoints" property, see
 * http://storaged.org/doc/udisks2-api/latest/gdbus-org.freedesktop.UDisks2.Filesystem.html#gdbus-property-org-freedesktop-UDisks2-Filesystem.MountPoints
 * We'll act as if you already did this and it was stored in `mountpoint`.
 */

// Terrible naming choices made the structure typename be the same as the function name, so even in C++ we have to use `struct`. Wah.
struct statvfs filesystem_info;
statvfs(mountpoint.c_str(), &filesystem_info);
auto bytes_per_block = filesystem_info.f_bsize;
auto blocks_free_for_unprivileged_users = filesystem_info.f_bavail;
auto blocks_free_total = filesystem_info.f_bfree;

相关内容