-
-
Save therealbnut/0866b2070968d7db1e89cc2536b6e797 to your computer and use it in GitHub Desktop.
Get Volume UUID for Path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DiskArbitration | |
extension URL { | |
var volumeUUID: String? { | |
guard self.isFileURL, let session = DASessionCreate(nil) else { | |
return nil | |
} | |
var fsStats = statfs() | |
guard statfs(self.path, &fsStats) == 0, | |
let deviceName = String(validatingUTF8: &fsStats.f_mntfromname.0), | |
let disk = DADiskCreateFromBSDName(nil, session, deviceName), | |
let description = DADiskCopyDescription(disk) as NSDictionary? else { | |
return nil | |
} | |
let key = kDADiskDescriptionVolumeUUIDKey as String | |
return description[key].map { uuidRef in | |
CFUUIDCreateString(nil, uuidRef as! CFUUID) as String | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment