From nobody Wed Feb 11 11:34:43 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.39 as permitted sender) client-ip=209.132.183.39; envelope-from=libvir-list-bounces@redhat.com; helo=mx6-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.39 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx6-phx2.redhat.com (mx6-phx2.redhat.com [209.132.183.39]) by mx.zohomail.com with SMTPS id 1487877873373725.7422715659023; Thu, 23 Feb 2017 11:24:33 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx6-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NJLEsB057102; Thu, 23 Feb 2017 14:21:15 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v1NJLB8Z001282 for ; Thu, 23 Feb 2017 14:21:11 -0500 Received: from angien.brq.redhat.com (dhcp129-47.brq.redhat.com [10.34.129.47]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1NJL8uU028635; Thu, 23 Feb 2017 14:21:10 -0500 From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 23 Feb 2017 20:21:54 +0100 Message-Id: <3c336801634cf426f4440eef31ffa109386acb99.1487877253.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [RFC PATCH 01/12] 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 07a35333b..69d1bc860 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2462,6 +2462,7 @@ virStorageFileGetMetadataInternal; virStorageFileGetRelativeBackingPath; virStorageFileGetSCSIKey; virStorageFileIsClusterFS; +virStorageFileParseBackingStoreStr; virStorageFileParseChainIndex; virStorageFileProbeFormat; virStorageFileResize; diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index c9420fdb7..3e711228b 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.11.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list