2

I have the following swift code, based on developer documentation here

On most systems I tried this the result matches with Disk Utility. But not all the time. I want to understand how Disk Utility calculates free space or what API it uses.

For example after deleting and clearing from trash 100 GB of VMWare Fusion Virtual Machines, Disk Utility Free Space hasn't updated; even after reboot. However output from the DiskArbitration APIs has updated.

Disk Utility shows free space at 41.45 GB, after deleting and clearing from trash 100 GB data.

At the same time the output of the code here, shows the space has been cleared:

Available capacity: 115.6 GB
Available capacity for important usage: 149.2 GB
Available capacity for opportunistic usage: 136.8 GB
Total Capacity: 250.7 GB
Program ended with exit code: 0

Example code:

import Foundation
import DiskArbitration

// from https://stackoverflow.com/questions/26198073/query-available-ios-disk-space-with-swift
func deviceRemainingFreeSpaceInBytes() -> Int64? {
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
    guard
        let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: documentDirectory),
        let freeSize = systemAttributes[.systemFreeSize] as? NSNumber
        else {
            // something failed
            return nil
    }
    return freeSize.int64Value
}

let fileURL = URL(fileURLWithPath:"/")
do {
    let formatter = ByteCountFormatter();

    let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey,.volumeAvailableCapacityKey,.volumeAvailableCapacityForOpportunisticUsageKey,.volumeTotalCapacityKey])

    if let AvailableCapacity = values.volumeAvailableCapacity {
        print("Available capacity: \(formatter.string(fromByteCount:Int64(AvailableCapacity)))")
    } else {
        print("Capacity is unavailable")
    }

    if let bytes = deviceRemainingFreeSpaceInBytes() {
        print("Free Space on System: \(formatter.string(fromByteCount:bytes))")
    } else {
        print("failed")
    }

    if let AvailableCapacityForImportantUsage = values.volumeAvailableCapacityForImportantUsage {
        print("Available capacity for important usage: \(formatter.string(fromByteCount:AvailableCapacityForImportantUsage))")
    } else {
        print("Capacity is unavailable")
    }

    if let AvailableCapacityForOpportunisticUsage = values.volumeAvailableCapacityForOpportunisticUsage {
        print("Available capacity for opportunistic usage: \(formatter.string(fromByteCount:AvailableCapacityForOpportunisticUsage))")
    } else {
        print("Capacity is unavailable")
    }

    if let TotalCapacity = values.volumeTotalCapacity {
        print("Total Capacity: \(formatter.string(fromByteCount:Int64(TotalCapacity)))")
    } else {
        print("Capacity is unavailable")
    }
} catch {
    print("Error retrieving capacity: \(error.localizedDescription)")
}
Malcolm McCaffery
  • 2,468
  • 1
  • 22
  • 43

0 Answers0