]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Introduce virshStorageVolNameCompleter
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 12 Jan 2018 13:15:11 +0000 (14:15 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 24 Jan 2018 12:51:19 +0000 (13:51 +0100)
This one is a bit simpler since virStoragePoolListAllVolumes()
has no flags yet.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
tools/virsh-completer.c
tools/virsh-completer.h
tools/virsh-volume.c

index 947c326fcc80580591eabc0dabb8592448816fe8..4e1bd9fbb154f1d81f796605c6e5de102a40fc6d 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "virsh-completer.h"
 #include "virsh.h"
+#include "virsh-pool.h"
 #include "virsh-util.h"
 #include "internal.h"
 #include "viralloc.h"
@@ -195,3 +196,54 @@ virshStoragePoolNameCompleter(vshControl *ctl,
     VIR_FREE(ret);
     return NULL;
 }
+
+
+char **
+virshStorageVolNameCompleter(vshControl *ctl,
+                             const vshCmd *cmd,
+                             unsigned int flags)
+{
+    virshControlPtr priv = ctl->privData;
+    virStoragePoolPtr pool = NULL;
+    virStorageVolPtr *vols = NULL;
+    int nvols = 0;
+    size_t i = 0;
+    char **ret = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if (!(pool = virshCommandOptPool(ctl, cmd, "pool", NULL)))
+        return false;
+
+    if ((nvols = virStoragePoolListAllVolumes(pool, &vols, flags)) < 0)
+        goto error;
+
+    if (VIR_ALLOC_N(ret, nvols + 1) < 0)
+        goto error;
+
+    for (i = 0; i < nvols; i++) {
+        const char *name = virStorageVolGetName(vols[i]);
+
+        if (VIR_STRDUP(ret[i], name) < 0)
+            goto error;
+
+        virStorageVolFree(vols[i]);
+    }
+    VIR_FREE(vols);
+    virStoragePoolFree(pool);
+
+    return ret;
+
+ error:
+    for (; i < nvols; i++)
+        virStorageVolFree(vols[i]);
+    VIR_FREE(vols);
+    for (i = 0; i < nvols; i++)
+        VIR_FREE(ret[i]);
+    VIR_FREE(ret);
+    virStoragePoolFree(pool);
+    return NULL;
+}
index 249e793b9f5e76f7ed519f09c89950fc77374a85..1e4ce593244a79ceac7bf9bab778574de32745b1 100644 (file)
@@ -42,4 +42,8 @@ char ** virshStoragePoolNameCompleter(vshControl *ctl,
                                       const vshCmd *cmd,
                                       unsigned int flags);
 
+char ** virshStorageVolNameCompleter(vshControl *ctl,
+                                     const vshCmd *cmd,
+                                     unsigned int flags);
+
 #endif
index bfbb7c7d7a3b789fd1709f0d79e78fddd99f8d23..bacbec0d27b73e61d8fec9973a79c261aceedd09 100644 (file)
@@ -63,7 +63,8 @@
     {.name = "vol", \
      .type = VSH_OT_DATA, \
      .flags = VSH_OFLAG_REQ, \
-     .help = N_("vol name, key or path") \
+     .help = N_("vol name, key or path"), \
+     .completer = virshStorageVolNameCompleter, \
     }
 
 virStorageVolPtr