5

我在 Objective C 中编写了一个小命令行实用程序,它将检查给定路径是否是挂载点,如果不是,则将网络共享挂载到它。我打算用 bash 写这个,但选择尝试学习 Objective C。我正在寻找类似这样的Objective C:

mount | grep some_path

基本上,我可以使用一个函数来测试给定路径当前是否用作挂载点。任何帮助,将不胜感激。谢谢!

4

2 回答 2

5

经过一番研究,我最终使用了这段代码,以防将来有人需要它:

        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 键过滤会为您提供远程文件系统的挂载点列表,因为本地文件系统没有该键集。

于 2013-09-29T23:06:58.110 回答
1

迅速:

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
}
于 2017-12-01T22:49:04.393 回答