Skip to contentSkip to conversation

Fix size default for qemu-img

PATCH Superseded [email protected] 1 participant 3 messages Dec 9, 2010

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Fix size default for qemu-img

From: Jes Sorensen <[email protected]>

Kevin pointed out that my chance to img_create()'s handling of the image size, changed the previous default of byte for size if no suffix was specified, since strtosz() defaults to MB.

This patch set introduces strtosz_suffix() and then changes img_create() to use that instead, thereby restoring the old default behavior.

Jes Sorensen (2):
Introduce strtosz_suffix()
Make img_create() use strtosz_suffix()

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

PATCH 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..1d24d9a 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, 0);
+}
qemu-common.h +7 -0
index de82c2e..dc44cd6 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);
 
 /* path.c */
 void init_paths(const char *prefix);
Signature · 1 line
1.7.3.2
#2 lore raw

PATCH 2/2 Make img_create() use strtosz_suffix()

In reply to [email protected]

From: Jes Sorensen <[email protected]>

This reestablished the old default of using bytes as the default for the size argument, and not MB as we do in pretty much every other place.

---
 qemu-img.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
qemu-img.c +1 -1
index 9a5e7e1..603bdb3 100644
@@ -331,7 +331,7 @@ static int img_create(int argc, char **argv)
     /* Get image size, if specified */
     if (optind < argc) {
         ssize_t sval;
-        sval = strtosz(argv[optind++], NULL);
+        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 ");
Signature · 1 line
1.7.3.2