]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: cmdBlockcopy: Fix generator of block copy disk XML
authorPeter Krempa <pkrempa@redhat.com>
Mon, 25 Apr 2022 08:16:13 +0000 (10:16 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 25 Apr 2022 14:37:05 +0000 (16:37 +0200)
In a recent commit I've attempted to rewrite the XML generator to use
virXMLFormatElement instead of manual steps. Unfortunately the commit
had multiple problems resulting in a garbled XML:

1) in certain cases the wrong buffer was used resulting in misplaced
   snippets
2) the child element buffer was improperly set up so sub-elements were
   not indented

This resulted in following XML being generated:

 $ virsh blockcopy cd vda /tmp/test.copy --raw --print-xml
 type='file''/tmp/test.copy'/>
 <driver type='raw'/>
 <disk>
 <source file=</disk>

To fix this we'll generate the '<source>' element in one go and use the
proper buffer for it and other places.

Fixes: 1cd95f858ab83f2baab0e35070d00766bb06ce3a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2078274
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tools/virsh-domain.c

index 452e518156f5bf9e689eb3debd1bda3b124b4c40..ba492e807ef6fdc7312733e48611a08240176b07 100644 (file)
@@ -2481,18 +2481,17 @@ cmdBlockcopy(vshControl *ctl, const vshCmd *cmd)
         if (!xmlstr) {
             g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
             g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
-            g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+            g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(&buf);
 
             if (blockdev) {
                 virBufferAddLit(&attrBuf, " type='block'");
-                virBufferAddLit(&childBuf, "<source dev=");
+                virBufferEscapeString(&childBuf, "<source dev='%s'/>\n", dest);
             } else {
-                virBufferAddLit(&buf, " type='file'");
-                virBufferAddLit(&childBuf, "<source file=");
+                virBufferAddLit(&attrBuf, " type='file'");
+                virBufferEscapeString(&childBuf, "<source file='%s'/>\n", dest);
             }
 
-            virBufferEscapeString(&buf, "'%s'/>\n", dest);
-            virBufferEscapeString(&buf, "<driver type='%s'/>\n", format);
+            virBufferEscapeString(&childBuf, "<driver type='%s'/>\n", format);
             virXMLFormatElement(&buf, "disk", &attrBuf, &childBuf);
             xmlstr = virBufferContentAndReset(&buf);
         }