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

时间:2013-10-17 22:34:12

标签: objective-c cocoa

我在本地和已安装文件系统之间复制文件,其中安装的系统可以是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);
}

这为我的笔记本电脑硬盘和我的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

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

谢谢!

1 个答案:

答案 0 :(得分:5)

要确定连接类型(当前活动的界面),您可以使用System Configuration FrameworkSCNetworkConfiguration中的一些函数可能提供有关当前活动接口的信息......似乎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;
}
相关问题