views:

1493

answers:

2

I am using statfs() which gives me the free blocks available to a non-superuser.

I am unsure how to convert this into KB/MB/GB.

The values that are returned are:

fundamental file system block size: 4096
total data blocks in file system: 3805452
free blocks in fs: 63425
free blocks avail to non-superuser: 63425
total file nodes in file system: 3805450
free file nodes in fs: 63425

The value I am interested is saying 63425, but I am not sure what that means KB/MB/GB wise.

I am running this on the iPhone and an application is supposed to have access to 2GB I believe, if that much is open on the device, which in my case I do.

So I should be getting a value somewhere around 2G, I would think.

I ran 63425 against an online blocks to MB converter but that gives me a result of 30MB which shouldn't be the case.

Can anyone help me in figuring out how to get KB/MB/GB from this info?

Thanks.

+2  A: 

Block size is 4096 bytes, or 4KB. Thus 63425 blocks is 63425 * 4KB = 253,700KB, which is roughly 248MB.

tvanfosson
+1  A: 

I am having success using the following:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

struct statfs tStats;

statfs([[paths lastObject] cString], &tStats);

unsigned long long Available = ((unsigned long long)tStats.f_bavail) * ((unsigned long long)tStats.f_bsize);

if (Available > 1024)
{
 //Kilobytes
 Available = Available / 1024;

 diskSpaceLbl.text = [[@"Available Disk Space: " stringByAppendingFormat:@"%llu", Available] stringByAppendingString:@" KB"];
}

if (Available > 1024)
{
 //Megabytes
 Available = Available / 1024;

 diskSpaceLbl.text = [[@"Available Disk Space: " stringByAppendingFormat:@"%llu", Available] stringByAppendingString:@" MB"];
}

if (Available > 1024)
{
 //Gigabytes
 Available = Available / 1024;

 diskSpaceLbl.text = [[@"Available Disk Space: " stringByAppendingFormat:@"%llu", Available] stringByAppendingString:@" GB"];
}
kdbdallas