2009-03-28 02:57:34 -06:00
|
|
|
// Most of the code in this file was shamelessly ripped from libcdio With minor adjustments
|
2009-02-24 08:06:52 -07:00
|
|
|
|
|
|
|
#include "CDUtils.h"
|
2009-03-28 02:57:34 -06:00
|
|
|
#include "Common.h"
|
2009-02-24 08:06:52 -07:00
|
|
|
|
2010-01-19 12:28:27 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#define PATH_MAX MAX_PATH
|
|
|
|
|
|
|
|
#elif __APPLE__
|
|
|
|
#include <paths.h>
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#include <IOKit/IOKitLib.h>
|
|
|
|
#include <IOKit/storage/IOCDMedia.h>
|
|
|
|
#include <IOKit/storage/IOMedia.h>
|
|
|
|
#include <IOKit/IOBSD.h>
|
|
|
|
|
|
|
|
#elif __linux__
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/cdrom.h>
|
|
|
|
#endif // WIN32
|
|
|
|
|
2009-02-24 08:06:52 -07:00
|
|
|
#ifdef _WIN32
|
2009-03-28 02:57:34 -06:00
|
|
|
// Returns a string that can be used in a CreateFile call if
|
|
|
|
// c_drive letter is a character. If not NULL is returned.
|
2010-07-07 13:47:45 -06:00
|
|
|
const char *is_cdrom(const char c_drive_letter)
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
|
|
|
|
UINT uDriveType;
|
|
|
|
char sz_win32_drive[4];
|
|
|
|
|
|
|
|
sz_win32_drive[0]= c_drive_letter;
|
|
|
|
sz_win32_drive[1]=':';
|
|
|
|
sz_win32_drive[2]='\\';
|
|
|
|
sz_win32_drive[3]='\0';
|
|
|
|
|
|
|
|
uDriveType = GetDriveType(sz_win32_drive);
|
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
switch(uDriveType)
|
|
|
|
{
|
|
|
|
case DRIVE_CDROM:
|
|
|
|
{
|
|
|
|
char sz_win32_drive_full[] = "\\\\.\\X:";
|
|
|
|
sz_win32_drive_full[4] = c_drive_letter;
|
|
|
|
return __strdup(&sz_win32_drive_full[4]);
|
|
|
|
}
|
2009-02-24 08:06:52 -07:00
|
|
|
default:
|
|
|
|
//cdio_debug("Drive %c is not a CD-ROM", c_drive_letter);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
// Returns a pointer to an array of strings with the device names
|
2010-07-07 13:47:45 -06:00
|
|
|
std::vector<std::string> cdio_get_devices() {
|
2010-02-16 01:46:21 -07:00
|
|
|
std::vector<std::string> drives;
|
2009-02-24 08:06:52 -07:00
|
|
|
char drive_letter;
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
// Scan the system for CD-ROM drives.
|
2009-02-24 08:06:52 -07:00
|
|
|
for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
|
2010-07-07 13:47:45 -06:00
|
|
|
const char *drive_str=is_cdrom(drive_letter);
|
2009-02-24 08:06:52 -07:00
|
|
|
if (drive_str != NULL) {
|
2010-02-16 01:46:21 -07:00
|
|
|
drives.push_back(drive_str);
|
2010-02-15 21:34:41 -07:00
|
|
|
delete drive_str;
|
2009-02-24 08:06:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return drives;
|
|
|
|
}
|
2010-07-07 13:47:45 -06:00
|
|
|
#elif defined __APPLE__
|
|
|
|
// Returns a pointer to an array of strings with the device names
|
|
|
|
std::vector<std::string> cdio_get_devices()
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
io_object_t next_media;
|
|
|
|
mach_port_t master_port;
|
|
|
|
kern_return_t kern_result;
|
|
|
|
io_iterator_t media_iterator;
|
|
|
|
CFMutableDictionaryRef classes_to_match;
|
2010-02-16 01:46:21 -07:00
|
|
|
std::vector<std::string> drives;
|
2009-02-24 08:06:52 -07:00
|
|
|
|
|
|
|
kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
|
2010-07-07 13:47:45 -06:00
|
|
|
if( kern_result != KERN_SUCCESS )
|
2010-02-16 01:46:21 -07:00
|
|
|
return( drives );
|
2009-02-24 08:06:52 -07:00
|
|
|
|
|
|
|
classes_to_match = IOServiceMatching( kIOCDMediaClass );
|
2010-07-07 13:47:45 -06:00
|
|
|
if( classes_to_match == NULL )
|
2010-02-16 01:46:21 -07:00
|
|
|
return( drives );
|
2009-02-24 08:06:52 -07:00
|
|
|
|
|
|
|
CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectableKey),
|
|
|
|
kCFBooleanTrue );
|
|
|
|
|
|
|
|
kern_result = IOServiceGetMatchingServices( master_port,
|
|
|
|
classes_to_match,
|
|
|
|
&media_iterator );
|
2010-07-07 13:47:45 -06:00
|
|
|
if( kern_result != KERN_SUCCESS)
|
2010-02-16 01:46:21 -07:00
|
|
|
return( drives );
|
2009-02-24 08:06:52 -07:00
|
|
|
|
|
|
|
next_media = IOIteratorNext( media_iterator );
|
2010-07-07 13:47:45 -06:00
|
|
|
if( next_media != 0 )
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
char psz_buf[0x32];
|
|
|
|
size_t dev_path_length;
|
|
|
|
CFTypeRef str_bsd_path;
|
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
do
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
str_bsd_path =
|
|
|
|
IORegistryEntryCreateCFProperty( next_media,
|
|
|
|
CFSTR( kIOBSDNameKey ),
|
|
|
|
kCFAllocatorDefault,
|
|
|
|
0 );
|
2010-07-07 13:47:45 -06:00
|
|
|
if( str_bsd_path == NULL )
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
IOObjectRelease( next_media );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
// Below, by appending 'r' to the BSD node name, we indicate
|
|
|
|
// a raw disk. Raw disks receive I/O requests directly and
|
|
|
|
// don't go through a buffer cache.
|
2009-02-24 08:06:52 -07:00
|
|
|
snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
|
|
|
|
dev_path_length = strlen( psz_buf );
|
|
|
|
|
2009-02-27 03:40:16 -07:00
|
|
|
if( CFStringGetCString( (CFStringRef)str_bsd_path,
|
2009-02-24 08:06:52 -07:00
|
|
|
(char*)&psz_buf + dev_path_length,
|
|
|
|
sizeof(psz_buf) - dev_path_length,
|
2010-07-07 13:47:45 -06:00
|
|
|
kCFStringEncodingASCII))
|
|
|
|
{
|
2010-02-16 01:46:21 -07:00
|
|
|
if(psz_buf != NULL)
|
|
|
|
{
|
|
|
|
std::string str = psz_buf;
|
|
|
|
drives.push_back(str);
|
|
|
|
}
|
2009-02-24 08:06:52 -07:00
|
|
|
}
|
|
|
|
CFRelease( str_bsd_path );
|
|
|
|
IOObjectRelease( next_media );
|
|
|
|
|
|
|
|
} while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
|
|
|
|
}
|
|
|
|
IOObjectRelease( media_iterator );
|
|
|
|
return drives;
|
|
|
|
}
|
2010-07-07 13:47:45 -06:00
|
|
|
#elif defined __linux__
|
|
|
|
// checklist: /dev/cdrom, /dev/dvd /dev/hd?, /dev/scd? /dev/sr?
|
2009-02-24 08:06:52 -07:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
const char * format;
|
|
|
|
unsigned int num_min;
|
|
|
|
unsigned int num_max;
|
2010-07-07 13:47:45 -06:00
|
|
|
} checklist[] =
|
2009-02-24 08:06:52 -07:00
|
|
|
{
|
2010-07-07 13:47:45 -06:00
|
|
|
{ "/dev/cdrom", 0, 0},
|
|
|
|
{ "/dev/dvd", 0, 0},
|
2009-02-24 08:06:52 -07:00
|
|
|
{ "/dev/hd%c", 'a', 'z' },
|
|
|
|
{ "/dev/scd%d", 0, 27 },
|
|
|
|
{ "/dev/sr%d", 0, 27 },
|
2010-07-07 13:47:45 -06:00
|
|
|
{ NULL }
|
2009-02-24 08:06:52 -07:00
|
|
|
};
|
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
// Returns true if a device is a block or char device and not a symbolic link
|
|
|
|
bool is_device(const char *source_name)
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
struct stat buf;
|
2010-07-07 13:47:45 -06:00
|
|
|
if (0 != lstat(source_name, &buf))
|
2009-02-24 08:06:52 -07:00
|
|
|
return false;
|
2010-07-07 13:47:45 -06:00
|
|
|
|
|
|
|
return ((S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode)) && !S_ISLNK(buf.st_mode));
|
2009-02-24 08:06:52 -07:00
|
|
|
}
|
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
// Check a device to see if it is a DVD/CD-ROM drive
|
|
|
|
static bool is_cdrom(const char *drive, char *mnttype)
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
bool is_cd=false;
|
|
|
|
int cdfd;
|
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
// Check if the device exists
|
|
|
|
if (!is_device(drive))
|
2009-02-24 08:06:52 -07:00
|
|
|
return(false);
|
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
// If it does exist, verify that it is a cdrom/dvd drive
|
2009-02-24 08:06:52 -07:00
|
|
|
cdfd = open(drive, (O_RDONLY|O_NONBLOCK), 0);
|
2010-07-07 13:47:45 -06:00
|
|
|
if ( cdfd >= 0 )
|
|
|
|
{
|
|
|
|
if (ioctl(cdfd, CDROM_GET_CAPABILITY, 0) != -1)
|
2009-02-24 08:06:52 -07:00
|
|
|
is_cd = true;
|
|
|
|
close(cdfd);
|
|
|
|
}
|
|
|
|
return(is_cd);
|
|
|
|
}
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
// Returns a pointer to an array of strings with the device names
|
2010-07-07 13:47:45 -06:00
|
|
|
std::vector<std::string> cdio_get_devices ()
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
char drive[40];
|
2010-02-16 01:46:21 -07:00
|
|
|
std::vector<std::string> drives;
|
2009-02-24 08:06:52 -07:00
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
// Scan the system for DVD/CD-ROM drives.
|
|
|
|
for ( i=0; checklist[i].format; ++i )
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
unsigned int j;
|
2010-07-07 13:47:45 -06:00
|
|
|
for ( j=checklist[i].num_min; j<=checklist[i].num_max; ++j )
|
|
|
|
{
|
|
|
|
sprintf(drive, checklist[i].format, j);
|
|
|
|
if ( (is_cdrom(drive, NULL)) > 0 )
|
|
|
|
{
|
2010-02-16 01:46:21 -07:00
|
|
|
std::string str = drive;
|
|
|
|
drives.push_back(str);
|
2009-02-24 08:06:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return drives;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
// Returns true if device is a cdrom/dvd drive
|
|
|
|
bool cdio_is_cdrom(std::string device)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
// Resolve symbolic links. This allows symbolic links to valid cdrom/dvd drives to
|
|
|
|
// be passed from the command line with the -e flag.
|
|
|
|
char *devname = realpath(device.c_str(), NULL);
|
|
|
|
if (!devname)
|
|
|
|
return false;
|
2009-02-24 08:06:52 -07:00
|
|
|
#endif
|
2009-03-28 02:57:34 -06:00
|
|
|
|
2010-02-16 01:46:21 -07:00
|
|
|
std::vector<std::string> devices = cdio_get_devices();
|
2009-02-24 08:06:52 -07:00
|
|
|
bool res = false;
|
2010-07-07 13:47:45 -06:00
|
|
|
for (unsigned int i = 0; i < devices.size(); i++)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
if (strncmp(devices[i].c_str(), devname, PATH_MAX) == 0)
|
|
|
|
#else
|
|
|
|
if (strncmp(devices[i].c_str(), device.c_str(), PATH_MAX) == 0)
|
|
|
|
#endif
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
res = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-07 13:47:45 -06:00
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
if (devname)
|
|
|
|
free(devname);
|
|
|
|
#endif
|
2009-02-24 08:06:52 -07:00
|
|
|
|
2010-02-16 01:46:21 -07:00
|
|
|
devices.clear();
|
2009-02-24 08:06:52 -07:00
|
|
|
return res;
|
|
|
|
}
|
2009-03-28 02:57:34 -06:00
|
|
|
|