Skip to content

Instantly share code, notes, and snippets.

@therealbnut
Forked from adgray/volumeUUIDForPath.swift
Created April 24, 2017 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therealbnut/0866b2070968d7db1e89cc2536b6e797 to your computer and use it in GitHub Desktop.
Save therealbnut/0866b2070968d7db1e89cc2536b6e797 to your computer and use it in GitHub Desktop.
Get Volume UUID for Path
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