statfs on macOS doesn't give me full disk capacity. Why?

67 Views Asked by At

I'm trying to get the storage drive capacity and the free space on it from my macOS app.

I do the following:

struct statfs stStg;

if(statfs(".", &stStg) == 0)
{
    uint64_t total_sz = stStg.f_bsize * stStg.f_blocks;
    uint64_t free_sz = stStg.f_bsize * stStg.f_bfree;

    double fTotal = (double)total_sz / (1024.0 * 1024.0 * 1024.0);
    double fFree = (double)free_sz / (1024.0 * 1024.0 * 1024.0);

    printf(""%.02f GB (%.01f%% free)"
                 ,
                 fTotal,
                 100.0 * fFree / fTotal);
}

It gives me:

460.43 GB (66.5% free)

But when I check it in Finder, I'm getting a larger capacity:

enter image description here

What am I doing wrong?

1

There are 1 best solutions below

3
jabaa On BEST ANSWER

1024.0 * 1024.0 * 1024.0 B is GiB, not GB. These are two different units. GB is 1000 * 1000 * 1000 B.

1024 * 1024 * 1024 / (1000 * 1000 * 1000) * 460.43 GiB = 494.38 GB

For more information, see Wikipedia