From nobody Mon Feb 9 19:05:47 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1599190929; cv=none; d=zohomail.com; s=zohoarc; b=gVDTmrj/UjWE1dsRrkCqaJRwvH1nBOcRQe5byqKCf5WmsDznMTHHqUoH+lLr2KWQxWmHCrpKPmBj8DMcWIEY2ZYjTyCDuZMQ903IblMybEXiC2PBMEtBuk36hs9L2I9ixH060iUOQB7YMzgpxKtB0AOLQESG1AsQptSmTA3NqYs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599190929; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=U4JxVLrjVgyIj0vhl3g1DLzmiHhMUKmSgxiL8q60vhk=; b=OfFEkZ2YcwnS0dzXPgtl90l3iJA7YOZX2yc7jT/9+Dz66n+ZtWEe7ZHpui0FEbbgF9Fqw2J+et01Igfu6Qm7Lw4/qDflEKQ7XbqpMo4hx/uYzEOmJ3huPrlWe4VvyB0qgjGjfN1GngRRAp4NWAYjzdSKL+zApALgapflLdgoYnQ= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1599190929710879.3277780059807; Thu, 3 Sep 2020 20:42:09 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-210-j11aBe1lM0-ZZVWNP4Iwlw-1; Thu, 03 Sep 2020 23:42:05 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6FBD81084C9F; Fri, 4 Sep 2020 03:41:54 +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 4B8991002391; Fri, 4 Sep 2020 03:41:54 +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 17B58181A2E6; Fri, 4 Sep 2020 03:41:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0843fAYH030574 for ; Thu, 3 Sep 2020 23:41:11 -0400 Received: by smtp.corp.redhat.com (Postfix) id A85A3117DB01; Fri, 4 Sep 2020 03:41:10 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9FC6C117A976 for ; Fri, 4 Sep 2020 03:41:08 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 395E3101A569 for ; Fri, 4 Sep 2020 03:41:08 +0000 (UTC) Received: from mail-m24147.qiye.163.com (mail-m24147.qiye.163.com [220.194.24.147]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-56-0OncxFzeMVKKblLJjzCc-Q-1; Thu, 03 Sep 2020 23:41:03 -0400 Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp4 (Coremail) with SMTP id JedpCgAHcNgStlFfYHYgCg--.1426S6; Fri, 04 Sep 2020 11:35:48 +0800 (CST) X-MC-Unique: j11aBe1lM0-ZZVWNP4Iwlw-1 X-MC-Unique: 0OncxFzeMVKKblLJjzCc-Q-1 From: Shi Lei To: libvir-list@redhat.com Subject: [RFCv2 04/46] util: Add some xml-helper-functions to cooperate with xmlgen Date: Fri, 4 Sep 2020 11:34:56 +0800 Message-Id: <20200904033538.418579-5-shi_lei@massclouds.com> In-Reply-To: <20200904033538.418579-1-shi_lei@massclouds.com> References: <20200904033538.418579-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: JedpCgAHcNgStlFfYHYgCg--.1426S6 X-Coremail-Antispam: 1Uf129KBjvJXoWxJFykXw1furWrCw1UtryUAwb_yoWrZFWUpF Z8tr95trn5J34fCrsa9r1xWrs8Zr4SqrW3Ww1furyDArWfXw45Jr4fAFyYgw18ArW8Wr1j qa1YgF15uF48KFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0piAnY3UUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbihQWVT1sfnZ2FpQAAs+ X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false; X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 1) virXMLChildNode and virXMLChildNodeSet Parse xml without using xmlXPathContext. 2) virXMLChildPropString Support to parse attribute by path. 3) virXMLFlag Convert opaque to flag. Signed-off-by: Shi Lei --- src/libvirt_private.syms | 4 ++ src/util/virxml.c | 105 +++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 +++ 3 files changed, 115 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 5736a2d..191eab0 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3505,7 +3505,11 @@ virVsockSetGuestCid; virParseScaledValue; virXMLCheckIllegalChars; virXMLChildElementCount; +virXMLChildNode; +virXMLChildNodeSet; +virXMLChildPropString; virXMLExtractNamespaceXML; +virXMLFlag; virXMLFormatElement; virXMLNodeContentString; virXMLNodeNameEqual; diff --git a/src/util/virxml.c b/src/util/virxml.c index 5315d4f..a7d9a2c 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1481,3 +1481,108 @@ virParseScaledValue(const char *xpath, *val =3D bytes; return 1; } + + +/** + * virXMLChildNode: + * @node: Parent XML dom node pointer + * @name: Name of the child element + * + * Convenience function to return the child element of a XML node. + * + * Returns the pointer of child element node or NULL in case of failure. + * If there are many nodes match condition, it only returns the first node. + */ +xmlNodePtr +virXMLChildNode(xmlNodePtr node, const char *name) +{ + xmlNodePtr cur =3D node->children; + while (cur) { + if (cur->type =3D=3D XML_ELEMENT_NODE && virXMLNodeNameEqual(cur, = name)) + return cur; + cur =3D cur->next; + } + return NULL; +} + + +/** + * virXMLChildPropString: + * @node: Parent XML dom node pointer + * @path: Path of the property (attribute) to get + * + * Convenience function to return copy of an attribute value by a path. + * Path consists of names of child elements and an attribute. + * The delimiter is '/'. + * + * E.g.: For , + * the path is 'son/grandSon/name' to get attribute 'name'. + * + * Returns the property (attribute) value as string or NULL in case of fai= lure. + * The caller is responsible for freeing the returned buffer. + */ +char * +virXMLChildPropString(xmlNodePtr node, const char *path) +{ + char *next; + char *sep; + xmlNodePtr tnode =3D node; + g_autofree char *tmp =3D NULL; + tmp =3D g_strdup(path); + + next =3D tmp; + while ((sep =3D strchr(next, '/'))) { + *sep =3D '\0'; + + if ((tnode =3D virXMLChildNode(tnode, next)) =3D=3D NULL) + return NULL; + + next =3D sep + 1; + } + + return virXMLPropString(tnode, next); +} + + +/** + * virXMLChildNodeSet: + * @node: Parent XML dom node pointer + * @name: Name of the children element + * @list: the returned list of nodes (or NULL if only count matters) + * + * Convenience function to evaluate a set of children elements + * + * Returns the number of nodes found in which case @list is set (and + * must be freed) or -1 if the evaluation failed. + */ +int +virXMLChildNodeSet(xmlNodePtr node, const char *name, xmlNodePtr **list) +{ + size_t count =3D 0; + xmlNodePtr cur =3D node->children; + + if (list !=3D NULL) + *list =3D NULL; + + while (cur) { + if (cur->type =3D=3D XML_ELEMENT_NODE && virXMLNodeNameEqual(cur, = name)) { + if (list !=3D NULL) { + if (VIR_APPEND_ELEMENT_COPY(*list, count, cur) < 0) + return -1; + } else { + count++; + } + } + cur =3D cur->next; + } + return count; +} + + +unsigned int +virXMLFlag(void *opaque) +{ + if (opaque) + return *((unsigned int *) opaque); + return 0; +} diff --git a/src/util/virxml.h b/src/util/virxml.h index 0301f15..85e8eed 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -79,6 +79,10 @@ char * virXMLPropStringLimit(xmlNodePtr node, char * virXMLNodeContentString(xmlNodePtr node); long virXMLChildElementCount(xmlNodePtr node); =20 +xmlNodePtr virXMLChildNode(xmlNodePtr node, const char *name); +int virXMLChildNodeSet(xmlNodePtr node, const char *name, xmlNodePtr **lis= t); +char * virXMLChildPropString(xmlNodePtr node, const char *path); + /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, const char *filename, @@ -279,3 +283,5 @@ int virParseScaledValue(const char *xpath, unsigned long long scale, unsigned long long max, bool required); + +unsigned int virXMLFlag(void *opaque); --=20 2.25.1