Received: from eggs.gnu.org ([2001:4830:134:3::10]:36296) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB7kD-0004tT-D4 for qemu-devel@nongnu.org; Tue, 13 Jan 2015 15:07:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YB7k9-00055m-Cz for qemu-devel@nongnu.org; Tue, 13 Jan 2015 15:07:17 -0500 Received: from mail-qa0-x233.google.com ([2607:f8b0:400d:c00::233]:41651) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB7k9-00055c-9I for qemu-devel@nongnu.org; Tue, 13 Jan 2015 15:07:13 -0500 Received: by mail-qa0-f51.google.com with SMTP id f12so2870455qad.10 for ; Tue, 13 Jan 2015 12:07:12 -0800 (PST) From: Programmingkid Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Tue, 13 Jan 2015 15:07:08 -0500 Message-Id: <71BB46AD-4E5B-4876-84F0-12260D6846D3@gmail.com> Mime-Version: 1.0 (Apple Message framework v1084) Subject: [Qemu-devel] [PATCH v6] block/raw-posix.c: Fixes raw_getlength() on Mac OS X so that it reports the correct length of a real CD List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell , Kevin Wolf , Stefan Hajnoczi Cc: qemu-devel qemu-devel Allows QEMU on Mac OS X to use a real cdrom again. Signed-off-by: John Arbuckle --- Added fallback code - uses lseek() if ioctl() fails. block/raw-posix.c | 25 ++++++++++++++++++++++++- 1 files changed, 24 insertions(+), 1 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index e51293a..5815707 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -1312,7 +1312,30 @@ again: if (size == 0) #endif #if defined(__APPLE__) && defined(__MACH__) - size = LLONG_MAX; + { + uint64_t sectors = 0; + uint32_t sector_size = 0; + bool ioctl_problem = false; + ret = 0; + + /* Query the number of sectors on the disk */ + ret = ioctl(fd, DKIOCGETBLOCKCOUNT, §ors); + if (ret != 0) + ioctl_problem = true; + + /* Query the size of each sector */ + ret = ioctl(fd, DKIOCGETBLOCKSIZE, §or_size); + if (ret != 0) + ioctl_problem = true; + + /* If everything is ok */ + if (ioctl_problem == false) + size = sectors * sector_size; + + /* If a problem occurred with ioctl(), fallback to lseek() */ + else + size = lseek(fd, 0LL, SEEK_END); + } #else size = lseek(fd, 0LL, SEEK_END); if (size < 0) { -- 1.7.5.4