列出所有驱动器/分区,并获得/基于Cocoa开发/ RDISC设备 [英] List all drives/partitions, and get /dev/rdisc device with Cocoa

查看:317
本文介绍了列出所有驱动器/分区,并获得/基于Cocoa开发/ RDISC设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法列出可用的驱动器,类似于磁盘工具,并获得相关的的/ dev / rdisk中* 设备呢?

Is there a way to list available drives, similar to Disk Utility, and get the associated /dev/rdisk* device for it?

磁盘工具可以访问这些数据 - 当你选择一个驱动器和preSS的信息按钮,它会列出。

Disk Utility has access to this data - when you select a drive and press the Info button, it lists..

Partition Map Scheme : GUID Partition Table
Disk Identifier : disk0
Media Name : Hitachi HTS541612J9SA00 Media

..或者选择分区:

..or select a partition:

Disk Identifier : disk0s3
Mount Point : /Volumes/BOOTCAMP

是否有可可API来获取这个?如果是这样,什么是通过Interface Builder来显示这一点的最好方法是什么?

Is there a Cocoa API to get at this? If so, what is the best way to display this via Interface Builder?

推荐答案

由于土拨鼠指出, IORegistry 确实是去到源万物设备相关的。在由于IOKit 文档是非常详细的和有益的;你应该开始<一href=\"http://developer.apple.com/mac/library/DOCUMENTATION/DeviceDrivers/Conceptual/IOKitFundamentals/Introduction/Introduction.html\">IOKit基本面,然后按<一个href=\"http://developer.apple.com/mac/library/DOCUMENTATION/DeviceDrivers/Conceptual/AccessingHardware/AH%5FIntro/AH%5FIntro.html\">Accessing从应用程序的硬件,最后检查出<一个href=\"http://developer.apple.com/mac/library/documentation/DeviceDrivers/Conceptual/WorkingWStorage/WWStorage%5FIntro/WWStorage%5FIntro.html\">Device文件访问指南存储设备如果你想获得的信息BSD方式。

As groundhog points out, the IORegistry is indeed the go-to source for all things device-related. The IOKit docs are very detailed and helpful; you should start with IOKit Fundamentals, then hit Accessing Hardware from Applications, then finally check out Device File Access Guide for Storage Devices if you want to get at information the BSD way.

在这种情况下,可以使磁盘仲裁框架做一些查询IO注册表和注册的通知繁重的工作。这节省了很多code,但做你想要做的一切,你最终需要使用由于IOKit 功能与底层 IOMedia 对象(请参阅<$c$c>DADiskCopyIOMedia()).

In this case, you can make the Disk Arbitration framework do some of the heavy lifting of querying the IO registry and registering for notifications. This saves a lot of code, but to do everything you want to do, you'll eventually need to use IOKit functions with the underlying IOMedia object (see DADiskCopyIOMedia()).

您可以很容易地编写围绕重新present磁盘的IO注册表中的 IOMedia 对象可可的包装,然后使用对象控制器属性绑定到你的UI

You could readily write a Cocoa wrapper around the IOMedia objects that represent the disks in the IO registry, then use object controllers to bind properties to your UI.

下面是通过磁盘仲裁框架报考硬盘外观通知,让你开始的例子:

Here's an example of registering for disk appearance notifications through the Disk Arbitration framework to get you started:

// gcc -Wall -framework Foundation -framework DiskArbitration disk_arbiter.m -o disk_arbiter
/* @file disk_arbiter.m
 * @author Jeremy W. Sherman
 * @date 2009-10-03
 *
 * Demonstrates registering for disk appeared notifications from
 * the DiskArbitration framework.
 *
 * Note that disk appeared notifications are delivered for all
 * already-appeared disks at the time of registration, and then
 * trickle in as the events actually happen thereafter.
 */
#import <Foundation/Foundation.h>
#import <DiskArbitration/DiskArbitration.h>
#import <signal.h>

sig_atomic_t sShouldExit = 0;

static void RegisterInterruptHandler(void);
static void HandleInterrupt(int);

static void OnDiskAppeared(DADiskRef disk, void *__attribute__((__unused__)));

int
main(void) {
  CFStringRef const kDARunLoopMode = kCFRunLoopDefaultMode;

  RegisterInterruptHandler();

  // Set up session.
  DASessionRef session = DASessionCreate(kCFAllocatorDefault);
  DARegisterDiskAppearedCallback(session, NULL/*all disks*/, OnDiskAppeared, (void *)NULL);
  DASessionScheduleWithRunLoop(session, CFRunLoopGetCurrent(), kDARunLoopMode);

  // Run event loop.
  printf("Starting...\n(Press Ctrl-C to exit.)\n\n");
  const Boolean kAndReturnAfterHandlingSource = TRUE;
  const CFTimeInterval kForOneSecond = 1.0;
  while (!sShouldExit)
    (void)CFRunLoopRunInMode(kCFRunLoopDefaultMode,
                             kForOneSecond, kAndReturnAfterHandlingSource);

  // Tear down and exit.
  printf("\nExiting...\n");
  DASessionUnscheduleFromRunLoop(session, CFRunLoopGetCurrent(), kDARunLoopMode);
  CFRelease(session);
  exit(EXIT_SUCCESS);
  return EXIT_SUCCESS;
}

static void
RegisterInterruptHandler(void) {
  struct sigaction sigact;
  sigact.sa_handler = HandleInterrupt;
  (void)sigaction(SIGINT, &sigact, NULL/*discard previous handler*/);
}

static void
HandleInterrupt(int __attribute__((__unused__)) signo) {
  sShouldExit = 1;
  RegisterInterruptHandler();
}


static void
OnDiskAppeared(DADiskRef disk, void *__attribute__((__unused__)) ctx) {
  printf("Lo, a disk appears!\n");
  CFShow(disk);
}

这是从样品运行输出:

$ ./disk_arbiter 
Starting...
(Press Ctrl-C to exit.)

Lo, a disk appears!
<DADisk 0x104f80 [0xa01c01a0]>{id = /dev/disk3}
Lo, a disk appears!
<DADisk 0x105b40 [0xa01c01a0]>{id = /dev/disk2s1}
Lo, a disk appears!
<DADisk 0x105ae0 [0xa01c01a0]>{id = /dev/disk2s2}
Lo, a disk appears!
<DADisk 0x105b60 [0xa01c01a0]>{id = /dev/disk2}
Lo, a disk appears!
<DADisk 0x105950 [0xa01c01a0]>{id = /dev/disk1}
Lo, a disk appears!
<DADisk 0x105bc0 [0xa01c01a0]>{id = /dev/disk1s1}
Lo, a disk appears!
<DADisk 0x105540 [0xa01c01a0]>{id = /dev/disk0}
Lo, a disk appears!
<DADisk 0x105660 [0xa01c01a0]>{id = /dev/disk0s1}
Lo, a disk appears!
<DADisk 0x1054a0 [0xa01c01a0]>{id = /dev/disk0s2}
^C
Exiting...

这篇关于列出所有驱动器/分区,并获得/基于Cocoa开发/ RDISC设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