[libvirt PATCH 3/4] domain_conf: refactor virDomainLoaderDefFormatNvram

Pavel Hrdina posted 4 patches 5 days, 18 hours ago
[libvirt PATCH 3/4] domain_conf: refactor virDomainLoaderDefFormatNvram
Posted by Pavel Hrdina 5 days, 18 hours ago
Use the new virXMLFormatDirect in order to remove usage of
virXMLFormatInternal.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 src/conf/domain_conf.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 94f456a362..0bcbf32bb4 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -26980,10 +26980,8 @@ virDomainLoaderDefFormatNvram(virBuffer *buf,
                               unsigned int flags)
 {
     g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
-    g_auto(virBuffer) childBufDirect = VIR_BUFFER_INITIALIZER;
-    g_auto(virBuffer) childBufChild = VIR_BUFFER_INIT_CHILD(buf);
-    virBuffer *childBuf = &childBufDirect;
-    bool childNewline = false;
+    g_auto(virBuffer) directBuf = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf);
 
     virBufferEscapeString(&attrBuf, " template='%s'", loader->nvramTemplate);
 
@@ -26996,14 +26994,11 @@ virDomainLoaderDefFormatNvram(virBuffer *buf,
         virStorageSource *src = loader->nvram;
 
         if (!loader->newStyleNVRAM) {
-            virBufferEscapeString(&childBufDirect, "%s", src->path);
+            virBufferEscapeString(&directBuf, "%s", src->path);
         } else {
-            childNewline = true;
-            childBuf = &childBufChild;
-
             virBufferAsprintf(&attrBuf, " type='%s'", virStorageTypeToString(src->type));
 
-            if (virDomainDiskSourceFormat(&childBufChild, src, "source", 0,
+            if (virDomainDiskSourceFormat(&childBuf, src, "source", 0,
                                           false, flags, false, false, xmlopt) < 0)
                 return -1;
         }
@@ -27014,7 +27009,10 @@ virDomainLoaderDefFormatNvram(virBuffer *buf,
         }
     }
 
-    virXMLFormatElementInternal(buf, "nvram", &attrBuf, childBuf, false, childNewline);
+    if (virBufferUse(&directBuf) > 0)
+        virXMLFormatElementDirect(buf, "nvram", &attrBuf, &directBuf);
+    else
+        virXMLFormatElement(buf, "nvram", &attrBuf, &childBuf);
 
     return 0;
 }
-- 
2.48.1
Re: [libvirt PATCH 3/4] domain_conf: refactor virDomainLoaderDefFormatNvram
Posted by Ján Tomko 5 days ago
On a Thursday in 2025, Pavel Hrdina wrote:
>Use the new virXMLFormatDirect in order to remove usage of
>virXMLFormatInternal.
>
>Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
>---
> src/conf/domain_conf.c | 18 ++++++++----------
> 1 file changed, 8 insertions(+), 10 deletions(-)
>
>diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
>index 94f456a362..0bcbf32bb4 100644
>--- a/src/conf/domain_conf.c
>+++ b/src/conf/domain_conf.c
>@@ -27014,7 +27009,10 @@ virDomainLoaderDefFormatNvram(virBuffer *buf,
>         }
>     }
>
>-    virXMLFormatElementInternal(buf, "nvram", &attrBuf, childBuf, false, childNewline);
>+    if (virBufferUse(&directBuf) > 0)
>+        virXMLFormatElementDirect(buf, "nvram", &attrBuf, &directBuf);
>+    else
>+        virXMLFormatElement(buf, "nvram", &attrBuf, &childBuf);
>

Not a fan of these extra lines.

'<' is not a valid character in the element's "direct" value.
Instead of the "childNewline" boolean, maybe virXMLFormatElement can check
the first character for this and decide based on that?

Jano

>     return 0;
> }
>-- 
>2.48.1
>
Re: [libvirt PATCH 3/4] domain_conf: refactor virDomainLoaderDefFormatNvram
Posted by Pavel Hrdina 5 days ago
On Fri, Mar 07, 2025 at 01:37:49PM +0100, Ján Tomko wrote:
> On a Thursday in 2025, Pavel Hrdina wrote:
> > Use the new virXMLFormatDirect in order to remove usage of
> > virXMLFormatInternal.
> > 
> > Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
> > ---
> > src/conf/domain_conf.c | 18 ++++++++----------
> > 1 file changed, 8 insertions(+), 10 deletions(-)
> > 
> > diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> > index 94f456a362..0bcbf32bb4 100644
> > --- a/src/conf/domain_conf.c
> > +++ b/src/conf/domain_conf.c
> > @@ -27014,7 +27009,10 @@ virDomainLoaderDefFormatNvram(virBuffer *buf,
> >         }
> >     }
> > 
> > -    virXMLFormatElementInternal(buf, "nvram", &attrBuf, childBuf, false, childNewline);
> > +    if (virBufferUse(&directBuf) > 0)
> > +        virXMLFormatElementDirect(buf, "nvram", &attrBuf, &directBuf);
> > +    else
> > +        virXMLFormatElement(buf, "nvram", &attrBuf, &childBuf);
> > 
> 
> Not a fan of these extra lines.
>
> '<' is not a valid character in the element's "direct" value.
> Instead of the "childNewline" boolean, maybe virXMLFormatElement can check
> the first character for this and decide based on that?

It would remove few lines but hide the logic inside virXMLFormatElement
and make the code less readable IMHO. This makes it explicit what is
happening and I don't know if there is any other part of our XML that
would need this special case where the content can be both value or
subelement.

Pavel

> Jano
> 
> >     return 0;
> > }
> > -- 
> > 2.48.1
> >