How to get mount point information from an API on

2019-09-11 02:58发布

问题:

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.



标签: macos api mount