如何通过filesystem实战查询硬盘分区挂载点及剩余空间详情?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1295个文字,预计阅读时间需要6分钟。
由于 `std::filesystem::space` 接收的是路径,但它实际查询的是该路径所在 挂载点(mount point) 的文件系统信息,而不是路径本身的剩余空间。因此,它返回的是挂载点的文件系统总空间、已用空间和可用空间,而不是路径本身的剩余空间。
常见错误是误以为它能递归计算目录占用,其实它底层调用的是 POSIX statvfs()(Linux/macOS)或 GetDiskFreeSpaceEx()(Windows),只和挂载点绑定。
- 必须传入一个**实际存在于目标分区上的有效路径**(哪怕只是
"/"、"/boot"、"/mnt/data") - 若路径不存在或无权限访问,
std::filesystem::space会抛出std::filesystem::filesystem_error -
available字段才是用户真正可用的剩余字节数(已扣除 root 预留空间);free是所有非 root 用户可用量;capacity是总容量
try { auto info = std::filesystem::space("/mnt/backup"); std::cout << "Total: " << info.capacity << " " << "Free: " << info.free << " " << "Avail: " << info.available << " "; } catch (const std::filesystem::filesystem_error& e) { std::cerr << "Failed to query space: " << e.what() << " "; }
如何枚举系统上所有挂载点(Linux/macOS)?
C++ 标准库不提供挂载点枚举接口,需依赖系统 API。Linux 下最直接的方式是解析 /proc/mounts(或更可靠的 /proc/self/mounts),每行格式为:device name mount_point fs_type options dump pass。注意过滤掉虚拟文件系统(如 proc、sysfs、devtmpfs)。
- 优先读取
/proc/self/mounts,比/proc/mounts更稳定(避免被其他进程修改) - 跳过以
none、tmpfs、devtmpfs、debugfs等开头的fs_type - 挂载点路径需用
std::filesystem::path标准化(处理..、重复/),再传给space() - 某些挂载点可能无读权限(如
/boot/efi),需捕获异常并跳过
std::vector<std::pair<std::string, std::string>> get_mount_points() { std::vector<std::pair<std::string, std::string>> mounts; std::ifstream f("/proc/self/mounts"); std::string line; while (std::getline(f, line)) { std::istringstream iss(line); std::string dev, mp, fstype; iss >> dev >> mp >> fstype; if (fstype == "ext4" || fstype == "xfs" || fstype == "btrfs" || fstype == "ntfs" || fstype == "vfat") { mounts.emplace_back(mp, fstype); } } return mounts; }
Windows 下怎么拿到每个驱动器盘符的剩余空间?
Windows 没有“挂载点”概念,而是以驱动器盘符(C:、D:)为单位。直接用 std::filesystem::space 传入盘符根路径即可,但要注意格式:必须带冒号和反斜杠,即 "C:",不能写成 "C:" 或 "C:/"(后者在 Windows 上可能被解释为相对路径,导致查到当前工作目录所在盘符)。
立即学习“C++免费学习笔记(深入)”;
-
"C:\\"是最安全的写法(双反斜杠转义后为单个) - 遍历所有可能盘符可调用
GetLogicalDrives()(Windows API),或硬编码A–Z并用std::filesystem::exists("X:\")过滤无效盘符 - NTFS 卷支持压缩/配额,
available已自动反映用户配额限制
for (char c = 'A'; c <= 'Z'; ++c) { std::string root = std::string(1, c) + ":\"; if (!std::filesystem::exists(root)) continue; try { auto s = std::filesystem::space(root); std::cout << root << ": " << s.available / (1024*1024) << " MiB available "; } catch (...) { /* skip inaccessible drives */ } }
跨平台获取挂载点信息时最容易忽略的三个细节
一是 std::filesystem::space 返回的字节数是 uintmax_t,在 32 位环境或旧编译器下可能溢出,打印前务必转成 double 或使用 std::to_string 避免截断;二是 Linux 下某些挂载点(如 bind mount、overlayfs)可能返回与父挂载点相同的空间数据,无法区分逻辑隔离;三是 macOS 的 apfs 卷共享物理空间,space() 返回的是整个 APFS 容器的统计值,不是单个卷独占容量——这意味着你看到的 “/Volumes/Data” 剩余空间,可能和 “/” 完全一致。
- 永远检查异常,不要假设路径一定可访问
- 对用户展示时,优先用
available而非free,它才是真正能写的字节数 - 若需精确到目录级磁盘使用(非剩余空间),得用
std::filesystem::recursive_directory_iterator手动累加,和space()完全是两回事
本文共计1295个文字,预计阅读时间需要6分钟。
由于 `std::filesystem::space` 接收的是路径,但它实际查询的是该路径所在 挂载点(mount point) 的文件系统信息,而不是路径本身的剩余空间。因此,它返回的是挂载点的文件系统总空间、已用空间和可用空间,而不是路径本身的剩余空间。
常见错误是误以为它能递归计算目录占用,其实它底层调用的是 POSIX statvfs()(Linux/macOS)或 GetDiskFreeSpaceEx()(Windows),只和挂载点绑定。
- 必须传入一个**实际存在于目标分区上的有效路径**(哪怕只是
"/"、"/boot"、"/mnt/data") - 若路径不存在或无权限访问,
std::filesystem::space会抛出std::filesystem::filesystem_error -
available字段才是用户真正可用的剩余字节数(已扣除 root 预留空间);free是所有非 root 用户可用量;capacity是总容量
try { auto info = std::filesystem::space("/mnt/backup"); std::cout << "Total: " << info.capacity << " " << "Free: " << info.free << " " << "Avail: " << info.available << " "; } catch (const std::filesystem::filesystem_error& e) { std::cerr << "Failed to query space: " << e.what() << " "; }
如何枚举系统上所有挂载点(Linux/macOS)?
C++ 标准库不提供挂载点枚举接口,需依赖系统 API。Linux 下最直接的方式是解析 /proc/mounts(或更可靠的 /proc/self/mounts),每行格式为:device name mount_point fs_type options dump pass。注意过滤掉虚拟文件系统(如 proc、sysfs、devtmpfs)。
- 优先读取
/proc/self/mounts,比/proc/mounts更稳定(避免被其他进程修改) - 跳过以
none、tmpfs、devtmpfs、debugfs等开头的fs_type - 挂载点路径需用
std::filesystem::path标准化(处理..、重复/),再传给space() - 某些挂载点可能无读权限(如
/boot/efi),需捕获异常并跳过
std::vector<std::pair<std::string, std::string>> get_mount_points() { std::vector<std::pair<std::string, std::string>> mounts; std::ifstream f("/proc/self/mounts"); std::string line; while (std::getline(f, line)) { std::istringstream iss(line); std::string dev, mp, fstype; iss >> dev >> mp >> fstype; if (fstype == "ext4" || fstype == "xfs" || fstype == "btrfs" || fstype == "ntfs" || fstype == "vfat") { mounts.emplace_back(mp, fstype); } } return mounts; }
Windows 下怎么拿到每个驱动器盘符的剩余空间?
Windows 没有“挂载点”概念,而是以驱动器盘符(C:、D:)为单位。直接用 std::filesystem::space 传入盘符根路径即可,但要注意格式:必须带冒号和反斜杠,即 "C:",不能写成 "C:" 或 "C:/"(后者在 Windows 上可能被解释为相对路径,导致查到当前工作目录所在盘符)。
立即学习“C++免费学习笔记(深入)”;
-
"C:\\"是最安全的写法(双反斜杠转义后为单个) - 遍历所有可能盘符可调用
GetLogicalDrives()(Windows API),或硬编码A–Z并用std::filesystem::exists("X:\")过滤无效盘符 - NTFS 卷支持压缩/配额,
available已自动反映用户配额限制
for (char c = 'A'; c <= 'Z'; ++c) { std::string root = std::string(1, c) + ":\"; if (!std::filesystem::exists(root)) continue; try { auto s = std::filesystem::space(root); std::cout << root << ": " << s.available / (1024*1024) << " MiB available "; } catch (...) { /* skip inaccessible drives */ } }
跨平台获取挂载点信息时最容易忽略的三个细节
一是 std::filesystem::space 返回的字节数是 uintmax_t,在 32 位环境或旧编译器下可能溢出,打印前务必转成 double 或使用 std::to_string 避免截断;二是 Linux 下某些挂载点(如 bind mount、overlayfs)可能返回与父挂载点相同的空间数据,无法区分逻辑隔离;三是 macOS 的 apfs 卷共享物理空间,space() 返回的是整个 APFS 容器的统计值,不是单个卷独占容量——这意味着你看到的 “/Volumes/Data” 剩余空间,可能和 “/” 完全一致。
- 永远检查异常,不要假设路径一定可访问
- 对用户展示时,优先用
available而非free,它才是真正能写的字节数 - 若需精确到目录级磁盘使用(非剩余空间),得用
std::filesystem::recursive_directory_iterator手动累加,和space()完全是两回事

