]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Fix some rounding errors in human_size and generally clean up the code
authorNeil Brown <neilb@suse.de>
Wed, 8 Jun 2005 01:43:59 +0000 (01:43 +0000)
committerNeil Brown <neilb@suse.de>
Wed, 8 Jun 2005 01:43:59 +0000 (01:43 +0000)
Signed-off-by: Neil Brown <neilb@cse.unsw.edu.au>
ChangeLog
util.c

index 040e9254fd8cae6444f6d1d552a0f0124a5b87a9..2e7ba8f49bdb505b421f49ca0c736c7212b169a5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,6 +12,7 @@ Changes Prior to 1.12.0 release
        --auto was ignored if --scan was given
     -   Fix a few type casts
     -   Fix parsing of /dev/md/N in is_standard
+    -   Fix rounding errors in human_size()
        
 Changes Prior to 1.11.0 release
     -   Fix embarassing bug which causes --add to always fail.
diff --git a/util.c b/util.c
index 15dac7d084767df4aa4cf680dccd9f2e16d0d5c9..add88de4dc7ef795c52595165acd1d5c44c4385b 100644 (file)
--- a/util.c
+++ b/util.c
@@ -525,24 +525,31 @@ unsigned long calc_sb_csum(mdp_super_t *super)
 char *human_size(long long bytes)
 {
        static char buf[30];
-       
+
+       /* We convert bytes to either centi-M{ega,ibi}bytes or
+        * centi-G{igi,ibi}bytes, with appropriate rounding,
+        * and then print 1/100th of those as a decimal.
+        * We allow upto 2048Megabytes before converting to
+        * gigabytes, as that shows more precision and isn't
+        * too large a number.
+        * Terrabytes are not yet handled.
+        */
 
        if (bytes < 5000*1024)
                buf[0]=0;
-       else if (bytes < 2*1024LL*1024LL*1024LL)
+       else if (bytes < 2*1024LL*1024LL*1024LL) {
+               long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
+               long cMB  = (bytes / ( 1000000LL / 200LL ) +1) /2;
                sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
-                       (long)(bytes>>20),
-                       (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100),
-                       (long)(bytes/1000/1000),
-                       (long)(((bytes%1000000)+5000)/10000)
-                       );
-       else
+                       cMiB/100 , cMiB % 100,
+                       cMB/100, cMB % 100);
+       } else {
+               long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
+               long cGB  = (bytes / (1000000000LL/200LL ) +1) /2;
                sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
-                       (long)(bytes>>30),
-                       (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100),
-                       (long)(bytes/1000LL/1000LL/1000LL),
-                       (long)((((bytes/1000)%1000000)+5000)/10000)
-                       );
+                       cGiB/100 , cGiB % 100,
+                       cGB/100, cGB % 100);
+       }
        return buf;
 }