render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
c1c534
From 155bc6e4428d2a01a1b63f0a4221211c2116e893 Mon Sep 17 00:00:00 2001
c1c534
Message-Id: <155bc6e4428d2a01a1b63f0a4221211c2116e893@dist-git>
c1c534
From: Martin Kletzander <mkletzan@redhat.com>
c1c534
Date: Wed, 31 Jan 2018 16:32:09 +0100
c1c534
Subject: [PATCH] util: Introduce virFormatIntPretty
c1c534
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
c1c534
c1c534
We can't output better memory sizes if we want to be compatible with libvirt
c1c534
older than the one which introduced /memory/unit, but for new things we can just
c1c534
output nicer capacity to the user if available.  And this function enables that.
c1c534
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
Reviewed-by: John Ferlan <jferlan@redhat.com>
c1c534
(cherry picked from commit 87a8a30d613dd2061377aa46fa6e9a05e12aa1b4)
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
---
c1c534
 src/libvirt_private.syms |  1 +
c1c534
 src/util/virutil.c       | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
c1c534
 src/util/virutil.h       |  4 ++++
c1c534
 3 files changed, 56 insertions(+)
c1c534
c1c534
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
c1c534
index 57999e77b0..5a87276a2e 100644
c1c534
--- a/src/libvirt_private.syms
c1c534
+++ b/src/libvirt_private.syms
c1c534
@@ -2938,6 +2938,7 @@ virDoubleToStr;
c1c534
 virEnumFromString;
c1c534
 virEnumToString;
c1c534
 virFormatIntDecimal;
c1c534
+virFormatIntPretty;
c1c534
 virGetDeviceID;
c1c534
 virGetDeviceUnprivSGIO;
c1c534
 virGetEnvAllowSUID;
c1c534
diff --git a/src/util/virutil.c b/src/util/virutil.c
c1c534
index 170e921920..2379cf019d 100644
c1c534
--- a/src/util/virutil.c
c1c534
+++ b/src/util/virutil.c
c1c534
@@ -485,6 +485,57 @@ virFormatIntDecimal(char *buf, size_t buflen, int val)
c1c534
 }
c1c534
 
c1c534
 
c1c534
+/**
c1c534
+ * virFormatIntPretty
c1c534
+ *
c1c534
+ * @val: Value in bytes to be shortened
c1c534
+ * @unit: unit to be used
c1c534
+ *
c1c534
+ * Similar to vshPrettyCapacity, but operates on integers and not doubles
c1c534
+ *
c1c534
+ * Returns shortened value that can be used with @unit.
c1c534
+ */
c1c534
+unsigned long long
c1c534
+virFormatIntPretty(unsigned long long val,
c1c534
+                   const char **unit)
c1c534
+{
c1c534
+    unsigned long long limit = 1024;
c1c534
+
c1c534
+    if (val % limit || val == 0) {
c1c534
+        *unit = "B";
c1c534
+        return val;
c1c534
+    }
c1c534
+    limit *= 1024;
c1c534
+    if (val % limit) {
c1c534
+        *unit = "KiB";
c1c534
+        return val / (limit / 1024);
c1c534
+    }
c1c534
+    limit *= 1024;
c1c534
+    if (val % limit) {
c1c534
+        *unit = "MiB";
c1c534
+        return val / (limit / 1024);
c1c534
+    }
c1c534
+    limit *= 1024;
c1c534
+    if (val % limit) {
c1c534
+        *unit = "GiB";
c1c534
+        return val / (limit / 1024);
c1c534
+    }
c1c534
+    limit *= 1024;
c1c534
+    if (val % limit) {
c1c534
+        *unit = "TiB";
c1c534
+        return val / (limit / 1024);
c1c534
+    }
c1c534
+    limit *= 1024;
c1c534
+    if (val % limit) {
c1c534
+        *unit = "PiB";
c1c534
+        return val / (limit / 1024);
c1c534
+    }
c1c534
+    limit *= 1024;
c1c534
+    *unit = "EiB";
c1c534
+    return val / (limit / 1024);
c1c534
+}
c1c534
+
c1c534
+
c1c534
 const char *virEnumToString(const char *const*types,
c1c534
                             unsigned int ntypes,
c1c534
                             int type)
c1c534
diff --git a/src/util/virutil.h b/src/util/virutil.h
c1c534
index a862a8a637..9ee728235f 100644
c1c534
--- a/src/util/virutil.h
c1c534
+++ b/src/util/virutil.h
c1c534
@@ -66,6 +66,10 @@ int virParseVersionString(const char *str, unsigned long *version,
c1c534
 char *virFormatIntDecimal(char *buf, size_t buflen, int val)
c1c534
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
c1c534
 
c1c534
+unsigned long long
c1c534
+virFormatIntPretty(unsigned long long val,
c1c534
+                   const char **unit);
c1c534
+
c1c534
 int virDiskNameParse(const char *name, int *disk, int *partition);
c1c534
 int virDiskNameToIndex(const char* str);
c1c534
 char *virIndexToDiskName(int idx, const char *prefix);
c1c534
-- 
c1c534
2.16.1
c1c534