How to query purgeable space?

Scenario

SSD: 500GB

Used: 150GB

Puregable 330GB



Attempt to install: 50GB

-> Mac lies and says not enough "available space".



Let's say a user tries to install something and their drive is consumed by "purgeable" space. When querying disk drive space the normal way that you do for every operating system in the universe (except Mac apparently), you get the size available.



Mac gives you purgeable space + used space as "available space". So if you write code that tries to tell the user if they're out of space before installing your product, it looks like the user doesn't have enough space to install. As a developer, how can I query just the purgeable space so that in the event their mac lies to me to tell me that they don't have enough space, I can help the user make an informed decision?



For example, in this case a user has plenty of space. But their mac would tell them they are out of space. How can I say, "It looks like you are out of space, but your Mac has 330GBs of purgeable space. Would you like to install anyway?"



Can I query the amount of purgeable space? If not can I query whether they have storage optimization turned on or not? At that point I could at least suggest that their mac might be lying to them and they may be able to install anyway.




What relevent API flags/functions are there for devs around this - I can't seem to find any.

Replies

This is a difficult topic. Ideally, you should check for free space (true free space) before allocating storage. You should also handle any failures to allocate storage. But as I understand it, the operating system is not going to free up storage until after a failure. It does that on its own schedule. All you can do is suggest the user try again later after freeing up more storage.


You seem to refere to your own installer. That is even more problematic. The idea is to distribute your apps in the Mac App Store, or at least using official package installers, or even zip downloads. In all these cases, it is Apple (or the user) that has to deal with the availble vs. free question. If you are injecting your software into the installation process, you are making it your responsibility.


Further complicating this is the Finder. It is only going to report "available" space. So when the user invariably complains that they have "plenty of free space", they are complaining about the App Store, an installer, or a download. If you assume this responsibility, they can now complain about your code and you now have to deal with it.


If you really want to do this, here is what you do:

1) Get a URL to the root mount point. If you are on Catalina, you may have to track down the data volume.

2) Call NSURL "getResourceValue:forKey:error:" on the URL using the key "NSURLVolumeAvailableCapacityForImportantUsageKey".


This is not quite what you asked, but I think it is all there is. There is an "NSURLVolumeAvailableCapacityForOpportunisticUsageKey". I really don't understand the documentation (in the header file) for these two entries. I have found that "NSURLVolumeAvailableCapacityForImportantUsageKey" is equivalent to the Finder's display of "available" space, which is what I was interested in. There is no guarantee that any of this is truly "purgeable" or ever would be.