1

我正在分发使用光盘的软件,并且在默认全速下它太嘈杂而无法接受。我的目标是使用 ioctl 降低磁盘的速度,但我不知道如何从 /Volumes/MyDisk/Application 中找到 /dev/disk(n)。

以下是我到目前为止所拥有的,但我不希望磁盘路径硬编码。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <IOKit/storage/IOCDMediaBSDClient.h>

int main () {
    // ------------------------------------
    //   Open Drive
    // ------------------------------------
    int fd = open("/dev/disk1",O_RDONLY);
    if (fd == -1) {
        printf("Error opening drive \n");
        exit(1);
    }

    // ------------------------------------
    //   Get Speed
    // ------------------------------------
    unsigned int speed;
    if (ioctl(fd,DKIOCCDGETSPEED,&speed)) {
        printf("Must not be a CD \n");
    }
    else {
        printf("CD Speed: %d KB/s \n",speed);
    }

    // ------------------------------------
    //   Close Drive
    // ------------------------------------
    close(fd);
    return 0;
}
4

1 回答 1

2

您可能需要遍历 /dev 中的磁盘条目,打开每个条目,然后使用其他一些 ioctl() 来识别它们的类型。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <IOKit/storage/IOCDMediaBSDClient.h>

int main( int argc, char *argv[])
{
    int i, fd;
    unsigned short speed;
    char disk[40];

    for (i = 0; i < 100; ++i)
    {
        sprintf( disk, "/dev/disk%u", i);
        fd = open( disk, O_RDONLY);
        if (fd != -1)
        {
            if (ioctl( fd, DKIOCCDGETSPEED, &speed))
            {
                printf( "%s is not a CD\n", disk);
            }
            else
            {
                printf( "%s CD Speed is %u KB/s\n", disk, speed);
            }
            close( fd);
        }
    }

    return 0;
}

在我的旧 MacBook Pro 上,DVD 驱动器中没有磁盘,它告诉我 disk0 和 disk1 都不是 CD 驱动器。加载磁盘(并且代码固定为使用无符号速度缩写)时,它会将 /dev/disk2 报告为速度为 4234 KB/s 的 CD。

于 2012-02-17T00:29:10.490 回答