From nobody Sun Feb 8 22:06:20 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1549651109463744.1597406605028; Fri, 8 Feb 2019 10:38:29 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 12E5DC0740DD; Fri, 8 Feb 2019 18:38:27 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B651117BAD; Fri, 8 Feb 2019 18:38:26 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5BC7018033B9; Fri, 8 Feb 2019 18:38:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x18Ibfi4007639 for ; Fri, 8 Feb 2019 13:37:41 -0500 Received: by smtp.corp.redhat.com (Postfix) id D9B2C60C55; Fri, 8 Feb 2019 18:37:41 +0000 (UTC) Received: from unknown0050b6a41c42.attlocal.net.com (ovpn-117-20.phx2.redhat.com [10.3.117.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9476C60BE0 for ; Fri, 8 Feb 2019 18:37:41 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Fri, 8 Feb 2019 13:37:09 -0500 Message-Id: <20190208183726.30903-16-jferlan@redhat.com> In-Reply-To: <20190208183726.30903-1-jferlan@redhat.com> References: <20190208183726.30903-1-jferlan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 15/32] storage: Use VIR_AUTOFREE for storage util X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 08 Feb 2019 18:38:28 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Let's make use of the auto __cleanup capabilities cleaning up any now unnecessary goto paths. Signed-off-by: John Ferlan Reviewed-by: Erik Skultety Reviewed-by: J=C3=A1n Tomko --- src/storage/storage_util.c | 335 +++++++++++++------------------------ 1 file changed, 113 insertions(+), 222 deletions(-) diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index e4913b3405..1c290f1995 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -136,9 +136,9 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol, size_t rbytes =3D READ_BLOCK_SIZE_DEFAULT; int wbytes =3D 0; int interval; - char *zerobuf =3D NULL; - char *buf =3D NULL; struct stat st; + VIR_AUTOFREE(char *) zerobuf =3D NULL; + VIR_AUTOFREE(char *) buf =3D NULL; =20 if ((inputfd =3D open(inputvol->target.path, O_RDONLY)) < 0) { ret =3D -errno; @@ -241,9 +241,6 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol, cleanup: VIR_FORCE_CLOSE(inputfd); =20 - VIR_FREE(zerobuf); - VIR_FREE(buf); - return ret; } =20 @@ -617,9 +614,9 @@ storageBackendCreatePloop(virStoragePoolObjPtr pool ATT= RIBUTE_UNUSED, unsigned int flags) { int ret =3D -1; - char *create_tool =3D NULL; bool created =3D false; VIR_AUTOPTR(virCommand) cmd =3D NULL; + VIR_AUTOFREE(char *) create_tool =3D NULL; =20 virCheckFlags(0, -1); =20 @@ -677,7 +674,6 @@ storageBackendCreatePloop(virStoragePoolObjPtr pool ATT= RIBUTE_UNUSED, created =3D true; ret =3D virCommandRun(cmd, NULL); cleanup: - VIR_FREE(create_tool); if (ret < 0 && created) virFileDeleteTree(vol->target.path); return ret; @@ -688,9 +684,8 @@ static int storagePloopResize(virStorageVolDefPtr vol, unsigned long long capacity) { - int ret =3D -1; - char *resize_tool =3D NULL; VIR_AUTOPTR(virCommand) cmd =3D NULL; + VIR_AUTOFREE(char *) resize_tool =3D NULL; =20 resize_tool =3D virFindFileInPath("ploop"); if (!resize_tool) { @@ -703,9 +698,7 @@ storagePloopResize(virStorageVolDefPtr vol, =20 virCommandAddArgFormat(cmd, "%s/DiskDescriptor.xml", vol->target.path); =20 - ret =3D virCommandRun(cmd, NULL); - VIR_FREE(resize_tool); - return ret; + return virCommandRun(cmd, NULL); } =20 =20 @@ -881,7 +874,7 @@ storageBackendCreateQemuImgSetBacking(virStoragePoolObj= Ptr pool, { virStoragePoolDefPtr def =3D virStoragePoolObjGetDef(pool); int accessRetCode =3D -1; - char *absolutePath =3D NULL; + VIR_AUTOFREE(char *) absolutePath =3D NULL; =20 if (info->format =3D=3D VIR_STORAGE_FILE_RAW) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", @@ -927,7 +920,6 @@ storageBackendCreateQemuImgSetBacking(virStoragePoolObj= Ptr pool, return -1; accessRetCode =3D access(absolutePath ? absolutePath : info->backingPath, R_OK); - VIR_FREE(absolutePath); if (accessRetCode !=3D 0) { virReportSystemError(errno, _("inaccessible backing store volume %s"), @@ -944,13 +936,12 @@ storageBackendCreateQemuImgSetOptions(virCommandPtr c= md, virStorageEncryptionInfoDefPtr encin= fo, struct _virStorageBackendQemuImgInfo= *info) { - char *opts =3D NULL; + VIR_AUTOFREE(char *) opts =3D NULL; =20 if (storageBackendCreateQemuImgOpts(encinfo, &opts, info) < 0) return -1; if (opts) virCommandAddArgList(cmd, "-o", opts, NULL); - VIR_FREE(opts); =20 return 0; } @@ -967,7 +958,7 @@ storageBackendCreateQemuImgSecretObject(virCommandPtr c= md, const char *secretAlias) { virBuffer buf =3D VIR_BUFFER_INITIALIZER; - char *commandStr =3D NULL; + VIR_AUTOFREE(char *) commandStr =3D NULL; =20 virBufferAsprintf(&buf, "secret,id=3D%s,file=3D", secretAlias); virQEMUBuildBufferEscapeComma(&buf, secretPath); @@ -981,7 +972,6 @@ storageBackendCreateQemuImgSecretObject(virCommandPtr c= md, =20 virCommandAddArgList(cmd, "--object", commandStr, NULL); =20 - VIR_FREE(commandStr); return 0; } =20 @@ -997,7 +987,7 @@ storageBackendResizeQemuImgImageOpts(virCommandPtr cmd, const char *secretAlias) { virBuffer buf =3D VIR_BUFFER_INITIALIZER; - char *commandStr =3D NULL; + VIR_AUTOFREE(char *) commandStr =3D NULL; =20 virBufferAsprintf(&buf, "driver=3Dluks,key-secret=3D%s,file.filename= =3D", secretAlias); @@ -1012,7 +1002,6 @@ storageBackendResizeQemuImgImageOpts(virCommandPtr cm= d, =20 virCommandAddArgList(cmd, "--image-opts", commandStr, NULL); =20 - VIR_FREE(commandStr); return 0; } =20 @@ -1118,9 +1107,9 @@ virStorageBackendCreateQemuImgCmdFromVol(virStoragePo= olObjPtr pool, .secretAlias =3D NULL, }; virStorageEncryptionPtr enc =3D vol->target.encryption; - char *inputSecretAlias =3D NULL; virStorageEncryptionPtr inputenc =3D inputvol ? inputvol->target.encry= ption : NULL; virStorageEncryptionInfoDefPtr encinfo =3D NULL; + VIR_AUTOFREE(char *) inputSecretAlias =3D NULL; =20 virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, NULL); =20 @@ -1222,13 +1211,11 @@ virStorageBackendCreateQemuImgCmdFromVol(virStorage= PoolObjPtr pool, =20 } VIR_FREE(info.secretAlias); - VIR_FREE(inputSecretAlias); =20 return cmd; =20 error: VIR_FREE(info.secretAlias); - VIR_FREE(inputSecretAlias); virCommandFree(cmd); return NULL; } @@ -1337,10 +1324,10 @@ storageBackendCreateQemuImg(virStoragePoolObjPtr po= ol, unsigned int flags) { int ret =3D -1; - char *create_tool; - char *secretPath =3D NULL; - char *inputSecretPath =3D NULL; virStorageVolEncryptConvertStep convertStep =3D VIR_STORAGE_VOL_ENCRYP= T_NONE; + VIR_AUTOFREE(char *) create_tool =3D NULL; + VIR_AUTOFREE(char *) secretPath =3D NULL; + VIR_AUTOFREE(char *) inputSecretPath =3D NULL; =20 virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, -1); =20 @@ -1388,15 +1375,10 @@ storageBackendCreateQemuImg(virStoragePoolObjPtr po= ol, } while (convertStep !=3D VIR_STORAGE_VOL_ENCRYPT_DONE); =20 cleanup: - if (secretPath) { + if (secretPath) unlink(secretPath); - VIR_FREE(secretPath); - } - if (inputSecretPath) { + if (inputSecretPath) unlink(inputSecretPath); - VIR_FREE(inputSecretPath); - } - VIR_FREE(create_tool); return ret; } =20 @@ -1717,23 +1699,16 @@ virStorageBackendVolOpen(const char *path, struct s= tat *sb, static bool storageBackendIsPloopDir(char *path) { - bool ret =3D false; - char *root =3D NULL; - char *desc =3D NULL; - if (virAsprintf(&root, "%s/root.hds", path) < 0) - return ret; - if (!virFileExists(root)) - goto cleanup; - if (virAsprintf(&desc, "%s/DiskDescriptor.xml", path) < 0) - goto cleanup; - if (!virFileExists(desc)) - goto cleanup; + VIR_AUTOFREE(char *) root =3D NULL; + VIR_AUTOFREE(char *) desc =3D NULL; =20 - ret =3D true; - cleanup: - VIR_FREE(root); - VIR_FREE(desc); - return ret; + if (virAsprintf(&root, "%s/root.hds", path) < 0 || + !virFileExists(root) || + virAsprintf(&desc, "%s/DiskDescriptor.xml", path) < 0 || + !virFileExists(desc)) + return false; + + return true; } =20 /* In case of ploop volumes, path to volume is the path to the ploop @@ -1745,20 +1720,14 @@ static int storageBackendRedoPloopUpdate(virStorageSourcePtr target, struct stat *sb, int *fd, unsigned int flags) { - char *path =3D NULL; - int ret =3D -1; + VIR_AUTOFREE(char *) path =3D NULL; =20 if (virAsprintf(&path, "%s/root.hds", target->path) < 0) return -1; VIR_FORCE_CLOSE(*fd); if ((*fd =3D virStorageBackendVolOpen(path, sb, flags)) < 0) - goto cleanup; - ret =3D virStorageBackendUpdateVolTargetInfoFD(target, *fd, sb); - - cleanup: - - VIR_FREE(path); - return ret; + return -1; + return virStorageBackendUpdateVolTargetInfoFD(target, *fd, sb); } =20 /* @@ -1784,8 +1753,8 @@ storageBackendUpdateVolTargetInfo(virStorageVolType v= oltype, { int ret, fd =3D -1; struct stat sb; - char *buf =3D NULL; ssize_t len =3D VIR_STORAGE_MAX_HEADER; + VIR_AUTOFREE(char *) buf =3D NULL; =20 if ((ret =3D virStorageBackendVolOpen(target->path, &sb, openflags)) <= 0) goto cleanup; @@ -1842,7 +1811,6 @@ storageBackendUpdateVolTargetInfo(virStorageVolType v= oltype, =20 cleanup: VIR_FORCE_CLOSE(fd); - VIR_FREE(buf); return ret; } =20 @@ -2316,12 +2284,12 @@ storageBackendResizeQemuImg(virStoragePoolObjPtr po= ol, unsigned long long capacity) { int ret =3D -1; - char *img_tool =3D NULL; const char *type; - char *secretPath =3D NULL; - char *secretAlias =3D NULL; virStorageEncryptionPtr enc =3D vol->target.encryption; VIR_AUTOPTR(virCommand) cmd =3D NULL; + VIR_AUTOFREE(char *) img_tool =3D NULL; + VIR_AUTOFREE(char *) secretPath =3D NULL; + VIR_AUTOFREE(char *) secretAlias =3D NULL; =20 if (enc && (enc->format =3D=3D VIR_STORAGE_ENCRYPTION_FORMAT_QCOW || enc->format =3D=3D VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT) = && @@ -2382,12 +2350,8 @@ storageBackendResizeQemuImg(virStoragePoolObjPtr poo= l, ret =3D virCommandRun(cmd, NULL); =20 cleanup: - VIR_FREE(img_tool); - if (secretPath) { + if (secretPath) unlink(secretPath); - VIR_FREE(secretPath); - } - VIR_FREE(secretAlias); return ret; } =20 @@ -2441,35 +2405,29 @@ virStorageBackendVolResizeLocal(virStoragePoolObjPt= r pool, static int storageBackendPloopHasSnapshots(char *path) { - char *output =3D NULL; char *snap_tool =3D NULL; - int ret =3D -1; VIR_AUTOPTR(virCommand) cmd =3D NULL; + VIR_AUTOFREE(char *) output =3D NULL; =20 snap_tool =3D virFindFileInPath("ploop"); if (!snap_tool) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("unable to find ploop, please install " "ploop tools")); - return ret; + return -1; } =20 cmd =3D virCommandNewArgList(snap_tool, "snapshot-list", NULL); virCommandAddArgFormat(cmd, "%s/DiskDescriptor.xml", path); virCommandSetOutputBuffer(cmd, &output); =20 - if ((ret =3D virCommandRun(cmd, NULL)) < 0) - goto cleanup; + if (virCommandRun(cmd, NULL) < 0) + return -1; =20 - if (!strstr(output, "root.hds.")) { - ret =3D 1; - goto cleanup; - } - ret =3D 0; + if (!strstr(output, "root.hds.")) + return 1; =20 - cleanup: - VIR_FREE(output); - return ret; + return 0; } =20 =20 @@ -2481,11 +2439,10 @@ virStorageBackendVolUploadLocal(virStoragePoolObjPt= r pool ATTRIBUTE_UNUSED, unsigned long long len, unsigned int flags) { - char *path =3D NULL; char *target_path =3D vol->target.path; - int ret =3D -1; int has_snap =3D 0; bool sparse =3D flags & VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM; + VIR_AUTOFREE(char *)path =3D NULL; =20 virCheckFlags(VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM, -1); /* if volume has target format VIR_STORAGE_FILE_PLOOP @@ -2496,12 +2453,12 @@ virStorageBackendVolUploadLocal(virStoragePoolObjPt= r pool ATTRIBUTE_UNUSED, /* Fail if the volume contains snapshots or we failed to check it.= */ has_snap =3D storageBackendPloopHasSnapshots(vol->target.path); if (has_snap < 0) { - goto cleanup; + return -1; } else if (!has_snap) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("can't upload volume, all existing snapshots" " will be lost")); - goto cleanup; + return -1; } =20 if (virAsprintf(&path, "%s/root.hds", vol->target.path) < 0) @@ -2511,12 +2468,8 @@ virStorageBackendVolUploadLocal(virStoragePoolObjPtr= pool ATTRIBUTE_UNUSED, =20 /* Not using O_CREAT because the file is required to already exist at * this point */ - ret =3D virFDStreamOpenBlockDevice(stream, target_path, - offset, len, sparse, O_WRONLY); - - cleanup: - VIR_FREE(path); - return ret; + return virFDStreamOpenBlockDevice(stream, target_path, + offset, len, sparse, O_WRONLY); } =20 int @@ -2527,34 +2480,29 @@ virStorageBackendVolDownloadLocal(virStoragePoolObj= Ptr pool ATTRIBUTE_UNUSED, unsigned long long len, unsigned int flags) { - char *path =3D NULL; char *target_path =3D vol->target.path; - int ret =3D -1; int has_snap =3D 0; bool sparse =3D flags & VIR_STORAGE_VOL_DOWNLOAD_SPARSE_STREAM; + VIR_AUTOFREE(char *) path =3D NULL; =20 virCheckFlags(VIR_STORAGE_VOL_DOWNLOAD_SPARSE_STREAM, -1); if (vol->target.format =3D=3D VIR_STORAGE_FILE_PLOOP) { has_snap =3D storageBackendPloopHasSnapshots(vol->target.path); if (has_snap < 0) { - goto cleanup; + return -1; } else if (!has_snap) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("can't download volume, all existing snapshot= s" " will be lost")); - goto cleanup; + return -1; } if (virAsprintf(&path, "%s/root.hds", vol->target.path) < 0) - goto cleanup; + return -1; target_path =3D path; } =20 - ret =3D virFDStreamOpenBlockDevice(stream, target_path, - offset, len, sparse, O_RDONLY); - - cleanup: - VIR_FREE(path); - return ret; + return virFDStreamOpenBlockDevice(stream, target_path, + offset, len, sparse, O_RDONLY); } =20 =20 @@ -2604,14 +2552,14 @@ storageBackendWipeLocal(const char *path, size_t writebuf_length, bool zero_end) { - int ret =3D -1, written =3D 0; + int written =3D 0; unsigned long long remaining =3D 0; off_t size; size_t write_size =3D 0; - char *writebuf =3D NULL; + VIR_AUTOFREE(char *) writebuf =3D NULL; =20 if (VIR_ALLOC_N(writebuf, writebuf_length) < 0) - goto cleanup; + return -1; =20 if (!zero_end) { if ((size =3D lseek(fd, 0, SEEK_SET)) < 0) { @@ -2619,7 +2567,7 @@ storageBackendWipeLocal(const char *path, _("Failed to seek to the start in volume " "with path '%s'"), path); - goto cleanup; + return -1; } } else { if ((size =3D lseek(fd, -wipe_len, SEEK_END)) < 0) { @@ -2627,7 +2575,7 @@ storageBackendWipeLocal(const char *path, _("Failed to seek to %llu bytes to the en= d " "in volume with path '%s'"), wipe_len, path); - goto cleanup; + return -1; } } =20 @@ -2644,7 +2592,7 @@ storageBackendWipeLocal(const char *path, "storage volume with path '%s'"), write_size, path); =20 - goto cleanup; + return -1; } =20 remaining -=3D written; @@ -2654,16 +2602,12 @@ storageBackendWipeLocal(const char *path, virReportSystemError(errno, _("cannot sync data to volume with path '%s'"= ), path); - goto cleanup; + return -1; } =20 VIR_DEBUG("Wrote %llu bytes to volume with path '%s'", wipe_len, path); =20 - ret =3D 0; - - cleanup: - VIR_FREE(writebuf); - return ret; + return 0; } =20 =20 @@ -2763,12 +2707,10 @@ static int storageBackendVolWipePloop(virStorageVolDefPtr vol, unsigned int algorithm) { - char *target_path =3D NULL; - char *disk_desc =3D NULL; - char *create_tool =3D NULL; VIR_AUTOPTR(virCommand) cmd =3D NULL; - - int ret =3D -1; + VIR_AUTOFREE(char *) target_path =3D NULL; + VIR_AUTOFREE(char *) disk_desc =3D NULL; + VIR_AUTOFREE(char *) create_tool =3D NULL; =20 create_tool =3D virFindFileInPath("ploop"); if (!create_tool) { @@ -2778,24 +2720,24 @@ storageBackendVolWipePloop(virStorageVolDefPtr vol, } =20 if (virAsprintf(&target_path, "%s/root.hds", vol->target.path) < 0) - goto cleanup; + return -1; =20 if (virAsprintf(&disk_desc, "%s/DiskDescriptor.xml", vol->target.path)= < 0) - goto cleanup; + return -1; =20 if (storageBackendVolWipeLocalFile(target_path, algorithm, vol->target.allocation, false) < 0) - goto cleanup; + return -1; =20 if (virFileRemove(disk_desc, 0, 0) < 0) { virReportError(errno, _("Failed to delete DiskDescriptor.xml of vo= lume '%s'"), vol->target.path); - goto cleanup; + return -1; } if (virFileRemove(target_path, 0, 0) < 0) { virReportError(errno, _("failed to delete root.hds of volume '%s'"= ), vol->target.path); - goto cleanup; + return -1; } =20 cmd =3D virCommandNewArgList(create_tool, "init", "-s", NULL); @@ -2804,13 +2746,7 @@ storageBackendVolWipePloop(virStorageVolDefPtr vol, (1024 * 1024))); virCommandAddArgList(cmd, "-t", "ext4", NULL); virCommandAddArg(cmd, target_path); - ret =3D virCommandRun(cmd, NULL); - - cleanup: - VIR_FREE(disk_desc); - VIR_FREE(target_path); - VIR_FREE(create_tool); - return ret; + return virCommandRun(cmd, NULL); } =20 =20 @@ -2850,20 +2786,19 @@ int virStorageBackendBuildLocal(virStoragePoolObjPtr pool) { virStoragePoolDefPtr def =3D virStoragePoolObjGetDef(pool); - int ret =3D -1; - char *parent =3D NULL; char *p =3D NULL; mode_t mode; bool needs_create_as_uid; unsigned int dir_create_flags; + VIR_AUTOFREE(char *) parent =3D NULL; =20 if (VIR_STRDUP(parent, def->target.path) < 0) - goto cleanup; + return -1; if (!(p =3D strrchr(parent, '/'))) { virReportError(VIR_ERR_INVALID_ARG, _("path '%s' is not absolute"), def->target.path); - goto cleanup; + return -1; } =20 if (p !=3D parent) { @@ -2873,7 +2808,7 @@ virStorageBackendBuildLocal(virStoragePoolObjPtr pool) if (virFileMakePath(parent) < 0) { virReportSystemError(errno, _("cannot create path '%s'"), parent); - goto cleanup; + return -1; } } =20 @@ -2890,18 +2825,11 @@ virStorageBackendBuildLocal(virStoragePoolObjPtr po= ol) /* Now create the final dir in the path with the uid/gid/mode * requested in the config. If the dir already exists, just set * the perms. */ - if (virDirCreate(def->target.path, - mode, - def->target.perms.uid, - def->target.perms.gid, - dir_create_flags) < 0) - goto cleanup; - - ret =3D 0; - - cleanup: - VIR_FREE(parent); - return ret; + return virDirCreate(def->target.path, + mode, + def->target.perms.uid, + def->target.perms.gid, + dir_create_flags); } =20 =20 @@ -2942,12 +2870,12 @@ virStorageUtilGlusterExtractPoolSources(const char = *host, { xmlDocPtr doc =3D NULL; xmlXPathContextPtr ctxt =3D NULL; - xmlNodePtr *nodes =3D NULL; virStoragePoolSource *src =3D NULL; - char *volname =3D NULL; size_t i; int nnodes; int ret =3D -1; + VIR_AUTOFREE(xmlNodePtr *) nodes =3D NULL; + VIR_AUTOFREE(char *) volname =3D NULL; =20 if (!(doc =3D virXMLParseStringCtxt(xml, _("(gluster_cli_output)"), &c= txt))) goto cleanup; @@ -2991,8 +2919,6 @@ virStorageUtilGlusterExtractPoolSources(const char *h= ost, ret =3D nnodes; =20 cleanup: - VIR_FREE(volname); - VIR_FREE(nodes); xmlXPathFreeContext(ctxt); xmlFreeDoc(doc); =20 @@ -3021,12 +2947,10 @@ virStorageBackendFindGlusterPoolSources(const char = *host, virStoragePoolSourceListPtr list, bool report) { - char *glusterpath =3D NULL; - char *outbuf =3D NULL; int rc; VIR_AUTOPTR(virCommand) cmd =3D NULL; - - int ret =3D -1; + VIR_AUTOFREE(char *) glusterpath =3D NULL; + VIR_AUTOFREE(char *) outbuf =3D NULL; =20 if (!(glusterpath =3D virFindFileInPath("gluster"))) { if (report) { @@ -3047,19 +2971,12 @@ virStorageBackendFindGlusterPoolSources(const char = *host, virCommandSetOutputBuffer(cmd, &outbuf); =20 if (virCommandRun(cmd, &rc) < 0) - goto cleanup; - - if (rc !=3D 0) { - ret =3D 0; - goto cleanup; - } + return -1; =20 - ret =3D virStorageUtilGlusterExtractPoolSources(host, outbuf, list, po= oltype); + if (rc !=3D 0) + return 0; =20 - cleanup: - VIR_FREE(outbuf); - VIR_FREE(glusterpath); - return ret; + return virStorageUtilGlusterExtractPoolSources(host, outbuf, list, poo= ltype); } =20 =20 @@ -3297,11 +3214,11 @@ virStorageBackendPARTEDFindLabel(const char *device, const char *const args[] =3D { device, "print", "--script", NULL, }; - char *output =3D NULL; - char *error =3D NULL; char *start, *end; int ret =3D VIR_STORAGE_PARTED_ERROR; VIR_AUTOPTR(virCommand) cmd =3D NULL; + VIR_AUTOFREE(char *) output =3D NULL; + VIR_AUTOFREE(char *) error =3D NULL; =20 cmd =3D virCommandNew(PARTED); virCommandAddArgSet(cmd, args); @@ -3316,7 +3233,7 @@ virStorageBackendPARTEDFindLabel(const char *device, (error && strstr(error, "unrecognised disk label"))) { ret =3D VIR_STORAGE_PARTED_UNKNOWN; } - goto cleanup; + return ret; } =20 /* Search for "Partition Table:" in the output. If not present, @@ -3325,8 +3242,7 @@ virStorageBackendPARTEDFindLabel(const char *device, if (!(start =3D strstr(output, "Partition Table: ")) || !(end =3D strstr(start, "\n"))) { VIR_DEBUG("Unable to find tag in output: %s", output); - ret =3D VIR_STORAGE_PARTED_NOPTTYPE; - goto cleanup; + return VIR_STORAGE_PARTED_NOPTTYPE; } start +=3D strlen("Partition Table: "); *end =3D '\0'; @@ -3336,21 +3252,14 @@ virStorageBackendPARTEDFindLabel(const char *device, start +=3D 2; =20 /* Make sure we know about this type */ - if (virStoragePoolFormatDiskTypeFromString(start) < 0) { - ret =3D VIR_STORAGE_PARTED_PTTYPE_UNK; - goto cleanup; - } + if (virStoragePoolFormatDiskTypeFromString(start) < 0) + return VIR_STORAGE_PARTED_PTTYPE_UNK; =20 /* Does the on disk match what the pool desired? */ if (STREQ(start, format)) - ret =3D VIR_STORAGE_PARTED_MATCH; - else - ret =3D VIR_STORAGE_PARTED_DIFFERENT; + return VIR_STORAGE_PARTED_MATCH; =20 - cleanup: - VIR_FREE(output); - VIR_FREE(error); - return ret; + return VIR_STORAGE_PARTED_DIFFERENT; } =20 =20 @@ -3803,9 +3712,9 @@ virStorageBackendSCSINewLun(virStoragePoolObjPtr pool, const char *dev) { virStoragePoolDefPtr def =3D virStoragePoolObjGetDef(pool); - char *devpath =3D NULL; int retval =3D -1; VIR_AUTOPTR(virStorageVolDef) vol =3D NULL; + VIR_AUTOFREE(char *) devpath =3D NULL; =20 /* Check if the pool is using a stable target path. The call to * virStorageBackendStablePath will fail if the pool target path @@ -3820,11 +3729,11 @@ virStorageBackendSCSINewLun(virStoragePoolObjPtr po= ol, virReportError(VIR_ERR_INVALID_ARG, _("unable to use target path '%s' for dev '%s'"), NULLSTR(def->target.path), dev); - goto cleanup; + return -1; } =20 if (VIR_ALLOC(vol) < 0) - goto cleanup; + return -1; =20 vol->type =3D VIR_STORAGE_VOL_BLOCK; =20 @@ -3834,10 +3743,10 @@ virStorageBackendSCSINewLun(virStoragePoolObjPtr po= ol, * just leave 'host' out */ if (virAsprintf(&(vol->name), "unit:%u:%u:%u", bus, target, lun) < 0) - goto cleanup; + return -1; =20 if (virAsprintf(&devpath, "/dev/%s", dev) < 0) - goto cleanup; + return -1; =20 VIR_DEBUG("Trying to create volume for '%s'", devpath); =20 @@ -3850,7 +3759,7 @@ virStorageBackendSCSINewLun(virStoragePoolObjPtr pool, if ((vol->target.path =3D virStorageBackendStablePath(pool, devpath, true)) =3D=3D NULL) - goto cleanup; + return -1; =20 if (STREQ(devpath, vol->target.path) && !(STREQ(def->target.path, "/dev") || @@ -3859,37 +3768,29 @@ virStorageBackendSCSINewLun(virStoragePoolObjPtr po= ol, VIR_DEBUG("No stable path found for '%s' in '%s'", devpath, def->target.path); =20 - retval =3D -2; - goto cleanup; + return -2; } =20 /* Allow a volume read failure to ignore or skip this block file */ if ((retval =3D virStorageBackendUpdateVolInfo(vol, true, VIR_STORAGE_VOL_OPEN_DEFA= ULT, VIR_STORAGE_VOL_READ_NOER= ROR)) < 0) - goto cleanup; - - /* Reset retval for proper error handling */ - retval =3D -1; + return retval; =20 vol->key =3D virStorageBackendSCSISerial(vol->target.path, (def->source.adapter.type =3D= =3D VIR_STORAGE_ADAPTER_TYPE_FC_HO= ST)); if (!vol->key) - goto cleanup; + return -1; =20 def->capacity +=3D vol->target.capacity; def->allocation +=3D vol->target.allocation; =20 if (virStoragePoolObjAddVol(pool, vol) < 0) - goto cleanup; + return -1; vol =3D NULL; =20 - retval =3D 0; - - cleanup: - VIR_FREE(devpath); - return retval; + return 0; } =20 =20 @@ -3899,11 +3800,11 @@ getNewStyleBlockDevice(const char *lun_path, const char *block_name ATTRIBUTE_UNUSED, char **block_device) { - char *block_path =3D NULL; DIR *block_dir =3D NULL; struct dirent *block_dirent =3D NULL; int retval =3D -1; int direrr; + VIR_AUTOFREE(char *) block_path =3D NULL; =20 if (virAsprintf(&block_path, "%s/block", lun_path) < 0) goto cleanup; @@ -3929,7 +3830,6 @@ getNewStyleBlockDevice(const char *lun_path, =20 cleanup: VIR_DIR_CLOSE(block_dir); - VIR_FREE(block_path); return retval; } =20 @@ -3979,11 +3879,11 @@ getBlockDevice(uint32_t host, uint32_t lun, char **block_device) { - char *lun_path =3D NULL; DIR *lun_dir =3D NULL; struct dirent *lun_dirent =3D NULL; int retval =3D -1; int direrr; + VIR_AUTOFREE(char *) lun_path =3D NULL; =20 *block_device =3D NULL; =20 @@ -4021,7 +3921,6 @@ getBlockDevice(uint32_t host, =20 cleanup: VIR_DIR_CLOSE(lun_dir); - VIR_FREE(lun_path); return retval; } =20 @@ -4037,15 +3936,14 @@ getDeviceType(uint32_t host, uint32_t lun, int *type) { - char *type_path =3D NULL; char typestr[3]; char *gottype, *p; FILE *typefile; - int retval =3D 0; + VIR_AUTOFREE(char *) type_path =3D NULL; =20 if (virAsprintf(&type_path, "/sys/bus/scsi/devices/%u:%u:%u:%u/type", host, bus, target, lun) < 0) - goto out; + return -1; =20 typefile =3D fopen(type_path, "r"); if (typefile =3D=3D NULL) { @@ -4053,8 +3951,7 @@ getDeviceType(uint32_t host, _("Could not find typefile '%s'"), type_path); /* there was no type file; that doesn't seem right */ - retval =3D -1; - goto out; + return -1; } =20 gottype =3D fgets(typestr, 3, typefile); @@ -4065,8 +3962,7 @@ getDeviceType(uint32_t host, _("Could not read typefile '%s'"), type_path); /* we couldn't read the type file; have to give up */ - retval =3D -1; - goto out; + return -1; } =20 /* we don't actually care about p, but if you pass NULL and the last @@ -4077,15 +3973,12 @@ getDeviceType(uint32_t host, _("Device type '%s' is not an integer"), typestr); /* Hm, type wasn't an integer; seems strange */ - retval =3D -1; - goto out; + return -1; } =20 VIR_DEBUG("Device type is %d", *type); =20 - out: - VIR_FREE(type_path); - return retval; + return 0; } =20 =20 @@ -4107,7 +4000,7 @@ processLU(virStoragePoolObjPtr pool, { int retval =3D -1; int device_type; - char *block_device =3D NULL; + VIR_AUTOFREE(char *) block_device =3D NULL; =20 VIR_DEBUG("Processing LU %u:%u:%u:%u", host, bus, target, lun); @@ -4139,14 +4032,12 @@ processLU(virStoragePoolObjPtr pool, if (retval < 0) { VIR_DEBUG("Failed to create new storage volume for %u:%u:%u:%u", host, bus, target, lun); - goto cleanup; + return retval; } =20 VIR_DEBUG("Created new storage volume for %u:%u:%u:%u successfully", host, bus, target, lun); =20 - cleanup: - VIR_FREE(block_device); return retval; } =20 --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list