df和statfs的输出不同

时间:2014-02-11 05:13:48

标签: c system-calls

为什么df命令和statfs()系统调用值的输出不同:

调用statfs的程序:

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <string.h>
#include <stdlib.h>


int main()
{
    struct statfs vfs;

    if (statfs("/", & vfs) != 0) {
        fprintf(stderr, "%s: statfs failed: %s\n",
            "/", strerror(errno));
        exit(0);
    }

    printf("mounted on %s:\n","/");

    printf("\tf_bsize: %ld\n", vfs.f_bsize);
    printf("\tf_blocks: %ld\n", vfs.f_blocks);
    printf("\tf_bfree: %ld\n", vfs.f_bfree);
    printf("\tf_bavail: %ld\n", vfs.f_bavail);
    printf("\tf_files: %ld\n", vfs.f_files);
    printf("\tf_ffree: %ld\n", vfs.f_ffree);

    return 0;
}

输出:

    mounted on /:
        f_bsize: 4096
        f_blocks: 119189762
        f_bfree: 112718672
        f_bavail: 106662506
        f_files: 30285824
        f_ffree: 29990111

输出df命令:

    ~$ df /
    Filesystem     1K-blocks     Used Available Use% Mounted on
    /dev/sda1      476759048 25882620 426651764   6% /

df命令在内部调用statfs系统调用本身,但为什么输出结构值和df命令的输出不同,任何人都可以清楚地解释我。

1 个答案:

答案 0 :(得分:0)

df的数据可能基于f_bavail,而不是f_bfree因此,计算总空间和可用空间可能如下

long long Total_Space = vfs.f_blocks;
Total_Space *= vfs.f_frsize;
Total_Space /= 1024;
long long Avail_Space = vfs.f_bfree;
Avail_Space *= vfs.f_frsize;
Avail_Space /= 1024;

printf("Total Space=%lldKb Available Space=%lldKB\n",Total_Space,Avail_Space);