From 09b91873d124019c0ddfc5c714191443a010f581 Mon Sep 17 00:00:00 2001 Message-Id: <09b91873d124019c0ddfc5c714191443a010f581.1418766606.git.jen@redhat.com> In-Reply-To: <6f81b4847eb68ebdf54a8f1a771e19d112d74152.1418766606.git.jen@redhat.com> References: <6f81b4847eb68ebdf54a8f1a771e19d112d74152.1418766606.git.jen@redhat.com> From: Fam Zheng Date: Thu, 4 Dec 2014 00:05:05 -0600 Subject: [CHANGE 11/31] vmdk: extract vmdk_read_desc To: rhvirt-patches@redhat.com, jen@redhat.com RH-Author: Fam Zheng Message-id: <1417651524-18041-12-git-send-email-famz@redhat.com> Patchwork-id: 62684 O-Subject: [RHEL-7.1 qemu-kvm PATCH v5 11/30] vmdk: extract vmdk_read_desc Bugzilla: 1134251 RH-Acked-by: Jeffrey Cody RH-Acked-by: Markus Armbruster RH-Acked-by: Max Reitz From: Paolo Bonzini Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng Signed-off-by: Kevin Wolf (cherry picked from commit a8842e6d2acc815e9660cc743bd0b0daba18c935) Signed-off-by: Fam Zheng Signed-off-by: Jeff E. Nelson --- block/vmdk.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index eda2962..425064c 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -529,6 +529,32 @@ static int vmdk_open_vmfs_sparse(BlockDriverState *bs, static int vmdk_open_desc_file(BlockDriverState *bs, int flags, uint64_t desc_offset, Error **errp); +static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset, + Error **errp) +{ + int64_t size; + char *buf; + int ret; + + size = bdrv_getlength(file); + if (size < 0) { + error_setg_errno(errp, -size, "Could not access file"); + return NULL; + } + + size = MIN(size, 1 << 20); /* avoid unbounded allocation */ + buf = g_malloc0(size + 1); + + ret = bdrv_pread(file, desc_offset, buf, size); + if (ret < 0) { + error_setg_errno(errp, -ret, "Could not read from file"); + g_free(buf); + return NULL; + } + + return buf; +} + static int vmdk_open_vmdk4(BlockDriverState *bs, BlockDriverState *file, int flags, Error **errp) @@ -823,23 +849,16 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags, uint64_t desc_offset, Error **errp) { int ret; - char *buf = NULL; + char *buf; char ct[128]; BDRVVmdkState *s = bs->opaque; - int64_t size; - size = bdrv_getlength(bs->file); - if (size < 0) { + buf = vmdk_read_desc(bs->file, desc_offset, errp); + if (!buf) { return -EINVAL; - } - - size = MIN(size, 1 << 20); /* avoid unbounded allocation */ - buf = g_malloc0(size + 1); - - ret = bdrv_pread(bs->file, desc_offset, buf, size); - if (ret < 0) { goto exit; } + if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) { error_setg(errp, "invalid VMDK image descriptor"); ret = -EINVAL; -- 2.1.0