5

Does anyone know what apis Apple is using for it's Get Info panel to determine free space in Lion? All of the code I have tried to get the same Available Space that Apple is reporting is failing, even Quick Look isn't displaying the same space that Get Info shows. This seems to happen if I delete a bunch of files and attempt to read available space.

Incorrect free space on Lion

When I use NSFileManager -> NSFileSystemFreeSize I get 42918273024 bytes

When I use NSURL -> NSURLVolumeAvailableCapacityKey i get 42918273024 bytes

When I use statfs -> buffer.f_bsize * buffer.f_bfree i get 43180417024 bytes

statfs gets similar results to Quick Look, but how do I match Get Info?

seanalltogether
  • 3,542
  • 3
  • 26
  • 24
  • It may be the difference between base 2 and base 10 calculations. I thought that apple changed to base 10 for storage space system wide in SL, but I dunno why else they would be that far off. They might be slightly different because of swap, caches, and the screenshots – Josh The Geek Jul 23 '12 at 20:16
  • I don't remember the formula for converting between the two. – Josh The Geek Jul 23 '12 at 20:17
  • 1
    Mountain Lion added NSByteCountFormatter which may help you out. see https://developer.apple.com/library/mac///#/library/mac/documentation/Foundation/Reference/NSByteCountFormatter_Class/Reference/Reference.html – Jay Wardell Aug 02 '12 at 03:11

2 Answers2

1

You are probably seeing a result of local Time Machine snapshot backups. The following quotes are from the following Apple Support article - OS X Lion: About Time Machine's "local snapshots" on portable Macs:

Time Machine in OS X Lion includes a new feature called "local snapshots" that keeps copies of files you create, modify or delete on your internal disk. Local snapshots compliment regular Time Machine backups (that are stored on your external disk or Time Capsule) giving you a "safety net" for times when you might be away from your external backup disk or Time Capsule and accidentally delete a file.

The article finishes by saying:

Note: You may notice a difference in available space statistics between Disk Utility, Finder, and Get Info inspectors. This is expected and can be safely ignored. The Finder displays the available space on the disk without accounting for the local snapshots, because local snapshots will surrender their disk space if needed.

It looks like all the programmatic methods of measuring available disk space that you have tried give the true free space value on the disk, not the space that can be made available by removing local Time Machine backups. I doubt command line tools like df have been made aware of local Time Machine backups either.

mttrb
  • 8,297
  • 3
  • 35
  • 57
0

This is a bit of a workaround, not a real api, but the good old unix command df -H will get you the same information as in the 'get info' panel, you just need to select the line of your disk and parse the output.

The df program has many other options that you might want to explore. In this particular case the -H switch tells the program to spit out the numbers in human readable format and to use base 10 sizes.

Take a look here on how to run command lines from within an app and get the output inside your program: Execute a terminal command from a Cocoa app

I believe that the underpinnings of both df and the get info panel are very likely to be the same thing.

Community
  • 1
  • 1
dade
  • 188
  • 1
  • 6