Programmatically mount a disk/volume using only a given file path in Mac OS X

≡放荡痞女 提交于 2019-12-07 04:58:26

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13426063/programmatically-mount-a-disk-volume-using-only-a-given-file-path-in-mac-os-x

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