开发者

Cocoa application get size of /dev/disk0s1

开发者 https://www.devze.com 2023-02-24 09:42 出处:网络
How could I get the size in bytes of a partition by it\'s device name (e.g. /d开发者_运维技巧ev/disk0s1) in a Cocoa application? Maybe I should use Disk Arbitration framework somehow?You’re right —

How could I get the size in bytes of a partition by it's device name (e.g. /d开发者_运维技巧ev/disk0s1) in a Cocoa application? Maybe I should use Disk Arbitration framework somehow?


You’re right — you can get that information by using the Disk Arbitration framework:

DASessionRef session = DASessionCreate(NULL);

if (session) {
    DADiskRef disk = DADiskCreateFromBSDName(NULL, session, "/dev/disk0s1");

    if (disk) {
        CFDictionaryRef diskDescription = DADiskCopyDescription(disk);
        NSDictionary *diskData = (NSDictionary *)diskDescription;
        NSString *diskSizeKey = (NSString *)kDADiskDescriptionMediaSizeKey;
        unsigned long size = [[diskData objectForKey:diskSizeKey]
            unsignedLongValue];

        NSLog(@"size in bytes = %lu", size);

        CFRelease(diskDescription);    
        CFRelease(disk);
    }
    else NSLog(@"Error while getting DA disk for /dev/disk0s1");

    CFRelease(session);
}
else NSLog(@"Error while creating DA session");

Note that /dev/disk0s1 is the EFI partition.

0

精彩评论

暂无评论...
验证码 换一张
取 消