我正在Objective C中编写一个小命令行实用程序,它将检查给定路径是否为挂载点,如果不是,则会为其安装网络共享。我打算用bash写这个,但是选择尝试学习Objective C。我正在寻找像这样的目标C:
mount | grep some_path
基本上我可以用来测试给定路径当前是否用作挂载点的函数。任何帮助,将不胜感激。谢谢!
答案 0 :(得分:3)
经过一些研究后,我最终使用了这段代码,以防将来任何人需要它:
NSArray * keys = [NSArray arrayWithObjects:NSURLVolumeURLForRemountingKey, nil];
NSArray * mountPaths = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];
NSError * error;
NSURL * remount;
for (NSURL * mountPath in mountPaths) {
[mountPath getResourceValue:&remount forKey:NSURLVolumeURLForRemountingKey error:&error];
if(remount){
if ([[[NSURL URLWithString:share] host] isEqualToString:[remount host]] && [[[NSURL URLWithString:share] path] isEqualToString:[remount path]]) {
printf("Already mounted at %s\n", [[mountPath path] UTF8String]);
return 0;
}
}
}
注意,NSURL共享作为远程共享的路径传递给函数。通过remount键进行过滤,可以为远程文件系统提供一个mountpoint列表,因为本地文件系统没有该密钥集。
答案 1 :(得分:0)
在swift中:
func findMountPoint(shareURL: URL) -> URL?{
guard let urls = self.mountedVolumeURLs(includingResourceValuesForKeys: [.volumeURLForRemountingKey], options: [.produceFileReferenceURLs]) else {return nil}
for u in urls{
guard let resources = try? u.resourceValues(forKeys: [.volumeURLForRemountingKey]) else{
continue
}
guard let remountURL = resources.volumeURLForRemounting else{
continue
}
if remountURL.host == shareURL.host && remountURL.path == shareURL.path{
return u
}
}
return nil
}