From dcda2bf4c11071b2f9d68806a7f800d059dafaf4 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 14 Feb 2019 16:20:25 +0100 Subject: [PATCH] util: Introduce function for allocating virStorageSource Add virStorageSourceNew and refactor places allocating that structure to use the helper. Signed-off-by: Peter Krempa Reviewed-by: Erik Skultety --- src/conf/domain_conf.c | 10 +++++----- src/conf/snapshot_conf.c | 4 ++-- src/conf/storage_conf.c | 2 +- src/libvirt_private.syms | 1 + src/qemu/qemu_domain.c | 4 ++-- src/qemu/qemu_driver.c | 2 +- src/qemu/qemu_migration.c | 4 ++-- src/storage/storage_backend_gluster.c | 2 +- src/storage/storage_backend_logical.c | 2 +- src/storage/storage_util.c | 4 ++-- src/util/virstoragefile.c | 20 ++++++++++++++++---- src/util/virstoragefile.h | 1 + tests/qemublocktest.c | 2 +- tests/virstoragetest.c | 2 +- 14 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index cd69323f28..614fae9b4f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1881,7 +1881,7 @@ virDomainDiskDefNew(virDomainXMLOptionPtr xmlopt) if (VIR_ALLOC(ret) < 0) return NULL; - if (VIR_ALLOC(ret->src) < 0) + if (!(ret->src = virStorageSourceNew())) goto error; if (xmlopt && @@ -2099,7 +2099,7 @@ virDomainFSDefNew(void) if (VIR_ALLOC(ret) < 0) return NULL; - if (VIR_ALLOC(ret->src) < 0) + if (!(ret->src = virStorageSourceNew())) goto cleanup; return ret; @@ -7674,7 +7674,7 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode, /* For the purposes of command line creation, this needs to look * like a disk storage source */ - if (VIR_ALLOC(iscsisrc->src) < 0) + if (!(iscsisrc->src = virStorageSourceNew())) return -1; iscsisrc->src->type = VIR_STORAGE_TYPE_NETWORK; iscsisrc->src->protocol = VIR_STORAGE_NET_PROTOCOL_ISCSI; @@ -9167,7 +9167,7 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr ctxt, goto cleanup; } - if (VIR_ALLOC(backingStore) < 0) + if (!(backingStore = virStorageSourceNew())) goto cleanup; /* backing store is always read-only */ @@ -9325,7 +9325,7 @@ virDomainDiskDefMirrorParse(virDomainDiskDefPtr def, char *blockJob = NULL; int ret = -1; - if (VIR_ALLOC(def->mirror) < 0) + if (!(def->mirror = virStorageSourceNew())) goto cleanup; if ((blockJob = virXMLPropString(cur, "job"))) { diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c index b16f450a01..5127ebbdfd 100644 --- a/src/conf/snapshot_conf.c +++ b/src/conf/snapshot_conf.c @@ -122,7 +122,7 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node, ctxt->node = node; - if (VIR_ALLOC(def->src) < 0) + if (!(def->src = virStorageSourceNew())) goto cleanup; def->name = virXMLPropString(node, "name"); @@ -621,7 +621,7 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr def, if (virBitmapIsBitSet(map, i)) continue; disk = &def->disks[ndisks++]; - if (VIR_ALLOC(disk->src) < 0) + if (!(disk->src = virStorageSourceNew())) goto cleanup; if (VIR_STRDUP(disk->name, def->dom->disks[i]->dst) < 0) goto cleanup; diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c index a2ddecf0f2..d7c17db669 100644 --- a/src/conf/storage_conf.c +++ b/src/conf/storage_conf.c @@ -1200,7 +1200,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, } if ((backingStore = virXPathString("string(./backingStore/path)", ctxt))) { - if (VIR_ALLOC(def->target.backingStore) < 0) + if (!(def->target.backingStore = virStorageSourceNew())) return NULL; def->target.backingStore->type = VIR_STORAGE_TYPE_FILE; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 67579742fd..0a959c4bf9 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2925,6 +2925,7 @@ virStorageSourceIsLocalStorage; virStorageSourceIsRelative; virStorageSourceIsSameLocation; virStorageSourceNetworkAssignDefaultPorts; +virStorageSourceNew; virStorageSourceNewFromBacking; virStorageSourceNewFromBackingAbsolute; virStorageSourceParseRBDColonString; diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index ac01e861f7..0c8feb25be 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2742,7 +2742,7 @@ qemuDomainObjPrivateXMLParseJobNBDSource(xmlNodePtr node, goto cleanup; } - if (VIR_ALLOC(migrSource) < 0) + if (!(migrSource = virStorageSourceNew())) goto cleanup; if (!(type = virXMLPropString(ctxt->node, "type"))) { @@ -9050,7 +9050,7 @@ qemuDomainDetermineDiskChain(virQEMUDriverPtr driver, /* terminate the chain for such images as the code below would do */ if (!disksrc->backingStore && - VIR_ALLOC(disksrc->backingStore) < 0) + !(disksrc->backingStore = virStorageSourceNew())) goto cleanup; /* host cdrom requires special treatment in qemu, so we need to check diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 971f915619..f94955a22f 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -17984,7 +17984,7 @@ qemuDomainBlockRebase(virDomainPtr dom, const char *path, const char *base, return qemuDomainBlockPullCommon(driver, vm, path, base, bandwidth, flags); /* If we got here, we are doing a block copy rebase. */ - if (VIR_ALLOC(dest) < 0) + if (!(dest = virStorageSourceNew())) goto cleanup; dest->type = (flags & VIR_DOMAIN_BLOCK_REBASE_COPY_DEV) ? VIR_STORAGE_TYPE_BLOCK : VIR_STORAGE_TYPE_FILE; diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index c93ae33476..8a9dbb5e63 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -794,14 +794,14 @@ qemuMigrationSrcNBDStorageCopyBlockdev(virQEMUDriverPtr driver, VIR_DEBUG("starting blockdev mirror for disk=%s to host=%s", diskAlias, host); - if (VIR_ALLOC(copysrc) < 0) + if (!(copysrc = virStorageSourceNew())) goto cleanup; copysrc->type = VIR_STORAGE_TYPE_NETWORK; copysrc->protocol = VIR_STORAGE_NET_PROTOCOL_NBD; copysrc->format = VIR_STORAGE_FILE_RAW; - if (VIR_ALLOC(copysrc->backingStore) < 0) + if (!(copysrc->backingStore = virStorageSourceNew())) goto cleanup; if (VIR_STRDUP(copysrc->path, diskAlias) < 0) diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_backend_gluster.c index 846a647cb6..854ecf2b67 100644 --- a/src/storage/storage_backend_gluster.c +++ b/src/storage/storage_backend_gluster.c @@ -294,7 +294,7 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlusterStatePtr state, goto cleanup; if (meta->backingStoreRaw) { - if (VIR_ALLOC(vol->target.backingStore) < 0) + if (!(vol->target.backingStore = virStorageSourceNew())) goto cleanup; vol->target.backingStore->type = VIR_STORAGE_TYPE_NETWORK; diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c index f153d23aec..77e4dfb8b1 100644 --- a/src/storage/storage_backend_logical.c +++ b/src/storage/storage_backend_logical.c @@ -308,7 +308,7 @@ virStorageBackendLogicalMakeVol(char **const groups, * lv is created with "--virtualsize"). */ if (groups[1] && STRNEQ(groups[1], "") && (groups[1][0] != '[')) { - if (VIR_ALLOC(vol->target.backingStore) < 0) + if (!(vol->target.backingStore = virStorageSourceNew())) goto cleanup; if (virAsprintf(&vol->target.backingStore->path, "%s/%s", diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index f18e38733a..3a6cc34b68 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -3402,7 +3402,7 @@ storageBackendProbeTarget(virStorageSourcePtr target, if (!virStorageSourceIsLocalStorage(target->backingStore)) { virStorageSourceFree(target->backingStore); - if (VIR_ALLOC(target->backingStore) < 0) + if (!(target->backingStore = virStorageSourceNew())) return -1; target->backingStore->type = VIR_STORAGE_TYPE_NETWORK; @@ -3576,7 +3576,7 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool) goto cleanup; VIR_DIR_CLOSE(dir); - if (VIR_ALLOC(target)) + if (!(target = virStorageSourceNew())) goto cleanup; if ((fd = open(def->target.path, O_RDONLY)) < 0) { diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index a6d44b64a4..7f52a5fdc7 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -2245,7 +2245,7 @@ virStorageSourceCopy(const virStorageSource *src, { virStorageSourcePtr def = NULL; - if (VIR_ALLOC(def) < 0) + if (!(def = virStorageSourceNew())) return NULL; def->id = src->id; @@ -2562,6 +2562,18 @@ virStorageSourceClear(virStorageSourcePtr def) } +virStorageSourcePtr +virStorageSourceNew(void) +{ + virStorageSourcePtr ret = NULL; + + if (VIR_ALLOC(ret) < 0) + return NULL; + + return ret; +} + + void virStorageSourceFree(virStorageSourcePtr def) { @@ -2580,7 +2592,7 @@ virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent, virStorageSourcePtr def; VIR_AUTOFREE(char *) dirname = NULL; - if (VIR_ALLOC(def) < 0) + if (!(def = virStorageSourceNew())) return NULL; /* store relative name */ @@ -3627,7 +3639,7 @@ virStorageSourceNewFromBackingAbsolute(const char *path) virStorageSourcePtr def; int rc; - if (VIR_ALLOC(def) < 0) + if (!(def = virStorageSourceNew())) return NULL; if (virStorageIsFile(path)) { @@ -4900,7 +4912,7 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src, } } else { /* add terminator */ - if (VIR_ALLOC(backingStore) < 0) + if (!(backingStore = virStorageSourceNew())) goto cleanup; } diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index 8c3a36d473..48af06653e 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -431,6 +431,7 @@ int virStorageSourceGetActualType(const virStorageSource *def); bool virStorageSourceIsLocalStorage(const virStorageSource *src); bool virStorageSourceIsEmpty(virStorageSourcePtr src); bool virStorageSourceIsBlockLocal(const virStorageSource *src); +virStorageSourcePtr virStorageSourceNew(void); void virStorageSourceFree(virStorageSourcePtr def); void virStorageSourceBackingStoreClear(virStorageSourcePtr def); int virStorageSourceUpdatePhysicalSize(virStorageSourcePtr src, diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c index d7e5e72a0b..813b20a08d 100644 --- a/tests/qemublocktest.c +++ b/tests/qemublocktest.c @@ -55,7 +55,7 @@ testBackingXMLjsonXML(const void *args) VIR_AUTOPTR(virStorageSource) xmlsrc = NULL; VIR_AUTOPTR(virStorageSource) jsonsrc = NULL; - if (VIR_ALLOC(xmlsrc) < 0) + if (!(xmlsrc = virStorageSourceNew())) return -1; xmlsrc->type = data->type; diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index 646ae78ff0..da70beb1f7 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -98,7 +98,7 @@ testStorageFileGetMetadata(const char *path, virStorageSourcePtr ret = NULL; VIR_AUTOPTR(virStorageSource) def = NULL; - if (VIR_ALLOC(def) < 0) + if (!(def = virStorageSourceNew())) return NULL; def->type = VIR_STORAGE_TYPE_FILE;