【发布时间】:2019-10-16 15:27:32
【问题描述】:
当我运行df -h 时,我可以看到在/dev 我使用6M,大小为40M,可用大小为34M。
如何使用 c 代码获取此信息?
【问题讨论】:
-
您可以查看原始代码:df.c
当我运行df -h 时,我可以看到在/dev 我使用6M,大小为40M,可用大小为34M。
如何使用 c 代码获取此信息?
【问题讨论】:
来自here:
使用statvfs API:
// header for statvfs
#include <sys/statvfs.h>
statvfs的原型是
int statvfs(const char *path, struct statvfs *buf);
结果会填充到buf statvfs struct:
struct statvfs {
unsigned long f_bsize; /* filesystem 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; /* filesystem ID */
unsigned long f_flag; /* mount flags */
unsigned long f_namemax; /* maximum filename length */
};
返回类型为:
成功时返回零。出错时,返回 -1,并且 errno 为 适当设置。
还可以查看statvfs 命令的man3 联机帮助页以获取更多详细信息。
【讨论】:
你检查过source of df from Coreutils吗?
它在 Linux 以及其他类似 POSIX 的系统上使用 sys/statvfs.h 中的 statvfs()。
【讨论】: