From nobody Mon Feb 9 09:34:10 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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 170.10.133.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=1648477665; cv=none; d=zohomail.com; s=zohoarc; b=VwVY7fCQDyuDMOhoX83b/L9d/AxJxTeZrgDKm9/HyUeG95ruzOeLSathaIk1hxtLWctNF4apf762girDDd7B7Oxfigq3ie2FPnWe72gneNDTdsovSjHFS7Aqt/OpByXBRQUvoEPQlDJ+OPz4HsmzPMPQJqUEq9k01Gu70GUQ7Fg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1648477665; 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=hB9VslFVJc3Wh23eT7aLLGLB4l+wp5/4f8/qIC+gPtQ=; b=KE3/l0y/nzCxypQoWht+dv4M010z23+R1/3ML2TvQxbbu+IY+7Vy0PZq0iGZUEKFmGeN156R2VvniarbjzVoLbjJed3EjZbQSTRA9+THlBIuXaf7luy5x+hyYlOVwKLctBBuZgQGRw01MYQGZOprdG59BZXKXNvQZ6FUgAJbJdY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1648477665595848.6093189824636; Mon, 28 Mar 2022 07:27:45 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-668-sHg6YEgEM8u2l9u9FWbGfg-1; Mon, 28 Mar 2022 10:27:40 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 06DA118E004C; Mon, 28 Mar 2022 14:27:38 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 97A56401DB1; Mon, 28 Mar 2022 14:27:36 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 722E21947BBF; Mon, 28 Mar 2022 14:27:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id DEDC41947BBE for ; Mon, 28 Mar 2022 14:27:35 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id D0CE94066C81; Mon, 28 Mar 2022 14:27:35 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4DD984066C80 for ; Mon, 28 Mar 2022 14:27:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648477664; 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=hB9VslFVJc3Wh23eT7aLLGLB4l+wp5/4f8/qIC+gPtQ=; b=PeT1Kc/v5PMV7vYaPPKnNDPpjrYcjZ5aTUf5wS+YKiZK49+634imZZy3Z3Rwov15JUSD9/ HiP88av7cypOfZzYh5dTJWUzEZXZW0qImBVkn3BsMUbVVg0rar1P0tPfSXjys+RaOFf80d wW6gBOgl+J1YQZNBwh1nORrT9UDfYvw= X-MC-Unique: sHg6YEgEM8u2l9u9FWbGfg-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 1/3] virXMLFormatElement: Introduce virXMLFormatElementInternal Date: Mon, 28 Mar 2022 16:27:30 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 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) X-ZM-MESSAGEID: 1648477667054100003 Content-Type: text/plain; charset="utf-8"; x-default="true" The new function aggregates the internal working of virXMLFormatElement and virXMLFormatElementEmpty and also allows skipping the newline after the opening tag to allow using this helper also in cases where we don't format any child elements but directly a value. Signed-off-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/virxml.c | 58 +++++++++++++++++++++++++++++++--------- src/util/virxml.h | 7 +++++ 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 8a3e5f7f7c..3645a2f447 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3634,6 +3634,7 @@ virXMLCheckIllegalChars; virXMLExtractNamespaceXML; virXMLFormatElement; virXMLFormatElementEmpty; +virXMLFormatElementInternal; virXMLFormatMetadata; virXMLNewNode; virXMLNodeContentString; diff --git a/src/util/virxml.c b/src/util/virxml.c index 8ff59e7cda..0a447df676 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1591,21 +1591,48 @@ virXMLValidatorFree(virXMLValidator *validator) } -/* same as virXMLFormatElement but outputs an empty element if @attrBuf and - * @childBuf are both empty */ +/** + * virXMLFormatElementInternal + * @buf: the parent buffer where the element will be placed + * @name: the name of the element + * @attrBuf: buffer with attributes for element, may be NULL + * @childBuf: buffer with child elements, may be NULL + * @allowEmpty: Format empty element if @attrBuf and @childBuf are empty + * @childNewline: Add a newline after the opening element before formattin= g @childBuf + * + * Helper to format element where attributes or child elements + * are optional and may not be formatted. If both @attrBuf and + * @childBuf are NULL or are empty buffers the element is not + * formatted. + * + * Passing false for @childNewline allows to format elements where we dire= ctly + * output a value without subelements. + * + * Both passed buffers are always consumed and freed. + */ void -virXMLFormatElementEmpty(virBuffer *buf, - const char *name, - virBuffer *attrBuf, - virBuffer *childBuf) +virXMLFormatElementInternal(virBuffer *buf, + const char *name, + virBuffer *attrBuf, + virBuffer *childBuf, + bool allowEmpty, + bool childNewline) { + if (!allowEmpty) { + if ((!attrBuf || virBufferUse(attrBuf) =3D=3D 0) && + (!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"); + virBufferAddLit(buf, ">"); + if (childNewline) + virBufferAddLit(buf, "\n"); virBufferAddBuffer(buf, childBuf); virBufferAsprintf(buf, "\n", name); } else { @@ -1616,6 +1643,17 @@ virXMLFormatElementEmpty(virBuffer *buf, virBufferFreeAndReset(childBuf); } +/* 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) +{ + virXMLFormatElementInternal(buf, name, attrBuf, childBuf, true, true); +} + /** * virXMLFormatElement @@ -1637,11 +1675,7 @@ virXMLFormatElement(virBuffer *buf, virBuffer *attrBuf, virBuffer *childBuf) { - if ((!attrBuf || virBufferUse(attrBuf) =3D=3D 0) && - (!childBuf || virBufferUse(childBuf) =3D=3D 0)) - return; - - virXMLFormatElementEmpty(buf, name, attrBuf, childBuf); + virXMLFormatElementInternal(buf, name, attrBuf, childBuf, false, true); } diff --git a/src/util/virxml.h b/src/util/virxml.h index c39eae6282..a13853983a 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -315,6 +315,13 @@ void virXMLValidatorFree(virXMLValidator *validator); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virXMLValidator, virXMLValidatorFree); +void +virXMLFormatElementInternal(virBuffer *buf, + const char *name, + virBuffer *attrBuf, + virBuffer *childBuf, + bool allowEmpty, + bool childNewline); void virXMLFormatElement(virBuffer *buf, const char *name, --=20 2.35.1