c++ - 如何以编程方式获取 Linux 中目录的可用磁盘空间

是否有一个函数可以返回给定目录路径的驱动器分区上有多少可用空间?

最佳答案

检查 man statvfs(2)

我相信您可以将“可用空间”计算为 f_bsize * f_bfree

NAME
       statvfs, fstatvfs - get file system statistics

SYNOPSIS
       #include <sys/statvfs.h>

       int statvfs(const char *path, struct statvfs *buf);
       int fstatvfs(int fd, struct statvfs *buf);

DESCRIPTION
       The function statvfs() returns information about a mounted file system.
       path is the pathname of any file within the mounted file  system.   buf
       is a pointer to a statvfs structure defined approximately as follows:

           struct statvfs {
               unsigned long  f_bsize;    /* file system block size */
               unsigned long  f_frsize;   /* fragment size */
               fsblkcnt_t     f_blocks;   /* size of fs in f_frsize units */
               fsblkcnt_t     f_bfree;    /* # free blocks */
               fsblkcnt_t     f_bavail;   /* # free blocks for unprivileged users */
               fsfilcnt_t     f_files;    /* # inodes */
               fsfilcnt_t     f_ffree;    /* # free inodes */
               fsfilcnt_t     f_favail;   /* # free inodes for unprivileged users */
               unsigned long  f_fsid;     /* file system ID */
               unsigned long  f_flag;     /* mount flags */
               unsigned long  f_namemax;  /* maximum filename length */
           };

https://stackoverflow.com/questions/3992171/

相关文章:

MySQL - 错误 1045 - 访问被拒绝

linux - 在文件夹中的 gzip 文件中查找字符串

linux - 单个文件卷挂载为 Docker 中的目录

python - 如何使用python2.7 pip而不是默认pip

linux - 如何让 tcpdump 写入文件和标准输出适当的数据?

linux - 如何找出我在 Amazon EC2 上的存储空间已满的原因?

linux - 如何在没有任何用户配置的情况下启动 shell?

linux - Ubuntu:使用 curl 下载图像

linux - 如何在 Linux 中使用单行命令获取 Java 版本

linux - Gnome-terminal - 如何自动将选择复制到剪贴板?