]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
mkfs: use cvtnum from libfrog
authorDave Chinner <dchinner@redhat.com>
Tue, 7 Apr 2020 18:29:39 +0000 (14:29 -0400)
committerEric Sandeen <sandeen@sandeen.net>
Tue, 7 Apr 2020 18:29:39 +0000 (14:29 -0400)
Move the checks for zero block/sector size to the libfrog code
and return -1LL as an invalid value instead. Catch the invalid
value in mkfs and error out there instead of inside cvtnum.

Also rename the libfrog block/sector size variables so they don't
shadow the mkfs global variables of the same name and mark the
string being passed in as a const.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
include/xfs_multidisk.h
libfrog/convert.c
libfrog/convert.h
mkfs/proto.c
mkfs/xfs_mkfs.c

index 79500ed95a3894a31c047c70a5e809c2a8e5ecdf..1b9936ec768c663f1599aa55fb905d70ff59bf95 100644 (file)
@@ -42,9 +42,6 @@
 #define XFS_NOMULTIDISK_AGLOG          2       /* 4 AGs */
 #define XFS_MULTIDISK_AGCOUNT          (1 << XFS_MULTIDISK_AGLOG)
 
-extern long long cvtnum(unsigned int blksize, unsigned int sectsize,
-                       const char *str);
-
 /* proto.c */
 extern char *setup_proto (char *fname);
 extern void parse_proto (xfs_mount_t *mp, struct fsxattr *fsx, char **pp);
index 8d4d4077b3313b6c8061fb62f213bf0818c0f353..6b8ff30de24a34b58213a6eb7fd9ee52b56d5164 100644 (file)
@@ -182,9 +182,9 @@ cvt_u16(
 
 long long
 cvtnum(
-       size_t          blocksize,
-       size_t          sectorsize,
-       char            *s)
+       size_t          blksize,
+       size_t          sectsize,
+       const char      *s)
 {
        long long       i;
        char            *sp;
@@ -202,9 +202,13 @@ cvtnum(
        c = tolower(*sp);
        switch (c) {
        case 'b':
-               return i * blocksize;
+               if (!blksize)
+                       return -1LL;
+               return i * blksize;
        case 's':
-               return i * sectorsize;
+               if (!sectsize)
+                       return -1LL;
+               return i * sectsize;
        case 'k':
                return KILOBYTES(i);
        case 'm':
index 321540aa630c4be5cfaeb90e928fa3481d1f3e06..b307d31ce955f423a83e9cdca022f8f131b4ed17 100644 (file)
@@ -14,7 +14,7 @@ extern uint64_t       cvt_u64(char *s, int base);
 extern uint32_t        cvt_u32(char *s, int base);
 extern uint16_t        cvt_u16(char *s, int base);
 
-extern long long cvtnum(size_t blocksize, size_t sectorsize, char *s);
+extern long long cvtnum(size_t blocksize, size_t sectorsize, const char *s);
 extern void cvtstr(double value, char *str, size_t sz);
 extern unsigned long cvttime(char *s);
 
index ab01c8b0d178d9aa5a8ddb6def63cfacda12aa12..01b30c5f1b15ca9ada9b87410aed1c88f62856ca 100644 (file)
@@ -6,7 +6,7 @@
 
 #include "libxfs.h"
 #include <sys/stat.h>
-#include "xfs_multidisk.h"
+#include "libfrog/convert.h"
 
 /*
  * Prototypes for internal functions.
index 19a8e6d1d344213fde0e33eda64e42b8b7c56fa2..f14ce8db5a74906cefa0d2509762e2c971c7b812 100644 (file)
@@ -10,6 +10,7 @@
 #include "libxcmd.h"
 #include "libfrog/fsgeom.h"
 #include "libfrog/topology.h"
+#include "libfrog/convert.h"
 
 #define TERABYTES(count, blog) ((uint64_t)(count) << (40 - (blog)))
 #define GIGABYTES(count, blog) ((uint64_t)(count) << (30 - (blog)))
@@ -942,69 +943,6 @@ unknown(
        usage();
 }
 
-long long
-cvtnum(
-       unsigned int    blksize,
-       unsigned int    sectsize,
-       const char      *s)
-{
-       long long       i;
-       char            *sp;
-       int             c;
-
-       i = strtoll(s, &sp, 0);
-       if (i == 0 && sp == s)
-               return -1LL;
-       if (*sp == '\0')
-               return i;
-
-       if (sp[1] != '\0')
-               return -1LL;
-
-       if (*sp == 'b') {
-               if (!blksize) {
-                       fprintf(stderr,
-_("Blocksize must be provided prior to using 'b' suffix.\n"));
-                       usage();
-               } else {
-                       return i * blksize;
-               }
-       }
-       if (*sp == 's') {
-               if (!sectsize) {
-                       fprintf(stderr,
-_("Sectorsize must be specified prior to using 's' suffix.\n"));
-                       usage();
-               } else {
-                       return i * sectsize;
-               }
-       }
-
-       c = tolower(*sp);
-       switch (c) {
-       case 'e':
-               i *= 1024LL;
-               /* fall through */
-       case 'p':
-               i *= 1024LL;
-               /* fall through */
-       case 't':
-               i *= 1024LL;
-               /* fall through */
-       case 'g':
-               i *= 1024LL;
-               /* fall through */
-       case 'm':
-               i *= 1024LL;
-               /* fall through */
-       case 'k':
-               return i * 1024LL;
-       default:
-               break;
-       }
-       return -1LL;
-}
-
 static void
 check_device_type(
        const char      *name,
@@ -1374,9 +1312,13 @@ getnum(
         * convert it ourselves to guarantee there is no trailing garbage in the
         * number.
         */
-       if (sp->convert)
+       if (sp->convert) {
                c = cvtnum(blocksize, sectorsize, str);
-       else {
+               if (c == -1LL) {
+                       illegal_option(str, opts, index,
+                               _("Not a valid value or illegal suffix"));
+               }
+       } else {
                char            *str_end;
 
                c = strtoll(str, &str_end, 0);