HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Mounting a disk with the DiskArbitration framework

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thediskwithmountingdiskarbitrationframework

Problem

I have the following code that makes use of the DiskArbitration framework to mount a disk to its default location:

#import 
#import 

void MountCallback(DADiskRef disk, DADissenterRef dissenter, void *context);

int main(int argc, const char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s \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 to stderr.



  • 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?

Solution

A few years later, and not being a C expert;
As far as I know (using Pascal myself), your application needs to wait for the callback to happen. By the looks of it though (again: not an expert) your application may already closed/finished before the callback actually happened.

Context

StackExchange Code Review Q#123791, answer score: 2

Revisions (0)

No revisions yet.