From 97d570f0c9650a25185fe187b97731d30d086fbb Mon Sep 17 00:00:00 2001 From: EmptyChaos Date: Tue, 26 Apr 2016 11:24:08 +0000 Subject: DriveReader: Fix View > Show Drives DriveReader::m_size was never initialized which was indirectly causing CGameListCtrl to crash Dolphin when it tried to insert a character at a negative index in a string. Reading one sector at a time is very inefficient and appears to be causing timing issues during boot so SectorReader has been enhanced to support batching. SectorReader has been given a working cache system. --- Source/Core/DiscIO/DriveBlob.cpp | 92 ++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 33 deletions(-) (limited to 'Source/Core/DiscIO/DriveBlob.cpp') diff --git a/Source/Core/DiscIO/DriveBlob.cpp b/Source/Core/DiscIO/DriveBlob.cpp index 695b694f47..7aaefc402d 100644 --- a/Source/Core/DiscIO/DriveBlob.cpp +++ b/Source/Core/DiscIO/DriveBlob.cpp @@ -18,6 +18,16 @@ #ifdef _WIN32 #include "Common/StringUtil.h" +#else +#include +#include // fileno +#if defined __linux__ +#include // BLKGETSIZE64 +#elif defined __FreeBSD__ +#include // DIOCGMEDIASIZE +#elif defined __APPLE__ +#include // DKIOCGETBLOCKCOUNT / DKIOCGETBLOCKSIZE +#endif #endif namespace DiscIO @@ -25,24 +35,39 @@ namespace DiscIO DriveReader::DriveReader(const std::string& drive) { + // 32 sectors is roughly the optimal amount a CD Drive can read in + // a single IO cycle. Larger values yield no performance improvement + // and just cause IO stalls from the read delay. Smaller values allow + // the OS IO and seeking overhead to ourstrip the time actually spent + // transferring bytes from the media. + SetChunkSize(32); // 32*2048 = 64KiB + SetSectorSize(2048); #ifdef _WIN32 - SectorReader::SetSectorSize(2048); auto const path = UTF8ToTStr(std::string("\\\\.\\") + drive); m_disc_handle = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr); - if (m_disc_handle != INVALID_HANDLE_VALUE) + if (IsOK()) { // Do a test read to make sure everything is OK, since it seems you can get // handles to empty drives. DWORD not_used; - std::vector buffer(m_blocksize); - if (!ReadFile(m_disc_handle, buffer.data(), m_blocksize, ¬_used, nullptr)) + std::vector buffer(GetSectorSize()); + if (!ReadFile(m_disc_handle, buffer.data(), GetSectorSize(), ¬_used, nullptr)) { // OK, something is wrong. CloseHandle(m_disc_handle); m_disc_handle = INVALID_HANDLE_VALUE; - return; } + } + if (IsOK()) + { + // Initialize m_size by querying the volume capacity. + STORAGE_READ_CAPACITY storage_size; + storage_size.Version = sizeof(storage_size); + DWORD bytes = 0; + DeviceIoControl(m_disc_handle, IOCTL_STORAGE_READ_CAPACITY, nullptr, 0, + &storage_size, sizeof(storage_size), &bytes, nullptr); + m_size = bytes ? storage_size.DiskLength.QuadPart : 0; #ifdef _LOCKDRIVE // Do we want to lock the drive? // Lock the compact disc in the CD-ROM drive to prevent accidental @@ -53,10 +78,24 @@ DriveReader::DriveReader(const std::string& drive) 0, &dwNotUsed, nullptr); #endif #else - SectorReader::SetSectorSize(2048); m_file.Open(drive, "rb"); if (m_file) { + int fd = fileno(m_file.GetHandle()); +#if defined __linux__ + // NOTE: Doesn't matter if it fails, m_size was initialized to zero + ioctl(fd, BLKGETSIZE64, &m_size); // u64* +#elif defined __FreeBSD__ + off_t size = 0; + ioctl(fd, DIOCGMEDIASIZE, &size); // off_t* + m_size = size; +#elif defined __APPLE__ + u64 count = 0; + u32 block_size = 0; + ioctl(fd, DKIOCGETBLOCKCOUNT, &count); // u64* + ioctl(fd, DKIOCGETBLOCKSIZE, &block_size); // u32* + m_size = count * block_size; +#endif #endif } else @@ -95,45 +134,32 @@ std::unique_ptr DriveReader::Create(const std::string& drive) return reader; } -void DriveReader::GetBlock(u64 block_num, u8* out_ptr) +bool DriveReader::GetBlock(u64 block_num, u8* out_ptr) { - std::vector sector(m_blocksize); - -#ifdef _WIN32 - u64 offset = m_blocksize * block_num; - LONG off_low = (LONG)offset & 0xFFFFFFFF; - LONG off_high = (LONG)(offset >> 32); - DWORD not_used; - SetFilePointer(m_disc_handle, off_low, &off_high, FILE_BEGIN); - if (!ReadFile(m_disc_handle, sector.data(), m_blocksize, ¬_used, nullptr)) - PanicAlertT("Disc Read Error"); -#else - m_file.Seek(m_blocksize * block_num, SEEK_SET); - m_file.ReadBytes(sector.data(), m_blocksize); -#endif - - std::copy(sector.begin(), sector.end(), out_ptr); + return DriveReader::ReadMultipleAlignedBlocks(block_num, 1, out_ptr); } bool DriveReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8* out_ptr) { #ifdef _WIN32 - u64 offset = m_blocksize * block_num; - LONG off_low = (LONG)offset & 0xFFFFFFFF; - LONG off_high = (LONG)(offset >> 32); - DWORD not_used; - SetFilePointer(m_disc_handle, off_low, &off_high, FILE_BEGIN); - if (!ReadFile(m_disc_handle, out_ptr, (DWORD)(m_blocksize * num_blocks), ¬_used, nullptr)) + LARGE_INTEGER offset; + offset.QuadPart = GetSectorSize() * block_num; + SetFilePointerEx(m_disc_handle, offset, nullptr, FILE_BEGIN); + DWORD bytes_read; + if (!ReadFile(m_disc_handle, out_ptr, static_cast(GetSectorSize() * num_blocks), + &bytes_read, nullptr)) { PanicAlertT("Disc Read Error"); return false; } + return bytes_read == GetSectorSize() * num_blocks; #else - fseeko(m_file.GetHandle(), (m_blocksize * block_num), SEEK_SET); - if (fread(out_ptr, 1, (m_blocksize * num_blocks), m_file.GetHandle()) != (m_blocksize * num_blocks)) - return false; + m_file.Seek(GetSectorSize() * block_num, SEEK_SET); + if (m_file.ReadBytes(out_ptr, num_blocks * GetSectorSize())) + return true; + m_file.Clear(); + return false; #endif - return true; } } // namespace -- cgit v1.2.3