differ in output of df and statfs

穿精又带淫゛_ 提交于 2019-12-11 08:51:48

问题


why is the output of df command and statfs() system call values are different:

program to call 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;
}

output:

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

output of df command:

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

df command internally calls the statfs systemcall itself, but why is the output is different on structure values and the output of df command, can anyone explain me clearly.


回答1:


df's data may be based on f_bavail, not f_bfree So for calculation of total space and available space may be as follows

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);


来源:https://stackoverflow.com/questions/21693673/differ-in-output-of-df-and-statfs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!