Programmatically mount a disk/volume using only a

2019-04-29 23:59发布

I have a list of file paths. These paths may lead to files on unmounted Volumes. How can I programmatically tell the OS to mount the volume before I try to access the file?

I know this is possible somehow, as iTunes appears to do it. For example, if I initiate playback of a song located on an unmounted network attached volume in iTunes, the volume is mounted automatically for me.

Is the OS supposed to mount the volume automatically for me if I attempt to open the file programmatically at the specified path? Or do I need to mount the drive manually using the Disk Arbitration framework or something similar?

If I need to use the Disk Arbitration framework, what specifically needs to be done?

2条回答
看我几分像从前
2楼-- · 2019-04-30 00:39

Have you tried using aliases? I think they're now known as bookmarks. It's possible that they will cause volumes to be mounted when you resolve them.

查看更多
神经病院院长
3楼-- · 2019-04-30 00:49

Short answer: No need to mess with the "Disk Arbitration framework". The Alias Manager can handle it all for you.

Chris Suter was already pretty close. There are two solutions, API wise:

  1. Aliases. Those existed already in pre-OSX MacOS and are structured files that contain several ways to locate a file or folder, even it it has been moved or renamed. They also contain information about the volume they were on, including mounting information if it's on a network volumes. You can see these in action for yourself if you simply create an Alias of a file (using the Finder's "Make Alias" command in the File menu), then rename or move it, or unmount its volume. The Finder will attempt to bring the volume back, even ask you to provide the login information if necessary. There is also the "Alias Manager" API, including the function FSResolveAliasFileWithMountFlags which is exactly what you asked for: It lets you not only find the target of an alias but also choose if the disk shall be made available if it's unmounted, and if the user should be asked to login if necessary.

  2. CFURL Bookmarks. This is the the modern replacement of the Alias API and exists only since OSX 10.6. CFURL Bookmarks are better managed by the system at runtime. I do not know if they have a equivalent on-disk file representation such as the classic Alias files do, but maybe they're even the very same.

In any case, those should be all the pointers you need, I hope.

查看更多
登录 后发表回答