[libvirt PATCH 00/10] virsh: moments: use g_auto more (glib chronicles)

Ján Tomko (10): vsh: table: define cleanup function virsh: add cleanup function for virshDomainCheckpoint virsh: add cleanup function for virshDomainSnapshot virsh: checkpoint: move variables inside the loop virsh: snapshot: move variables inside the loop virsh: cmdCheckpointInfo: delete unused variables virsh: checkpoint: use g_auto where possible virsh: checkpoint: remove pointless cleanup labels virsh: snapshot: use g_auto where possible virsh: snapshot: remove pointless cleanup labels tools/virsh-checkpoint.c | 188 ++++++++----------------- tools/virsh-snapshot.c | 288 +++++++++++++-------------------------- tools/virsh-util.h | 4 + tools/vsh-table.h | 1 + 4 files changed, 156 insertions(+), 325 deletions(-) -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/vsh-table.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/vsh-table.h b/tools/vsh-table.h index a8cfca62d5..5ce416cfa1 100644 --- a/tools/vsh-table.h +++ b/tools/vsh-table.h @@ -30,3 +30,4 @@ vshTable *vshTableNew(const char *format, ...); int vshTableRowAppend(vshTable *table, const char *arg, ...); void vshTablePrintToStdout(vshTable *table, vshControl *ctl); char *vshTablePrintToString(vshTable *table, bool header); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(vshTable, vshTableFree); -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-util.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/virsh-util.h b/tools/virsh-util.h index 87673bce9f..ba79d04fb8 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -50,8 +50,10 @@ void virshSecretFree(virSecretPtr secret); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshSecret, virshSecretFree); +typedef virDomainCheckpoint virshDomainCheckpoint; void virshDomainCheckpointFree(virDomainCheckpointPtr chk); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshDomainCheckpoint, virshDomainCheckpointFree); void virshDomainSnapshotFree(virDomainSnapshotPtr snap); -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-util.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/virsh-util.h b/tools/virsh-util.h index ba79d04fb8..6115b05d4d 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -55,8 +55,10 @@ void virshDomainCheckpointFree(virDomainCheckpointPtr chk); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshDomainCheckpoint, virshDomainCheckpointFree); +typedef virDomainSnapshot virshDomainSnapshot; void virshDomainSnapshotFree(virDomainSnapshotPtr snap); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(virshDomainSnapshot, virshDomainSnapshotFree); int virshDomainState(vshControl *ctl, -- 2.31.1

Use g_auto to free them instead of open-coding it. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-checkpoint.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/tools/virsh-checkpoint.c b/tools/virsh-checkpoint.c index e88f9ffb47..2b89f376c9 100644 --- a/tools/virsh-checkpoint.c +++ b/tools/virsh-checkpoint.c @@ -716,9 +716,6 @@ cmdCheckpointList(vshControl *ctl, bool ret = false; unsigned int flags = 0; size_t i; - xmlDocPtr xml = NULL; - xmlXPathContextPtr ctxt = NULL; - char *doc = NULL; virDomainCheckpointPtr checkpoint = NULL; long long creation_longlong; g_autoptr(GDateTime) then = NULL; @@ -728,7 +725,6 @@ cmdCheckpointList(vshControl *ctl, bool parent = vshCommandOptBool(cmd, "parent"); bool roots = vshCommandOptBool(cmd, "roots"); const char *from_chk = NULL; - char *parent_chk = NULL; virDomainCheckpointPtr start = NULL; struct virshCheckpointList *checkpointlist = NULL; vshTable *table = NULL; @@ -805,14 +801,12 @@ cmdCheckpointList(vshControl *ctl, for (i = 0; i < checkpointlist->nchks; i++) { g_autofree gchar *thenstr = NULL; + g_autoptr(xmlDoc) xml = NULL; + g_autoptr(xmlXPathContext) ctxt = NULL; + g_autofree char *parent_chk = NULL; + g_autofree char *doc = NULL; const char *chk_name; - /* free up memory from previous iterations of the loop */ - VIR_FREE(parent_chk); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xml); - VIR_FREE(doc); - checkpoint = checkpointlist->chks[i].chk; chk_name = virDomainCheckpointGetName(checkpoint); assert(chk_name); @@ -856,13 +850,8 @@ cmdCheckpointList(vshControl *ctl, ret = true; cleanup: - /* this frees up memory from the last iteration of the loop */ virshCheckpointListFree(checkpointlist); - VIR_FREE(parent_chk); virshDomainCheckpointFree(start); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xml); - VIR_FREE(doc); virshDomainFree(dom); vshTableFree(table); -- 2.31.1

Use g_auto to free them, instead of open-coding it. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-snapshot.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c index e64117785c..350f9e803c 100644 --- a/tools/virsh-snapshot.c +++ b/tools/virsh-snapshot.c @@ -1490,11 +1490,7 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd) bool ret = false; unsigned int flags = 0; size_t i; - xmlDocPtr xml = NULL; - xmlXPathContextPtr ctxt = NULL; - char *doc = NULL; virDomainSnapshotPtr snapshot = NULL; - char *state = NULL; long long creation_longlong; g_autoptr(GDateTime) then = NULL; bool tree = vshCommandOptBool(cmd, "tree"); @@ -1504,7 +1500,6 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd) bool roots = vshCommandOptBool(cmd, "roots"); bool current = vshCommandOptBool(cmd, "current"); const char *from_snap = NULL; - char *parent_snap = NULL; virDomainSnapshotPtr start = NULL; struct virshSnapshotList *snaplist = NULL; vshTable *table = NULL; @@ -1592,15 +1587,13 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd) for (i = 0; i < snaplist->nsnaps; i++) { g_autofree gchar *thenstr = NULL; + g_autoptr(xmlDoc) xml = NULL; + g_autoptr(xmlXPathContext) ctxt = NULL; + g_autofree char *parent_snap = NULL; + g_autofree char *state = NULL; + g_autofree char *doc = NULL; const char *snap_name; - /* free up memory from previous iterations of the loop */ - VIR_FREE(parent_snap); - VIR_FREE(state); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xml); - VIR_FREE(doc); - snapshot = snaplist->snaps[i].snap; snap_name = virDomainSnapshotGetName(snapshot); assert(snap_name); @@ -1648,14 +1641,8 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - /* this frees up memory from the last iteration of the loop */ virshSnapshotListFree(snaplist); - VIR_FREE(parent_snap); - VIR_FREE(state); virshDomainSnapshotFree(start); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xml); - VIR_FREE(doc); virshDomainFree(dom); vshTableFree(table); -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-checkpoint.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tools/virsh-checkpoint.c b/tools/virsh-checkpoint.c index 2b89f376c9..9e942a226b 100644 --- a/tools/virsh-checkpoint.c +++ b/tools/virsh-checkpoint.c @@ -478,8 +478,6 @@ cmdCheckpointInfo(vshControl *ctl, virDomainCheckpointPtr checkpoint = NULL; const char *name; char *parent = NULL; - xmlDocPtr xmldoc = NULL; - xmlXPathContextPtr ctxt = NULL; bool ret = false; int count; unsigned int flags; @@ -522,8 +520,6 @@ cmdCheckpointInfo(vshControl *ctl, ret = true; cleanup: - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xmldoc); VIR_FREE(parent); virshDomainCheckpointFree(checkpoint); virshDomainFree(dom); -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-checkpoint.c | 78 +++++++++++++--------------------------- 1 file changed, 24 insertions(+), 54 deletions(-) diff --git a/tools/virsh-checkpoint.c b/tools/virsh-checkpoint.c index 9e942a226b..5c1d25ecf8 100644 --- a/tools/virsh-checkpoint.c +++ b/tools/virsh-checkpoint.c @@ -47,7 +47,7 @@ virshCheckpointCreate(vshControl *ctl, const char *from) { bool ret = false; - virDomainCheckpointPtr checkpoint; + g_autoptr(virshDomainCheckpoint) checkpoint = NULL; const char *name = NULL; checkpoint = virDomainCheckpointCreateXML(dom, buffer, flags); @@ -70,7 +70,6 @@ virshCheckpointCreate(vshControl *ctl, ret = true; cleanup: - virshDomainCheckpointFree(checkpoint); return ret; } @@ -114,10 +113,10 @@ static bool cmdCheckpointCreate(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *from = NULL; - char *buffer = NULL; + g_autofree char *buffer = NULL; unsigned int flags = 0; VSH_REQUIRE_OPTION("redefine-validate", "redefine"); @@ -146,9 +145,6 @@ cmdCheckpointCreate(vshControl *ctl, ret = virshCheckpointCreate(ctl, dom, buffer, flags, from); cleanup: - VIR_FREE(buffer); - virshDomainFree(dom); - return ret; } @@ -165,7 +161,7 @@ virshParseCheckpointDiskspec(vshControl *ctl, const char *name = NULL; const char *checkpoint = NULL; const char *bitmap = NULL; - char **array = NULL; + g_auto(GStrv) array = NULL; int narray; size_t i; @@ -193,7 +189,6 @@ virshParseCheckpointDiskspec(vshControl *ctl, cleanup: if (ret < 0) vshError(ctl, _("unable to parse diskspec: %s"), str); - g_strfreev(array); return ret; } @@ -238,9 +233,9 @@ static bool cmdCheckpointCreateAs(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; - char *buffer = NULL; + g_autofree char *buffer = NULL; const char *name = NULL; const char *desc = NULL; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; @@ -286,9 +281,6 @@ cmdCheckpointCreateAs(vshControl *ctl, ret = virshCheckpointCreate(ctl, dom, buffer, flags, NULL); cleanup: - VIR_FREE(buffer); - virshDomainFree(dom); - return ret; } @@ -353,9 +345,9 @@ static bool cmdCheckpointEdit(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; - virDomainCheckpointPtr checkpoint = NULL; - virDomainCheckpointPtr edited = NULL; + g_autoptr(virshDomain) dom = NULL; + g_autoptr(virshDomainCheckpoint) checkpoint = NULL; + g_autoptr(virshDomainCheckpoint) edited = NULL; const char *name = NULL; const char *edited_name; bool ret = false; @@ -404,9 +396,6 @@ cmdCheckpointEdit(vshControl *ctl, cleanup: if (!ret && name) vshError(ctl, _("Failed to update %s"), name); - virshDomainCheckpointFree(edited); - virshDomainCheckpointFree(checkpoint); - virshDomainFree(dom); return ret; } @@ -420,7 +409,7 @@ virshGetCheckpointParent(vshControl *ctl, virDomainCheckpointPtr checkpoint, char **parent_name) { - virDomainCheckpointPtr parent = NULL; + g_autoptr(virshDomainCheckpoint) parent = NULL; int ret = -1; *parent_name = NULL; @@ -441,7 +430,6 @@ virshGetCheckpointParent(vshControl *ctl, } else { vshResetLibvirtError(); } - virshDomainCheckpointFree(parent); return ret; } @@ -474,10 +462,10 @@ static bool cmdCheckpointInfo(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom; - virDomainCheckpointPtr checkpoint = NULL; + g_autoptr(virshDomain) dom = NULL; + g_autoptr(virshDomainCheckpoint) checkpoint = NULL; const char *name; - char *parent = NULL; + g_autofree char *parent = NULL; bool ret = false; int count; unsigned int flags; @@ -520,9 +508,6 @@ cmdCheckpointInfo(vshControl *ctl, ret = true; cleanup: - VIR_FREE(parent); - virshDomainCheckpointFree(checkpoint); - virshDomainFree(dom); return ret; } @@ -708,7 +693,7 @@ static bool cmdCheckpointList(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; unsigned int flags = 0; size_t i; @@ -721,9 +706,9 @@ cmdCheckpointList(vshControl *ctl, bool parent = vshCommandOptBool(cmd, "parent"); bool roots = vshCommandOptBool(cmd, "roots"); const char *from_chk = NULL; - virDomainCheckpointPtr start = NULL; + g_autoptr(virshDomainCheckpoint) start = NULL; struct virshCheckpointList *checkpointlist = NULL; - vshTable *table = NULL; + g_autoptr(vshTable) table = NULL; VSH_EXCLUSIVE_OPTIONS_VAR(tree, name); VSH_EXCLUSIVE_OPTIONS_VAR(parent, roots); @@ -847,10 +832,6 @@ cmdCheckpointList(vshControl *ctl, cleanup: virshCheckpointListFree(checkpointlist); - virshDomainCheckpointFree(start); - virshDomainFree(dom); - vshTableFree(table); - return ret; } @@ -894,11 +875,11 @@ static bool cmdCheckpointDumpXML(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *name = NULL; - virDomainCheckpointPtr checkpoint = NULL; - char *xml = NULL; + g_autoptr(virshDomainCheckpoint) checkpoint = NULL; + g_autofree char *xml = NULL; unsigned int flags = 0; if (vshCommandOptBool(cmd, "security-info")) @@ -922,10 +903,6 @@ cmdCheckpointDumpXML(vshControl *ctl, ret = true; cleanup: - VIR_FREE(xml); - virshDomainCheckpointFree(checkpoint); - virshDomainFree(dom); - return ret; } @@ -957,11 +934,11 @@ static bool cmdCheckpointParent(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *name = NULL; - virDomainCheckpointPtr checkpoint = NULL; - char *parent = NULL; + g_autoptr(virshDomainCheckpoint) checkpoint = NULL; + g_autofree char *parent = NULL; dom = virshCommandOptDomain(ctl, cmd, NULL); if (dom == NULL) @@ -983,10 +960,6 @@ cmdCheckpointParent(vshControl *ctl, ret = true; cleanup: - VIR_FREE(parent); - virshDomainCheckpointFree(checkpoint); - virshDomainFree(dom); - return ret; } @@ -1031,10 +1004,10 @@ static bool cmdCheckpointDelete(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *name = NULL; - virDomainCheckpointPtr checkpoint = NULL; + g_autoptr(virshDomainCheckpoint) checkpoint = NULL; unsigned int flags = 0; dom = virshCommandOptDomain(ctl, cmd, NULL); @@ -1065,9 +1038,6 @@ cmdCheckpointDelete(vshControl *ctl, ret = true; cleanup: - virshDomainCheckpointFree(checkpoint); - virshDomainFree(dom); - return ret; } -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-checkpoint.c | 87 ++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 58 deletions(-) diff --git a/tools/virsh-checkpoint.c b/tools/virsh-checkpoint.c index 5c1d25ecf8..1f3a318014 100644 --- a/tools/virsh-checkpoint.c +++ b/tools/virsh-checkpoint.c @@ -46,19 +46,18 @@ virshCheckpointCreate(vshControl *ctl, unsigned int flags, const char *from) { - bool ret = false; g_autoptr(virshDomainCheckpoint) checkpoint = NULL; const char *name = NULL; checkpoint = virDomainCheckpointCreateXML(dom, buffer, flags); if (checkpoint == NULL) - goto cleanup; + return false; name = virDomainCheckpointGetName(checkpoint); if (!name) { vshError(ctl, "%s", _("Could not get checkpoint name")); - goto cleanup; + return false; } if (from) @@ -67,10 +66,7 @@ virshCheckpointCreate(vshControl *ctl, else vshPrintExtra(ctl, _("Domain checkpoint %s created"), name); - ret = true; - - cleanup: - return ret; + return true; } @@ -114,7 +110,6 @@ cmdCheckpointCreate(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *from = NULL; g_autofree char *buffer = NULL; unsigned int flags = 0; @@ -129,23 +124,20 @@ cmdCheckpointCreate(vshControl *ctl, flags |= VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE; if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) - goto cleanup; + return false; if (vshCommandOptStringReq(ctl, cmd, "xmlfile", &from) < 0) - goto cleanup; + return false; if (!from) { buffer = g_strdup("<domaincheckpoint/>"); } else { if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) { vshSaveLibvirtError(); - goto cleanup; + return false; } } - ret = virshCheckpointCreate(ctl, dom, buffer, flags, from); - - cleanup: - return ret; + return virshCheckpointCreate(ctl, dom, buffer, flags, from); } @@ -234,7 +226,6 @@ cmdCheckpointCreateAs(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; g_autofree char *buffer = NULL; const char *name = NULL; const char *desc = NULL; @@ -250,7 +241,7 @@ cmdCheckpointCreateAs(vshControl *ctl, if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0 || vshCommandOptStringReq(ctl, cmd, "description", &desc) < 0) - goto cleanup; + return false; virBufferAddLit(&buf, "<domaincheckpoint>\n"); virBufferAdjustIndent(&buf, 2); @@ -262,7 +253,7 @@ cmdCheckpointCreateAs(vshControl *ctl, virBufferAdjustIndent(&buf, 2); while ((opt = vshCommandOptArgv(ctl, cmd, opt))) { if (virshParseCheckpointDiskspec(ctl, &buf, opt->data) < 0) - goto cleanup; + return false; } virBufferAdjustIndent(&buf, -2); virBufferAddLit(&buf, "</disks>\n"); @@ -274,14 +265,10 @@ cmdCheckpointCreateAs(vshControl *ctl, if (vshCommandOptBool(cmd, "print-xml")) { vshPrint(ctl, "%s\n", buffer); - ret = true; - goto cleanup; + return true; } - ret = virshCheckpointCreate(ctl, dom, buffer, flags, NULL); - - cleanup: - return ret; + return virshCheckpointCreate(ctl, dom, buffer, flags, NULL); } @@ -466,7 +453,6 @@ cmdCheckpointInfo(vshControl *ctl, g_autoptr(virshDomainCheckpoint) checkpoint = NULL; const char *name; g_autofree char *parent = NULL; - bool ret = false; int count; unsigned int flags; @@ -476,7 +462,7 @@ cmdCheckpointInfo(vshControl *ctl, if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, &checkpoint, &name) < 0) - goto cleanup; + return false; vshPrint(ctl, "%-15s %s\n", _("Name:"), name); vshPrint(ctl, "%-15s %s\n", _("Domain:"), virDomainGetName(dom)); @@ -484,7 +470,7 @@ cmdCheckpointInfo(vshControl *ctl, if (virshGetCheckpointParent(ctl, checkpoint, &parent) < 0) { vshError(ctl, "%s", _("unexpected problem querying checkpoint state")); - goto cleanup; + return false; } vshPrint(ctl, "%-15s %s\n", _("Parent:"), parent ? parent : "-"); @@ -494,21 +480,18 @@ cmdCheckpointInfo(vshControl *ctl, if (count < 0) { if (last_error->code == VIR_ERR_NO_SUPPORT) { vshResetLibvirtError(); - ret = true; + return true; } - goto cleanup; + return false; } vshPrint(ctl, "%-15s %d\n", _("Children:"), count); flags = VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS; count = virDomainCheckpointListAllChildren(checkpoint, NULL, flags); if (count < 0) - goto cleanup; + return false; vshPrint(ctl, "%-15s %d\n", _("Descendants:"), count); - ret = true; - - cleanup: - return ret; + return true; } @@ -876,7 +859,6 @@ cmdCheckpointDumpXML(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *name = NULL; g_autoptr(virshDomainCheckpoint) checkpoint = NULL; g_autofree char *xml = NULL; @@ -894,16 +876,13 @@ cmdCheckpointDumpXML(vshControl *ctl, if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, &checkpoint, &name) < 0) - goto cleanup; + return false; if (!(xml = virDomainCheckpointGetXMLDesc(checkpoint, flags))) - goto cleanup; + return false; vshPrint(ctl, "%s", xml); - ret = true; - - cleanup: - return ret; + return true; } @@ -935,32 +914,28 @@ cmdCheckpointParent(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *name = NULL; g_autoptr(virshDomainCheckpoint) checkpoint = NULL; g_autofree char *parent = NULL; dom = virshCommandOptDomain(ctl, cmd, NULL); if (dom == NULL) - goto cleanup; + return false; if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, &checkpoint, &name) < 0) - goto cleanup; + return false; if (virshGetCheckpointParent(ctl, checkpoint, &parent) < 0) - goto cleanup; + return false; if (!parent) { vshError(ctl, _("checkpoint '%s' has no parent"), name); - goto cleanup; + return false; } vshPrint(ctl, "%s", parent); - ret = true; - - cleanup: - return ret; + return true; } @@ -1005,18 +980,17 @@ cmdCheckpointDelete(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *name = NULL; g_autoptr(virshDomainCheckpoint) checkpoint = NULL; unsigned int flags = 0; dom = virshCommandOptDomain(ctl, cmd, NULL); if (dom == NULL) - goto cleanup; + return false; if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, &checkpoint, &name) < 0) - goto cleanup; + return false; if (vshCommandOptBool(cmd, "children")) flags |= VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN; @@ -1032,13 +1006,10 @@ cmdCheckpointDelete(vshControl *ctl, vshPrintExtra(ctl, _("Domain checkpoint %s deleted\n"), name); } else { vshError(ctl, _("Failed to delete checkpoint %s"), name); - goto cleanup; + return false; } - ret = true; - - cleanup: - return ret; + return true; } -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-snapshot.c | 135 +++++++++++++---------------------------- 1 file changed, 43 insertions(+), 92 deletions(-) diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c index 350f9e803c..889959640d 100644 --- a/tools/virsh-snapshot.c +++ b/tools/virsh-snapshot.c @@ -42,7 +42,7 @@ virshSnapshotCreate(vshControl *ctl, virDomainPtr dom, const char *buffer, unsigned int flags, const char *from) { bool ret = false; - virDomainSnapshotPtr snapshot; + g_autoptr(virshDomainSnapshot) snapshot = NULL; bool halt = false; const char *name = NULL; @@ -99,7 +99,6 @@ virshSnapshotCreate(vshControl *ctl, virDomainPtr dom, const char *buffer, ret = true; cleanup: - virshDomainSnapshotFree(snapshot); return ret; } @@ -162,10 +161,10 @@ static const vshCmdOptDef opts_snapshot_create[] = { static bool cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *from = NULL; - char *buffer = NULL; + g_autofree char *buffer = NULL; unsigned int flags = 0; if (vshCommandOptBool(cmd, "redefine")) @@ -206,9 +205,6 @@ cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd) ret = virshSnapshotCreate(ctl, dom, buffer, flags, from); cleanup: - VIR_FREE(buffer); - virshDomainFree(dom); - return ret; } @@ -221,7 +217,7 @@ virshParseSnapshotMemspec(vshControl *ctl, virBuffer *buf, const char *str) int ret = -1; const char *snapshot = NULL; const char *file = NULL; - char **array = NULL; + g_auto(GStrv) array = NULL; int narray; size_t i; @@ -251,7 +247,6 @@ virshParseSnapshotMemspec(vshControl *ctl, virBuffer *buf, const char *str) cleanup: if (ret < 0) vshError(ctl, _("unable to parse memspec: %s"), str); - g_strfreev(array); return ret; } @@ -264,7 +259,7 @@ virshParseSnapshotDiskspec(vshControl *ctl, virBuffer *buf, const char *str) const char *driver = NULL; const char *stype = NULL; const char *file = NULL; - char **array = NULL; + g_auto(GStrv) array = NULL; int narray; size_t i; bool isFile = true; @@ -319,7 +314,6 @@ virshParseSnapshotDiskspec(vshControl *ctl, virBuffer *buf, const char *str) cleanup: if (ret < 0) vshError(ctl, _("unable to parse diskspec: %s"), str); - g_strfreev(array); return ret; } @@ -391,9 +385,9 @@ static const vshCmdOptDef opts_snapshot_create_as[] = { static bool cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; - char *buffer = NULL; + g_autofree char *buffer = NULL; const char *name = NULL; const char *desc = NULL; const char *memspec = NULL; @@ -460,9 +454,6 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd) ret = virshSnapshotCreate(ctl, dom, buffer, flags, NULL); cleanup: - VIR_FREE(buffer); - virshDomainFree(dom); - return ret; } @@ -538,9 +529,9 @@ static const vshCmdOptDef opts_snapshot_edit[] = { static bool cmdSnapshotEdit(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; - virDomainSnapshotPtr snapshot = NULL; - virDomainSnapshotPtr edited = NULL; + g_autoptr(virshDomain) dom = NULL; + g_autoptr(virshDomainSnapshot) snapshot = NULL; + g_autoptr(virshDomainSnapshot) edited = NULL; const char *name = NULL; const char *edited_name; bool ret = false; @@ -610,9 +601,6 @@ cmdSnapshotEdit(vshControl *ctl, const vshCmd *cmd) cleanup: if (!ret && name) vshError(ctl, _("Failed to update %s"), name); - virshDomainSnapshotFree(edited); - virshDomainSnapshotFree(snapshot); - virshDomainFree(dom); return ret; } @@ -650,11 +638,11 @@ static const vshCmdOptDef opts_snapshot_current[] = { static bool cmdSnapshotCurrent(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; int current; - virDomainSnapshotPtr snapshot = NULL; - char *xml = NULL; + g_autoptr(virshDomainSnapshot) snapshot = NULL; + g_autofree char *xml = NULL; const char *snapshotname = NULL; unsigned int flags = 0; const char *domname; @@ -671,7 +659,7 @@ cmdSnapshotCurrent(vshControl *ctl, const vshCmd *cmd) goto cleanup; if (snapshotname) { - virDomainSnapshotPtr snapshot2 = NULL; + g_autoptr(virshDomainSnapshot) snapshot2 = NULL; flags = (VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE | VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT); @@ -689,7 +677,6 @@ cmdSnapshotCurrent(vshControl *ctl, const vshCmd *cmd) if (!(snapshot2 = virDomainSnapshotCreateXML(dom, xml, flags))) goto cleanup; - virshDomainSnapshotFree(snapshot2); vshPrintExtra(ctl, _("Snapshot %s set as current"), snapshotname); ret = true; goto cleanup; @@ -724,10 +711,6 @@ cmdSnapshotCurrent(vshControl *ctl, const vshCmd *cmd) cleanup: if (!ret) vshReportError(ctl); - VIR_FREE(xml); - virshDomainSnapshotFree(snapshot); - virshDomainFree(dom); - return ret; } @@ -739,10 +722,10 @@ static int virshGetSnapshotParent(vshControl *ctl, virDomainSnapshotPtr snapshot, char **parent_name) { - virDomainSnapshotPtr parent = NULL; - char *xml = NULL; - xmlDocPtr xmldoc = NULL; - xmlXPathContextPtr ctxt = NULL; + g_autoptr(virshDomainSnapshot) parent = NULL; + g_autofree char *xml = NULL; + g_autoptr(xmlDoc) xmldoc = NULL; + g_autoptr(xmlXPathContext) ctxt = NULL; int ret = -1; virshControl *priv = ctl->privData; @@ -784,10 +767,6 @@ virshGetSnapshotParent(vshControl *ctl, virDomainSnapshotPtr snapshot, } else { vshResetLibvirtError(); } - virshDomainSnapshotFree(parent); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xmldoc); - VIR_FREE(xml); return ret; } @@ -799,11 +778,11 @@ static int virshSnapshotFilter(vshControl *ctl, virDomainSnapshotPtr snapshot, unsigned int flags) { - char *xml = NULL; - xmlDocPtr xmldoc = NULL; - xmlXPathContextPtr ctxt = NULL; + g_autofree char *xml = NULL; + g_autoptr(xmlDoc) xmldoc = NULL; + g_autoptr(xmlXPathContext) ctxt = NULL; int ret = -1; - char *state = NULL; + g_autofree char *state = NULL; if (!snapshot) return 1; @@ -841,10 +820,6 @@ virshSnapshotFilter(vshControl *ctl, virDomainSnapshotPtr snapshot, } cleanup: - VIR_FREE(state); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xmldoc); - VIR_FREE(xml); return ret; } @@ -875,15 +850,15 @@ static const vshCmdOptDef opts_snapshot_info[] = { static bool cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom; - virDomainSnapshotPtr snapshot = NULL; + g_autoptr(virshDomain) dom = NULL; + g_autoptr(virshDomainSnapshot) snapshot = NULL; const char *name; - char *doc = NULL; - xmlDocPtr xmldoc = NULL; - xmlXPathContextPtr ctxt = NULL; - char *state = NULL; + g_autofree char *doc = NULL; + g_autoptr(xmlDoc) xmldoc = NULL; + g_autoptr(xmlXPathContext) ctxt = NULL; + g_autofree char *state = NULL; int external; - char *parent = NULL; + g_autofree char *parent = NULL; bool ret = false; int count; unsigned int flags; @@ -906,14 +881,13 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) * attempt a fallback. */ current = virDomainSnapshotIsCurrent(snapshot, 0); if (current < 0) { - virDomainSnapshotPtr other = virDomainSnapshotCurrent(dom, 0); + g_autoptr(virshDomainSnapshot) other = NULL; vshResetLibvirtError(); current = 0; if (other) { if (STREQ(name, virDomainSnapshotGetName(other))) current = 1; - virshDomainSnapshotFree(other); } } vshPrint(ctl, "%-15s %s\n", _("Current:"), @@ -1007,13 +981,6 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - VIR_FREE(state); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xmldoc); - VIR_FREE(doc); - VIR_FREE(parent); - virshDomainSnapshotFree(snapshot); - virshDomainFree(dom); return ret; } @@ -1486,7 +1453,7 @@ static const vshCmdOptDef opts_snapshot_list[] = { static bool cmdSnapshotList(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; unsigned int flags = 0; size_t i; @@ -1500,9 +1467,9 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd) bool roots = vshCommandOptBool(cmd, "roots"); bool current = vshCommandOptBool(cmd, "current"); const char *from_snap = NULL; - virDomainSnapshotPtr start = NULL; + g_autoptr(virshDomainSnapshot) start = NULL; struct virshSnapshotList *snaplist = NULL; - vshTable *table = NULL; + g_autoptr(vshTable) table = NULL; VSH_EXCLUSIVE_OPTIONS_VAR(tree, name); VSH_EXCLUSIVE_OPTIONS_VAR(parent, roots); @@ -1642,10 +1609,6 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd) cleanup: virshSnapshotListFree(snaplist); - virshDomainSnapshotFree(start); - virshDomainFree(dom); - vshTableFree(table); - return ret; } @@ -1680,11 +1643,11 @@ static const vshCmdOptDef opts_snapshot_dumpxml[] = { static bool cmdSnapshotDumpXML(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *name = NULL; - virDomainSnapshotPtr snapshot = NULL; - char *xml = NULL; + g_autoptr(virshDomainSnapshot) snapshot = NULL; + g_autofree char *xml = NULL; unsigned int flags = 0; if (vshCommandOptBool(cmd, "security-info")) @@ -1706,10 +1669,6 @@ cmdSnapshotDumpXML(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - VIR_FREE(xml); - virshDomainSnapshotFree(snapshot); - virshDomainFree(dom); - return ret; } @@ -1740,11 +1699,11 @@ static const vshCmdOptDef opts_snapshot_parent[] = { static bool cmdSnapshotParent(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *name = NULL; - virDomainSnapshotPtr snapshot = NULL; - char *parent = NULL; + g_autoptr(virshDomainSnapshot) snapshot = NULL; + g_autofree char *parent = NULL; dom = virshCommandOptDomain(ctl, cmd, NULL); if (dom == NULL) @@ -1766,10 +1725,6 @@ cmdSnapshotParent(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - VIR_FREE(parent); - virshDomainSnapshotFree(snapshot); - virshDomainFree(dom); - return ret; } @@ -1812,10 +1767,10 @@ static const vshCmdOptDef opts_snapshot_revert[] = { static bool cmdDomainSnapshotRevert(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *name = NULL; - virDomainSnapshotPtr snapshot = NULL; + g_autoptr(virshDomainSnapshot) snapshot = NULL; unsigned int flags = 0; bool force = false; int result; @@ -1852,8 +1807,6 @@ cmdDomainSnapshotRevert(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - virshDomainSnapshotFree(snapshot); - virshDomainFree(dom); return ret; } @@ -1897,10 +1850,10 @@ static const vshCmdOptDef opts_snapshot_delete[] = { static bool cmdSnapshotDelete(vshControl *ctl, const vshCmd *cmd) { - virDomainPtr dom = NULL; + g_autoptr(virshDomain) dom = NULL; bool ret = false; const char *name = NULL; - virDomainSnapshotPtr snapshot = NULL; + g_autoptr(virshDomainSnapshot) snapshot = NULL; unsigned int flags = 0; dom = virshCommandOptDomain(ctl, cmd, NULL); @@ -1934,8 +1887,6 @@ cmdSnapshotDelete(vshControl *ctl, const vshCmd *cmd) ret = true; cleanup: - virshDomainSnapshotFree(snapshot); - virshDomainFree(dom); return ret; } -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-snapshot.c | 130 +++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 84 deletions(-) diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c index 889959640d..ee99d0e061 100644 --- a/tools/virsh-snapshot.c +++ b/tools/virsh-snapshot.c @@ -41,7 +41,6 @@ static bool virshSnapshotCreate(vshControl *ctl, virDomainPtr dom, const char *buffer, unsigned int flags, const char *from) { - bool ret = false; g_autoptr(virshDomainSnapshot) snapshot = NULL; bool halt = false; const char *name = NULL; @@ -64,12 +63,12 @@ virshSnapshotCreate(vshControl *ctl, virDomainPtr dom, const char *buffer, persistent = virDomainIsPersistent(dom); if (persistent < 0) { vshReportError(ctl); - goto cleanup; + return false; } if (!persistent) { vshError(ctl, "%s", _("cannot halt after snapshot of transient domain")); - goto cleanup; + return false; } if (virDomainIsActive(dom) == 1) halt = true; @@ -78,17 +77,17 @@ virshSnapshotCreate(vshControl *ctl, virDomainPtr dom, const char *buffer, } if (snapshot == NULL) - goto cleanup; + return false; if (halt && virDomainDestroy(dom) < 0) { vshReportError(ctl); - goto cleanup; + return false; } name = virDomainSnapshotGetName(snapshot); if (!name) { vshError(ctl, "%s", _("Could not get snapshot name")); - goto cleanup; + return false; } if (from) @@ -96,10 +95,7 @@ virshSnapshotCreate(vshControl *ctl, virDomainPtr dom, const char *buffer, else vshPrintExtra(ctl, _("Domain snapshot %s created"), name); - ret = true; - - cleanup: - return ret; + return true; } /* @@ -162,7 +158,6 @@ static bool cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *from = NULL; g_autofree char *buffer = NULL; unsigned int flags = 0; @@ -189,23 +184,20 @@ cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd) flags |= VIR_DOMAIN_SNAPSHOT_CREATE_VALIDATE; if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) - goto cleanup; + return false; if (vshCommandOptStringReq(ctl, cmd, "xmlfile", &from) < 0) - goto cleanup; + return false; if (!from) { buffer = g_strdup("<domainsnapshot/>"); } else { if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) { vshSaveLibvirtError(); - goto cleanup; + return false; } } - ret = virshSnapshotCreate(ctl, dom, buffer, flags, from); - - cleanup: - return ret; + return virshSnapshotCreate(ctl, dom, buffer, flags, from); } /* @@ -386,7 +378,6 @@ static bool cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; g_autofree char *buffer = NULL; const char *name = NULL; const char *desc = NULL; @@ -417,7 +408,7 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd) if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0 || vshCommandOptStringReq(ctl, cmd, "description", &desc) < 0) - goto cleanup; + return false; virBufferAddLit(&buf, "<domainsnapshot>\n"); virBufferAdjustIndent(&buf, 2); @@ -425,17 +416,17 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd) virBufferEscapeString(&buf, "<description>%s</description>\n", desc); if (vshCommandOptStringReq(ctl, cmd, "memspec", &memspec) < 0) - goto cleanup; + return false; if (memspec && virshParseSnapshotMemspec(ctl, &buf, memspec) < 0) - goto cleanup; + return false; if (vshCommandOptBool(cmd, "diskspec")) { virBufferAddLit(&buf, "<disks>\n"); virBufferAdjustIndent(&buf, 2); while ((opt = vshCommandOptArgv(ctl, cmd, opt))) { if (virshParseSnapshotDiskspec(ctl, &buf, opt->data) < 0) - goto cleanup; + return false; } virBufferAdjustIndent(&buf, -2); virBufferAddLit(&buf, "</disks>\n"); @@ -447,14 +438,10 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd) if (vshCommandOptBool(cmd, "print-xml")) { vshPrint(ctl, "%s\n", buffer); - ret = true; - goto cleanup; + return true; } - ret = virshSnapshotCreate(ctl, dom, buffer, flags, NULL); - - cleanup: - return ret; + return virshSnapshotCreate(ctl, dom, buffer, flags, NULL); } /* Helper for resolving {--current | --ARG name} into a snapshot @@ -789,11 +776,11 @@ virshSnapshotFilter(vshControl *ctl, virDomainSnapshotPtr snapshot, xml = virDomainSnapshotGetXMLDesc(snapshot, 0); if (!xml) - goto cleanup; + return -1; xmldoc = virXMLParseStringCtxt(xml, _("(domain_snapshot)"), &ctxt); if (!xmldoc) - goto cleanup; + return -1; /* Libvirt 1.0.1 and newer never call this function, because the * filtering is already supported by the listing functions. Older @@ -803,7 +790,7 @@ virshSnapshotFilter(vshControl *ctl, virDomainSnapshotPtr snapshot, state = virXPathString("string(/domainsnapshot/state)", ctxt); if (!state) { vshError(ctl, "%s", _("unable to perform snapshot filtering")); - goto cleanup; + return -1; } if (STREQ(state, "disk-snapshot")) { ret = ((flags & (VIR_DOMAIN_SNAPSHOT_LIST_DISK_ONLY | @@ -819,7 +806,6 @@ virshSnapshotFilter(vshControl *ctl, virDomainSnapshotPtr snapshot, ret = !!(flags & VIR_DOMAIN_SNAPSHOT_LIST_ACTIVE); } - cleanup: return ret; } @@ -859,7 +845,6 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) g_autofree char *state = NULL; int external; g_autofree char *parent = NULL; - bool ret = false; int count; unsigned int flags; int current; @@ -872,7 +857,7 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) if (virshLookupSnapshot(ctl, cmd, "snapshotname", true, dom, &snapshot, &name) < 0) - goto cleanup; + return false; vshPrint(ctl, "%-15s %s\n", _("Name:"), name); vshPrint(ctl, "%-15s %s\n", _("Domain:"), virDomainGetName(dom)); @@ -897,17 +882,17 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) * state of the machine at the time of the snapshot. */ doc = virDomainSnapshotGetXMLDesc(snapshot, 0); if (!doc) - goto cleanup; + return false; xmldoc = virXMLParseStringCtxt(doc, _("(domain_snapshot)"), &ctxt); if (!xmldoc) - goto cleanup; + return false; state = virXPathString("string(/domainsnapshot/state)", ctxt); if (!state) { vshError(ctl, "%s", _("unexpected problem reading snapshot xml")); - goto cleanup; + return false; } vshPrint(ctl, "%-15s %s\n", _("State:"), state); @@ -933,7 +918,7 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) if (external < 0) { vshError(ctl, "%s", _("unexpected problem reading snapshot xml")); - goto cleanup; + return false; } vshPrint(ctl, "%-15s %s\n", _("Location:"), external ? _("external") : _("internal")); @@ -946,24 +931,22 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) /* Children, Descendants. After this point, the fallback to * compute children is too expensive, so we gracefully quit if the * APIs don't exist. */ - if (priv->useSnapshotOld) { - ret = true; - goto cleanup; - } + if (priv->useSnapshotOld) + return true; flags = 0; count = virDomainSnapshotNumChildren(snapshot, flags); if (count < 0) { if (last_error->code == VIR_ERR_NO_SUPPORT) { vshResetLibvirtError(); - ret = true; + return true; } - goto cleanup; + return false; } vshPrint(ctl, "%-15s %d\n", _("Children:"), count); flags = VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS; count = virDomainSnapshotNumChildren(snapshot, flags); if (count < 0) - goto cleanup; + return false; vshPrint(ctl, "%-15s %d\n", _("Descendants:"), count); /* Metadata; the fallback here relies on the fact that metadata @@ -978,10 +961,7 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd) vshPrint(ctl, "%-15s %s\n", _("Metadata:"), metadata ? _("yes") : _("no")); - ret = true; - - cleanup: - return ret; + return true; } /* Helpers for collecting a list of snapshots. */ @@ -1644,7 +1624,6 @@ static bool cmdSnapshotDumpXML(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *name = NULL; g_autoptr(virshDomainSnapshot) snapshot = NULL; g_autofree char *xml = NULL; @@ -1660,16 +1639,13 @@ cmdSnapshotDumpXML(vshControl *ctl, const vshCmd *cmd) return false; if (!(snapshot = virDomainSnapshotLookupByName(dom, name, 0))) - goto cleanup; + return false; if (!(xml = virDomainSnapshotGetXMLDesc(snapshot, flags))) - goto cleanup; + return false; vshPrint(ctl, "%s", xml); - ret = true; - - cleanup: - return ret; + return true; } /* @@ -1700,32 +1676,28 @@ static bool cmdSnapshotParent(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *name = NULL; g_autoptr(virshDomainSnapshot) snapshot = NULL; g_autofree char *parent = NULL; dom = virshCommandOptDomain(ctl, cmd, NULL); if (dom == NULL) - goto cleanup; + return false; if (virshLookupSnapshot(ctl, cmd, "snapshotname", true, dom, &snapshot, &name) < 0) - goto cleanup; + return false; if (virshGetSnapshotParent(ctl, snapshot, &parent) < 0) - goto cleanup; + return false; if (!parent) { vshError(ctl, _("snapshot '%s' has no parent"), name); - goto cleanup; + return false; } vshPrint(ctl, "%s", parent); - ret = true; - - cleanup: - return ret; + return true; } /* @@ -1768,7 +1740,6 @@ static bool cmdDomainSnapshotRevert(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *name = NULL; g_autoptr(virshDomainSnapshot) snapshot = NULL; unsigned int flags = 0; @@ -1788,11 +1759,11 @@ cmdDomainSnapshotRevert(vshControl *ctl, const vshCmd *cmd) dom = virshCommandOptDomain(ctl, cmd, NULL); if (dom == NULL) - goto cleanup; + return false; if (virshLookupSnapshot(ctl, cmd, "snapshotname", true, dom, &snapshot, &name) < 0) - goto cleanup; + return false; result = virDomainRevertToSnapshot(snapshot, flags); if (result < 0 && force && @@ -1802,13 +1773,9 @@ cmdDomainSnapshotRevert(vshControl *ctl, const vshCmd *cmd) result = virDomainRevertToSnapshot(snapshot, flags); } if (result < 0) - goto cleanup; + return false; - ret = true; - - cleanup: - - return ret; + return true; } /* @@ -1851,18 +1818,17 @@ static bool cmdSnapshotDelete(vshControl *ctl, const vshCmd *cmd) { g_autoptr(virshDomain) dom = NULL; - bool ret = false; const char *name = NULL; g_autoptr(virshDomainSnapshot) snapshot = NULL; unsigned int flags = 0; dom = virshCommandOptDomain(ctl, cmd, NULL); if (dom == NULL) - goto cleanup; + return false; if (virshLookupSnapshot(ctl, cmd, "snapshotname", true, dom, &snapshot, &name) < 0) - goto cleanup; + return false; if (vshCommandOptBool(cmd, "children")) flags |= VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN; @@ -1881,14 +1847,10 @@ cmdSnapshotDelete(vshControl *ctl, const vshCmd *cmd) vshPrintExtra(ctl, _("Domain snapshot %s deleted\n"), name); } else { vshError(ctl, _("Failed to delete snapshot %s"), name); - goto cleanup; + return false; } - ret = true; - - cleanup: - - return ret; + return true; } const vshCmdDef snapshotCmds[] = { -- 2.31.1

On Tue, Aug 10, 2021 at 19:44:59 +0200, Ján Tomko wrote:
Ján Tomko (10): vsh: table: define cleanup function virsh: add cleanup function for virshDomainCheckpoint virsh: add cleanup function for virshDomainSnapshot virsh: checkpoint: move variables inside the loop virsh: snapshot: move variables inside the loop virsh: cmdCheckpointInfo: delete unused variables virsh: checkpoint: use g_auto where possible virsh: checkpoint: remove pointless cleanup labels virsh: snapshot: use g_auto where possible virsh: snapshot: remove pointless cleanup labels
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
participants (2)
-
Ján Tomko
-
Peter Krempa