gpt4 book ai didi

cocoa - 列出所有驱动器/分区,并使用 Cocoa 获取/dev/rdisc 设备

转载 作者:行者123 更新时间:2023-12-04 13:35:36 24 4
gpt4 key购买 nike

有没有办法列出可用驱动器,类似于磁盘工具,并获取相关的 /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 显示它的最佳方式是什么?

最佳答案

正如土拨鼠指出的那样,IORegistry确实是所有与设备相关的东西的首选来源。 IOKit文档非常详细且很有帮助;你应该从 IOKit Fundamentals 开始,然后点击 Accessing Hardware from Applications ,然后最后查看Device File Access Guide for Storage Devices如果您想以 BSD 方式获取信息。

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

您可以轻松地在 IOMedia 周围编写一个 Cocoa 包装器。 IO 注册表中表示磁盘的对象,然后使用对象 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com