3

One of the benefits of APFS announced at WWDC 2016 was the ability to get the size of a directory very quickly, instead of having to walk the whole folder structure. The demo given was the Get Info window in Finder not having to asynchronously calculate the size of a directory.

I have an iOS app where I need to display the file size of a directory. Right now I do it recursively, summing up the sizes of all the files in all the subdirectories. But now that APFS has shipped with iOS 10.3, I'd like to take advantage of the new file system to do it much more efficiently. Is there an API for this somewhere? I don't see it referenced in the NSFileManager documentation. Right now I use attributesOfItemAtPath and the NSFileSize key.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • 1
    APFS should be transparent at that level as far as I know. do you wanna use things like low-level functions? – Mert Buran Mar 30 '17 at 15:54
  • @MertBuran it makes sense for the file system would be transparent, but it must affect the behavior of some API somewhere. Is that just at the low level? – Tom Hamming Mar 30 '17 at 16:15
  • if it is transparent that means filesystem API doesn't change, otherwise it wouldn't be transparent :) there is some changes in `diskutil`'s API, now you have `diskutil apfs` command, but i guess there won't be a change in `NSFileManager` level. if APFS optimizes file size calculation, you already have it. – Mert Buran Mar 30 '17 at 16:28
  • @MertBuran my understanding from the WWDC presentation was that the size of the folder is actually stored with the folder. So I guess the question becomes how to correctly ask a folder for its size? – Tom Hamming Mar 30 '17 at 16:53
  • @TomHamming my first guess would be your current approach, looking up that value on the directory itself instead of recursively scanning. Does that not work? – Tom Harrington Mar 30 '17 at 16:59
  • @TomHarrington not on cursory experimentation. I got tens of kilobytes on a directory I know is over 100 MB. – Tom Hamming Mar 31 '17 at 15:36

1 Answers1

3

I asked this in the APFS lab at WWDC 2018, and it would be done via an API called dirstat_np, but it's currently a private API and not available for public use. It may be offered at some point as a public API but no promises. I'm going to file a radar.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145