]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh-completer: introduce virshPagesizeNodeToString
authorJán Tomko <jtomko@redhat.com>
Fri, 29 Mar 2019 17:31:01 +0000 (18:31 +0100)
committerJán Tomko <jtomko@redhat.com>
Wed, 3 Apr 2019 08:29:15 +0000 (10:29 +0200)
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>
tools/virsh-completer.c

index 5688a7fd8d0e1e9a2643ba7f301a3397cac840bf..aac4579d1fc96b375afdbcf21e5c3484357f1362 100644 (file)
@@ -615,27 +615,44 @@ virshSnapshotNameCompleter(vshControl *ctl,
     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 **
 virshAllocpagesPagesizeCompleter(vshControl *ctl,
                                  const vshCmd *cmd ATTRIBUTE_UNUSED,
                                  unsigned int flags)
 {
-    unsigned long long byteval = 0;
     VIR_AUTOPTR(xmlXPathContext) ctxt = NULL;
     virshControlPtr priv = ctl->privData;
     unsigned int npages = 0;
     VIR_AUTOFREE(xmlNodePtr *) pages = NULL;
     VIR_AUTOPTR(xmlDoc) doc = NULL;
-    double size = 0;
     size_t i = 0;
-    const char *suffix = NULL;
     const char *cellnum = NULL;
     bool cellno = vshCommandOptBool(cmd, "cellno");
     VIR_AUTOFREE(char *) path = NULL;
-    VIR_AUTOFREE(char *) pagesize = NULL;
     VIR_AUTOFREE(char *) cap_xml = NULL;
     char **ret = NULL;
-    VIR_AUTOFREE(char *) unit = NULL;
     VIR_AUTOSTRINGLIST tmp = NULL;
 
     virCheckFlags(0, NULL);
@@ -667,16 +684,7 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
         return NULL;
 
     for (i = 0; i < npages; i++) {
-        VIR_FREE(pagesize);
-        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)
+        if (!(tmp[i] = virshPagesizeNodeToString(pages[i])))
             return NULL;
     }