]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Several bug fixes to list of domains. Display '-' instead of '-1' for inactive domains
authorDaniel P. Berrange <berrange@redhat.com>
Sun, 3 Sep 2006 17:34:04 +0000 (17:34 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Sun, 3 Sep 2006 17:34:04 +0000 (17:34 +0000)
ChangeLog
src/virsh.c

index 677be72f37583a8d3601d115d4c90e53feefc8f7..ddaf43f5e779f1def2754a7e720dfb8f73d6e75f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Sun Sep  3 12:34:23 EDT 2006 Daniel Berrange <berrange@redhat.com>
+
+       * src/virsh.c: use the return value of virConnectListDomains when
+       iterating over list of ids/names, because it is not neccessarily
+       the same as the value returned by virConnectNumOfDomains. Use qsort
+       to sort active domains by Id, and inactive domains by name, since 
+       there is no guarenteed sort ordering when listing domains. For inactive
+       domains display a '-' instead of '-1' to make it clear they have no
+       sensible ID number.
+       
 Sat Sep  2 22:28:18 CEST 2006 Daniel Veillard <veillard@redhat.com>
 
        * src/xen_internal.c: converting to handle the new incompatible 
index 62473d1cdfec88ed25b7dffe980247701282db14..eebb91fcc1ba1d94d8c308aa26c6678914e32d9e 100644 (file)
@@ -321,7 +321,22 @@ static vshCmdOptDef opts_list[] = {
 };
 
 
+static int domidsorter(const void *a, const void *b) {
+  const int *ia = (const int *)a;
+  const int *ib = (const int *)b;
+
+  if (*ia > *ib)
+    return 1;
+  else if (*ia < *ib)
+    return -1;
+  return 0;
+}
+static int domnamesorter(const void *a, const void *b) {
+  const char **sa = (const char**)a;
+  const char **sb = (const char**)b;
 
+  return strcasecmp(*sa, *sb);
+}
 static int
 cmdList(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
 {
@@ -345,11 +360,13 @@ cmdList(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
       if (maxid) {
         ids = vshMalloc(ctl, sizeof(int) * maxid);
        
-        if (virConnectListDomains(ctl->conn, &ids[0], maxid) < 0) {
+        if ((maxid = virConnectListDomains(ctl->conn, &ids[0], maxid)) < 0) {
          vshError(ctl, FALSE, "failed to list active domains.");
          free(ids);
          return FALSE;
         }
+       
+       qsort(&ids[0], maxid, sizeof(int), domidsorter);
       }
     }
     if (inactive) {
@@ -361,15 +378,17 @@ cmdList(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
         return FALSE;
       }
       if (maxname) {
-        names = vshMalloc(ctl, sizeof(int) * maxname);
+        names = vshMalloc(ctl, sizeof(char *) * maxname);
        
-        if (virConnectListDefinedDomains(ctl->conn, names, maxname) < 0) {
+        if ((maxname = virConnectListDefinedDomains(ctl->conn, names, maxname)) < 0) {
          vshError(ctl, FALSE, "failed to list inactive domains.");
          if (ids)
            free(ids);
          free(names);
          return FALSE;
         }
+
+       qsort(&names[0], maxname, sizeof(char*), domnamesorter);
       }
     }
     vshPrintExtra(ctl, "%3s %-20s %s\n", "Id", "Name", "State");
@@ -394,6 +413,7 @@ cmdList(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
     }
     for (i = 0; i < maxname; i++) {
         int ret;
+        unsigned int id;
         virDomainInfo info;
         virDomainPtr dom = virDomainLookupByName(ctl->conn, names[i]);
 
@@ -401,12 +421,21 @@ cmdList(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
         if (!dom)
             continue;
         ret = virDomainGetInfo(dom, &info);
-
-        vshPrint(ctl, "%3d %-20s %s\n",
-                 virDomainGetID(dom),
-                names[i],
-                 ret <
-                 0 ? "no state" : vshDomainStateToString(info.state));
+       id = virDomainGetID(dom);
+
+       if (id == ((unsigned int)-1)) {
+         vshPrint(ctl, "%3s %-20s %s\n",
+                  "-",
+                  names[i],
+                  ret <
+                  0 ? "no state" : vshDomainStateToString(info.state));
+       } else {
+         vshPrint(ctl, "%3d %-20s %s\n",
+                  id,
+                  names[i],
+                  ret <
+                  0 ? "no state" : vshDomainStateToString(info.state));
+       }
         virDomainFree(dom);
     }
     if (ids)
@@ -950,6 +979,7 @@ cmdDominfo(vshControl * ctl, vshCmd * cmd)
     virDomainInfo info;
     virDomainPtr dom;
     int ret = TRUE;
+    unsigned int id;
     char *str, uuid[37];
 
     if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
@@ -958,7 +988,11 @@ cmdDominfo(vshControl * ctl, vshCmd * cmd)
     if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL)))
         return FALSE;
 
-    vshPrint(ctl, "%-15s %d\n", "Id:", virDomainGetID(dom));
+    id = virDomainGetID(dom);
+    if (id == ((unsigned int)-1))
+      vshPrint(ctl, "%-15s %s\n", "Id:", "-");
+    else
+      vshPrint(ctl, "%-15s %d\n", "Id:", id);
     vshPrint(ctl, "%-15s %s\n", "Name:", virDomainGetName(dom));
     if (virDomainGetUUIDString(dom, &uuid[0])==0)
         vshPrint(ctl, "%-15s %s\n", "UUID:", uuid);
@@ -1415,6 +1449,7 @@ static int
 cmdDomid(vshControl * ctl, vshCmd * cmd)
 {
     virDomainPtr dom;
+    unsigned int id;
 
     if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
         return FALSE;
@@ -1422,7 +1457,11 @@ cmdDomid(vshControl * ctl, vshCmd * cmd)
                                     VSH_DOMBYNAME|VSH_DOMBYUUID)))
         return FALSE;
     
-    vshPrint(ctl, "%d\n", virDomainGetID(dom));
+    id = virDomainGetID(dom);
+    if (id == ((unsigned int)-1))
+      vshPrint(ctl, "%s\n", "-");
+    else
+      vshPrint(ctl, "%d\n", id);
     virDomainFree(dom);
     return TRUE;
 }