2015-05-23 22:32:32 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-23 22:32:32 -06:00
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
// Most of the code in this file was shamelessly ripped from libcdio With minor adjustments
|
2010-07-21 21:29:35 -06:00
|
|
|
|
2021-12-09 19:22:16 -07:00
|
|
|
#include "Common/CDUtils.h"
|
|
|
|
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2019-06-14 08:53:46 -06:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2010-01-19 12:28:27 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#elif __APPLE__
|
2010-07-12 14:11:19 -06:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2010-07-21 21:29:35 -06:00
|
|
|
#include <IOKit/IOBSD.h>
|
2010-01-19 12:28:27 -07:00
|
|
|
#include <IOKit/IOKitLib.h>
|
|
|
|
#include <IOKit/storage/IOCDMedia.h>
|
|
|
|
#include <IOKit/storage/IOMedia.h>
|
2010-07-12 14:11:19 -06:00
|
|
|
#include <paths.h>
|
2010-07-21 21:29:35 -06:00
|
|
|
#else
|
2019-10-06 12:26:31 -06:00
|
|
|
#include <climits>
|
2010-01-19 12:28:27 -07:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <unistd.h>
|
2010-01-19 12:28:27 -07:00
|
|
|
#endif // WIN32
|
|
|
|
|
2010-07-21 21:29:35 -06:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <linux/cdrom.h>
|
|
|
|
#endif
|
|
|
|
|
2021-12-09 19:22:16 -07:00
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
2018-05-27 16:21:59 -06:00
|
|
|
namespace Common
|
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
#ifdef _WIN32
|
2011-03-11 23:50:20 -07:00
|
|
|
// takes a root drive path, returns true if it is a cdrom drive
|
2018-05-27 16:24:43 -06:00
|
|
|
static bool IsCDROM(const TCHAR* drive)
|
2010-07-07 13:47:45 -06:00
|
|
|
{
|
2011-03-11 23:50:20 -07:00
|
|
|
return (DRIVE_CDROM == GetDriveType(drive));
|
|
|
|
}
|
2010-07-21 21:29:35 -06:00
|
|
|
|
2011-03-11 23:50:20 -07:00
|
|
|
// Returns a vector with the device names
|
2018-05-27 16:24:43 -06:00
|
|
|
std::vector<std::string> GetCDDevices()
|
2011-03-11 23:50:20 -07:00
|
|
|
{
|
|
|
|
std::vector<std::string> drives;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
const DWORD buffsize = GetLogicalDriveStrings(0, nullptr);
|
2013-02-27 17:51:02 -07:00
|
|
|
std::vector<TCHAR> buff(buffsize);
|
|
|
|
if (GetLogicalDriveStrings(buffsize, buff.data()) == buffsize - 1)
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
2013-02-27 17:51:02 -07:00
|
|
|
auto drive = buff.data();
|
2011-03-11 23:50:20 -07:00
|
|
|
while (*drive)
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
2018-05-27 16:24:43 -06:00
|
|
|
if (IsCDROM(drive))
|
2011-03-11 23:50:20 -07:00
|
|
|
{
|
2013-02-27 17:51:02 -07:00
|
|
|
std::string str(TStrToUTF8(drive));
|
2011-03-11 23:50:20 -07:00
|
|
|
str.pop_back(); // we don't want the final backslash
|
|
|
|
drives.push_back(std::move(str));
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-03-11 23:50:20 -07:00
|
|
|
// advance to next drive
|
|
|
|
while (*drive++)
|
|
|
|
{
|
|
|
|
}
|
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
|
2018-05-27 16:24:43 -06:00
|
|
|
std::vector<std::string> GetCDDevices()
|
2010-07-07 13:47:45 -06:00
|
|
|
{
|
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;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
kern_result = IOMasterPort(MACH_PORT_NULL, &master_port);
|
2014-03-10 05:30:55 -06:00
|
|
|
if (kern_result != KERN_SUCCESS)
|
2015-02-15 12:43:31 -07:00
|
|
|
return drives;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
classes_to_match = IOServiceMatching(kIOCDMediaClass);
|
2014-03-10 05:30:55 -06:00
|
|
|
if (classes_to_match == nullptr)
|
2015-02-15 12:43:31 -07:00
|
|
|
return drives;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
CFDictionarySetValue(classes_to_match, CFSTR(kIOMediaEjectableKey), kCFBooleanTrue);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
kern_result = IOServiceGetMatchingServices(master_port, classes_to_match, &media_iterator);
|
2014-03-10 05:30:55 -06:00
|
|
|
if (kern_result != KERN_SUCCESS)
|
2015-02-15 12:43:31 -07:00
|
|
|
return drives;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
next_media = IOIteratorNext(media_iterator);
|
2014-03-10 05:30:55 -06:00
|
|
|
if (next_media != 0)
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
CFTypeRef str_bsd_path;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-07-07 13:47:45 -06:00
|
|
|
do
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
|
|
|
str_bsd_path =
|
2015-02-15 12:43:31 -07:00
|
|
|
IORegistryEntryCreateCFProperty(next_media, CFSTR(kIOBSDNameKey), kCFAllocatorDefault, 0);
|
2014-03-10 05:30:55 -06:00
|
|
|
if (str_bsd_path == nullptr)
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
2015-02-15 12:43:31 -07:00
|
|
|
IOObjectRelease(next_media);
|
2009-02-24 08:06:52 -07:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-21 01:11:58 -06:00
|
|
|
if (CFGetTypeID(str_bsd_path) == CFStringGetTypeID())
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
2014-10-21 01:11:58 -06:00
|
|
|
size_t buf_size = CFStringGetLength((CFStringRef)str_bsd_path) * 4 + 1;
|
|
|
|
char* buf = new char[buf_size];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-21 01:11:58 -06:00
|
|
|
if (CFStringGetCString((CFStringRef)str_bsd_path, buf, buf_size, kCFStringEncodingUTF8))
|
2010-02-16 01:46:21 -07:00
|
|
|
{
|
2014-10-21 01:11:58 -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.
|
|
|
|
drives.push_back(std::string(_PATH_DEV "r") + buf);
|
2010-02-16 01:46:21 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-21 01:11:58 -06:00
|
|
|
delete[] buf;
|
2009-02-24 08:06:52 -07:00
|
|
|
}
|
2015-02-15 12:43:31 -07:00
|
|
|
CFRelease(str_bsd_path);
|
|
|
|
IOObjectRelease(next_media);
|
|
|
|
} while ((next_media = IOIteratorNext(media_iterator)) != 0);
|
2009-02-24 08:06:52 -07:00
|
|
|
}
|
2015-02-15 12:43:31 -07:00
|
|
|
IOObjectRelease(media_iterator);
|
2009-02-24 08:06:52 -07:00
|
|
|
return drives;
|
|
|
|
}
|
2010-07-21 21:29:35 -06:00
|
|
|
#else
|
2010-07-07 13:47:45 -06:00
|
|
|
// checklist: /dev/cdrom, /dev/dvd /dev/hd?, /dev/scd? /dev/sr?
|
2019-06-14 08:53:46 -06:00
|
|
|
struct CheckListEntry
|
2009-02-24 08:06:52 -07:00
|
|
|
{
|
2016-01-21 12:46:25 -07:00
|
|
|
const char* format;
|
2010-07-21 21:29:35 -06:00
|
|
|
unsigned int num_min;
|
|
|
|
unsigned int num_max;
|
2019-06-14 08:53:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
constexpr CheckListEntry checklist[] = {
|
2010-07-21 21:29:35 -06:00
|
|
|
#ifdef __linux__
|
2019-06-14 08:53:46 -06:00
|
|
|
{"/dev/cdrom", 0, 0}, {"/dev/dvd", 0, 0}, {"/dev/hd{}", 'a', 'z'},
|
|
|
|
{"/dev/scd{}", 0, 27}, {"/dev/sr{}", 0, 27},
|
2010-07-21 21:29:35 -06:00
|
|
|
#else
|
2019-06-14 08:53:46 -06:00
|
|
|
{"/dev/acd{}", 0, 27},
|
|
|
|
{"/dev/cd{}", 0, 27},
|
2010-07-21 21:29:35 -06:00
|
|
|
#endif
|
2019-06-14 08:53:46 -06:00
|
|
|
{nullptr, 0, 0},
|
|
|
|
};
|
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
|
2018-05-27 16:24:43 -06:00
|
|
|
static bool IsDevice(const std::string& source_name)
|
2010-07-07 13:47:45 -06:00
|
|
|
{
|
2009-02-24 08:06:52 -07:00
|
|
|
struct stat buf;
|
2014-02-07 18:23:34 -07:00
|
|
|
if (0 != lstat(source_name.c_str(), &buf))
|
2009-02-24 08:06:52 -07:00
|
|
|
return false;
|
2010-07-07 13:47:45 -06:00
|
|
|
|
2010-07-21 21:29:35 -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
|
2018-05-27 16:39:54 -06:00
|
|
|
static bool IsCDROM(const std::string& drive)
|
2010-07-07 13:47:45 -06:00
|
|
|
{
|
|
|
|
// Check if the device exists
|
2018-05-27 16:24:43 -06:00
|
|
|
if (!IsDevice(drive))
|
2015-02-15 12:43:31 -07:00
|
|
|
return false;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
bool is_cd = false;
|
2010-07-07 13:47:45 -06:00
|
|
|
// If it does exist, verify that it is a cdrom/dvd drive
|
2015-02-15 12:43:31 -07:00
|
|
|
int cdfd = open(drive.c_str(), (O_RDONLY | O_NONBLOCK), 0);
|
2014-03-10 05:30:55 -06:00
|
|
|
if (cdfd >= 0)
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
|
|
|
#ifdef __linux__
|
2010-07-07 13:47:45 -06:00
|
|
|
if (ioctl(cdfd, CDROM_GET_CAPABILITY, 0) != -1)
|
2010-07-21 21:29:35 -06:00
|
|
|
#endif
|
2009-02-24 08:06:52 -07:00
|
|
|
is_cd = true;
|
|
|
|
close(cdfd);
|
2010-07-21 21:29:35 -06:00
|
|
|
}
|
2015-02-15 12:43:31 -07:00
|
|
|
return is_cd;
|
2009-02-24 08:06:52 -07:00
|
|
|
}
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
// Returns a pointer to an array of strings with the device names
|
2018-05-27 16:24:43 -06:00
|
|
|
std::vector<std::string> GetCDDevices()
|
2010-07-07 13:47:45 -06:00
|
|
|
{
|
2010-02-16 01:46:21 -07:00
|
|
|
std::vector<std::string> drives;
|
2010-07-07 13:47:45 -06:00
|
|
|
// Scan the system for DVD/CD-ROM drives.
|
2014-02-07 18:23:34 -07:00
|
|
|
for (unsigned int i = 0; checklist[i].format; ++i)
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
2014-02-07 18:23:34 -07:00
|
|
|
for (unsigned int j = checklist[i].num_min; j <= checklist[i].num_max; ++j)
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
2022-06-11 07:39:04 -06:00
|
|
|
std::string drive = fmt::format(fmt::runtime(checklist[i].format), j);
|
2018-05-27 16:39:54 -06:00
|
|
|
if (IsCDROM(drive))
|
2010-07-21 21:29:35 -06:00
|
|
|
{
|
2014-02-07 18:23:34 -07:00
|
|
|
drives.push_back(std::move(drive));
|
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
|
2018-05-27 16:24:43 -06:00
|
|
|
bool IsCDROMDevice(std::string device)
|
2010-07-07 13:47:45 -06:00
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
#ifndef _WIN32
|
2010-07-21 21:29:35 -06:00
|
|
|
// Resolve symbolic links. This allows symbolic links to valid
|
|
|
|
// drives to be passed from the command line with the -e flag.
|
2019-10-06 12:26:31 -06:00
|
|
|
char resolved_path[PATH_MAX];
|
2016-01-21 12:46:25 -07:00
|
|
|
char* devname = realpath(device.c_str(), resolved_path);
|
2010-07-07 13:47:45 -06:00
|
|
|
if (!devname)
|
|
|
|
return false;
|
2013-10-28 23:09:01 -06:00
|
|
|
device = devname;
|
2009-02-24 08:06:52 -07:00
|
|
|
#endif
|
2009-03-28 02:57:34 -06:00
|
|
|
|
2018-05-27 16:24:43 -06:00
|
|
|
std::vector<std::string> devices = GetCDDevices();
|
2014-02-07 18:23:34 -07:00
|
|
|
for (const std::string& d : devices)
|
2014-02-09 08:12:59 -07:00
|
|
|
{
|
2014-02-07 18:23:34 -07:00
|
|
|
if (d == device)
|
|
|
|
return true;
|
2014-02-09 08:12:59 -07:00
|
|
|
}
|
2014-02-07 18:23:34 -07:00
|
|
|
return false;
|
2009-02-24 08:06:52 -07:00
|
|
|
}
|
2018-05-27 16:21:59 -06:00
|
|
|
} // namespace Common
|