Domanda

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.

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top