]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Reject negative numbers in vshCommandOptULongLong
authorPeter Krempa <pkrempa@redhat.com>
Wed, 4 Jun 2014 09:08:08 +0000 (11:08 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 12 Jun 2014 12:06:21 +0000 (14:06 +0200)
To follow the new semantics of the vshCommandOptToU* functions convert
this one to reject negative numbers too. To allow using -1 for "maximum"
semantics for the vol-*load two bandwidth functions that use this helper
introduce vshCommandOptULongLongWrap.

tools/virsh-volume.c
tools/virsh.c
tools/virsh.h

index f284fa52225c841480026f22fa9f24afb4f19786..724a86bc66bc916cbe54258802c5c13245c4d9f3 100644 (file)
@@ -677,12 +677,12 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
     const char *name = NULL;
     unsigned long long offset = 0, length = 0;
 
-    if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
+    if (vshCommandOptULongLongWrap(cmd, "offset", &offset) < 0) {
         vshError(ctl, _("Unable to parse integer"));
         return false;
     }
 
-    if (vshCommandOptULongLong(cmd, "length", &length) < 0) {
+    if (vshCommandOptULongLongWrap(cmd, "length", &length) < 0) {
         vshError(ctl, _("Unable to parse integer"));
         return false;
     }
@@ -787,12 +787,12 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
     unsigned long long offset = 0, length = 0;
     bool created = false;
 
-    if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
+    if (vshCommandOptULongLongWrap(cmd, "offset", &offset) < 0) {
         vshError(ctl, _("Unable to parse integer"));
         return false;
     }
 
-    if (vshCommandOptULongLong(cmd, "length", &length) < 0) {
+    if (vshCommandOptULongLongWrap(cmd, "length", &length) < 0) {
         vshError(ctl, _("Unable to parse integer"));
         return false;
     }
index 011477178714ea55aa861e8b8831d948b8ae368b..1bac84281ae4740c9d660bcc64e1c3a68149b9cf 100644 (file)
@@ -1699,31 +1699,60 @@ vshCommandOptLongLong(const vshCmd *cmd, const char *name,
     return 1;
 }
 
+static int
+vshCommandOptULongLongInternal(const vshCmd *cmd,
+                               const char *name,
+                               unsigned long long *value,
+                               bool wrap)
+{
+    vshCmdOpt *arg;
+    int ret;
+
+    if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
+        return ret;
+
+    if (wrap) {
+        if (virStrToLong_ull(arg->data, NULL, 10, value) < 0)
+            return -1;
+    } else {
+        if (virStrToLong_ullp(arg->data, NULL, 10, value) < 0)
+            return -1;
+    }
+
+    return 1;
+}
+
 /**
  * vshCommandOptULongLong:
  * @cmd command reference
  * @name option name
  * @value result
  *
- * Returns option as long long
+ * Returns option as long long, rejects negative numbers
  * See vshCommandOptInt()
  */
 int
 vshCommandOptULongLong(const vshCmd *cmd, const char *name,
                        unsigned long long *value)
 {
-    vshCmdOpt *arg;
-    int ret;
-
-    ret = vshCommandOpt(cmd, name, &arg, true);
-    if (ret <= 0)
-        return ret;
-
-    if (virStrToLong_ull(arg->data, NULL, 10, value) < 0)
-        return -1;
-    return 1;
+    return vshCommandOptULongLongInternal(cmd, name, value, false);
 }
 
+/**
+ * vshCommandOptULongLongWrap:
+ * @cmd command reference
+ * @name option name
+ * @value result
+ *
+ * Returns option as long long, wraps negative numbers to positive
+ * See vshCommandOptInt()
+ */
+int
+vshCommandOptULongLongWrap(const vshCmd *cmd, const char *name,
+                       unsigned long long *value)
+{
+    return vshCommandOptULongLongInternal(cmd, name, value, true);
+}
 
 /**
  * vshCommandOptScaledInt:
index 4f5c336b4ad494c97c1a753e97a55f9a45a457c8..7656407c9be532c1fb5c6b36fb4f2b2192676006 100644 (file)
@@ -307,6 +307,9 @@ int vshCommandOptLongLong(const vshCmd *cmd, const char *name,
 int vshCommandOptULongLong(const vshCmd *cmd, const char *name,
                            unsigned long long *value)
     ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptULongLongWrap(const vshCmd *cmd, const char *name,
+                               unsigned long long *value)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
 int vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
                            unsigned long long *value, int scale,
                            unsigned long long max)