I have the following code that makes use of the DiskArbitration framework to mount a disk to its default location:
#import <CoreFoundation/CoreFoundation.h>
#import <DiskArbitration/DiskArbitration.h>
void MountCallback(DADiskRef disk, DADissenterRef dissenter, void *context);
int main(int argc, const char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <disk>\n", argv[0]);
return EXIT_FAILURE;
}
const char *deviceName = argv[1];
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, deviceName);
// Pass NULL for a "standard" mount path.
DADiskMount(disk, NULL, kDADiskMountOptionWhole, MountCallback, (void *)deviceName);
DASessionSetDispatchQueue(session, NULL);
CFRelease(session);
session = NULL;
return EXIT_SUCCESS;
}
void MountCallback(DADiskRef disk, DADissenterRef dissenter, void *context) {
const char *mountedDisk = context;
fprintf(stderr, "Device mounted: %s\n", mountedDisk);
fflush(stderr);
return;
}
To compile the code:
clang -Wall -Werror -g -v main.m -lobjc -framework DiskArbitration -framework Foundation -o mount
To run the program:
./mount diskN
The program works fine, however as a developer without much knowledge on the Apple platform, I'm interested in the following points:
- The
MountCallback
function doesn't appear to be called. I don't see the line I'm printing tostderr
. - In case the mount operation fails, how do I access the error, and use it return a different exit status on
main
? (I assume the error is caught from the callback, but can't find documentation about it). - Am I releasing the session and other resources I'm using for the task correctly?