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