How to get diskutil info output in cocoa application

Is there a way to programmatically get the same information that it provides diskutil info / | grep "Free Space"? (For obvious reasons, I'd rather do this than just parse the result of this command.)

I am currently using statfs; however, I was informed that the space for these reports is not always accurate, because OS X also hosts temporary files, such as Time Machine snapshots, on disk. These files are automatically deleted if the space ends, and the OS does not report the use of these files. In other words, it statfsoften gives less free space than diskutil info, or looks at disk information in the Finder.

+4
source share
1 answer

You can use popen(3):

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    FILE *f;
    char info[256];

    f = popen("/usr/sbin/diskutil info /", "r");
    if (f == NULL) {
        perror("Failed to run diskutil");
        exit(0);
    }

    while (fgets(info, sizeof(info), f) != NULL) {
        printf("%s", info);
    }

    pclose(f);

    return 0;
}

EDIT

Sorry, I did not carefully read the question. You can also use Disk Arbitration Framework . There is also some sample code that might be useful ( FSMegaInfo ).

UPDATE

I looked at the output from otool -L $(which diskutil)and it seems that it uses a closed framework called DiskManagement.framework. Looking at the result class-dump, I saw a method volumeFreeSpaceForDisk:error:. So, the dimensions that I got from diskutil -info /and FSMegaInfo FSGetVolumeInfo /, and my tool:

  • diskutil: 427031642112 Bytes

  • my tool: volumeFreeSpaceForDisk: 427031642112

  • FSMegaInfo: freeBytes = 427031642112 (397 GB)

, , , ( ), diskutil 1000, FSMegaInfo 1024, GB - ( , df -h df -h diskutil - 10 2).

:

#import <Foundation/Foundation.h>
#import "DiskManagement.h"
#import <DiskArbitration/DADisk.h>

int main(int argc, char *argv[])
{
    int                 err;
    const char *        bsdName = "disk0s2";
    DASessionRef        session;
    DADiskRef           disk;
    CFDictionaryRef     descDict;
    session  = NULL;
    disk     = NULL;
    descDict = NULL;
    if (err == 0) {session = DASessionCreate(NULL); if (session == NULL) {err = EINVAL;}}
    if (err == 0) {disk = DADiskCreateFromBSDName(NULL, session, bsdName); if (disk == NULL) {err = EINVAL;}}
    if (err == 0) {descDict = DADiskCopyDescription(disk); if (descDict == NULL) {err = EINVAL;}}

    DMManager *dmMan = [DMManager sharedManager];
    NSLog(@"blockSizeForDisk: %@", [dmMan blockSizeForDisk:disk error:nil]);
    NSLog(@"totalSizeForDisk: %@", [dmMan totalSizeForDisk:disk error:nil]);
    NSLog(@"volumeTotalSizeForDisk: %@", [dmMan volumeTotalSizeForDisk:disk error:nil]);
    NSLog(@"volumeFreeSpaceForDisk: %@", [dmMan volumeFreeSpaceForDisk:disk error:nil]);

    return 0;
}

DiskManagement.h, class-dump /System/Library/PrivateFrameworks/DiskManagement.framework/Versions/Current/DiskManagement > DiskManagement.h, , private frameworks -F/System/Library/PrivateFrameworks/ -framework.

:

clang -g tool.m -F/System/Library/PrivateFrameworks/ -framework Foundation -framework DiskArbitration -framework DiskManagement -o tool

UPDATE2: . FSMegaInfo , stat /Volumes/.MobileBackups , statfs("/", &stats).

+6

All Articles