From: Eric Blake <eblake@redhat.com>
To: Pavel Hrdina <phrdina@redhat.com>
Cc: lcapitulino@redhat.com, qemu-devel@nongnu.org, armbru@redhat.com
Subject: Re: [Qemu-devel] [PATCH 03/11] savevm: update bdrv_snapshot_find() to find snapshot by id or name
Date: Tue, 16 Apr 2013 11:34:22 -0600 [thread overview]
Message-ID: <516D8B9E.6010609@redhat.com> (raw)
In-Reply-To: <17626585faf740878394cb2cbdc733cfb0c535e8.1366127809.git.phrdina@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 5890 bytes --]
On 04/16/2013 10:05 AM, Pavel Hrdina wrote:
> Finding snapshot by a name which could also be an id isn't best way
> how to do it. There will be rewrite of savevm, loadvm and delvm to
> improve the behavior of these commands. The savevm and loadvm will
> have their own patch series.
>
> Now bdrv_snapshot_find takes more parameters. The name parameter will
> be matched only against the name of the snapshot and the same applies
> to id parameter.
>
> There is one exception. If you set the last parameter, the name parameter
> will be matched against the name or the id of a snapshot. This exception
> is only for backward compatibility for other commands and it will be
> dropped after all commands will be rewritten.
Fair enough, even if it is a bit of churn on the signature and callers.
>
> We only need to know if that snapshot exists or not. We don't care
> about any error message. If snapshot exists it returns 1 otherwise
> it returns 0.
If you are only returning a yes/no result, bool is better than int.
>
> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
> ---
> savevm.c | 73 ++++++++++++++++++++++++++++++++++++++++++++--------------------
> 1 file changed, 50 insertions(+), 23 deletions(-)
>
> diff --git a/savevm.c b/savevm.c
> index 6af84fd..96a2340 100644
> --- a/savevm.c
> +++ b/savevm.c
> @@ -2197,25 +2197,55 @@ out:
> }
>
> static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
Return bool...
> - const char *name)
> + const char *name, const char *id, bool old_match)
> {
> QEMUSnapshotInfo *sn_tab, *sn;
> - int nb_sns, i, ret;
> + int nb_sns, i, found = 0;
... and initialize 'bool found = false'.
> +
> + if (!name && !id) {
> + return found;
> + }
It may be appropriate to do assert(name || id), if you want to guarantee
that all callers supply at least one of the two arguments (and that the
QMP code where both arguments are optional does its own sanity checking
that the user supplied an argument.
>
> - ret = -ENOENT;
> nb_sns = bdrv_snapshot_list(bs, &sn_tab);
> - if (nb_sns < 0)
> - return ret;
> + if (nb_sns < 0) {
> + return found;
Calling the return value 'found' makes the code read awkwardly when
reporting failure; maybe keeping it named 'ret' is better after all.
> + }
> +
> for(i = 0; i < nb_sns; i++) {
As long as you are touching this code, you could insert the space after
'for'.
> sn = &sn_tab[i];
> - if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
> - *sn_info = *sn;
> - ret = 0;
> - break;
> + if (name && id) {
> + if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
> + *sn_info = *sn;
> + found = 1;
> + break;
> + }
> + } else if (name) {
> + /* for compatibility for old bdrv_snapshot_find call
> + * will be removed */
> + if (old_match) {
> + if (!strcmp(sn->id_str, id) || !strcmp(sn->name, name)) {
> + *sn_info = *sn;
> + found = 1;
> + break;
> + }
> + } else {
> + if (!strcmp(sn->name, name)) {
> + *sn_info = *sn;
> + found = 1;
> + break;
> + }
> + }
> + } else if (id) {
> + if (!strcmp(sn->id_str, id)) {
> + *sn_info = *sn;
> + found = 1;
> + break;
> + }
Note that this says that if both name and id are specified, then that
takes priority, no matter what was specified for old_match.
> }
> }
> +
> g_free(sn_tab);
> - return ret;
> + return found;
> }
>
> /*
> @@ -2229,8 +2259,8 @@ static int del_existing_snapshots(Monitor *mon, const char *name)
>
> bs = NULL;
> while ((bs = bdrv_next(bs))) {
> - if (bdrv_can_snapshot(bs) &&
> - bdrv_snapshot_find(bs, snapshot, name) >= 0)
> + if (bdrv_can_snapshot(bs)
> + && bdrv_snapshot_find(bs, snapshot, name, name, true))
Needless style change. The old style was sufficient, with:
if (bdrv_can_snapshot(bs) &&
bdrv_snapshot_find(bs, snapshot, name, name, true))
Except that bdrv_snapshot_find(bs, snapshot, name, name, anything)
results in a lookup that requires sn->id_str and sn->name be the same
string, which is not what you intended.
You really want to be using bdrv_snapshot_find(bs, snapshot, NULL, name,
true), if you want the old_match parameter to make a difference.
...
> @@ -2474,7 +2501,7 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict)
> {
> BlockDriverState *bs, *bs1;
> QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
> - int nb_sns, i, ret, available;
> + int nb_sns, i, available;
> int total;
> int *available_snapshots;
> char buf[256];
> @@ -2505,8 +2532,8 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict)
>
> while ((bs1 = bdrv_next(bs1))) {
> if (bdrv_can_snapshot(bs1) && bs1 != bs) {
> - ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
> - if (ret < 0) {
> + if (!bdrv_snapshot_find(bs1, sn_info, sn->name, sn->id_str,
> + true)) {
This was the only conversion that passed both id and name that looked
correct. All the other conversions to 'name, name, true' need fixing.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]
next prev parent reply other threads:[~2013-04-16 17:34 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-16 16:05 [Qemu-devel] [PATCH 00/11] covert savevm, loadvm and delvm into qapi Pavel Hrdina
2013-04-16 16:05 ` [Qemu-devel] [PATCH 01/11] qemu-img: introduce qemu_img_handle_error() Pavel Hrdina
2013-04-16 16:46 ` Eric Blake
2013-04-18 11:44 ` Kevin Wolf
2013-04-18 11:52 ` Pavel Hrdina
2013-04-18 12:59 ` Kevin Wolf
2013-04-18 13:09 ` Pavel Hrdina
2013-04-18 15:23 ` Luiz Capitulino
2013-04-16 16:05 ` [Qemu-devel] [PATCH 02/11] block: update error reporting for bdrv_snapshot_delete() and related functions Pavel Hrdina
2013-04-16 17:14 ` Eric Blake
2013-04-18 12:55 ` Kevin Wolf
2013-04-18 13:09 ` Eric Blake
2013-04-18 13:51 ` Kevin Wolf
2013-04-18 13:19 ` Pavel Hrdina
2013-04-18 13:41 ` Kevin Wolf
2013-04-16 16:05 ` [Qemu-devel] [PATCH 03/11] savevm: update bdrv_snapshot_find() to find snapshot by id or name Pavel Hrdina
2013-04-16 17:34 ` Eric Blake [this message]
2013-04-18 13:17 ` Kevin Wolf
2013-04-16 16:05 ` [Qemu-devel] [PATCH 04/11] qapi: Convert delvm Pavel Hrdina
2013-04-16 19:39 ` Eric Blake
2013-04-18 13:28 ` Kevin Wolf
2013-04-16 16:05 ` [Qemu-devel] [PATCH 05/11] block: update error reporting for bdrv_snapshot_goto() and related functions Pavel Hrdina
2013-04-16 20:48 ` Eric Blake
2013-04-23 14:08 ` Kevin Wolf
2013-04-16 16:05 ` [Qemu-devel] [PATCH 06/11] savevm: update error reporting for qemu_loadvm_state() Pavel Hrdina
2013-04-16 21:42 ` Eric Blake
2013-04-16 16:05 ` [Qemu-devel] [PATCH 07/11] qapi: Convert loadvm Pavel Hrdina
2013-04-16 23:43 ` Eric Blake
2013-04-18 10:34 ` Pavel Hrdina
2013-04-16 16:05 ` [Qemu-devel] [PATCH 08/11] block: update error reporting for bdrv_snapshot_create() and related functions Pavel Hrdina
2013-04-16 23:54 ` Eric Blake
2013-04-16 16:05 ` [Qemu-devel] [PATCH 09/11] savevm: update error reporting off qemu_savevm_state() " Pavel Hrdina
2013-04-17 0:02 ` Eric Blake
2013-04-16 16:05 ` [Qemu-devel] [PATCH 10/11] qapi: Convert savevm Pavel Hrdina
2013-04-16 16:05 ` [Qemu-devel] [PATCH 11/11] savevm: remove backward compatibility from bdrv_snapshot_find() Pavel Hrdina
2013-04-17 2:53 ` Wenchao Xia
2013-04-17 7:52 ` Pavel Hrdina
2013-04-17 10:19 ` Wenchao Xia
2013-04-17 10:51 ` Pavel Hrdina
2013-04-17 18:14 ` Eric Blake
2013-04-17 18:22 ` Eric Blake
2013-04-18 4:31 ` Wenchao Xia
2013-04-18 7:20 ` Wenchao Xia
2013-04-18 10:22 ` Pavel Hrdina
2013-04-19 0:28 ` Wenchao Xia
2013-04-24 3:51 ` Wenchao Xia
2013-04-24 9:37 ` Pavel Hrdina
2013-04-16 16:33 ` [Qemu-devel] [PATCH 00/11] covert savevm, loadvm and delvm into qapi Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=516D8B9E.6010609@redhat.com \
--to=eblake@redhat.com \
--cc=armbru@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=phrdina@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.