如何在 MacOS 中获取 USB 驱动器的 Total/Available/Used space - Swift?

How to get Total/Available/Used space of a USB drive in MacOS - Swift?

如何在 Swift (MacOS) 中确定 USB 驱动器的总计、可用和已用 space?

有几篇关于此的好帖子(例如: and https://developer.apple.com/documentation/foundation/urlresourcekey/checking_volume_storage_capacity),但它们都只是获取操作系统驱动器的 space 而不是 USB 驱动器。

例如,我有一个 64GB 的 USB 驱动器,卷名为“myusb”,因此 MacOS 将驱动器安装在 /Volumes/myusb。 Finder 显示 USB 驱动器的总 space 为 62.91GB,可用为 62.29GB,已用为 625,999,872 字节。

问题似乎是当我给出 USB 驱动器的路径时,由于它显然是主 / 路径的一部分,它返回的信息是 / 这是我的 OS 驱动器,而不是USB 驱动器。

这是我在尝试确定 USB 驱动器的空闲 space 以及 returns 值为 292298430687 字节(这是我 space 的可用 OS 驱动器,不是 USB 驱动器):

/**
   Returns URL of root of USB drive - i.e. /Volumes/myusb
   Uses bundleURL as .app file being executed is located on the USB drive
*/
static func getRootURL() -> URL {
    let bundlePath = Bundle.main.bundleURL
    let bundlePathComponents = bundlePath.pathComponents
        
    let destinationRootPathURL = URL(fileURLWithPath: bundlePathComponents[0])
        .appendingPathComponent(bundlePathComponents[1])
        .appendingPathComponent(bundlePathComponents[2])
        
    return destinationRootPathURL
}

/**
   returns free space of USB drive
*/
func getAvailableSpaceInBytes() -> Int64 {
    if #available(OSX 10.13, *) {
        if let freeSpace = try? getRootURL().resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey])
                .volumeAvailableCapacityForImportantUsage {
                return freeSpace
        }
    } else {
        // Fallback on earlier versions
        guard let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: getRootURL().path),
              let freeSize = systemAttributes[FileAttributeKey.systemFreeSize] as? NSNumber
        else {
            // something failed so return nil
            return 0
        }
            
        return freeSize.int64Value
    }
        
        return 0
    }

您可以使用 FileManager 的 mountedVolumeURLs 方法获取所有已安装的卷并从中获取 volumeAvailableCapacityForImportantUsage 资源 key/value:


extension FileManager {
    static var mountedVolumes: [URL] {
        (FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil) ?? []).filter({[=10=].path.hasPrefix("/Volumes/")})
    }
}

extension URL {
    var volumeTotalCapacity: Int? {
        (try? resourceValues(forKeys: [.volumeTotalCapacityKey]))?.volumeTotalCapacity
    }
    var volumeAvailableCapacityForImportantUsage: Int64? {
        (try? resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey]))?.volumeAvailableCapacityForImportantUsage
    }
    var name: String? {
        (try? resourceValues(forKeys: [.nameKey]))?.name
    }
    
}

用法:

for url in FileManager.mountedVolumes {
    print(url.name ?? "Untitled")
    print("Capacity:", url.volumeTotalCapacity ?? "nil")
    print("Available:", url.volumeAvailableCapacityForImportantUsage ?? "nil")
    print("Used:", (try? url.sizeOnDisk()) ?? "nil") // check the other link below
}

对于带有 BigSur 安装程序的 16GB USB 驱动器,上面的代码将打印出来

Install macOS Big Sur
Capacity: 15180193792
Available: 2232998976
Used: 12.93 GB on disk


要获取卷“sizeOnDisk”的已用space,您可以查看此