From: Peter Krempa Date: Tue, 11 Jul 2017 15:45:12 +0000 (+0200) Subject: qemu: blockcopy: Refactor logic checking the target storage file X-Git-Tag: v3.6.0-rc1~118 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=703abf1d79038dc1943e161d45982dcd045da34b;p=thirdparty%2Flibvirt.git qemu: blockcopy: Refactor logic checking the target storage file 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. --- diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 9c709fb26a..97b23245fb 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -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;