Athanase
Athanase

Reputation: 943

Statfs return strange values

I'm currently trying to get disk space in byte en used space in byte with statfs(). I made a small function but i get really strange values. (I'm working on a ubuntu 32bit system)

Here is the code:

bool
CheckDiskSpace( const CLString &devPath, ulonglong &diskSize, ulonglong &totalFreeBytes )
{
bool retVal = false;
struct statfs fs;
if( ( statfs( devPath.c_str(), &fs ) ) < 0 ) {
    printf( "Failed to stat %s: %s\n", devPath.c_str() ,strerror( errno ) );
    return false;
} else {
    diskSize = fs.f_blocks * fs.f_bsize;
    totalFreeBytes = fs.f_bfree * fs.f_bsize;
    retVal = true;
}
return retVal;
}

int main()
{
  ulonglong diskSize, totalFreeBytes;
  CheckDiskSpace( "/dev/sda5", diskSize, totalFreeBytes );
  printf( "Disk size: %llu Byte\n", diskSize );
  printf( "Free size: %llu Byte\n", totalFreeBytes );
}

And I get:

Disk size: 1798447104 Byte
Free size: 1798443008 Byte

I do really not understand this result because with "df" command I get:

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda5            111148848  47454952  58047832  45% /
udev                   1756296         4   1756292   1% /dev
tmpfs                   705324       912    704412   1% /run
none                      5120         0      5120   0% /run/lock
none                   1763300      1460   1761840   1% /run/shm

Any help is very appeciated !

PS: I have a 120 GB ssd and my partition is in Ext4.

EDIT: ulonglong is a predifined type:

typedef unsigned long long ulonglong;

Upvotes: 2

Views: 4453

Answers (2)

Quentin Casasnovas
Quentin Casasnovas

Reputation: 1099

You are getting the result for the tmpfs mounted on /dev as the /dev/sda5 is on that filesystem. If you want to know for your root filesystem, just use / or any path not in /dev nor /run.

Upvotes: 3

fazo
fazo

Reputation: 1827

i think what you want is:

CheckDiskSpace( "/dev/sda5", &diskSize, &totalFreeBytes );

because diskSize and totalFreeBytes are not changed in CheckDiskSpace with your code

Upvotes: 0

Related Questions