[libvirt] [PATCH 3/4] snapshot: expose location through virsh snapshot-info

Peter Krempa pkrempa at redhat.com
Wed Nov 14 00:16:34 UTC 2012


On 11/13/12 20:09, Eric Blake wrote:
> Now that we can filter on this information, we should also make
> it easy to get at.
>
> * tools/virsh-snapshot.c (cmdSnapshotInfo): Add another output
> row.
> ---
>   tools/virsh-snapshot.c | 32 +++++++++++++++++++++++++++-----
>   1 file changed, 27 insertions(+), 5 deletions(-)
>
> diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c
> index d5c71be..da1e75f 100644
> --- a/tools/virsh-snapshot.c
> +++ b/tools/virsh-snapshot.c
> @@ -797,7 +797,8 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd)
>       virDomainSnapshotPtr snapshot = NULL;
>       const char *name;
>       char *doc = NULL;
> -    char *tmp;
> +    char *state;
> +    bool internal;
>       char *parent = NULL;
>       bool ret = false;
>       int count;
> @@ -839,15 +840,36 @@ cmdSnapshotInfo(vshControl *ctl, const vshCmd *cmd)
>       if (!doc)
>           goto cleanup;
>
> -    tmp = strstr(doc, "<state>");
> -    if (!tmp) {
> +    state = strstr(doc, "<state>");
> +    if (!state) {
>           vshError(ctl, "%s",
>                    _("unexpected problem reading snapshot xml"));
>           goto cleanup;
>       }
> -    tmp += strlen("<state>");
> +    state += strlen("<state>");
>       vshPrint(ctl, "%-15s %.*s\n", _("State:"),
> -             (int) (strchr(tmp, '<') - tmp), tmp);
> +             (int) (strchr(state, '<') - state), state);
> +
> +    /* In addition to state, location is useful.  If the snapshot has
> +     * a <memory> element, then the existence of snapshot='external'
> +     * prior to <domain> is the deciding factor; for snapshots
> +     * created prior to 1.0.1, a state of disk-only is the only
> +     * external snapshot.  */
> +    if (strstr(state, "<memory")) {
> +        char *domain = strstr(state, "<domain");

Bleah. Raw XML parsing. Wouldn't it be easier in and cleaner convert 
this piece code to use the XML parser and xpath?

> +
> +        if (!domain) {
> +            vshError(ctl, "%s",
> +                     _("unexpected problem reading snapshot xml"));
> +            goto cleanup;
> +        }
> +        internal = !memmem(state, domain - state, "snapshot='external'",
> +                           strlen("snapshot='external'"));
> +    } else {
> +        internal = !STRPREFIX(state, "disk-snapshot");
> +    }
> +    vshPrint(ctl, "%-15s %s\n", _("Location:"),
> +             internal ? _("internal") : _("external"));
>
>       if (vshGetSnapshotParent(ctl, snapshot, &parent) < 0)
>           goto cleanup;
>

The code looks OK in what it should be doing, but I don't like the raw 
XML parse-hacking stuff. The only reason to put this in as-is would be 
if it would be really complicated/overheading to use xpath here.

Peter




More information about the libvir-list mailing list