virsh-snapshot: Refactor cmdSnapshotCurrent

Use the exclusive parameter checker and touch up some parts to simplify
code.
This commit is contained in:
Peter Krempa 2013-03-07 11:07:03 +01:00
parent 020e210387
commit 4baf00fc30

View File

@ -686,9 +686,10 @@ cmdSnapshotCurrent(vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptBool(cmd, "security-info")) if (vshCommandOptBool(cmd, "security-info"))
flags |= VIR_DOMAIN_XML_SECURE; flags |= VIR_DOMAIN_XML_SECURE;
dom = vshCommandOptDomain(ctl, cmd, &domname); VSH_EXCLUSIVE_OPTIONS("name", "snapshotname");
if (dom == NULL)
goto cleanup; if (!(dom = vshCommandOptDomain(ctl, cmd, &domname)))
return false;
if (vshCommandOptStringReq(ctl, cmd, "snapshotname", &snapshotname) < 0) if (vshCommandOptStringReq(ctl, cmd, "snapshotname", &snapshotname) < 0)
goto cleanup; goto cleanup;
@ -698,52 +699,48 @@ cmdSnapshotCurrent(vshControl *ctl, const vshCmd *cmd)
flags = (VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE | flags = (VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT); VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT);
if (vshCommandOptBool(cmd, "name")) { if (!(snapshot = virDomainSnapshotLookupByName(dom, snapshotname, 0)))
vshError(ctl, "%s",
_("--name and snapshotname are mutually exclusive"));
goto cleanup;
}
snapshot = virDomainSnapshotLookupByName(dom, snapshotname, 0);
if (snapshot == NULL)
goto cleanup; goto cleanup;
xml = virDomainSnapshotGetXMLDesc(snapshot, VIR_DOMAIN_XML_SECURE); xml = virDomainSnapshotGetXMLDesc(snapshot, VIR_DOMAIN_XML_SECURE);
if (!xml) if (!xml)
goto cleanup; goto cleanup;
/* strstr is safe here, since xml came from libvirt API and not user */ /* strstr is safe here, since xml came from libvirt API and not user */
if (strstr(xml, "<state>disk-snapshot</state>")) if (strstr(xml, "<state>disk-snapshot</state>"))
flags |= VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY; flags |= VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY;
snapshot2 = virDomainSnapshotCreateXML(dom, xml, flags);
if (snapshot2 == NULL) if (!(snapshot2 = virDomainSnapshotCreateXML(dom, xml, flags)))
goto cleanup; goto cleanup;
virDomainSnapshotFree(snapshot2); virDomainSnapshotFree(snapshot2);
vshPrint(ctl, _("Snapshot %s set as current"), snapshotname); vshPrint(ctl, _("Snapshot %s set as current"), snapshotname);
ret = true; ret = true;
goto cleanup; goto cleanup;
} }
current = virDomainHasCurrentSnapshot(dom, 0); if ((current = virDomainHasCurrentSnapshot(dom, 0)) < 0)
if (current < 0) {
goto cleanup; goto cleanup;
} else if (!current) {
if (!current) {
vshError(ctl, _("domain '%s' has no current snapshot"), domname); vshError(ctl, _("domain '%s' has no current snapshot"), domname);
goto cleanup; goto cleanup;
} else { } else {
const char *name = NULL;
if (!(snapshot = virDomainSnapshotCurrent(dom, 0))) if (!(snapshot = virDomainSnapshotCurrent(dom, 0)))
goto cleanup; goto cleanup;
if (vshCommandOptBool(cmd, "name")) { if (vshCommandOptBool(cmd, "name")) {
name = virDomainSnapshotGetName(snapshot); const char *name;
if (!name) if (!(name = virDomainSnapshotGetName(snapshot)))
goto cleanup; goto cleanup;
} else {
xml = virDomainSnapshotGetXMLDesc(snapshot, flags);
if (!xml)
goto cleanup;
}
vshPrint(ctl, "%s", name ? name : xml); vshPrint(ctl, "%s", name);
} else {
if (!(xml = virDomainSnapshotGetXMLDesc(snapshot, flags)))
goto cleanup;
vshPrint(ctl, "%s", xml);
}
} }
ret = true; ret = true;
@ -754,7 +751,6 @@ cleanup:
VIR_FREE(xml); VIR_FREE(xml);
if (snapshot) if (snapshot)
virDomainSnapshotFree(snapshot); virDomainSnapshotFree(snapshot);
if (dom)
virDomainFree(dom); virDomainFree(dom);
return ret; return ret;