]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: blockcopy: Refactor logic checking the target storage file
authorPeter Krempa <pkrempa@redhat.com>
Tue, 11 Jul 2017 15:45:12 +0000 (17:45 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 20 Jul 2017 08:07:48 +0000 (10:07 +0200)
Use virStorageSource accessors to check the file and call
virStorageFileAccess before even attempting to stat the target. This
will be helpful once we try to add network destinations for block copy,
since there will be no need to stat them.

src/qemu/qemu_driver.c

index 9c709fb26a0d535e0381c438b8ffd6688d83506d..97b23245fb6814a0d973659991f9a6f410b8f758 100644 (file)
@@ -16692,36 +16692,49 @@ qemuDomainBlockCopyValidateMirror(virStorageSourcePtr mirror,
     int desttype = virStorageSourceGetActualType(mirror);
     struct stat st;
 
-    if (stat(mirror->path, &st) < 0) {
+    if (virStorageFileAccess(mirror, F_OK) < 0) {
         if (errno != ENOENT) {
-            virReportSystemError(errno, _("unable to stat for disk %s: %s"),
-                                 dst, mirror->path);
+            virReportSystemError(errno, "%s",
+                                 _("unable to verify existance of "
+                                   "block copy target"));
             return -1;
-        } else if (*reuse || desttype == VIR_STORAGE_TYPE_BLOCK) {
+        }
+
+        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);
+    } else {
+        if (virStorageFileStat(mirror, &st) < 0) {
+            virReportSystemError(errno,
+                                 _("unable to stat block copy target '%s'"),
+                                 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;
+
+        if (S_ISBLK(st.st_mode)) {
+            /* if the target is a block device, assume that we are reusing it,
+             * so there are no attempts to create it */
+            *reuse = true;
+        } else {
+            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;