[libvirt] [PATCH 4/5] qemu: blockcopy: Split out checking of the target image file

Peter Krempa pkrempa at redhat.com
Tue Jul 11 15:46:41 UTC 2017


Move the code into a separate function so that the flow of creating the
copy is more obvious and split into logical pieces.
---
 src/qemu/qemu_driver.c | 79 +++++++++++++++++++++++++++++---------------------
 1 file changed, 46 insertions(+), 33 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 48dc5e5cc..5beb14e64 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -16685,6 +16685,50 @@ qemuDomainBlockJobSetSpeed(virDomainPtr dom,
 }


+static int
+qemuDomainBlockCopyValidateMirror(virStorageSourcePtr mirror,
+                                  const char *dst,
+                                  bool *reuse)
+{
+    int desttype = virStorageSourceGetActualType(mirror);
+    struct stat st;
+
+    if (stat(mirror->path, &st) < 0) {
+        if (errno != ENOENT) {
+            virReportSystemError(errno, _("unable to stat for disk %s: %s"),
+                                 dst, mirror->path);
+            return -1;
+        } else if (*reuse || desttype == VIR_STORAGE_TYPE_BLOCK) {
+            virReportSystemError(errno,
+                                 _("missing destination file for disk %s: %s"),
+                                 dst, mirror->path);
+            return -1;
+        }
+    } else if (!S_ISBLK(st.st_mode)) {
+        if (st.st_size && !(*reuse)) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("external destination file for disk %s already "
+                             "exists and is not a block device: %s"),
+                           dst, mirror->path);
+            return -1;
+        }
+        if (desttype == VIR_STORAGE_TYPE_BLOCK) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("blockdev flag requested for disk %s, but file "
+                             "'%s' is not a block device"),
+                           dst, mirror->path);
+            return -1;
+        }
+    } else {
+        /* if the target is a block device, assume that we are reusing it, so
+         * there are no attempts to create it */
+        *reuse = true;
+    }
+
+    return 0;
+}
+
+
 /* bandwidth in bytes/s.  Caller must lock vm beforehand, and not
  * access mirror afterwards.  */
 static int
@@ -16703,11 +16747,9 @@ qemuDomainBlockCopyCommon(virDomainObjPtr vm,
     char *device = NULL;
     virDomainDiskDefPtr disk = NULL;
     int ret = -1;
-    struct stat st;
     bool need_unlink = false;
     virQEMUDriverConfigPtr cfg = NULL;
     const char *format = NULL;
-    int desttype = virStorageSourceGetActualType(mirror);
     virErrorPtr monitor_error = NULL;
     bool reuse = !!(flags & VIR_DOMAIN_BLOCK_COPY_REUSE_EXT);

@@ -16790,37 +16832,8 @@ qemuDomainBlockCopyCommon(virDomainObjPtr vm,
     if (qemuDomainStorageFileInit(driver, vm, mirror) < 0)
         goto endjob;

-    if (stat(mirror->path, &st) < 0) {
-        if (errno != ENOENT) {
-            virReportSystemError(errno, _("unable to stat for disk %s: %s"),
-                                 disk->dst, mirror->path);
-            goto endjob;
-        } else if (reuse || desttype == VIR_STORAGE_TYPE_BLOCK) {
-            virReportSystemError(errno,
-                                 _("missing destination file for disk %s: %s"),
-                                 disk->dst, mirror->path);
-            goto endjob;
-        }
-    } else if (!S_ISBLK(st.st_mode)) {
-        if (st.st_size && !reuse) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("external destination file for disk %s already "
-                             "exists and is not a block device: %s"),
-                           disk->dst, mirror->path);
-            goto endjob;
-        }
-        if (desttype == VIR_STORAGE_TYPE_BLOCK) {
-            virReportError(VIR_ERR_INVALID_ARG,
-                           _("blockdev flag requested for disk %s, but file "
-                             "'%s' is not a block device"),
-                           disk->dst, mirror->path);
-            goto endjob;
-        }
-    } else {
-        /* if the target is a block device, assume that we are reusing it, so
-         * there are no attempts to create it */
-        reuse = true;
-    }
+    if (qemuDomainBlockCopyValidateMirror(mirror, disk->dst, &reuse) < 0)
+        goto endjob;

     if (!mirror->format) {
         if (!reuse) {
-- 
2.12.2




More information about the libvir-list mailing list