Unmounting Drive/volume without ejecting

丶灬走出姿态 提交于 2019-12-05 04:38:17

问题


I am want to unmount a disk WITHOUT EJECTING. To do that I tried following code

{
  NSString *path;
  CFStringRef *volumeName=(__bridge CFStringRef)path;
  DASessionRef session = DASessionCreate(kCFAllocatorDefault);
  CFURLRef pathRef = CFURLCreateWithString(NULL, CFSTR("/volumes/Untitled"), NULL);
  DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, pathRef);
  DADiskUnmount(disk, kDADiskUnmountOptionForce, NULL, NULL);
}

This code is from this question, Thanks to @zeFree

Its working but I want dynamic path to the volume where as in the code its static. I tried changing NSString to CFStringRef and then tried to use at place of path("/volumes/Untitled") mention but its still same.

Any suggestion is welcome.


回答1:


First of all, you are strongly discouraged from using kDADiskUnmountOptionForce.

This is a method to unmount a volume at given URL with basic error handling and memory management.

- (BOOL)unmountVolumeAtURL:(NSURL *)url
    BOOL returnValue = NO;
    DASessionRef session = DASessionCreate(kCFAllocatorDefault);
    if (session) {
        DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, (__bridge CFURLRef)url);
        if (disk) {
            DADiskUnmount(disk, kDADiskUnmountOptionDefault, NULL , NULL);
            returnValue = YES;
            CFRelease(disk);
        } else {
            NSLog(@"Could't create disk reference from %@", url.path);
        }
    } else {
      NSLog(@"Could't create DiskArbritation session");
    }

    if (session) CFRelease(session);
    return returnValue;
}

The error handling could be still improved by providing a callback handler in the DADiskUnmount function.



来源:https://stackoverflow.com/questions/34809658/unmounting-drive-volume-without-ejecting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!