From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49168) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YBSZk-0000HY-7z for qemu-devel@nongnu.org; Wed, 14 Jan 2015 13:21:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YBSZg-00068P-US for qemu-devel@nongnu.org; Wed, 14 Jan 2015 13:21:52 -0500 Received: from mail-qg0-x22e.google.com ([2607:f8b0:400d:c04::22e]:39347) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YBSZg-00068L-Q1 for qemu-devel@nongnu.org; Wed, 14 Jan 2015 13:21:48 -0500 Received: by mail-qg0-f46.google.com with SMTP id q107so8197470qgd.5 for ; Wed, 14 Jan 2015 10:21:48 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii From: Programmingkid In-Reply-To: Date: Wed, 14 Jan 2015 13:21:44 -0500 Content-Transfer-Encoding: quoted-printable Message-Id: <3CC173D9-8BA2-4E8C-BC3C-64C267F4EC0F@gmail.com> References: <71BB46AD-4E5B-4876-84F0-12260D6846D3@gmail.com> Subject: Re: [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 Cc: Kevin Wolf , Stefan Hajnoczi , qemu-devel qemu-devel On Jan 14, 2015, at 12:02 PM, Peter Maydell wrote: > On 13 January 2015 at 20:07, Programmingkid = wrote: >> Allows QEMU on Mac OS X to use a real cdrom again. >>=20 >> Signed-off-by: John Arbuckle >>=20 >> --- >> Added fallback code - uses lseek() if ioctl() fails. >>=20 >> block/raw-posix.c | 25 ++++++++++++++++++++++++- >> 1 files changed, 24 insertions(+), 1 deletions(-) >>=20 >> 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 =3D=3D 0) >> #endif >> #if defined(__APPLE__) && defined(__MACH__) >> - size =3D LLONG_MAX; >> + { >> + uint64_t sectors =3D 0; >> + uint32_t sector_size =3D 0; >> + bool ioctl_problem =3D false; >> + ret =3D 0; >> + >> + /* Query the number of sectors on the disk */ >> + ret =3D ioctl(fd, DKIOCGETBLOCKCOUNT, §ors); >> + if (ret !=3D 0) >> + ioctl_problem =3D true; >> + >> + /* Query the size of each sector */ >> + ret =3D ioctl(fd, DKIOCGETBLOCKSIZE, §or_size); >> + if (ret !=3D 0) >> + ioctl_problem =3D true; >> + >> + /* If everything is ok */ >> + if (ioctl_problem =3D=3D false) >> + size =3D sectors * sector_size; >> + >> + /* If a problem occurred with ioctl(), fallback to lseek() = */ >> + else >> + size =3D lseek(fd, 0LL, SEEK_END); >> + } >> #else >=20 > This fixes the "make check" problem, but you're not catching > the possibility of lseek failing. (Also the if() statements > are all missing braces our coding style requires.) You can > avoid that bool flag like this: >=20 > { > uint64_t sectors =3D 0; > uint32_t sector_size =3D 0; >=20 > if (ioctl(fd, DKIOCGETBLOCKCOUNT, §ors) =3D=3D 0 > && ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) =3D=3D 0) { > size =3D sectors * sector_size; > } else { > size =3D lseek(fd, 0LL, SEEK_END); > if (size < 0) { > return -errno; > } > } > } Yeah, your code looks great. I will make a patch for it if you need me = to.=20