Post-release version bump to 11.0.0
[libvirt.git] / tools / virsh-completer-snapshot.c
blobc7974d715cac0be7bcd23107d80f920d1b66dcfc
1 /*
2 * virsh-completer-snapshot.c: virsh completer callbacks related to snapshots
4 * Copyright (C) 2019 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #include "virsh-completer-snapshot.h"
24 #include "virsh-util.h"
25 #include "virsh.h"
27 char **
28 virshSnapshotNameCompleter(vshControl *ctl,
29 const vshCmd *cmd,
30 unsigned int flags)
32 virshControl *priv = ctl->privData;
33 g_autoptr(virshDomain) dom = NULL;
34 virDomainSnapshotPtr *snapshots = NULL;
35 int rc;
36 int nsnapshots = 0;
37 size_t i = 0;
38 char **ret = NULL;
39 g_auto(GStrv) tmp = NULL;
41 virCheckFlags(0, NULL);
43 if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
44 return NULL;
46 if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
47 return NULL;
49 if ((rc = virDomainListAllSnapshots(dom, &snapshots, flags)) < 0)
50 goto cleanup;
51 nsnapshots = rc;
53 tmp = g_new0(char *, nsnapshots + 1);
55 for (i = 0; i < nsnapshots; i++) {
56 const char *name = virDomainSnapshotGetName(snapshots[i]);
58 tmp[i] = g_strdup(name);
61 ret = g_steal_pointer(&tmp);
63 cleanup:
64 for (i = 0; i < nsnapshots; i++)
65 virshDomainSnapshotFree(snapshots[i]);
66 g_free(snapshots);
67 return ret;