2012-02-16 114 views
1

我正在發佈使用光盤的軟件,並且在默認情況下,它的速度太嘈雜,無法接受。我的目標是使用ioctl來降低磁盤的速度,但我不知道如何從/ Volumes/MyDisk/Application中找到/ dev/disk(n)。OSX獲取CD速度(ioctl)

以下是我到目前爲止的內容,但我不希望磁盤路徑被硬編碼。

#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; 
} 

回答

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驅動器。加載磁盤(並且修改代碼以使用未簽名的short速度),它將/ dev/disk2報告爲CD,速度爲4234 KB /秒。

+0

感謝您的示例代碼!這聽起來像是它可能是最好的解決方案。我也在研究在捲上使用statfs.f_mntfromname,但它返回的東西不適用於ioctl。感謝您的迴應! – 2012-02-17 06:32:19