From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48180) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y8UIG-0002I0-GT for qemu-devel@nongnu.org; Tue, 06 Jan 2015 08:35:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y8UIC-0004bk-Fl for qemu-devel@nongnu.org; Tue, 06 Jan 2015 08:35:32 -0500 Received: from mail-we0-x22c.google.com ([2a00:1450:400c:c03::22c]:41050) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y8UIC-0004be-9c for qemu-devel@nongnu.org; Tue, 06 Jan 2015 08:35:28 -0500 Received: by mail-we0-f172.google.com with SMTP id k11so9619987wes.17 for ; Tue, 06 Jan 2015 05:35:27 -0800 (PST) Date: Tue, 6 Jan 2015 13:35:23 +0000 From: Stefan Hajnoczi Message-ID: <20150106133523.GI29775@stefanha-thinkpad.redhat.com> References: <1419692504-29373-1-git-send-email-peter@lekensteyn.nl> <1419692504-29373-2-git-send-email-peter@lekensteyn.nl> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="u/L2/WlOHZg+YGU4" Content-Disposition: inline In-Reply-To: <1419692504-29373-2-git-send-email-peter@lekensteyn.nl> Subject: Re: [Qemu-devel] [PATCH 01/10] block/dmg: properly detect the UDIF trailer List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Wu Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi --u/L2/WlOHZg+YGU4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Dec 27, 2014 at 04:01:35PM +0100, Peter Wu wrote: > diff --git a/block/dmg.c b/block/dmg.c > index e455886..df274f9 100644 > --- a/block/dmg.c > +++ b/block/dmg.c > @@ -131,6 +131,39 @@ static void update_max_chunk_size(BDRVDMGState *s, u= int32_t chunk, > } > } > =20 > +static int64_t dmg_find_koly_offset(BlockDriverState *file_bs) > +{ > + int64_t length; > + int64_t offset =3D 0; > + uint8_t buffer[515]; > + int i, ret; > + > + /* bdrv_getlength returns a multiple of block size (512), rounded up= =2E Since > + * dmg images can have odd sizes, try to look for the "koly" magic w= hich > + * marks the begin of the UDIF trailer (512 bytes). This magic can b= e found > + * in the last 511 bytes of the second-last sector or the first 4 by= tes of > + * the last sector (search space: 515 bytes) */ > + length =3D bdrv_getlength(file_bs); > + if (length < 512) { > + return length < 0 ? length : -EINVAL; dmg_open() should pass in Error *errp so a detailed error reporting can be used: if (length < 0) { error_setg_errno(errp, -length, "Failed to get file size while reading = UDIF trailer"); return length; } else if (length < 512) { error_set(errp, "dmg file must be at least 512 bytes long"); return -EINVAL; } This makes it much easier to pinpoint errors (instead of just -EINVAL) and also gives the user a hint about the cause. > + } > + if (length > 511 + 512) { > + offset =3D length - 511 - 512; > + } > + length =3D length < 515 ? length : 515; > + ret =3D bdrv_pread(file_bs, offset, buffer, length); > + if (ret < 4) { > + return ret < 0 ? ret : -EINVAL; bdrv_pread() does not return short reads. The return value will either be length or an error. This could be just: if (ret < 0) { error_setg_errno(errp, -ret, "Failed to read last sectors in dmg file"); return ret; } (The unique error string makes it easy to track down the location where the error occurs.) > + } > + for (i =3D 0; i < length - 3; i++) { > + if (buffer[i] =3D=3D 'k' && buffer[i+1] =3D=3D 'o' && > + buffer[i+2] =3D=3D 'l' && buffer[i+3] =3D=3D 'y') { > + return offset + i; > + } > + } > + return -EINVAL; error_set(errp, "Not a dmg file, unable to find UDIF footer"); return -EINVAL; --u/L2/WlOHZg+YGU4 Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJUq+SbAAoJEJykq7OBq3PIaDkH/2mQo+/7Gfgld0QIs5rBzg35 utkFzboy2V0HoG4jhZf0aBUXcThj6d4JcLM/I2ah0sEzVOcY2eowVL64PY/YiGRQ EWvCyA0B/n0Yk//oUJG2ZHJrj8DpsLH0DcdjrLggdvKPfakjR/I0qaAIl+N0LLGc XHigShKcQXgdIf2qDhfgk0lHj+Gy4At+23kYz7Iy1q/1/Fnx11YYInuxPN9j7gnI hbxQZ7dMs4OyG0OETyWS704hpTbAvmD4YsycmR2ElVkV06qB8sWW3Iib7UVSxBsD ONNrYg54EU5MJ3XuPLb1/luPaw7wHQFYwzi4QQhzYX99CoQxJzTtuzGo7qEQUog= =A1Qc -----END PGP SIGNATURE----- --u/L2/WlOHZg+YGU4--