尝试在OS X中卸载卷时解释返回值(反对者)

时间:2011-12-19 17:50:48

标签: objective-c cocoa diskarbitration

我正在尝试使用磁盘仲裁框架在我的Cocoa应用程序中卸载卷。

致电之前:

DADiskUnmount(disk, 
              kDADiskUnmountOptionDefault, 
              unmountCallback, 
              self );

我注册了一个后来调用的回调函数:

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

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

在这个函数中,我尝试解释dissenter返回值但是卡住了。我认为它应该是DAReturn类型并且具有类似kDAReturnBusy的值但是例如iTunes正在使用该卷,它无法卸载“ret”的值为0xc010,我不太明白。

如果卸载失败,我想找出无法卸载卷的原因,以及其他应用程序使用它时提醒用户关闭此应用程序。

1 个答案:

答案 0 :(得分:17)

  

但是,例如iTunes正在使用该卷,它无法卸载“ret”的值为0xc010,我不太明白。

您链接到的DAReturn类型的文档列出了所有磁盘仲裁常量,如下所示:

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

因此,DA的错误返回全部由三个组成部分组成,或者一起进行。

如果你看the documentation for DADissenterGetStatus,就说:

  

BSD返回码(如果适用)使用unix_err()进行编码。

如果您然后搜索unix_err的标题,可以在/usr/include/mach/error.h中找到它,其中包含:

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

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

又有这三个组成部分。 error.h中的一些其他宏将系统和子系统值(例如err_kernerr_sub(3))排列到这些位置。

现在,让我们打开计算器,按⌘3将其置于编程模式,将其切换到base-16,然后输入错误代码,看看它是什么:

  

0xC010

0000 0000 0000 0000 1100 0000 0001 0000
31                  15                0

根据上述布局将其分开,我们发现:

0000 00
31     

系统:0,error.h表示err_kern。这个错误来自内核。

       00 0000 0000 11
31                  15

子系统:3(0b11)。此加上系统代码与上述unix_err的定义相匹配。所以这是一个BSD返回码,正如DADissenterGetStatus所说。

                      00 0000 0001 0000
31                  15                0

个别错误代码:16(0x10,0b10000)。

UNIX / BSD错误在<sys/errno.h>中定义,其中包含:

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

这告诉我,您无法卸载该设备,因为它正在使用中。