[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 15/27] qemu-img: snapshot: make -l (list) the default, simplify o
From: |
Michael Tokarev |
Subject: |
[PATCH 15/27] qemu-img: snapshot: make -l (list) the default, simplify option handling |
Date: |
Wed, 24 Apr 2024 11:50:23 +0300 |
When no -l/-a/-c/-d specified, assume -l (list).
Use the same values for SNAPSHOT_LIST/etc constants as the
option chars (lacd), this makes it possible to simplify
option handling a lot, combining cases for 4 options into
one.
Also remove bdrv_oflags handling (only list can use RO mode).
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
---
docs/tools/qemu-img.rst | 2 +-
qemu-img.c | 52 ++++++++++++++---------------------------
2 files changed, 19 insertions(+), 35 deletions(-)
diff --git a/docs/tools/qemu-img.rst b/docs/tools/qemu-img.rst
index 9b628c4da5..df184d15b9 100644
--- a/docs/tools/qemu-img.rst
+++ b/docs/tools/qemu-img.rst
@@ -256,7 +256,7 @@ Parameters to snapshot subcommand:
.. option:: -l
- Lists all snapshots in the given image
+ Lists all snapshots in the given image (default action)
Command description:
diff --git a/qemu-img.c b/qemu-img.c
index 8adc324496..967f6343de 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -3585,10 +3585,11 @@ out:
return ret < 0;
}
-#define SNAPSHOT_LIST 1
-#define SNAPSHOT_CREATE 2
-#define SNAPSHOT_APPLY 3
-#define SNAPSHOT_DELETE 4
+/* the same as options */
+#define SNAPSHOT_LIST 'l'
+#define SNAPSHOT_CREATE 'c'
+#define SNAPSHOT_APPLY 'a'
+#define SNAPSHOT_DELETE 'd'
static int img_snapshot(const img_cmd_t *ccmd, int argc, char **argv)
{
@@ -3596,7 +3597,7 @@ static int img_snapshot(const img_cmd_t *ccmd, int argc,
char **argv)
BlockDriverState *bs;
QEMUSnapshotInfo sn;
char *filename, *fmt = NULL, *snapshot_name = NULL;
- int c, ret = 0, bdrv_oflags;
+ int c, ret = 0;
int action = 0;
bool quiet = false;
Error *err = NULL;
@@ -3604,7 +3605,6 @@ static int img_snapshot(const img_cmd_t *ccmd, int argc,
char **argv)
bool force_share = false;
int64_t rt;
- bdrv_oflags = BDRV_O_RDWR;
/* Parse commandline parameters */
for(;;) {
static const struct option long_options[] = {
@@ -3632,36 +3632,15 @@ static int img_snapshot(const img_cmd_t *ccmd, int
argc, char **argv)
case 'f':
fmt = optarg;
break;
- case 'l':
- if (action) {
- error_exit(argv[0], "Cannot mix '-l', '-a', '-c', '-d'");
- return 0;
- }
- action = SNAPSHOT_LIST;
- bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
- break;
- case 'a':
+ case SNAPSHOT_LIST:
+ case SNAPSHOT_APPLY:
+ case SNAPSHOT_CREATE:
+ case SNAPSHOT_DELETE:
if (action) {
error_exit(argv[0], "Cannot mix '-l', '-a', '-c', '-d'");
return 0;
}
- action = SNAPSHOT_APPLY;
- snapshot_name = optarg;
- break;
- case 'c':
- if (action) {
- error_exit(argv[0], "Cannot mix '-l', '-a', '-c', '-d'");
- return 0;
- }
- action = SNAPSHOT_CREATE;
- snapshot_name = optarg;
- break;
- case 'd':
- if (action) {
- error_exit(argv[0], "Cannot mix '-l', '-a', '-c', '-d'");
- return 0;
- }
- action = SNAPSHOT_DELETE;
+ action = c;
snapshot_name = optarg;
break;
case 'q':
@@ -3684,9 +3663,14 @@ static int img_snapshot(const img_cmd_t *ccmd, int argc,
char **argv)
}
filename = argv[optind++];
+ if (!action) {
+ action = SNAPSHOT_LIST;
+ }
+
/* Open the image */
- blk = img_open(image_opts, filename, fmt, bdrv_oflags, false, quiet,
- force_share);
+ blk = img_open(image_opts, filename, fmt,
+ action == SNAPSHOT_LIST ? 0 : BDRV_O_RDWR,
+ false, quiet, force_share);
if (!blk) {
return 1;
}
--
2.39.2
- [PATCH 05/27] qemu-img: create: refresh options/--help, (continued)
- [PATCH 05/27] qemu-img: create: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 06/27] qemu-img: factor out parse_output_format() and use it in the code, Michael Tokarev, 2024/04/24
- [PATCH 10/27] qemu-img: compare: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 11/27] qemu-img: convert: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 07/27] qemu-img: check: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 08/27] qemu-img: simplify --repair error message, Michael Tokarev, 2024/04/24
- [PATCH 09/27] qemu-img: commit: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 13/27] qemu-img: map: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 14/27] qemu-img: snapshot: allow specifying -f fmt, Michael Tokarev, 2024/04/24
- [PATCH 12/27] qemu-img: info: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 15/27] qemu-img: snapshot: make -l (list) the default, simplify option handling,
Michael Tokarev <=
- [PATCH 16/27] qemu-img: snapshot: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 17/27] qemu-img: rebase: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 18/27] qemu-img: resize: do not always eat last argument, Michael Tokarev, 2024/04/24
- [PATCH 20/27] qemu-img: amend: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 19/27] qemu-img: resize: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 22/27] qemu-img: bitmap: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 21/27] qemu-img: bench: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 23/27] qemu-img: dd: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 24/27] qemu-img: measure: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 25/27] qemu-img: implement short --help, remove global help() function, Michael Tokarev, 2024/04/24