1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

virsh-completer: introduce virshPagesizeNodeToString

A helper function that takes a XML node with a "size"
and "unit" attributes and converts it into a human-readable string.

Reduce the size and number of variables in the parent function.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Ján Tomko 2019-03-29 18:31:01 +01:00
parent b0325e07e6
commit cbc6ee722b

View File

@ -615,27 +615,44 @@ virshSnapshotNameCompleter(vshControl *ctl,
return ret; return ret;
} }
static char *
virshPagesizeNodeToString(xmlNodePtr node)
{
VIR_AUTOFREE(char *) pagesize = NULL;
VIR_AUTOFREE(char *) unit = NULL;
unsigned long long byteval = 0;
const char *suffix = NULL;
double size = 0;
char *ret;
pagesize = virXMLPropString(node, "size");
unit = virXMLPropString(node, "unit");
if (virStrToLong_ull(pagesize, NULL, 10, &byteval) < 0)
return NULL;
if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0)
return NULL;
size = vshPrettyCapacity(byteval, &suffix);
if (virAsprintf(&ret, "%.0f%s", size, suffix) < 0)
return NULL;
return ret;
}
char ** char **
virshAllocpagesPagesizeCompleter(vshControl *ctl, virshAllocpagesPagesizeCompleter(vshControl *ctl,
const vshCmd *cmd ATTRIBUTE_UNUSED, const vshCmd *cmd ATTRIBUTE_UNUSED,
unsigned int flags) unsigned int flags)
{ {
unsigned long long byteval = 0;
VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; VIR_AUTOPTR(xmlXPathContext) ctxt = NULL;
virshControlPtr priv = ctl->privData; virshControlPtr priv = ctl->privData;
unsigned int npages = 0; unsigned int npages = 0;
VIR_AUTOFREE(xmlNodePtr *) pages = NULL; VIR_AUTOFREE(xmlNodePtr *) pages = NULL;
VIR_AUTOPTR(xmlDoc) doc = NULL; VIR_AUTOPTR(xmlDoc) doc = NULL;
double size = 0;
size_t i = 0; size_t i = 0;
const char *suffix = NULL;
const char *cellnum = NULL; const char *cellnum = NULL;
bool cellno = vshCommandOptBool(cmd, "cellno"); bool cellno = vshCommandOptBool(cmd, "cellno");
VIR_AUTOFREE(char *) path = NULL; VIR_AUTOFREE(char *) path = NULL;
VIR_AUTOFREE(char *) pagesize = NULL;
VIR_AUTOFREE(char *) cap_xml = NULL; VIR_AUTOFREE(char *) cap_xml = NULL;
char **ret = NULL; char **ret = NULL;
VIR_AUTOFREE(char *) unit = NULL;
VIR_AUTOSTRINGLIST tmp = NULL; VIR_AUTOSTRINGLIST tmp = NULL;
virCheckFlags(0, NULL); virCheckFlags(0, NULL);
@ -667,16 +684,7 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
return NULL; return NULL;
for (i = 0; i < npages; i++) { for (i = 0; i < npages; i++) {
VIR_FREE(pagesize); if (!(tmp[i] = virshPagesizeNodeToString(pages[i])))
VIR_FREE(unit);
pagesize = virXMLPropString(pages[i], "size");
unit = virXMLPropString(pages[i], "unit");
if (virStrToLong_ull(pagesize, NULL, 10, &byteval) < 0)
return NULL;
if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0)
return NULL;
size = vshPrettyCapacity(byteval, &suffix);
if (virAsprintf(&tmp[i], "%.0f%s", size, suffix) < 0)
return NULL; return NULL;
} }