简体   繁体   English

使用DiskArbitration在OSX中卸载磁盘

[英]Unmount disk in OSX with DiskArbitration

I'm trying to unmount a disk in OSX. 我正在尝试在OSX中卸载磁盘。 The code works fine, but the callback is not called when the disk is unmounted successful only when it gives an error. 代码工作正常,但只有当磁盘发出错误时,才会在磁盘卸载成功时调用回调。 I read the DiskArbitrationProgGuide and followed the steps, but no progress yet. 我阅读了DiskArbitrationProgGuide并按照步骤操作,但还没有进展。 Could someone give me a help? 有人能给我一个帮助吗?

@interface DriverUtilitiesController()

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

@end

+ (void)umnountDrivePath:(NSString *)voulumePath
{
    DASessionRef session = DASessionCreate(kCFAllocatorDefault);

    CFURLRef path = CFURLCreateWithString(NULL, (__bridge CFStringRef)voulumePath, NULL);

    DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, path);

    DADiskUnmount(disk, kDADiskUnmountOptionDefault, unmount_done, NULL);

    CFRelease(disk);
}

#pragma mark - Unmount Callback

void unmount_done(DADiskRef disk,
                  DADissenterRef dissenter,
                  void *context)
{

    NSLog(@"Inside unmount_done");

    if (dissenter)
    {
        // Unmount failed. //
        NSLog(@"Unmount failed.");

    } else {
        NSLog(@"Unmounted Volume");
    }
}

Updating. 更新。 Thanks to Ken Thomases the code now works 感谢Ken Thomases,代码现在可以运行了

- (id)init
{
    self = [super init];

    self.session = DASessionCreate(kCFAllocatorDefault);

    DASessionScheduleWithRunLoop(_session, [[NSRunLoop mainRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);

    return self;
}


- (void)umnountDrivePath:(NSString *)voulumePath
{

    CFURLRef path = CFURLCreateWithString(NULL, (__bridge CFStringRef)voulumePath, NULL);

    DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, self.session, path);

    DADiskUnmount(disk, kDADiskUnmountOptionDefault, unmount_done, (__bridge void *)(self));

    CFRelease(disk);
}

void unmount_done(DADiskRef disk,
                  DADissenterRef dissenter,
                  void *context)
{
    if (dissenter)
    {
        // Unmount failed. //
        NSLog(@"Unmount failed.");

    } else {
        NSLog(@"Unmounted Volume");

    }

    DriverUtilitiesController *driverUtilitiesController = (__bridge DriverUtilitiesController *)context;


    [driverUtilitiesController clearUnmountCallback];
}

- (void)clearUnmountCallback
{
    DASessionUnscheduleFromRunLoop(_session, [[NSRunLoop mainRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);

    CFRelease(self.session);
}

DADiskUnmount() operates asynchronously. DADiskUnmount()异步方式运行。 The disk has not necessarily been unmounted by the time the function returns to your code. 在函数返回代码时,磁盘未必卸载。 If it succeeds, it may happen at some later time. 如果成功,可能会在以后的某个时间发生。 Your callback will be called at that time. 那时你的回调将被调用。

The mechanism by which a program waits for that event and calls your callback in response is either a run loop or a dispatch queue. 程序等待该事件并在响应中调用回调的机制是运行循环或调度队列。 The session object is responsible for managing this waiting and calling. 会话对象负责管理此等待和调用。 You need to schedule the session object on a run loop or dispatch queue. 您需要在运行循环或调度队列上安排会话对象。 You use either DASessionScheduleWithRunLoop() or DASessionSetDispatchQueue() , as described in Disk Arbitration Programming Guide: Using Disk Arbitration Notification and Approval Callbacks – Scheduling the Session with the Run Loop or Dispatch Queue . 您可以使用DASessionScheduleWithRunLoop()DASessionSetDispatchQueue() ,如“ 磁盘仲裁编程指南:使用磁盘仲裁通知和批准回调”中所述 - 使用“运行循环”或“调度队列”调度会话

That means that you don't want to create a new session object for every attempt to unmount a disk. 这意味着您不希望为每次卸载磁盘的尝试创建新的会话对象。 Also, you want to keep a reference to the session object so that you can unschedule and release it when you no longer need it (which would be sometime after you no longer need to get callbacks from it). 此外,您希望保留对会话对象的引用,以便在不再需要它时可以取消计划并释放它(在您不再需要从中获取回调之后的某个时间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM