snapshot: simplify indentation of cpu features

Auto-indent makes life a bit easier; this patch also drops unused
arguments and replaces a misspelled flag name with two entry points
instead, so that callers don't have to worry about how much spacing
is present when embedding cpu elements.

* src/conf/cpu_conf.h (virCPUFormatFlags): Delete.
(virCPUDefFormat): Drop unused argument.
(virCPUDefFormatBuf): Alter signature.
(virCPUDefFormatBufFull): New prototype.
* src/conf/cpu_conf.c (virCPUDefFormatBuf): Split...
(virCPUDefFormatBufFull): ...into new function.
(virCPUDefFormat): Adjust caller.
* src/conf/domain_conf.c (virDomainDefFormatInternal): Likewise.
* src/conf/capabilities.c (virCapabilitiesFormatXML): Likewise.
* src/cpu/cpu.c (cpuBaselineXML): Likewise.
* tests/cputest.c (cpuTestCompareXML): Likewise.
This commit is contained in:
Eric Blake 2011-09-21 17:42:07 -06:00
parent 9a220665e7
commit 4eedfd075e
6 changed files with 58 additions and 58 deletions

View File

@ -1,7 +1,7 @@
/*
* capabilities.c: hypervisor capabilities
*
* Copyright (C) 2006-2008, 2010 Red Hat, Inc.
* Copyright (C) 2006-2008, 2010-2011 Red Hat, Inc.
* Copyright (C) 2006-2008 Daniel P. Berrange
*
* This library is free software; you can redistribute it and/or
@ -681,8 +681,9 @@ virCapabilitiesFormatXML(virCapsPtr caps)
virBufferAddLit(&xml, " </features>\n");
}
virCPUDefFormatBuf(&xml, caps->host.cpu, " ",
VIR_CPU_FORMAT_EMBEDED);
virBufferAdjustIndent(&xml, 6);
virCPUDefFormatBuf(&xml, caps->host.cpu);
virBufferAdjustIndent(&xml, -6);
virBufferAddLit(&xml, " </cpu>\n");

View File

@ -305,13 +305,11 @@ error:
char *
virCPUDefFormat(virCPUDefPtr def,
const char *indent,
unsigned int flags)
virCPUDefFormat(virCPUDefPtr def)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
if (virCPUDefFormatBuf(&buf, def, indent, flags) < 0)
if (virCPUDefFormatBufFull(&buf, def) < 0)
goto cleanup;
if (virBufferError(&buf))
@ -327,54 +325,63 @@ cleanup:
}
int
virCPUDefFormatBufFull(virBufferPtr buf,
virCPUDefPtr def)
{
if (!def)
return 0;
if (def->type == VIR_CPU_TYPE_GUEST && def->model) {
const char *match;
if (!(match = virCPUMatchTypeToString(def->match))) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
_("Unexpected CPU match policy %d"), def->match);
return -1;
}
virBufferAsprintf(buf, "<cpu match='%s'>\n", match);
} else {
virBufferAddLit(buf, "<cpu>\n");
}
if (def->arch)
virBufferAsprintf(buf, " <arch>%s</arch>\n", def->arch);
virBufferAdjustIndent(buf, 2);
if (virCPUDefFormatBuf(buf, def) < 0)
return -1;
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</cpu>\n");
return 0;
}
int
virCPUDefFormatBuf(virBufferPtr buf,
virCPUDefPtr def,
const char *indent,
unsigned int flags)
virCPUDefPtr def)
{
unsigned int i;
if (!def)
return 0;
if (indent == NULL)
indent = "";
if (!def->model && def->nfeatures) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Non-empty feature list specified without CPU model"));
return -1;
}
if (!(flags & VIR_CPU_FORMAT_EMBEDED)) {
if (def->type == VIR_CPU_TYPE_GUEST && def->model) {
const char *match;
if (!(match = virCPUMatchTypeToString(def->match))) {
virCPUReportError(VIR_ERR_INTERNAL_ERROR,
_("Unexpected CPU match policy %d"), def->match);
return -1;
}
virBufferAsprintf(buf, "%s<cpu match='%s'>\n", indent, match);
}
else
virBufferAsprintf(buf, "%s<cpu>\n", indent);
if (def->arch)
virBufferAsprintf(buf, "%s <arch>%s</arch>\n", indent, def->arch);
}
if (def->model)
virBufferAsprintf(buf, "%s <model>%s</model>\n", indent, def->model);
virBufferAsprintf(buf, "<model>%s</model>\n", def->model);
if (def->vendor) {
virBufferAsprintf(buf, "%s <vendor>%s</vendor>\n",
indent, def->vendor);
virBufferAsprintf(buf, "<vendor>%s</vendor>\n", def->vendor);
}
if (def->sockets && def->cores && def->threads) {
virBufferAsprintf(buf, "%s <topology", indent);
virBufferAddLit(buf, "<topology");
virBufferAsprintf(buf, " sockets='%u'", def->sockets);
virBufferAsprintf(buf, " cores='%u'", def->cores);
virBufferAsprintf(buf, " threads='%u'", def->threads);
@ -399,18 +406,14 @@ virCPUDefFormatBuf(virBufferPtr buf,
_("Unexpected CPU feature policy %d"), feature->policy);
return -1;
}
virBufferAsprintf(buf, "%s <feature policy='%s' name='%s'/>\n",
indent, policy, feature->name);
}
else {
virBufferAsprintf(buf, "%s <feature name='%s'/>\n",
indent, feature->name);
virBufferAsprintf(buf, "<feature policy='%s' name='%s'/>\n",
policy, feature->name);
} else {
virBufferAsprintf(buf, "<feature name='%s'/>\n",
feature->name);
}
}
if (!(flags & VIR_CPU_FORMAT_EMBEDED))
virBufferAsprintf(buf, "%s</cpu>\n", indent);
return 0;
}

View File

@ -95,25 +95,19 @@ virCPUDefParseXML(const xmlNodePtr node,
xmlXPathContextPtr ctxt,
enum virCPUType mode);
enum virCPUFormatFlags {
VIR_CPU_FORMAT_EMBEDED = (1 << 0) /* embed into existing <cpu/> element
* in host capabilities */
};
bool
virCPUDefIsEqual(virCPUDefPtr src,
virCPUDefPtr dst);
char *
virCPUDefFormat(virCPUDefPtr def,
const char *indent,
unsigned int flags);
virCPUDefFormat(virCPUDefPtr def);
int
virCPUDefFormatBuf(virBufferPtr buf,
virCPUDefPtr def,
const char *indent,
unsigned int flags);
virCPUDefPtr def);
int
virCPUDefFormatBufFull(virBufferPtr buf,
virCPUDefPtr def);
int
virCPUDefAddFeature(virCPUDefPtr cpu,

View File

@ -10825,8 +10825,10 @@ virDomainDefFormatInternal(virDomainDefPtr def,
virBufferAddLit(buf, " </features>\n");
}
if (virCPUDefFormatBuf(buf, def->cpu, " ", 0) < 0)
virBufferAdjustIndent(buf, 2);
if (virCPUDefFormatBufFull(buf, def->cpu) < 0)
goto cleanup;
virBufferAdjustIndent(buf, -2);
virBufferAsprintf(buf, " <clock offset='%s'",
virDomainClockOffsetTypeToString(def->clock.offset));

View File

@ -320,7 +320,7 @@ cpuBaselineXML(const char **xmlCPUs,
if (!(cpu = cpuBaseline(cpus, ncpus, models, nmodels)))
goto error;
cpustr = virCPUDefFormat(cpu, "", 0);
cpustr = virCPUDefFormat(cpu);
cleanup:
if (cpus) {

View File

@ -176,7 +176,7 @@ cpuTestCompareXML(const char *arch,
if (virtTestLoadFile(xml, &expected) < 0)
goto cleanup;
if (!(actual = virCPUDefFormat(cpu, NULL, 0)))
if (!(actual = virCPUDefFormat(cpu)))
goto cleanup;
if (STRNEQ(expected, actual)) {