[PATCHv4 3/4] virsh: Reject negative numbers in vshCommandOptULongLong

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 |  8 ++++----
 tools/virsh.c        | 51 ++++++++++++++++++++++++++++++++++++++++-----------
 tools/virsh.h        |  3 +++
 3 files changed, 47 insertions(+), 15 deletions(-)

diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index f284fa5..724a86b 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -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;
     }
diff --git a/tools/virsh.c b/tools/virsh.c
index 77a4f99..82669b0 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -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:
diff --git a/tools/virsh.h b/tools/virsh.h
index 4f5c336..7656407 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -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)
-- 
1.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]