Let's do some more code reuse - there are 3 other callers that care to
check/get the compress program. Each of those though cares whether the
requested cfg image is valid and exists. So, add a parameter to handle
those cases.

NB: We won't need to initialize the returned value in the case where
the cfg image doesn't exist since the called program will handle that.

Signed-off-by: John Ferlan <jfer...@redhat.com>
---
 src/qemu/qemu_driver.c | 99 ++++++++++++++++++++------------------------------
 1 file changed, 40 insertions(+), 59 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 4cd0a07..505dd29 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3270,6 +3270,9 @@ qemuCompressProgramAvailable(virQEMUSaveFormat compress)
 /* qemuGetCompressionProgram:
  * @imageFormat: String representation from qemu.conf for the compression
  *               image format being used (dump, save, or snapshot).
+ * @use_raw_on_fail: Boolean indicating how to handle the error path. For
+ *                   callers that are OK with invalid data or inability to
+ *                   find the compression program, just return a raw format
  *
  * Returns:
  *    virQEMUSaveFormat    - Integer representation of the compression
@@ -3280,7 +3283,8 @@ qemuCompressProgramAvailable(virQEMUSaveFormat compress)
  *                           indicating none.
  */
 static virQEMUSaveFormat
-qemuGetCompressionProgram(const char *imageFormat)
+qemuGetCompressionProgram(const char *imageFormat,
+                          bool use_raw_on_fail)
 {
     virQEMUSaveFormat ret;
 
@@ -3296,17 +3300,31 @@ qemuGetCompressionProgram(const char *imageFormat)
     return ret;
 
  error:
-    if (ret < 0)
-        VIR_WARN("%s", _("Invalid dump image format specified in "
-                         "configuration file, using raw"));
-    else
-        VIR_WARN("%s", _("Compression program for dump image format "
-                         "in configuration file isn't available, "
-                         "using raw"));
+    if (ret < 0) {
+        if (use_raw_on_fail)
+            VIR_WARN("%s", _("Invalid dump image format specified in "
+                             "configuration file, using raw"));
+        else
+            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                           _("Invalid save image format specified "
+                             "in configuration file"));
+    } else {
+        if (use_raw_on_fail)
+            VIR_WARN("%s", _("Compression program for dump image format "
+                             "in configuration file isn't available, "
+                             "using raw"));
+        else
+            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                           _("Compression program for image format "
+                             "in configuration file isn't available"));
+    }
 
     /* Use "raw" as the format if the specified format is not valid,
      * or the compress program is not available. */
-    return QEMU_SAVE_FORMAT_RAW;
+    if (use_raw_on_fail)
+        return QEMU_SAVE_FORMAT_RAW;
+
+    return -1;
 }
 
 
@@ -3315,7 +3333,7 @@ qemuDomainSaveFlags(virDomainPtr dom, const char *path, 
const char *dxml,
                     unsigned int flags)
 {
     virQEMUDriverPtr driver = dom->conn->privateData;
-    int compressed = QEMU_SAVE_FORMAT_RAW;
+    int compressed;
     int ret = -1;
     virDomainObjPtr vm = NULL;
     virQEMUDriverConfigPtr cfg = NULL;
@@ -3325,21 +3343,9 @@ qemuDomainSaveFlags(virDomainPtr dom, const char *path, 
const char *dxml,
                   VIR_DOMAIN_SAVE_PAUSED, -1);
 
     cfg = virQEMUDriverGetConfig(driver);
-    if (cfg->saveImageFormat) {
-        compressed = qemuSaveCompressionTypeFromString(cfg->saveImageFormat);
-        if (compressed < 0) {
-            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                           _("Invalid save image format specified "
-                             "in configuration file"));
-            goto cleanup;
-        }
-        if (!qemuCompressProgramAvailable(compressed)) {
-            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                           _("Compression program for image format "
-                             "in configuration file isn't available"));
-            goto cleanup;
-        }
-    }
+    if ((compressed = qemuGetCompressionProgram(cfg->saveImageFormat,
+                                                false)) < 0)
+        goto cleanup;
 
     if (!(vm = qemuDomObjFromDomain(dom)))
         goto cleanup;
@@ -3388,7 +3394,7 @@ qemuDomainManagedSave(virDomainPtr dom, unsigned int 
flags)
 {
     virQEMUDriverPtr driver = dom->conn->privateData;
     virQEMUDriverConfigPtr cfg = NULL;
-    int compressed = QEMU_SAVE_FORMAT_RAW;
+    int compressed;
     virDomainObjPtr vm;
     char *name = NULL;
     int ret = -1;
@@ -3415,21 +3421,9 @@ qemuDomainManagedSave(virDomainPtr dom, unsigned int 
flags)
     }
 
     cfg = virQEMUDriverGetConfig(driver);
-    if (cfg->saveImageFormat) {
-        compressed = qemuSaveCompressionTypeFromString(cfg->saveImageFormat);
-        if (compressed < 0) {
-            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                           _("Invalid save image format specified "
-                             "in configuration file"));
-            goto cleanup;
-        }
-        if (!qemuCompressProgramAvailable(compressed)) {
-            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                           _("Compression program for image format "
-                             "in configuration file isn't available"));
-            goto cleanup;
-        }
-    }
+    if ((compressed = qemuGetCompressionProgram(cfg->saveImageFormat,
+                                                false)) < 0)
+        goto cleanup;
 
     if (!(name = qemuDomainManagedSavePath(driver, vm)))
         goto cleanup;
@@ -3590,7 +3584,7 @@ doCoreDump(virQEMUDriverPtr driver,
 
     /* We reuse "save" flag for "dump" here. Then, we can support the same
      * format in "save" and "dump". */
-    compress = qemuGetCompressionProgram(cfg->dumpImageFormat);
+    compress = qemuGetCompressionProgram(cfg->dumpImageFormat, true);
 
     /* Create an empty file with appropriate ownership.  */
     if (dump_flags & VIR_DUMP_BYPASS_CACHE) {
@@ -14318,7 +14312,7 @@ qemuDomainSnapshotCreateActiveExternal(virConnectPtr 
conn,
     int thaw = 0; /* 1 if freeze succeeded, -1 if freeze failed */
     bool pmsuspended = false;
     virQEMUDriverConfigPtr cfg = NULL;
-    int compressed = QEMU_SAVE_FORMAT_RAW;
+    int compressed;
 
     /* If quiesce was requested, then issue a freeze command, and a
      * counterpart thaw command when it is actually sent to agent.
@@ -14379,22 +14373,9 @@ qemuDomainSnapshotCreateActiveExternal(virConnectPtr 
conn,
                                           JOB_MASK(QEMU_JOB_MIGRATION_OP)));
 
         cfg = virQEMUDriverGetConfig(driver);
-        if (cfg->snapshotImageFormat) {
-            compressed = 
qemuSaveCompressionTypeFromString(cfg->snapshotImageFormat);
-            if (compressed < 0) {
-                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                               _("Invalid snapshot image format specified "
-                                 "in configuration file"));
-                goto cleanup;
-            }
-
-            if (!qemuCompressProgramAvailable(compressed)) {
-                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                               _("Compression program for image format "
-                                 "in configuration file isn't available"));
-                goto cleanup;
-            }
-        }
+        if ((compressed = qemuGetCompressionProgram(cfg->snapshotImageFormat,
+                                                    false)) < 0)
+            goto cleanup;
 
         if (!(xml = qemuDomainDefFormatLive(driver, vm->def, true, true)))
             goto cleanup;
-- 
2.7.4

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to