[libvirt] [PATCH 2/8] virsh: Introduce virshStorageVolNameCompleter

Michal Privoznik mprivozn at redhat.com
Fri Jan 12 14:37:36 UTC 2018


This one is a bit simpler since virStoragePoolListAllVolumes()
has no flags yet.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tools/virsh-completer.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh-completer.h |  4 ++++
 tools/virsh-volume.c    |  3 ++-
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index 8ca2fffd9..d6ac0ccb8 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -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"
@@ -198,3 +199,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;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index 249e793b9..1e4ce5932 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -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
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index b96e205f7..c1f36d9b9 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -61,7 +61,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
-- 
2.13.6




More information about the libvir-list mailing list