以编程方式检测已安装卷的连接类型

Tro*_*sen 1 cocoa objective-c

我在本地和挂载的文件系统之间复制文件,其中挂载的系统可以是USB,FireWire,AFP或远程服务器.我需要确定已安装卷正在使用的计算机的连接类型.我可以使用statfs系统调用来识别挂载的文件系统类型,但我无法弄清楚如何识别连接类型(FireWire,Wifi,eth,USB ......).我识别文件系统的代码是:

-(void) getVolumeInfo:(NSURL *) myurl
{
    struct statfs buf;
    statfs([myurl.path UTF8String], &buf);
    NSLog(@"Filesystem type: %s mounted filesystem: %s mounted as:  %s",buf.f_fstypename,buf.f_mntfromname,buf.f_mntonname);
}
Run Code Online (Sandbox Code Playgroud)

这为我的笔记本电脑硬盘和我的NAS服务器提供了以下输出.

Filesystem type: hfs mounted filesystem: /dev/disk0s2 mounted as: /
Filesystem type: afpfs mounted filesystem: //Trond%20Kristiansen@HerlighetNASserver._afpovertcp._tcp.local/home mounted as: /Volumes/home
Run Code Online (Sandbox Code Playgroud)

我的问题是:1)有没有人知道如何通过代码识别例如NAS服务器是如何连接的(wifi或网络电缆)2)无论如何我能检测到连接速度吗?

谢谢!

小智 5

要确定连接类型(当前活动的接口),您可以使用系统配置框架.SCNetworkConfiguration中的某些功能可能提供有关当前活动接口的信息......似乎SCNetworkConnection中定义了一些吞吐量/统计功能,但它们似乎适用于PPP连接类型.

您可能还会在NSWorkspace中找到一些有用的方法,但似乎您可能已经涵盖了文件系统信息.

#import <Foundation/Foundation.h>
#import <AppKit/NSWorkspace.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
    BOOL isRemovable, isWritable, isUnmountable;
    NSString *description, *type;

    [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath:@"/Volumes/Users"
                                                        isRemovable:&isRemovable
                                                         isWritable:&isWritable
                                                      isUnmountable:&isUnmountable
                                                        description:&description
                                                               type:&type];

    NSLog(@"Filesystem description:%@ type:%@ removable:%d writable:%d unmountable:%d", description, type, isRemovable, isWritable, isUnmountable);

    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)