]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Switch the virsh XML generation to use virBuffer instead of virAsprintf
authorDaniel P. Berrange <berrange@redhat.com>
Fri, 12 Nov 2010 13:25:55 +0000 (13:25 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 23 Nov 2010 15:00:34 +0000 (15:00 +0000)
The code generating XML for storage pool source discovery is
hardcoded to only allow a hostname and optional port number.
Refactor this code to make it easier to add support for extra
parameters.

* tools/virsh.c: Refactor XML generator

tools/virsh.c

index ae88cc022c05ba0abb5ab5d8da6f54d166393c8d..2e7cfd8b9bbdf824b0c56a0b946b362e80f6fad3 100644 (file)
@@ -5826,7 +5826,6 @@ cmdPoolDiscoverSourcesAs(vshControl * ctl, const vshCmd * cmd ATTRIBUTE_UNUSED)
     if (host) {
         size_t hostlen = strlen(host);
         char *port = vshCommandOptString(cmd, "port", &found);
-        int ret;
         if (!found) {
             port = strrchr(host, ':');
             if (port) {
@@ -5836,23 +5835,18 @@ cmdPoolDiscoverSourcesAs(vshControl * ctl, const vshCmd * cmd ATTRIBUTE_UNUSED)
                     port = NULL;
             }
         }
-        ret = port ?
-            virAsprintf(&srcSpec,
-                        "<source><host name='%.*s' port='%s'/></source>",
-                        (int)hostlen, host, port) :
-            virAsprintf(&srcSpec,
-                        "<source><host name='%.*s'/></source>",
-                        (int)hostlen, host);
-        if (ret < 0) {
-            switch (errno) {
-            case ENOMEM:
-                vshError(ctl, "%s", _("Out of memory"));
-                break;
-            default:
-                vshError(ctl, _("virAsprintf failed (errno %d)"), errno);
-            }
+        virBuffer buf = VIR_BUFFER_INITIALIZER;
+        virBufferAddLit(&buf, "<source>\n");
+        virBufferVSprintf(&buf, "  <host name='%.*s'",(int)hostlen, host);
+        if (port)
+            virBufferVSprintf(&buf, " port='%s'", port);
+        virBufferAddLit(&buf, "/>\n");
+        virBufferAddLit(&buf, "</source>\n");
+        if (virBufferError(&buf)) {
+            vshError(ctl, "%s", _("Out of memory"));
             return FALSE;
         }
+        srcSpec = virBufferContentAndReset(&buf);
     }
 
     srcList = virConnectFindStoragePoolSources(ctl->conn, type, srcSpec, 0);