From nobody Mon Feb 9 12:29:08 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618328326; cv=none; d=zohomail.com; s=zohoarc; b=AglA0PhqEVvnVJSL4vsjTWMh089aCkcfHM8ya4jyEoqD/enhvaGKOU7hIx6WcgTDIzjieRdQhXOgu+YMvumzuzwNGLpOPQANRjdl5UO61GQQ6UsBnmd6RUUIGyF6ofLwt9Egt0xrM8AFGHYcMZgu24UF69NB7B+DAhd60R2Uh0k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618328326; 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=t/h4n+r3BxDyaLXLUbFUyroCQY2kMvMg4kWl66xGwho=; b=k8xOxUHKQHT3BPJ3v6w4rH9dunpWleVuQmGEypXzG281jlRFCHJu42AaskSMeXKcPVB3UB3vO/iR2QOa4DgDyVwnohy29t5i7xrJa4hxuXqTOD8IRyoh+/j0J7gtJ7X4dXMeIHLHzWZl5wan09JSYmxZCTDK3Y0rNv1jbvaX4FU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1618328326816222.22683714730965; Tue, 13 Apr 2021 08:38:46 -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-231-Nkl9-Z67OjW3E7km2VMv5g-1; Tue, 13 Apr 2021 11:38:42 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CBA3E835B47; Tue, 13 Apr 2021 15:38:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A23F55D9CA; Tue, 13 Apr 2021 15:38:35 +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 09CF544A5B; Tue, 13 Apr 2021 15:38:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 13DFcYMX012886 for ; Tue, 13 Apr 2021 11:38:34 -0400 Received: by smtp.corp.redhat.com (Postfix) id 71CBD2B0AB; Tue, 13 Apr 2021 15:38:34 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB6F560DA0 for ; Tue, 13 Apr 2021 15:38:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618328325; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=t/h4n+r3BxDyaLXLUbFUyroCQY2kMvMg4kWl66xGwho=; b=h7zOPKPbToQRAvTXl2VDMbhGr6XhrN04BLpxRpAwMmrj1CTYZXn7b3ReaMP7YaQUvwsYpk JpAr66VBVI1SCDDArT+tWL2ie4kJ0R8ehR98IC0Y47A0K/Dpl4+KtG8bo5ZbI61t3IZbrT iNtE26GSYKPf6za7E/A9xNgkROxDL70= X-MC-Unique: Nkl9-Z67OjW3E7km2VMv5g-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [RFC PATCH 1/7] util: xml: Introduce virXMLFormatElementEmpty Date: Tue, 13 Apr 2021 17:38:21 +0200 Message-Id: <338ef258418f23c61092a7506a78511251982a53.1618328187.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Add a helper which will format an XML element with attributes and children, but compared to virXMLFormatElement it also formats an empty element if both buffers are empty. Signed-off-by: Peter Krempa Reviewed-by: Andrea Bolognani --- src/libvirt_private.syms | 1 + src/util/virxml.c | 42 ++++++++++++++++++++++++++-------------- src/util/virxml.h | 7 +++++++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b3f9c9681a..64002de39a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3541,6 +3541,7 @@ virXMLBufferCreate; virXMLCheckIllegalChars; virXMLExtractNamespaceXML; virXMLFormatElement; +virXMLFormatElementEmpty; virXMLNewNode; virXMLNodeContentString; virXMLNodeNameEqual; diff --git a/src/util/virxml.c b/src/util/virxml.c index 117f50f2bf..c2a49cbef2 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1314,6 +1314,32 @@ virXMLValidatorFree(virXMLValidator *validator) } +/* same as virXMLFormatElement but outputs an empty element if @attrBuf and + * @childBuf are both empty */ +void +virXMLFormatElementEmpty(virBuffer *buf, + const char *name, + virBuffer *attrBuf, + virBuffer *childBuf) +{ + virBufferAsprintf(buf, "<%s", name); + + if (attrBuf && virBufferUse(attrBuf) > 0) + virBufferAddBuffer(buf, attrBuf); + + if (childBuf && virBufferUse(childBuf) > 0) { + virBufferAddLit(buf, ">\n"); + virBufferAddBuffer(buf, childBuf); + virBufferAsprintf(buf, "\n", name); + } else { + virBufferAddLit(buf, "/>\n"); + } + + virBufferFreeAndReset(attrBuf); + virBufferFreeAndReset(childBuf); +} + + /** * virXMLFormatElement * @buf: the parent buffer where the element will be placed @@ -1338,21 +1364,7 @@ virXMLFormatElement(virBuffer *buf, (!childBuf || virBufferUse(childBuf) =3D=3D 0)) return; - virBufferAsprintf(buf, "<%s", name); - - if (attrBuf && virBufferUse(attrBuf) > 0) - virBufferAddBuffer(buf, attrBuf); - - if (childBuf && virBufferUse(childBuf) > 0) { - virBufferAddLit(buf, ">\n"); - virBufferAddBuffer(buf, childBuf); - virBufferAsprintf(buf, "\n", name); - } else { - virBufferAddLit(buf, "/>\n"); - } - - virBufferFreeAndReset(attrBuf); - virBufferFreeAndReset(childBuf); + virXMLFormatElementEmpty(buf, name, attrBuf, childBuf); } diff --git a/src/util/virxml.h b/src/util/virxml.h index de171dce12..a81db478f0 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -226,6 +226,13 @@ virXMLFormatElement(virBuffer *buf, virBuffer *attrBuf, virBuffer *childBuf); +void +virXMLFormatElementEmpty(virBuffer *buf, + const char *name, + virBuffer *attrBuf, + virBuffer *childBuf); + + struct _virXPathContextNodeSave { xmlXPathContextPtr ctxt; xmlNodePtr node; --=20 2.30.2