Browse Source

libobs: Fix free disk space calculation on macOS (#10187)

New space availability keys seem to have very specific file system
requirements not documented anywhere. Using the opportunistic free
space key opportunistically (and falling back on the legacy value
otherwise) should always yield a "good-enough" free disk space value.
Patrick Heyer 1 year ago
parent
commit
32ec6c17b5
1 changed files with 9 additions and 13 deletions
  1. 9 13
      libobs/util/platform-cocoa.m

+ 9 - 13
libobs/util/platform-cocoa.m

@@ -363,23 +363,19 @@ int64_t os_get_free_space(const char *path)
     if (path) {
     if (path) {
         NSURL *fileURL = [NSURL fileURLWithPath:@(path)];
         NSURL *fileURL = [NSURL fileURLWithPath:@(path)];
 
 
-        NSDictionary *values = [fileURL resourceValuesForKeys:@[NSURLVolumeIsLocalKey] error:nil];
+        NSArray *availableCapacityKeys = @[
+            NSURLVolumeAvailableCapacityKey, NSURLVolumeAvailableCapacityForImportantUsageKey,
+            NSURLVolumeAvailableCapacityForOpportunisticUsageKey
+        ];
 
 
-        BOOL isLocalVolume = [values[NSURLVolumeIsLocalKey] boolValue];
+        NSDictionary *values = [fileURL resourceValuesForKeys:availableCapacityKeys error:nil];
 
 
-        NSURLResourceKey volumeKey;
+        NSNumber *availableOpportunisticSpace = values[NSURLVolumeAvailableCapacityForOpportunisticUsageKey];
+        NSNumber *availableSpace = values[NSURLVolumeAvailableCapacityKey];
 
 
-        if (isLocalVolume) {
-            volumeKey = NSURLVolumeAvailableCapacityForOpportunisticUsageKey;
+        if (availableOpportunisticSpace.longValue > 0) {
+            return availableOpportunisticSpace.longValue;
         } else {
         } else {
-            volumeKey = NSURLVolumeAvailableCapacityKey;
-        }
-
-        values = [fileURL resourceValuesForKeys:@[volumeKey] error:nil];
-
-        NSNumber *availableSpace = values[volumeKey];
-
-        if (availableSpace) {
             return availableSpace.longValue;
             return availableSpace.longValue;
         }
         }
     }
     }