1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-23 21:34:54 +03:00

snapshot: virsh indentation cleanup

No semantic change; this will make it easier to refactor code.

* tools/virsh.c (cmdSnapshotList): Drop level of indentation, and
rename a variable.
This commit is contained in:
Eric Blake 2012-06-08 15:59:21 -06:00
parent c8564ad476
commit 903560245f

View File

@ -16599,10 +16599,9 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
unsigned int flags = 0; unsigned int flags = 0;
int parent_filter = 0; /* -1 for roots filtering, 0 for no parent int parent_filter = 0; /* -1 for roots filtering, 0 for no parent
information needed, 1 for parent column */ information needed, 1 for parent column */
int numsnaps;
char **names = NULL; char **names = NULL;
char **parents = NULL; char **parents = NULL;
int actual = 0; int count = 0;
int i; int i;
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
@ -16676,9 +16675,9 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
if (descendants || tree) { if (descendants || tree) {
flags |= VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS; flags |= VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS;
} }
numsnaps = ctl->useSnapshotOld ? -1 : count = ctl->useSnapshotOld ? -1 :
virDomainSnapshotNumChildren(start, flags); virDomainSnapshotNumChildren(start, flags);
if (numsnaps < 0) { if (count < 0) {
if (ctl->useSnapshotOld || if (ctl->useSnapshotOld ||
last_error->code == VIR_ERR_NO_SUPPORT) { last_error->code == VIR_ERR_NO_SUPPORT) {
/* We can emulate --from. */ /* We can emulate --from. */
@ -16687,27 +16686,27 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
last_error = NULL; last_error = NULL;
ctl->useSnapshotOld = true; ctl->useSnapshotOld = true;
flags &= ~VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS; flags &= ~VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS;
numsnaps = virDomainSnapshotNum(dom, flags); count = virDomainSnapshotNum(dom, flags);
} }
} else if (tree) { } else if (tree) {
numsnaps++; count++;
} }
} else { } else {
numsnaps = virDomainSnapshotNum(dom, flags); count = virDomainSnapshotNum(dom, flags);
/* Fall back to simulation if --roots was unsupported. */ /* Fall back to simulation if --roots was unsupported. */
/* XXX can we also emulate --leaves? */ /* XXX can we also emulate --leaves? */
if (numsnaps < 0 && last_error->code == VIR_ERR_INVALID_ARG && if (count < 0 && last_error->code == VIR_ERR_INVALID_ARG &&
(flags & VIR_DOMAIN_SNAPSHOT_LIST_ROOTS)) { (flags & VIR_DOMAIN_SNAPSHOT_LIST_ROOTS)) {
virFreeError(last_error); virFreeError(last_error);
last_error = NULL; last_error = NULL;
parent_filter = -1; parent_filter = -1;
flags &= ~VIR_DOMAIN_SNAPSHOT_LIST_ROOTS; flags &= ~VIR_DOMAIN_SNAPSHOT_LIST_ROOTS;
numsnaps = virDomainSnapshotNum(dom, flags); count = virDomainSnapshotNum(dom, flags);
} }
} }
if (numsnaps < 0) { if (count < 0) {
if (!last_error) if (!last_error)
vshError(ctl, _("missing support")); vshError(ctl, _("missing support"));
goto cleanup; goto cleanup;
@ -16725,41 +16724,40 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
"------------------------------------------------------------\n"); "------------------------------------------------------------\n");
} }
if (!numsnaps) { if (!count) {
ret = true; ret = true;
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(names, numsnaps) < 0) if (VIR_ALLOC_N(names, count) < 0)
goto cleanup; goto cleanup;
if (from && !ctl->useSnapshotOld) { if (from && !ctl->useSnapshotOld) {
/* When mixing --from and --tree, we want to start the tree at the /* When mixing --from and --tree, we want to start the tree at the
* given snapshot. Without --tree, only list the children. */ * given snapshot. Without --tree, only list the children. */
if (tree) { if (tree) {
if (numsnaps) if (count)
actual = virDomainSnapshotListChildrenNames(start, names + 1, count = virDomainSnapshotListChildrenNames(start, names + 1,
numsnaps - 1, count - 1, flags);
flags); if (count >= 0) {
if (actual >= 0) { count++;
actual++;
names[0] = vshStrdup(ctl, from); names[0] = vshStrdup(ctl, from);
} }
} else { } else {
actual = virDomainSnapshotListChildrenNames(start, names, count = virDomainSnapshotListChildrenNames(start, names,
numsnaps, flags); count, flags);
} }
} else { } else {
actual = virDomainSnapshotListNames(dom, names, numsnaps, flags); count = virDomainSnapshotListNames(dom, names, count, flags);
} }
if (actual < 0) if (count < 0)
goto cleanup; goto cleanup;
qsort(&names[0], actual, sizeof(char*), namesorter); qsort(&names[0], count, sizeof(char*), namesorter);
if (tree || (from && ctl->useSnapshotOld)) { if (tree || (from && ctl->useSnapshotOld)) {
parents = vshCalloc(ctl, sizeof(char *), actual); parents = vshCalloc(ctl, sizeof(char *), count);
for (i = (from && !ctl->useSnapshotOld); i < actual; i++) { for (i = (from && !ctl->useSnapshotOld); i < count; i++) {
if (from && ctl->useSnapshotOld && STREQ(names[i], from)) { if (from && ctl->useSnapshotOld && STREQ(names[i], from)) {
start_index = i; start_index = i;
continue; continue;
@ -16781,123 +16779,123 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
if (tree) { if (tree) {
struct vshTreeArray arrays = { names, parents }; struct vshTreeArray arrays = { names, parents };
for (i = 0 ; i < actual ; i++) { for (i = 0 ; i < count ; i++) {
if ((from && ctl->useSnapshotOld) ? STREQ(names[i], from) : if ((from && ctl->useSnapshotOld) ? STREQ(names[i], from) :
!parents[i] && !parents[i] &&
vshTreePrint(ctl, vshTreeArrayLookup, &arrays, actual, i) < 0) vshTreePrint(ctl, vshTreeArrayLookup, &arrays, count, i) < 0)
goto cleanup; goto cleanup;
} }
ret = true; ret = true;
goto cleanup; goto cleanup;
} else { }
if (ctl->useSnapshotOld && descendants) {
bool changed = false;
/* Make multiple passes over the list - first pass NULLs if (ctl->useSnapshotOld && descendants) {
* out all roots except start, remaining passes NULL out bool changed = false;
* any entry whose parent is not still in list. Also, we
* NULL out parent when name is known to be in list. /* Make multiple passes over the list - first pass NULLs out
* Sorry, this is O(n^3) - hope your hierarchy isn't huge. */ * all roots except start, remaining passes NULL out any entry
if (start_index < 0) { * whose parent is not still in list. Also, we NULL out
vshError(ctl, _("snapshot %s disappeared from list"), from); * parent when name is known to be in list. Sorry, this is
goto cleanup; * O(n^3) - hope your hierarchy isn't huge. */
if (start_index < 0) {
vshError(ctl, _("snapshot %s disappeared from list"), from);
goto cleanup;
}
for (i = 0; i < count; i++) {
if (i == start_index)
continue;
if (!parents[i]) {
VIR_FREE(names[i]);
} else if (STREQ(parents[i], from)) {
VIR_FREE(parents[i]);
changed = true;
} }
for (i = 0; i < actual; i++) { }
if (i == start_index) if (!changed) {
ret = true;
goto cleanup;
}
while (changed) {
changed = false;
for (i = 0; i < count; i++) {
bool found = false;
int j;
if (!names[i] || !parents[i])
continue; continue;
if (!parents[i]) { for (j = 0; j < count; j++) {
VIR_FREE(names[i]); if (!names[j] || i == j)
} else if (STREQ(parents[i], from)) {
VIR_FREE(parents[i]);
changed = true;
}
}
if (!changed) {
ret = true;
goto cleanup;
}
while (changed) {
changed = false;
for (i = 0; i < actual; i++) {
bool found = false;
int j;
if (!names[i] || !parents[i])
continue; continue;
for (j = 0; j < actual; j++) { if (STREQ(parents[i], names[j])) {
if (!names[j] || i == j) found = true;
continue; if (!parents[j])
if (STREQ(parents[i], names[j])) { VIR_FREE(parents[i]);
found = true; break;
if (!parents[j])
VIR_FREE(parents[i]);
break;
}
}
if (!found) {
changed = true;
VIR_FREE(names[i]);
} }
} }
if (!found) {
changed = true;
VIR_FREE(names[i]);
}
} }
VIR_FREE(names[start_index]); }
VIR_FREE(names[start_index]);
}
for (i = 0; i < count; i++) {
if (from && ctl->useSnapshotOld &&
(descendants ? !names[i] : STRNEQ_NULLABLE(parents[i], from)))
continue;
/* free up memory from previous iterations of the loop */
VIR_FREE(parent);
VIR_FREE(state);
if (snapshot)
virDomainSnapshotFree(snapshot);
xmlXPathFreeContext(ctxt);
xmlFreeDoc(xml);
VIR_FREE(doc);
snapshot = virDomainSnapshotLookupByName(dom, names[i], 0);
if (snapshot == NULL)
continue;
doc = virDomainSnapshotGetXMLDesc(snapshot, 0);
if (!doc)
continue;
xml = virXMLParseStringCtxt(doc, _("(domain_snapshot)"), &ctxt);
if (!xml)
continue;
if (parent_filter) {
parent = virXPathString("string(/domainsnapshot/parent/name)",
ctxt);
if (!parent && parent_filter < 0)
continue;
} }
for (i = 0; i < actual; i++) { state = virXPathString("string(/domainsnapshot/state)", ctxt);
if (from && ctl->useSnapshotOld && if (state == NULL)
(descendants ? !names[i] : STRNEQ_NULLABLE(parents[i], from))) continue;
continue; if (virXPathLongLong("string(/domainsnapshot/creationTime)", ctxt,
&creation_longlong) < 0)
/* free up memory from previous iterations of the loop */ continue;
VIR_FREE(parent); creation_time_t = creation_longlong;
VIR_FREE(state); if (creation_time_t != creation_longlong) {
if (snapshot) vshError(ctl, "%s", _("time_t overflow"));
virDomainSnapshotFree(snapshot); continue;
xmlXPathFreeContext(ctxt);
xmlFreeDoc(xml);
VIR_FREE(doc);
snapshot = virDomainSnapshotLookupByName(dom, names[i], 0);
if (snapshot == NULL)
continue;
doc = virDomainSnapshotGetXMLDesc(snapshot, 0);
if (!doc)
continue;
xml = virXMLParseStringCtxt(doc, _("(domain_snapshot)"), &ctxt);
if (!xml)
continue;
if (parent_filter) {
parent = virXPathString("string(/domainsnapshot/parent/name)",
ctxt);
if (!parent && parent_filter < 0)
continue;
}
state = virXPathString("string(/domainsnapshot/state)", ctxt);
if (state == NULL)
continue;
if (virXPathLongLong("string(/domainsnapshot/creationTime)", ctxt,
&creation_longlong) < 0)
continue;
creation_time_t = creation_longlong;
if (creation_time_t != creation_longlong) {
vshError(ctl, "%s", _("time_t overflow"));
continue;
}
localtime_r(&creation_time_t, &time_info);
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S %z",
&time_info);
if (parent)
vshPrint(ctl, " %-20s %-25s %-15s %s\n",
names[i], timestr, state, parent);
else
vshPrint(ctl, " %-20s %-25s %s\n", names[i], timestr, state);
} }
localtime_r(&creation_time_t, &time_info);
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S %z",
&time_info);
if (parent)
vshPrint(ctl, " %-20s %-25s %-15s %s\n",
names[i], timestr, state, parent);
else
vshPrint(ctl, " %-20s %-25s %s\n", names[i], timestr, state);
} }
ret = true; ret = true;
@ -16913,7 +16911,7 @@ cleanup:
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
xmlFreeDoc(xml); xmlFreeDoc(xml);
VIR_FREE(doc); VIR_FREE(doc);
for (i = 0; i < actual; i++) { for (i = 0; i < count; i++) {
VIR_FREE(names[i]); VIR_FREE(names[i]);
if (parents) if (parents)
VIR_FREE(parents[i]); VIR_FREE(parents[i]);