objective-ccocoadiskarbitration

Interpret return value (dissenter) when trying to unmount volume in OS X


I'm trying to unmount a volume in my Cocoa application using the Disk Arbitration Framework.

Before calling:

DADiskUnmount(disk, 
              kDADiskUnmountOptionDefault, 
              unmountCallback, 
              self );

I register a callback function that get's called afterwards:

void unmountCallback(DADiskRef disk, DADissenterRef dissenter, void *context )
{
   if (dissenter != NULL) 
    {
        DAReturn ret = DADissenterGetStatus(dissenter);

        switch (ret) {
        case kDAReturnBusy:
            printf("kDAReturnBusy\n");
            break;
    } 
}

In this function I try to interpret the dissenter return value but get stuck. I suppose it should be of type DAReturn and have a value like kDAReturnBusy But when e.g. iTunes is using the volume and it can not be unmounted "ret" has a value of 0xc010 that I don't quite understand.

In case unmounting fails I'd like to find out why the volume can't be unmounted and in case another application is using it remind the user of closing this application.


Solution

  • But when e.g. iTunes is using the volume and it can not be unmounted "ret" has a value of 0xc010 that I don't quite understand.

    The documentation you linked to, for the DAReturn type, lists all the Disk Arbitration constants as looking like this:

        kDAReturnError = err_local | err_local_diskarbitration | 0x01, /* ( 0xF8DA0001 ) */
    

    So, DA's error returns are all made of three components, OR'd together.

    If you look at the documentation for DADissenterGetStatus, it says:

    A BSD return code, if applicable, is encoded with unix_err().

    If you then search the headers for unix_err, you find it in /usr/include/mach/error.h, which says:

    /*    unix errors get lumped into one subsystem  */
    #define   unix_err(errno)     (err_kern|err_sub(3)|errno)
    

    and:

    /*
     *    error number layout as follows:
     *
     *    hi                     lo
     *    | system(6) | subsystem(12) | code(14) |
     */
    

    There's those three components again. Some other macros in error.h arrange the system and subsystem values (e.g., err_kern and err_sub(3)) into those positions.

    So now, let's open the Calculator, press ⌘3 to put it into programmer mode, switch it to base-16, and type in your error code, and see what it says:

    0xC010

    0000 0000 0000 0000 1100 0000 0001 0000
    31                  15                0
    

    Breaking that apart according to the above layout, we find:

    0000 00
    31     
    

    System: 0, which error.h says is err_kern. This error came from the kernel.

           00 0000 0000 11
    31                  15
    

    Subsystem: 3 (0b11). This plus the system code matches the aforementioned definition of unix_err. So this is a BSD return code, as DADissenterGetStatus said.

                          00 0000 0001 0000
    31                  15                0
    

    Individual error code: 16 (0x10, 0b10000).

    UNIX/BSD errors are defined in <sys/errno.h>, which says:

    #define EBUSY       16      /* Device / Resource busy */
    

    This suggests to me that you can't unmount that device because it's in use.