Search code examples
objective-cmacoscocoansfilemanager

Getting wrong Available Startup Disk Space for Mac 10.6 and above


I am using following code to get the available disk space of the startup disk.

NSFileManager *fm = [NSFileManager defaultManager];

double freeSpace = 0.0;
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfFileSystemForPath:@"/" error:nil];
if (!error) {
    freeSpace = [[attr objectForKey:NSFileSystemFreeSize] doubleValue];
}
//Convert from bytes to GB.
freeSpace = freeSpace/(1024*1024*1024);

But for MacOS 10.6 and above, it is giving the wrong size. After a while, I found out that for 10.5, we need to divide it by (1024*1024*1024) to get the correct available size. And for 10.6 and above, I have to divide it by (1000*1000*1000) to get correct size (which is displayed in Finder when we do right click on HardDisk -> Get Info). Now, I don't think dividing by 1000 instead of 1024 is the correct way for converting from KB to MB or whatever. So why is it like this in Mac 10.6 and above?

Is there any other function through which I can get available startup disk space correct upto 2 decimal points?


Solution

  • Disk drives, except some types of floppy drives, have always been described and sold in terms of powers of 1000. Memory has used powers of 1024, traditionally (ab)using the SI power-of-1000 prefixes. Programmers have tended to follow the memory convention, but it has never been universal and unambiguous.

    In 1999, the IEC recommended using SI prefixes (kilo, mega etc.) only for powers of 1000, and using a new set of prefixes for powers of 1024. Since 10.6, Mac OS X follows this recommendation for disks and files – thus more closely matching the advertised size of disks – but continues to use SI prefixes for powers of 1024 for RAM.