Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import DiskArbitration
- func volumeUUIDForPath(path: String) -> String?
- {
- var retval: String?
- if let session = DASessionCreate(kCFAllocatorDefault)
- {
- let fsStats = UnsafeMutablePointer<statfs>.alloc(1)
- defer { fsStats.destroy(1) }
- if statfs((path as NSString).UTF8String, fsStats) == 0
- {
- let deviceName = withUnsafePointer(&fsStats.memory.f_mntfromname,
- {
- (ptr) -> String? in
- let int8Ptr = unsafeBitCast(ptr, UnsafePointer<Int8>.self)
- return String.fromCString(int8Ptr)
- })
- if let device = deviceName,
- disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, device),
- description = DADiskCopyDescription(disk)
- {
- let myDict = description as NSDictionary
- let uuid = myDict[kDADiskDescriptionVolumeUUIDKey as String /* "DAVolumeUUIDD" */] as! CFUUID
- retval = CFUUIDCreateString(kCFAllocatorDefault, uuid) as String
- }
- else { retval = "" }
- }
- }
- return retval
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement