Blob Blame History Raw
From 6dda778b55b7508e626635fa67d8f727167a8bed Mon Sep 17 00:00:00 2001
Message-Id: <6dda778b55b7508e626635fa67d8f727167a8bed@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Tue, 2 Aug 2016 13:42:00 +0200
Subject: [PATCH] qemu: command: Refactor code extracted to
 qemuBuildDriveSourceStr

Avoid a large block by tweaking the condition skipping empty drives and
split up the switch containing two branches having different purpose.

(cherry picked from commit 1575f3e8d32b6049ec399cfb54ab36f4d0a0cb00)
https://bugzilla.redhat.com/show_bug.cgi?id=1247521 [gluster multi-host]
---
 src/qemu/qemu_command.c | 119 +++++++++++++++++++++++-------------------------
 1 file changed, 57 insertions(+), 62 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index bb40832..a3e838c 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1107,70 +1107,65 @@ qemuBuildDriveSourceStr(virDomainDiskDefPtr disk,
     if (qemuGetDriveSourceString(disk->src, secinfo, &source) < 0)
         goto cleanup;
 
-    if (source &&
-        !((disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
-           disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) &&
-          disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN)) {
-
-        virBufferAddLit(buf, "file=");
-
-        switch (actualType) {
-        case VIR_STORAGE_TYPE_DIR:
-            /* QEMU only supports magic FAT format for now */
-            if (disk->src->format > 0 &&
-                disk->src->format != VIR_STORAGE_FILE_FAT) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("unsupported disk driver type for '%s'"),
-                               virStorageFileFormatTypeToString(disk->src->format));
-                goto cleanup;
-            }
-
-            if (!disk->src->readonly) {
-                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                               _("cannot create virtual FAT disks in read-write mode"));
-                goto cleanup;
-            }
-
-            virBufferAddLit(buf, "fat:");
-
-            if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
-                virBufferAddLit(buf, "floppy:");
-
-            break;
-
-        case VIR_STORAGE_TYPE_BLOCK:
-            if (disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                               disk->src->type == VIR_STORAGE_TYPE_VOLUME ?
-                               _("tray status 'open' is invalid for block type volume") :
-                               _("tray status 'open' is invalid for block type disk"));
-                goto cleanup;
-            }
-
-            break;
-
-        default:
-            break;
-        }
-
-        virQEMUBuildBufferEscapeComma(buf, source);
-        virBufferAddLit(buf, ",");
-
-        if (secinfo && secinfo->type == VIR_DOMAIN_SECRET_INFO_TYPE_AES) {
-            virBufferAsprintf(buf, "password-secret=%s,",
-                              secinfo->s.aes.alias);
-        }
-
-        if (encinfo)
-            virQEMUBuildLuksOpts(buf, &disk->src->encryption->encinfo,
-                                 encinfo->s.aes.alias);
-
-        if (disk->src->format > 0 &&
-            disk->src->type != VIR_STORAGE_TYPE_DIR)
-            virBufferAsprintf(buf, "format=%s,",
-                              virStorageFileFormatTypeToString(disk->src->format));
+    /* nothing to format if the drive is empty */
+    if (!source ||
+        ((disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
+          disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) &&
+         disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN)) {
+        ret = 0;
+        goto cleanup;
     }
 
+    if (actualType == VIR_STORAGE_TYPE_BLOCK &&
+        disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       disk->src->type == VIR_STORAGE_TYPE_VOLUME ?
+                       _("tray status 'open' is invalid for block type volume") :
+                       _("tray status 'open' is invalid for block type disk"));
+        goto cleanup;
+    }
+
+    virBufferAddLit(buf, "file=");
+
+    /* for now the DIR based storage is handled by the magic FAT format */
+    if (actualType == VIR_STORAGE_TYPE_DIR) {
+        if (disk->src->format > 0 &&
+            disk->src->format != VIR_STORAGE_FILE_FAT) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("unsupported disk driver type for '%s'"),
+                           virStorageFileFormatTypeToString(disk->src->format));
+            goto cleanup;
+        }
+
+        if (!disk->src->readonly) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("cannot create virtual FAT disks in read-write mode"));
+            goto cleanup;
+        }
+
+        virBufferAddLit(buf, "fat:");
+
+        if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
+            virBufferAddLit(buf, "floppy:");
+    }
+
+    virQEMUBuildBufferEscapeComma(buf, source);
+    virBufferAddLit(buf, ",");
+
+    if (secinfo && secinfo->type == VIR_DOMAIN_SECRET_INFO_TYPE_AES) {
+        virBufferAsprintf(buf, "password-secret=%s,",
+                          secinfo->s.aes.alias);
+    }
+
+    if (encinfo)
+        virQEMUBuildLuksOpts(buf, &disk->src->encryption->encinfo,
+                             encinfo->s.aes.alias);
+
+    if (disk->src->format > 0 &&
+        disk->src->type != VIR_STORAGE_TYPE_DIR)
+        virBufferAsprintf(buf, "format=%s,",
+                          virStorageFileFormatTypeToString(disk->src->format));
+
     ret = 0;
 
  cleanup:
-- 
2.9.2