Skip to contentSkip to conversation

Clean up img_create() and introduce strtosz_suffix()

PATCH Applied [email protected] 4 participants 6 messages Dec 9, 2010 – Dec 16, 2010

lore
Share & tools
lore
#0 lore raw

v5 cover Clean up img_create() and introduce strtosz_suffix()

From: Jes Sorensen <[email protected]>

This patch set introduces strtosz_suffix() which is needed to be able to use strtosz parsing with a non MB default suffix. This is used to clean up qemu-img.c:img_create().

Kevin asked me to rebase this instead of applying the other patches on top, so please discard the previous versions. Sorry for the patch noise.

v5 fixes the two issues pointed out by Stefan, making the call in strtosz() explicitly use STRTOSZ_DEFSUFFIX_MB instead of 0 to specify the default and adds a named argument to the prototype for strtosz_suffix().

Jes Sorensen (2):
Introduce strtosz_suffix()
qemu-img.c: Clean up handling of image size in img_create()

 cutils.c      |   17 ++++++++++++++---
 qemu-common.h |    7 +++++++
 qemu-img.c    |   23 +++++++++++++++++------
 3 files changed, 38 insertions(+), 9 deletions(-)
Signature · 1 line
1.7.3.2
#1 lore raw

PATCH v5 1/2 Introduce strtosz_suffix()

From: Jes Sorensen <[email protected]>

This introduces strtosz_suffix() which allows the caller to specify a default suffix in case the non default of MB is wanted.

strtosz() is kept as a wrapper for strtosz_suffix() which keeps it's current default of MB.

---
 cutils.c      |   17 ++++++++++++++---
 qemu-common.h |    7 +++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
cutils.c +14 -3
index 28089aa..7984bc1 100644
@@ -291,10 +291,10 @@ int fcntl_setfl(int fd, int flag)
  * value must be terminated by whitespace, ',' or '\0'. Return -1 on
  * error.
  */
-ssize_t strtosz(const char *nptr, char **end)
+ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
 {
     ssize_t retval = -1;
-    char *endptr, c;
+    char *endptr, c, d;
     int mul_required = 0;
     double val, mul, integral, fraction;
 
@@ -313,10 +313,16 @@ ssize_t strtosz(const char *nptr, char **end)
      * part of a multi token argument.
      */
     c = *endptr;
+    d = c;
     if (isspace(c) || c == '\0' || c == ',') {
         c = 0;
+        if (default_suffix) {
+            d = default_suffix;
+        } else {
+            d = c;
+        }
     }
-    switch (c) {
+    switch (d) {
     case 'B':
     case 'b':
         mul = 1;
@@ -371,3 +377,8 @@ fail:
 
     return retval;
 }
+
+ssize_t strtosz(const char *nptr, char **end)
+{
+    return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
+}
qemu-common.h +7 -0
index de82c2e..1ed32e5 100644
@@ -149,7 +149,14 @@ time_t mktimegm(struct tm *tm);
 int qemu_fls(int i);
 int qemu_fdatasync(int fd);
 int fcntl_setfl(int fd, int flag);
+
+#define STRTOSZ_DEFSUFFIX_TB	'T'
+#define STRTOSZ_DEFSUFFIX_GB	'G'
+#define STRTOSZ_DEFSUFFIX_MB	'M'
+#define STRTOSZ_DEFSUFFIX_KB	'K'
+#define STRTOSZ_DEFSUFFIX_B	'B'
 ssize_t strtosz(const char *nptr, char **end);
+ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
 
 /* path.c */
 void init_paths(const char *prefix);
Signature · 1 line
1.7.3.2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] Introduce strtosz_suffix()

Sorry for the late review.

Quoted 19 lines · [email protected] writes:

From: Jes Sorensen <[email protected]>

This introduces strtosz_suffix() which allows the caller to specify a default suffix in case the non default of MB is wanted.

strtosz() is kept as a wrapper for strtosz_suffix() which keeps it's current default of MB.

---
 cutils.c      |   17 ++++++++++++++---
 qemu-common.h |    7 +++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
cutils.c +0 -0
index 28089aa..7984bc1 100644

cutils.c int fcntl_setfl(int fd, int flag) · line 294

Quoted 5 lines
cutils.c +1 -1
@@ -291,10 +291,10 @@ int fcntl_setfl(int fd, int flag)
  * value must be terminated by whitespace, ',' or '\0'. Return -1 on
  * error.
  */
-ssize_t strtosz(const char *nptr, char **end)
+ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)

Last parameter's const is of marginal uility. Matter of taste.

