Written by: Chad Armstrong
Created: 1 January 2010
Last modified: 6 January 2010
While working on Permanent Eraser 2.4.1, I began researching for a more efficient way to find the size of a folder and its contents. The method I was using before worked for the most part, but it was slow and not always perfect. The FolderSize project makes use of three different methods (Cocoa + Carbon, Carbon, and UNIX) and determines their capabilities.
For this experiment, I had three critical requirements when getting the size of a folder and its contents.
- Be able to run - If the method cannot even run, it is useless.
- Be correct - Return the proper results. Due to Mac OS X's hybrid nature, resource forks and symlinks can cause some problems when calculating a folder's size.
- Be fast - Once the prior two conditions are met, optimize the algorithm to provide a fast and efficient method.
- Cocoa + Carbon
This particular method I wrote for earlier versions of Permanent Eraser (version 2.4.0 and earlier). I tried to use Cocoa as much as possible by using the (slow)
NSDirectoryEnumerator. A small smattering of Carbon is used to properly get the proper size of files which also contain resource forks.
- Be able to run - Generally this method runs well, but I discovered it hangs when trying to size an application which contains the DotMac framework.
- Be correct - Due to the bit of Carbon code added, it is able to properly size files which contain resource forks, however it often returns an incorrect result when sizing a framework folder.
- Be fast - Unfortunately,
NSDirectoryEnumerator is quite slow in comparison to the other two methods. Where the other two methods could size a folder in the blink of an eye, the Cocoa + Carbon method could be noticeably slower.
Unfortunately, a (mostly) pure Cocoa way has displayed problems on all three counts.
This is a modified implementation of the
fastFolderSizeAtFSRef by Daniel Jalkut, posted on Apple's Cocoa mailing list in May 2005. I began using this method to get a file or folder's size starting with Permanent Eraser 2.4.1.
- Be able to run - No problems in running.
- Be correct - This proved to be the most reliable of the three methods and returned the most consistent (and correct) results.
- Be fast - In addition to being the most correct, it proved to be the fastest. In most tests, it generally ran seven times faster than the Cocoa + Carbon method, and just a hair faster than the UNIX method.
sizeForFolderStat comes from Chapter 9 of the book Advanced Mac OS X Programming by Aaron Hillegass and Mark Dalrymple. This makes use of straight C code, instead of implementing Carbon or Cocoa routines.
- Be able to run - In its current implementation, it works properly when sizing a folder, but the program will crash when trying to get the size of just a single file.
- Be correct - This works properly, as long as none of the files have resource forks. Due to how the algorithm is currently implemented, it does not recognize resource forks, so the returned value can be incorrect at times when run on Mac OS X systems.
- Be fast - A very quick and efficient method, generally only slightly slower than the Carbon method. Many times faster than the Cocoa + Carbon method.
Hands down, the best method was Carbon. I never encountered any problems when running it, it was correct, and it was fast.
- Reference files
- Related References
These links represent other material I discovered during my research, but did not explicitly use for the FolderSize project.