From nobody Wed Feb 11 10:03:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.37 as permitted sender) client-ip=209.132.183.37; envelope-from=libvir-list-bounces@redhat.com; helo=mx5-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.37 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx5-phx2.redhat.com (mx5-phx2.redhat.com [209.132.183.37]) by mx.zohomail.com with SMTPS id 1489596003896542.571626436405; Wed, 15 Mar 2017 09:40:03 -0700 (PDT) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx5-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v2FGambY016447; Wed, 15 Mar 2017 12:36:48 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v2FGakTv024892 for ; Wed, 15 Mar 2017 12:36:46 -0400 Received: from angien.brq.redhat.com (dhcp129-47.brq.redhat.com [10.34.129.47]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v2FGafIH000870; Wed, 15 Mar 2017 12:36:45 -0400 From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 15 Mar 2017 17:37:15 +0100 Message-Id: <272a9bc85055fecff8bc1a1fa2b7ac3e919d3dc9.1489595335.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 03/23] util: storage: Split out useful bits of virStorageFileParseChainIndex 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The function has very specific semantics. Split out the part that parses the backing store specification string into a separate helper so that it can be reused later while keeping the wrapper with existing semantics. Note that virStorageFileParseChainIndex is pretty well covered by the test suite. --- src/libvirt_private.syms | 1 + src/util/virstoragefile.c | 68 +++++++++++++++++++++++++++++++++++++++----= ---- src/util/virstoragefile.h | 5 ++++ 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 39e2b40c5..31d6085cb 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2476,6 +2476,7 @@ virStorageFileGetMetadataInternal; virStorageFileGetRelativeBackingPath; virStorageFileGetSCSIKey; virStorageFileIsClusterFS; +virStorageFileParseBackingStoreStr; virStorageFileParseChainIndex; virStorageFileProbeFormat; virStorageFileResize; diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index c9420fdb7..c8eb26aa7 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -1442,32 +1442,78 @@ int virStorageFileGetSCSIKey(const char *path, } #endif + +/** + * virStorageFileParseBackingStoreStr: + * @str: backing store specifier string to parse + * @target: returns target device portion of the string + * @chainIndex: returns the backing store portion of the string + * + * Parses the backing store specifier string such as vda[1], or sda into + * components and returns them via arguments. If the string did not specif= y an + * index, 0 is assumed. + * + * Returns 0 on success -1 on error + */ +int +virStorageFileParseBackingStoreStr(const char *str, + char **target, + unsigned int *chainIndex) +{ + char **strings =3D NULL; + size_t nstrings; + unsigned int idx =3D 0; + char *suffix; + int ret =3D -1; + + *chainIndex =3D 0; + + if (!(strings =3D virStringSplitCount(str, "[", 2, &nstrings))) + return -1; + + if (nstrings =3D=3D 2) { + if (virStrToLong_uip(strings[1], &suffix, 10, &idx) < 0 || + STRNEQ(suffix, "]")) + goto cleanup; + } + + if (target && + VIR_STRDUP(*target, strings[0]) < 0) + goto cleanup; + + *chainIndex =3D idx; + ret =3D 0; + + cleanup: + virStringListFreeCount(strings, nstrings); + return ret; +} + + int virStorageFileParseChainIndex(const char *diskTarget, const char *name, unsigned int *chainIndex) { - char **strings =3D NULL; unsigned int idx =3D 0; - char *suffix; + char *target =3D NULL; int ret =3D 0; *chainIndex =3D 0; - if (name && diskTarget) - strings =3D virStringSplit(name, "[", 2); + if (!name || !diskTarget) + return 0; - if (virStringListLength((const char * const *)strings) !=3D 2) - goto cleanup; + if (virStorageFileParseBackingStoreStr(name, &target, &idx) < 0) + return 0; - if (virStrToLong_uip(strings[1], &suffix, 10, &idx) < 0 || - STRNEQ(suffix, "]")) + if (idx =3D=3D 0) goto cleanup; - if (STRNEQ(diskTarget, strings[0])) { + if (STRNEQ(diskTarget, target)) { virReportError(VIR_ERR_INVALID_ARG, _("requested target '%s' does not match target '%s'= "), - strings[0], diskTarget); + target, diskTarget); ret =3D -1; goto cleanup; } @@ -1475,7 +1521,7 @@ virStorageFileParseChainIndex(const char *diskTarget, *chainIndex =3D idx; cleanup: - virStringListFree(strings); + VIR_FREE(target); return ret; } diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index bea1c35a2..5f6e41911 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -309,6 +309,11 @@ int virStorageFileParseChainIndex(const char *diskTarg= et, unsigned int *chainIndex) ATTRIBUTE_NONNULL(3); +int virStorageFileParseBackingStoreStr(const char *str, + char **target, + unsigned int *chainIndex) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); + virStorageSourcePtr virStorageFileChainLookup(virStorageSourcePtr chain, virStorageSourcePtr startFro= m, const char *name, --=20 2.12.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list