【问题标题】:Programmatically detect type of connection to mounted volume以编程方式检测到已安装卷的连接类型
【发布时间】:2013-10-18 16:04:40
【问题描述】:

我正在本地和已安装的文件系统之间复制文件,其中已安装的系统可以是 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) 无论如何我可以检测连接速度吗?

谢谢!

【问题讨论】:

  • 你能告诉我如何挂载文件系统吗?谢谢
  • 能否请您在问题中添加 2 件事:1)什么是“myurl”输入以及它的外观 2)您如何获得它 - 含义,您在哪里以及如何注册挂载卷......场景太模糊了

标签: objective-c cocoa


【解决方案1】:

要确定连接类型(当前活动接口),您可以使用System Configuration Framework。 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;
}

【讨论】:

  • 请注意,在较新的系统上,这可能会失败(并在 inout 参数上返回 NO/nil),因为沙盒在不先提示用户的情况下不允许您访问。如果可以,请使用已知可访问的路径,例如库。
  • NSWorkspace 真的很可爱,对许多用途都有帮助,我肯定会为这些用途使用它 - 有什么比一个清晰的、有这么多答案的单行更好的呢?但是,如果我在“挂载点”上需要更多“文件系统信息”,那么它是 USB 设备吗?它的序列号是多少?谁是它的创造者?您在“磁盘工具”中看到的那种信息。你知道在哪里可以买到这些吗?
  • API 似乎也在“撒谎”...这是我的外部 OWC 硬盘驱动器上一个卷的字符串输出,通过 usb-C/thunderbolt 连接:“MountPoint: /Volumes/MediaStore 文件系统描述:apfs 类型:apfs 可移动:0 可写:1 不可挂载:0" 而卷既不可挂载,又可移动???什么给了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 2011-11-20
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
相关资源
最近更新 更多