]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Display size with human_size_brief with a chosen prefix
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index eb4665024478aaa5d20299706baf8b018590f619..cb97816c346f5d4b5e88068d423090d00c10a2bc 100644 (file)
--- a/util.c
+++ b/util.c
@@ -188,13 +188,13 @@ int mdadm_version(char *version)
        return (a*1000000)+(b*1000)+c;
 }
 
-long long parse_size(char *size)
+unsigned long long parse_size(char *size)
 {
        /* parse 'size' which should be a number optionally
         * followed by 'K', 'M', or 'G'.
         * Without a suffix, K is assumed.
         * Number returned is in sectors (half-K)
-        * -1 returned on error.
+        * 0 returned on error.
         */
        char *c;
        long long s = strtoll(size, &c, 10);
@@ -214,9 +214,10 @@ long long parse_size(char *size)
                        s *= 1024 * 1024 * 2;
                        break;
                }
-       }
+       } else
+               s = 0;
        if (*c)
-               s = -1;
+               s = 0;
        return s;
 }
 
@@ -681,24 +682,49 @@ char *human_size(long long bytes)
        return buf;
 }
 
-char *human_size_brief(long long bytes)
+char *human_size_brief(long long bytes, int prefix)
 {
        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.
+        * Terabytes are not yet handled.
+        *
+        * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
+        * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
+        */
+
        if (bytes < 5000*1024)
-               snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
-                       (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
-                       );
-       else if (bytes < 2*1024LL*1024LL*1024LL)
-               snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
-                       (long)(bytes>>20),
-                       (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
-                       );
+               buf[0] = 0;
+       else if (prefix == IEC) {
+               if (bytes < 2*1024LL*1024LL*1024LL) {
+                       long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
+                       snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
+                               cMiB/100 , cMiB % 100);
+               } else {
+                       long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
+                       snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
+                                       cGiB/100 , cGiB % 100);
+               }
+       }
+       else if (prefix == JEDEC) {
+               if (bytes < 2*1024LL*1024LL*1024LL) {
+                       long cMB  = (bytes / ( 1000000LL / 200LL ) +1) /2;
+                       snprintf(buf, sizeof(buf), "%ld.%02ldMB",
+                                       cMB/100, cMB % 100);
+               } else {
+                       long cGB  = (bytes / (1000000000LL/200LL ) +1) /2;
+                       snprintf(buf, sizeof(buf), "%ld.%02ldGB",
+                                       cGB/100 , cGB % 100);
+               }
+       }
        else
-               snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
-                       (long)(bytes>>30),
-                       (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
-                       );
+               buf[0] = 0;
+
        return buf;
 }
 
@@ -811,10 +837,14 @@ int find_free_devnum(int use_partitions)
             devnum = devnum ? devnum-1 : (1<<20)-1) {
                char *dn;
                int _devnum;
+               char nbuf[50];
 
                _devnum = use_partitions ? (-1-devnum) : devnum;
                if (mddev_busy(_devnum))
                        continue;
+               sprintf(nbuf, "%s%d", use_partitions?"mdp":"md", devnum);
+               if (!conf_name_is_free(nbuf))
+                       continue;
                /* make sure it is new to /dev too, at least as a
                 * non-standard */
                dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
@@ -1803,43 +1833,3 @@ struct mdinfo *container_choose_spares(struct supertype *st,
        }
        return disks;
 }
-
-void *xmalloc(size_t len)
-{
-       void *rv = malloc(len);
-       char *msg;
-       if (rv)
-               return rv;
-       msg = Name ": memory allocation failure - aborting\n";
-       exit(4+!!write(2, msg, strlen(msg)));
-}
-
-void *xrealloc(void *ptr, size_t len)
-{
-       void *rv = realloc(ptr, len);
-       char *msg;
-       if (rv)
-               return rv;
-       msg = Name ": memory allocation failure - aborting\n";
-       exit(4+!!write(2, msg, strlen(msg)));
-}
-
-void *xcalloc(size_t num, size_t size)
-{
-       void *rv = calloc(num, size);
-       char *msg;
-       if (rv)
-               return rv;
-       msg = Name ": memory allocation failure - aborting\n";
-       exit(4+!!write(2, msg, strlen(msg)));
-}
-
-char *xstrdup(const char *str)
-{
-       char *rv = strdup(str);
-       char *msg;
-       if (rv)
-               return rv;
-       msg = Name ": memory allocation failure - aborting\n";
-       exit(4+!!write(2, msg, strlen(msg)));
-}