Quoted 9 lines
cutils.c +1 -1
 {
     ssize_t retval = -1;
-    char *endptr, c;
+    char *endptr, c, d;
     int mul_required = 0;
     double val, mul, integral, fraction;
 
@@ -313,10 +313,16 @@ ssize_t strtosz(const char *nptr, char **end)
      * part of a multi token argument.

cutils.c ssize_t strtosz(const char *nptr, char **end) · line 323

Quoted 10 lines
cutils.c +6 -0
      */
     c = *endptr;
+    d = c;
     if (isspace(c) || c == '\0' || c == ',') {
         c = 0;
+        if (default_suffix) {
+            d = default_suffix;
+        } else {
+            d = c;
+        }

The else clause "d = c" is effectively "d = 0" (due to prior "c = 0"), which is the same as "d = default_suffix" (due to else's condition). Thus, you can replace the conditional by a simple

    d = default_suffix;
Quoted 20 lines
cutils.c +6 -1
     }
-    switch (c) {
+    switch (d) {
     case 'B':
     case 'b':
         mul = 1;
@@ -371,3 +377,8 @@ fail:
 
     return retval;
 }
+
+ssize_t strtosz(const char *nptr, char **end)
+{
+    return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
+}
qemu-common.h +0 -0
index de82c2e..1ed32e5 100644

qemu-common.h time_t mktimegm(struct tm *tm); · line 157

Quoted 9 lines
qemu-common.h +6 -0
@@ -149,7 +149,14 @@ time_t mktimegm(struct tm *tm);
 int qemu_fls(int i);
 int qemu_fdatasync(int fd);
 int fcntl_setfl(int fd, int flag);
+
+#define STRTOSZ_DEFSUFFIX_TB	'T'
+#define STRTOSZ_DEFSUFFIX_GB	'G'
+#define STRTOSZ_DEFSUFFIX_MB	'M'
+#define STRTOSZ_DEFSUFFIX_KB	'K'
+#define STRTOSZ_DEFSUFFIX_B	'B'

I don't see what these defines buy us over the literals, but if it makes you or other reviewers happier...

Quoted 5 lines
qemu-common.h +1 -0
 ssize_t strtosz(const char *nptr, char **end);
+ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
 
 /* path.c */
 void init_paths(const char *prefix);
#3 lore raw

PATCH v5 2/2 qemu-img.c: Clean up handling of image size in img_create()

In reply to [email protected]

From: Jes Sorensen <[email protected]>

This cleans up the handling of image size in img_create() by parsing the value early, and then only setting it once if a value has been added as the last argument to the command line.

---
 qemu-img.c |   23 +++++++++++++++++------
 1 files changed, 17 insertions(+), 6 deletions(-)
qemu-img.c +17 -6
index d146d8c..f078718 100644
@@ -282,6 +282,7 @@ static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
 static int img_create(int argc, char **argv)
 {
     int c, ret = 0;
+    uint64_t img_size = -1;
     const char *fmt = "raw";
     const char *base_fmt = NULL;
     const char *filename;
@@ -329,6 +330,20 @@ static int img_create(int argc, char **argv)
     }
     filename = argv[optind++];
 
+    /* Get image size, if specified */
+    if (optind < argc) {
+        ssize_t sval;
+        sval = strtosz_suffix(argv[optind++], NULL, STRTOSZ_DEFSUFFIX_B);
+        if (sval < 0) {
+            error("Invalid image size specified! You may use k, M, G or "
+                  "T suffixes for ");
+            error("kilobytes, megabytes, gigabytes and terabytes.");
+            ret = -1;
+            goto out;
+        }
+        img_size = (uint64_t)sval;
+    }
+
     if (options && !strcmp(options, "?")) {
         ret = print_block_option_help(filename, fmt);
         goto out;
@@ -356,7 +371,8 @@ static int img_create(int argc, char **argv)
 
     /* Create parameter list with default values */
     param = parse_option_parameters("", create_options, param);
-    set_option_parameter_int(param, BLOCK_OPT_SIZE, -1);
+
+    set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
 
     /* Parse -o options */
     if (options) {
@@ -368,11 +384,6 @@ static int img_create(int argc, char **argv)
         }
     }
 
-    /* Add size to parameters */
-    if (optind < argc) {
-        set_option_parameter(param, BLOCK_OPT_SIZE, argv[optind++]);
-    }
-
     /* Add old-style options to parameters */
     ret = add_old_style_options(fmt, param, base_filename, base_fmt);
     if (ret < 0) {
Signature · 1 line
1.7.3.2
Applied
#5 lore raw
In reply to [email protected]

Am 09.12.2010 14:17, schrieb [email protected]:

Quoted 15 lines

From: Jes Sorensen <[email protected]>

This patch set introduces strtosz_suffix() which is needed to be able to use strtosz parsing with a non MB default suffix. This is used to clean up qemu-img.c:img_create().

Kevin asked me to rebase this instead of applying the other patches on top, so please discard the previous versions. Sorry for the patch noise.

v5 fixes the two issues pointed out by Stefan, making the call in strtosz() explicitly use STRTOSZ_DEFSUFFIX_MB instead of 0 to specify the default and adds a named argument to the prototype for strtosz_suffix().

Quoted 8 lines

Jes Sorensen (2):
Introduce strtosz_suffix()
qemu-img.c: Clean up handling of image size in img_create()

 cutils.c      |   17 ++++++++++++++---
 qemu-common.h |    7 +++++++
 qemu-img.c    |   23 +++++++++++++++++------
 3 files changed, 38 insertions(+), 9 deletions(-)

Thanks, applied all to the block branch.

Kevin