有没有办法列出类似于磁盘实用程序的可用驱动器,并为其获取关联的/dev/rdisk*设备?

“磁盘工具”可以访问此数据-选择驱动器并按“信息”按钮时,它将列出。

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

..或选择一个分区:
Disk Identifier : disk0s3
Mount Point : /Volumes/BOOTCAMP

是否有 cocoa API可以做到这一点?如果是这样,通过Interface Builder显示此内容的最佳方法是什么?

最佳答案

正如groundhog指出的那样,IORegistry确实是与设备相关的所有事物的首选源。 IOKit文档非常详细且很有帮助;您应该以IOKit Fundamentals开头,然后按Accessing Hardware from Applications,然后最后检查Device File Access Guide for Storage Devices,以获取有关BSD方式的信息。

在这种情况下,您可以使磁盘仲裁框架完成一些繁重的工作,例如查询IO注册表和注册通知。这样可以节省很多代码,但是要完成您想做的所有事情,最终将需要对基础IOKit对象使用IOMedia函数(请参阅 DADiskCopyIOMedia() )。

您可以轻松地围绕IOt注册表中代表磁盘的IOMedia对象编写一个Cocoa包装器,然后使用对象 Controller 将属性绑定(bind)到您的UI。

这是通过“磁盘仲裁”框架注册磁盘状态通知以使您入门的示例:

// 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 - 列出所有驱动器/分区,并通过Cocoa获取/dev/rdisc设备,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1515068/

10-12 14:17
查看更多