2

Do you know you to unmount a drive without ejecting it. NSWorkspace has some methods to unmount drives but it also eject them.

Any idea ?

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134

3 Answers3

3

I am doing it as follows and it un-mounts the drive but doesn't eject it.

(Actually I want to eject the disk, I can only un-mount the disk. :P Please share how to eject a disk.)

DASessionRef session = DASessionCreate(kCFAllocatorDefault);

CFURLRef path = CFURLCreateWithString(NULL, CFSTR("<path_to_your_volume_here>"), NULL);
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, path);

DADiskUnmount(disk, kDADiskUnmountOptionDefault, __unmountCallback, NULL);

This is the code I am still working on and is under development and testing.
I am creating the "path" manually. You can use (and share) a better method to get the path of volume in a generic way. Perhaps this answer has hints of doing it the right way.

I'll update when my development is refined and complete.

Community
  • 1
  • 1
zeFree
  • 2,129
  • 2
  • 31
  • 39
  • Works great. Just had to add the DiskArbitration FrameWork in XCode and in the headfile. And to eject the disk I use : `[[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtURL:[NSURL fileURLWithPath:device.path] error:&error];` – Matthieu Riegler Jan 29 '13 at 18:01
1

To eject the disk, unmount the disk as you stated, and then in your __unmountCallback do the following:

            DADiskRef disk2 = DADiskCopyWholeDisk(disk);
            DADiskEject(disk2,
                        kDADiskEjectOptionDefault,
                        NULL,
                        NULL);

You can pass any object as context to the DADiskUnmount() and then, for example, use it to determine if the respective disk should be ejected in the __unmountCallback.

Simon
  • 577
  • 3
  • 9