virsh: cmdAttachDisk: Use virXMLFormatElement

Convert the code to the new XML formatting approach for simpler code and
future additions.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Peter Krempa 2020-11-19 13:05:05 +01:00
parent 34f830022b
commit 61c8164502

View File

@ -558,6 +558,10 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
const char *stype = NULL; const char *stype = NULL;
int type = VIR_STORAGE_TYPE_NONE; int type = VIR_STORAGE_TYPE_NONE;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_auto(virBuffer) diskAttrBuf = VIR_BUFFER_INITIALIZER;
g_auto(virBuffer) diskChildBuf = VIR_BUFFER_INIT_CHILD(&buf);
g_auto(virBuffer) driverAttrBuf = VIR_BUFFER_INITIALIZER;
g_auto(virBuffer) sourceAttrBuf = VIR_BUFFER_INITIALIZER;
g_autofree char *xml = NULL; g_autofree char *xml = NULL;
struct stat st; struct stat st;
bool current = vshCommandOptBool(cmd, "current"); bool current = vshCommandOptBool(cmd, "current");
@ -622,71 +626,56 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
if (wwn && !virValidateWWN(wwn)) if (wwn && !virValidateWWN(wwn))
return false; return false;
/* Make XML of disk */ virBufferAsprintf(&diskAttrBuf, " type='%s'", virshAttachDiskSourceTypeToString(type));
virBufferAddLit(&buf, "<disk"); virBufferEscapeString(&diskAttrBuf, " device='%s'", device);
virBufferAsprintf(&buf, " type='%s'", virshAttachDiskSourceTypeToString(type));
if (device)
virBufferAsprintf(&buf, " device='%s'", device);
if (vshCommandOptBool(cmd, "rawio")) if (vshCommandOptBool(cmd, "rawio"))
virBufferAddLit(&buf, " rawio='yes'"); virBufferAddLit(&diskAttrBuf, " rawio='yes'");
virBufferAddLit(&buf, ">\n");
virBufferAdjustIndent(&buf, 2);
if (driver || subdriver || iothread || cache || io) { virBufferEscapeString(&driverAttrBuf, " name='%s'", driver);
virBufferAddLit(&buf, "<driver"); virBufferEscapeString(&driverAttrBuf, " type='%s'", subdriver);
virBufferEscapeString(&driverAttrBuf, " iothread='%s'", iothread);
virBufferEscapeString(&driverAttrBuf, " cache='%s'", cache);
virBufferEscapeString(&driverAttrBuf, " io='%s'", io);
if (driver) virXMLFormatElement(&diskChildBuf, "driver", &driverAttrBuf, NULL);
virBufferAsprintf(&buf, " name='%s'", driver);
if (subdriver)
virBufferAsprintf(&buf, " type='%s'", subdriver);
if (iothread)
virBufferAsprintf(&buf, " iothread='%s'", iothread);
if (cache)
virBufferAsprintf(&buf, " cache='%s'", cache);
if (io)
virBufferAsprintf(&buf, " io='%s'", io);
virBufferAddLit(&buf, "/>\n");
}
switch ((enum virshAttachDiskSourceType) type) { switch ((enum virshAttachDiskSourceType) type) {
case VIRSH_ATTACH_DISK_SOURCE_TYPE_FILE: case VIRSH_ATTACH_DISK_SOURCE_TYPE_FILE:
virBufferEscapeString(&buf, "<source file='%s'/>\n", source); virBufferEscapeString(&sourceAttrBuf, " file='%s'", source);
break; break;
case VIRSH_ATTACH_DISK_SOURCE_TYPE_BLOCK: case VIRSH_ATTACH_DISK_SOURCE_TYPE_BLOCK:
virBufferEscapeString(&buf, "<source dev='%s'/>\n", source); virBufferEscapeString(&sourceAttrBuf, " dev='%s'", source);
break; break;
case VIRSH_ATTACH_DISK_SOURCE_TYPE_NONE: case VIRSH_ATTACH_DISK_SOURCE_TYPE_NONE:
case VIRSH_ATTACH_DISK_SOURCE_TYPE_LAST: case VIRSH_ATTACH_DISK_SOURCE_TYPE_LAST:
break; break;
} }
virXMLFormatElement(&diskChildBuf, "source", &sourceAttrBuf, NULL);
virBufferAsprintf(&buf, "<target dev='%s'", target); virBufferAsprintf(&diskChildBuf, "<target dev='%s'", target);
if (targetbus) if (targetbus)
virBufferAsprintf(&buf, " bus='%s'", targetbus); virBufferAsprintf(&diskChildBuf, " bus='%s'", targetbus);
virBufferAddLit(&buf, "/>\n"); virBufferAddLit(&diskChildBuf, "/>\n");
if (mode) if (mode)
virBufferAsprintf(&buf, "<%s/>\n", mode); virBufferAsprintf(&diskChildBuf, "<%s/>\n", mode);
if (serial) if (serial)
virBufferAsprintf(&buf, "<serial>%s</serial>\n", serial); virBufferAsprintf(&diskChildBuf, "<serial>%s</serial>\n", serial);
if (alias) if (alias)
virBufferAsprintf(&buf, "<alias name='%s'/>\n", alias); virBufferAsprintf(&diskChildBuf, "<alias name='%s'/>\n", alias);
if (wwn) if (wwn)
virBufferAsprintf(&buf, "<wwn>%s</wwn>\n", wwn); virBufferAsprintf(&diskChildBuf, "<wwn>%s</wwn>\n", wwn);
if (straddr && if (straddr &&
cmdAttachDiskFormatAddress(ctl, &buf, straddr, target, multifunction) < 0) cmdAttachDiskFormatAddress(ctl, &diskChildBuf, straddr, target, multifunction) < 0)
return false; return false;
virBufferAdjustIndent(&buf, -2); virXMLFormatElement(&buf, "disk", &diskAttrBuf, &diskChildBuf);
virBufferAddLit(&buf, "</disk>\n");
xml = virBufferContentAndReset(&buf); xml = virBufferContentAndReset(&buf);