From 181acfe9a88b84f82b812937a37b81625d12dcdc Mon Sep 17 00:00:00 2001 From: John Ferlan Date: Wed, 9 Jan 2019 12:00:28 -0500 Subject: [PATCH] conf: Extract guest XML formatting from virCapabilitiesFormatXML Let's extract out the code into it's own method/helper. NB: One minor change between the two is usage of "buf" instead of "&buf" in the new code since we pass the address of &buf to the helper. Signed-off-by: John Ferlan ACKed-by: Michal Privoznik --- src/conf/capabilities.c | 112 ++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 51 deletions(-) diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index 863cd06a8d..c320c0f72d 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -1178,91 +1178,77 @@ virCapabilitiesFormatHostXML(virCapsHostPtr host, } -/** - * virCapabilitiesFormatXML: - * @caps: capabilities to format - * - * Convert the capabilities object into an XML representation - * - * Returns the XML document as a string - */ -char * -virCapabilitiesFormatXML(virCapsPtr caps) +static void +virCapabilitiesFormatGuestXML(virCapsPtr caps, + virBufferPtr buf) { - virBuffer buf = VIR_BUFFER_INITIALIZER; size_t i, j, k; - virBufferAddLit(&buf, "\n\n"); - virBufferAdjustIndent(&buf, 2); - - if (virCapabilitiesFormatHostXML(&caps->host, &buf) < 0) - goto error; - for (i = 0; i < caps->nguests; i++) { - virBufferAddLit(&buf, "\n"); - virBufferAdjustIndent(&buf, 2); - virBufferAsprintf(&buf, "%s\n", + virBufferAddLit(buf, "\n"); + virBufferAdjustIndent(buf, 2); + virBufferAsprintf(buf, "%s\n", virDomainOSTypeToString(caps->guests[i]->ostype)); if (caps->guests[i]->arch.id) - virBufferAsprintf(&buf, "\n", + virBufferAsprintf(buf, "\n", virArchToString(caps->guests[i]->arch.id)); - virBufferAdjustIndent(&buf, 2); - virBufferAsprintf(&buf, "%d\n", + virBufferAdjustIndent(buf, 2); + virBufferAsprintf(buf, "%d\n", caps->guests[i]->arch.wordsize); if (caps->guests[i]->arch.defaultInfo.emulator) - virBufferAsprintf(&buf, "%s\n", + virBufferAsprintf(buf, "%s\n", caps->guests[i]->arch.defaultInfo.emulator); if (caps->guests[i]->arch.defaultInfo.loader) - virBufferAsprintf(&buf, "%s\n", + virBufferAsprintf(buf, "%s\n", caps->guests[i]->arch.defaultInfo.loader); for (j = 0; j < caps->guests[i]->arch.defaultInfo.nmachines; j++) { virCapsGuestMachinePtr machine = caps->guests[i]->arch.defaultInfo.machines[j]; - virBufferAddLit(&buf, "canonical) - virBufferAsprintf(&buf, " canonical='%s'", machine->canonical); + virBufferAsprintf(buf, " canonical='%s'", machine->canonical); if (machine->maxCpus > 0) - virBufferAsprintf(&buf, " maxCpus='%d'", machine->maxCpus); - virBufferAsprintf(&buf, ">%s\n", machine->name); + virBufferAsprintf(buf, " maxCpus='%d'", machine->maxCpus); + virBufferAsprintf(buf, ">%s\n", machine->name); } for (j = 0; j < caps->guests[i]->arch.ndomains; j++) { - virBufferAsprintf(&buf, "guests[i]->arch.domains[j]->type)); if (!caps->guests[i]->arch.domains[j]->info.emulator && !caps->guests[i]->arch.domains[j]->info.loader && !caps->guests[i]->arch.domains[j]->info.nmachines) { - virBufferAddLit(&buf, "/>\n"); + virBufferAddLit(buf, "/>\n"); continue; } - virBufferAddLit(&buf, ">\n"); - virBufferAdjustIndent(&buf, 2); + virBufferAddLit(buf, ">\n"); + virBufferAdjustIndent(buf, 2); if (caps->guests[i]->arch.domains[j]->info.emulator) - virBufferAsprintf(&buf, "%s\n", + virBufferAsprintf(buf, "%s\n", caps->guests[i]->arch.domains[j]->info.emulator); if (caps->guests[i]->arch.domains[j]->info.loader) - virBufferAsprintf(&buf, "%s\n", + virBufferAsprintf(buf, "%s\n", caps->guests[i]->arch.domains[j]->info.loader); for (k = 0; k < caps->guests[i]->arch.domains[j]->info.nmachines; k++) { virCapsGuestMachinePtr machine = caps->guests[i]->arch.domains[j]->info.machines[k]; - virBufferAddLit(&buf, "canonical) - virBufferAsprintf(&buf, " canonical='%s'", machine->canonical); + virBufferAsprintf(buf, " canonical='%s'", machine->canonical); if (machine->maxCpus > 0) - virBufferAsprintf(&buf, " maxCpus='%d'", machine->maxCpus); - virBufferAsprintf(&buf, ">%s\n", machine->name); + virBufferAsprintf(buf, " maxCpus='%d'", machine->maxCpus); + virBufferAsprintf(buf, ">%s\n", machine->name); } - virBufferAdjustIndent(&buf, -2); - virBufferAddLit(&buf, "\n"); + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "\n"); } - virBufferAdjustIndent(&buf, -2); - virBufferAddLit(&buf, "\n"); + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "\n"); if (caps->guests[i]->nfeatures) { - virBufferAddLit(&buf, "\n"); - virBufferAdjustIndent(&buf, 2); + virBufferAddLit(buf, "\n"); + virBufferAdjustIndent(buf, 2); for (j = 0; j < caps->guests[i]->nfeatures; j++) { if (STREQ(caps->guests[i]->features[j]->name, "pae") || @@ -1270,22 +1256,46 @@ virCapabilitiesFormatXML(virCapsPtr caps) STREQ(caps->guests[i]->features[j]->name, "ia64_be") || STREQ(caps->guests[i]->features[j]->name, "cpuselection") || STREQ(caps->guests[i]->features[j]->name, "deviceboot")) { - virBufferAsprintf(&buf, "<%s/>\n", + virBufferAsprintf(buf, "<%s/>\n", caps->guests[i]->features[j]->name); } else { - virBufferAsprintf(&buf, "<%s default='%s' toggle='%s'/>\n", + virBufferAsprintf(buf, "<%s default='%s' toggle='%s'/>\n", caps->guests[i]->features[j]->name, caps->guests[i]->features[j]->defaultOn ? "on" : "off", caps->guests[i]->features[j]->toggle ? "yes" : "no"); } } - virBufferAdjustIndent(&buf, -2); - virBufferAddLit(&buf, "\n"); + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "\n"); } - virBufferAdjustIndent(&buf, -2); - virBufferAddLit(&buf, "\n\n"); + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "\n\n"); } +} + + +/** + * virCapabilitiesFormatXML: + * @caps: capabilities to format + * + * Convert the capabilities object into an XML representation + * + * Returns the XML document as a string + */ +char * +virCapabilitiesFormatXML(virCapsPtr caps) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + + virBufferAddLit(&buf, "\n\n"); + virBufferAdjustIndent(&buf, 2); + + if (virCapabilitiesFormatHostXML(&caps->host, &buf) < 0) + goto error; + + virCapabilitiesFormatGuestXML(caps, &buf); + virBufferAdjustIndent(&buf, -2); virBufferAddLit(&buf, "\n"); -- GitLab