lib: Eliminate use of virTristateSwitchTypeFromString()

There are couple of places (all of them in XML parsing) where
virTristateSwitchTypeFromString() is called. Well, the same
result can be achieved by virXMLPropTristateSwitch() and on fewer
lines.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2022-01-20 11:24:55 +01:00
parent 5a33dd25c1
commit 9086ae4fac
4 changed files with 67 additions and 119 deletions

View File

@ -323,12 +323,12 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
xmlNodePtr topology = NULL; xmlNodePtr topology = NULL;
VIR_XPATH_NODE_AUTORESTORE(ctxt) VIR_XPATH_NODE_AUTORESTORE(ctxt)
int n; int n;
int rv;
size_t i; size_t i;
g_autofree char *cpuMode = NULL; g_autofree char *cpuMode = NULL;
g_autofree char *fallback = NULL; g_autofree char *fallback = NULL;
g_autofree char *vendor_id = NULL; g_autofree char *vendor_id = NULL;
g_autofree char *tscScaling = NULL; g_autofree char *tscScaling = NULL;
g_autofree char *migratable = NULL;
g_autofree virHostCPUTscInfo *tsc = NULL; g_autofree virHostCPUTscInfo *tsc = NULL;
*cpu = NULL; *cpu = NULL;
@ -394,25 +394,17 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
def->mode = VIR_CPU_MODE_CUSTOM; def->mode = VIR_CPU_MODE_CUSTOM;
} }
if ((migratable = virXMLPropString(ctxt->node, "migratable"))) { if ((rv = virXMLPropTristateSwitch(ctxt->node, "migratable",
int val; VIR_XML_PROP_NONE,
&def->migratable)) < 0) {
if (def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH && return -1;
def->mode != VIR_CPU_MODE_MAXIMUM) { } else if (rv > 0 &&
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", def->mode != VIR_CPU_MODE_HOST_PASSTHROUGH &&
_("Attribute migratable is only allowed for " def->mode != VIR_CPU_MODE_MAXIMUM) {
"'host-passthrough' / 'maximum' CPU mode")); virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
return -1; _("Attribute migratable is only allowed for "
} "'host-passthrough' / 'maximum' CPU mode"));
return -1;
if ((val = virTristateSwitchTypeFromString(migratable)) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Invalid value in migratable attribute: '%s'"),
migratable);
return -1;
}
def->migratable = val;
} }
if (def->type == VIR_CPU_TYPE_GUEST) { if (def->type == VIR_CPU_TYPE_GUEST) {

View File

@ -7310,8 +7310,6 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
g_autofree char *rawio = NULL; g_autofree char *rawio = NULL;
g_autofree char *backendStr = NULL; g_autofree char *backendStr = NULL;
g_autofree char *model = NULL; g_autofree char *model = NULL;
g_autofree char *display = NULL;
g_autofree char *ramfb = NULL;
/* @managed can be read from the xml document - it is always an /* @managed can be read from the xml document - it is always an
* attribute of the toplevel element, no matter what type of * attribute of the toplevel element, no matter what type of
@ -7324,8 +7322,6 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
sgio = virXMLPropString(node, "sgio"); sgio = virXMLPropString(node, "sgio");
rawio = virXMLPropString(node, "rawio"); rawio = virXMLPropString(node, "rawio");
model = virXMLPropString(node, "model"); model = virXMLPropString(node, "model");
display = virXMLPropString(node, "display");
ramfb = virXMLPropString(node, "ramfb");
/* @type is passed in from the caller rather than read from the /* @type is passed in from the caller rather than read from the
* xml document, because it is specified in different places for * xml document, because it is specified in different places for
@ -7426,23 +7422,15 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
return -1; return -1;
} }
if (display && if (virXMLPropTristateSwitch(node, "display",
(mdevsrc->display = virTristateSwitchTypeFromString(display)) <= 0) { VIR_XML_PROP_NONE,
virReportError(VIR_ERR_XML_ERROR, &mdevsrc->display) < 0)
_("unknown value '%s' for <hostdev> attribute "
"'display'"),
display);
return -1; return -1;
}
if (ramfb && if (virXMLPropTristateSwitch(node, "ramfb",
(mdevsrc->ramfb = virTristateSwitchTypeFromString(ramfb)) <= 0) { VIR_XML_PROP_NONE,
virReportError(VIR_ERR_XML_ERROR, &mdevsrc->ramfb) < 0)
_("unknown value '%s' for <hostdev> attribute "
"'ramfb'"),
ramfb);
return -1; return -1;
}
} }
switch (def->source.subsys.type) { switch (def->source.subsys.type) {
@ -9924,11 +9912,10 @@ virDomainFSDefParseXML(virDomainXMLOption *xmlopt,
if (def->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_VIRTIOFS) { if (def->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_VIRTIOFS) {
g_autofree char *queue_size = virXPathString("string(./driver/@queue)", ctxt); g_autofree char *queue_size = virXPathString("string(./driver/@queue)", ctxt);
g_autofree char *binary = virXPathString("string(./binary/@path)", ctxt); g_autofree char *binary = virXPathString("string(./binary/@path)", ctxt);
g_autofree char *xattr = virXPathString("string(./binary/@xattr)", ctxt);
g_autofree char *cache = virXPathString("string(./binary/cache/@mode)", ctxt); g_autofree char *cache = virXPathString("string(./binary/cache/@mode)", ctxt);
g_autofree char *sandbox = virXPathString("string(./binary/sandbox/@mode)", ctxt); g_autofree char *sandbox = virXPathString("string(./binary/sandbox/@mode)", ctxt);
g_autofree char *posix_lock = virXPathString("string(./binary/lock/@posix)", ctxt); xmlNodePtr binary_node = virXPathNode("./binary", ctxt);
g_autofree char *flock = virXPathString("string(./binary/lock/@flock)", ctxt); xmlNodePtr binary_lock_node = virXPathNode("./binary/lock", ctxt);
int val; int val;
if (queue_size && virStrToLong_ull(queue_size, NULL, 10, &def->queue_size) < 0) { if (queue_size && virStrToLong_ull(queue_size, NULL, 10, &def->queue_size) < 0) {
@ -9941,14 +9928,20 @@ virDomainFSDefParseXML(virDomainXMLOption *xmlopt,
if (binary) if (binary)
def->binary = virFileSanitizePath(binary); def->binary = virFileSanitizePath(binary);
if (xattr) { if (virXMLPropTristateSwitch(binary_node, "xattr",
if ((val = virTristateSwitchTypeFromString(xattr)) <= 0) { VIR_XML_PROP_NONE,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, &def->xattr) < 0)
_("unknown xattr value '%s'"), xattr); goto error;
goto error;
} if (virXMLPropTristateSwitch(binary_lock_node, "posix",
def->xattr = val; VIR_XML_PROP_NONE,
} &def->posix_lock) < 0)
goto error;
if (virXMLPropTristateSwitch(binary_lock_node, "flock",
VIR_XML_PROP_NONE,
&def->flock) < 0)
goto error;
if (cache) { if (cache) {
if ((val = virDomainFSCacheModeTypeFromString(cache)) <= 0) { if ((val = virDomainFSCacheModeTypeFromString(cache)) <= 0) {
@ -9970,23 +9963,6 @@ virDomainFSDefParseXML(virDomainXMLOption *xmlopt,
def->sandbox = val; def->sandbox = val;
} }
if (posix_lock) {
if ((val = virTristateSwitchTypeFromString(posix_lock)) <= 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown posix lock value '%s'"), posix_lock);
goto error;
}
def->posix_lock = val;
}
if (flock) {
if ((val = virTristateSwitchTypeFromString(flock)) <= 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown flock value '%s'"), flock);
goto error;
}
def->flock = val;
}
} }
if (source == NULL && def->type != VIR_DOMAIN_FS_TYPE_RAM if (source == NULL && def->type != VIR_DOMAIN_FS_TYPE_RAM
@ -10309,8 +10285,6 @@ virDomainNetDefParseXML(virDomainXMLOption *xmlopt,
g_autofree char *model = NULL; g_autofree char *model = NULL;
g_autofree char *backend = NULL; g_autofree char *backend = NULL;
g_autofree char *txmode = NULL; g_autofree char *txmode = NULL;
g_autofree char *ioeventfd = NULL;
g_autofree char *event_idx = NULL;
g_autofree char *queues = NULL; g_autofree char *queues = NULL;
g_autofree char *rx_queue_size = NULL; g_autofree char *rx_queue_size = NULL;
g_autofree char *tx_queue_size = NULL; g_autofree char *tx_queue_size = NULL;
@ -10462,8 +10436,6 @@ virDomainNetDefParseXML(virDomainXMLOption *xmlopt,
backend = virXMLPropString(driver_node, "name"); backend = virXMLPropString(driver_node, "name");
txmode = virXMLPropString(driver_node, "txmode"); txmode = virXMLPropString(driver_node, "txmode");
ioeventfd = virXMLPropString(driver_node, "ioeventfd");
event_idx = virXMLPropString(driver_node, "event_idx");
queues = virXMLPropString(driver_node, "queues"); queues = virXMLPropString(driver_node, "queues");
rx_queue_size = virXMLPropString(driver_node, "rx_queue_size"); rx_queue_size = virXMLPropString(driver_node, "rx_queue_size");
tx_queue_size = virXMLPropString(driver_node, "tx_queue_size"); tx_queue_size = virXMLPropString(driver_node, "tx_queue_size");
@ -10828,24 +10800,17 @@ virDomainNetDefParseXML(virDomainXMLOption *xmlopt,
} }
def->driver.virtio.txmode = val; def->driver.virtio.txmode = val;
} }
if (ioeventfd) {
if ((val = virTristateSwitchTypeFromString(ioeventfd)) <= 0) { if (virXMLPropTristateSwitch(driver_node, "ioeventfd",
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, VIR_XML_PROP_NONE,
_("unknown interface ioeventfd mode '%s'"), &def->driver.virtio.ioeventfd) < 0)
ioeventfd); goto error;
goto error;
} if (virXMLPropTristateSwitch(driver_node, "event_idx",
def->driver.virtio.ioeventfd = val; VIR_XML_PROP_NONE,
} &def->driver.virtio.event_idx) < 0)
if (event_idx) { goto error;
if ((val = virTristateSwitchTypeFromString(event_idx)) <= 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown interface event_idx mode '%s'"),
event_idx);
goto error;
}
def->driver.virtio.event_idx = val;
}
if (queues) { if (queues) {
unsigned int q; unsigned int q;
if (virStrToLong_uip(queues, NULL, 10, &q) < 0) { if (virStrToLong_uip(queues, NULL, 10, &q) < 0) {
@ -19112,13 +19077,11 @@ virDomainDefParseMemory(virDomainDef *def,
} }
/* and info about it */ /* and info about it */
if ((tmp = virXPathString("string(./memory[1]/@dumpCore)", ctxt)) && if ((node = virXPathNode("./memory[1]", ctxt)) &&
(def->mem.dump_core = virTristateSwitchTypeFromString(tmp)) <= 0) { virXMLPropTristateSwitch(node, "dumpCore",
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, VIR_XML_PROP_NONE,
_("Invalid memory core dump attribute value '%s'"), tmp); &def->mem.dump_core) < 0)
return -1; return -1;
}
VIR_FREE(tmp);
tmp = virXPathString("string(./memoryBacking/source/@type)", ctxt); tmp = virXPathString("string(./memoryBacking/source/@type)", ctxt);
if (tmp) { if (tmp) {

View File

@ -272,9 +272,9 @@ struct _virDomainHostdevSubsysSCSI {
struct _virDomainHostdevSubsysMediatedDev { struct _virDomainHostdevSubsysMediatedDev {
int model; /* enum virMediatedDeviceModelType */ int model; /* enum virMediatedDeviceModelType */
int display; /* virTristateSwitch */ virTristateSwitch display;
char uuidstr[VIR_UUID_STRING_BUFLEN]; /* mediated device's uuid string */ char uuidstr[VIR_UUID_STRING_BUFLEN]; /* mediated device's uuid string */
int ramfb; /* virTristateSwitch */ virTristateSwitch ramfb;
}; };
typedef enum { typedef enum {
@ -2674,7 +2674,7 @@ struct _virDomainMemtune {
bool nosharepages; bool nosharepages;
bool locked; bool locked;
int dump_core; /* enum virTristateSwitch */ virTristateSwitch dump_core;
unsigned long long hard_limit; /* in kibibytes, limit at off_t bytes */ unsigned long long hard_limit; /* in kibibytes, limit at off_t bytes */
unsigned long long soft_limit; /* in kibibytes, limit at off_t bytes */ unsigned long long soft_limit; /* in kibibytes, limit at off_t bytes */
unsigned long long min_guarantee; /* in kibibytes, limit at off_t bytes */ unsigned long long min_guarantee; /* in kibibytes, limit at off_t bytes */

View File

@ -1444,36 +1444,29 @@ static int
x86ModelParseDecode(virCPUx86Model *model, x86ModelParseDecode(virCPUx86Model *model,
xmlXPathContextPtr ctxt) xmlXPathContextPtr ctxt)
{ {
g_autofree char *host = NULL; xmlNodePtr decode_node = NULL;
g_autofree char *guest = NULL; virTristateSwitch decodeHost;
int val; virTristateSwitch decodeGuest;
if ((host = virXPathString("string(./decode/@host)", ctxt))) if (!(decode_node = virXPathNode("./decode", ctxt))) {
val = virTristateSwitchTypeFromString(host);
else
val = VIR_TRISTATE_SWITCH_ABSENT;
if (val <= 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid or missing decode/host attribute in CPU model %s"), _("missing decode element in CPU model %s"),
model->name); model->name);
return -1; return -1;
} }
model->decodeHost = val == VIR_TRISTATE_SWITCH_ON;
if ((guest = virXPathString("string(./decode/@guest)", ctxt))) if (virXMLPropTristateSwitch(decode_node, "host",
val = virTristateSwitchTypeFromString(guest); VIR_XML_PROP_REQUIRED,
else &decodeHost) < 0)
val = VIR_TRISTATE_SWITCH_ABSENT;
if (val <= 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid or missing decode/guest attribute in CPU model %s"),
model->name);
return -1; return -1;
}
model->decodeGuest = val == VIR_TRISTATE_SWITCH_ON;
if (virXMLPropTristateSwitch(decode_node, "guest",
VIR_XML_PROP_REQUIRED,
&decodeGuest) < 0)
return -1;
virTristateSwitchToBool(decodeHost, &model->decodeHost);
virTristateSwitchToBool(decodeGuest, &model->decodeGuest);
return 0; return 0;
} }