2

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.

Community
  • 1
  • 1
Sahil_Saini
  • 396
  • 2
  • 15

1 Answers1

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.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Solved my problem sir. Would like to notify that some typing error is there like `DASessionRef session = DASession` and `if(disk) CFRelease(disk)` must be inside the block where it is declared. – Sahil_Saini Jan 18 '16 at 05:35