How to get mount point information from an API on Mac?

拥有回忆 提交于 2019-12-25 13:32:12

问题


When I issue the mount command, I get entries like the following:

//abc@host/b1 on /Volumes/b1 (smbfs, nodev, nosuid, mounted by abc)
//abc@host/b2 on /Volumes/b2 (smbfs, nodev, nosuid, mounted by abc)

These indicate that I have two samba shares mounted.

I'd rather not try to parse mount command outputs, but I do want to retrieve the mount points of of attached filesystems, especially from samba.

Is there a API on the Mac that allows me to do this, either in C, or shell, or Python, etc. ?


回答1:


You get an array of URLs for the mounted volumes using:

NSArray* keys = @[ /* ... */ ];
NSArray* urls = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys
                                                                                        options:NSVolumeEnumerationSkipHiddenVolumes];

I'll get to the keys array in a moment. Once you have those URLs, you can obtain information about them using the "resource value" APIs of NSURL. You get a single value using -[NSURL getResourceValue:forKey:error:]. You get several at a time using -resourceValuesForKeys:error:. You can optimize the fetching of whatever values you're interested in by specifying them in the keys array passed to the NSFileManager method, above.

A key that may be significant for working with network shares is NSURLVolumeURLForRemountingKey. Other keys are listed in the NSURL docs. Both the Command and Volume keys apply.



来源:https://stackoverflow.com/questions/31033605/how-to-get-mount-point-information-from-an-api-on-mac

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