render / rpms / libvirt

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