Whamcloud - gitweb
Add new function in e2p for parsing the number of blocks on the command line
authorTheodore Ts'o <tytso@mit.edu>
Wed, 5 Jan 2005 08:01:06 +0000 (03:01 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 5 Jan 2005 08:01:06 +0000 (03:01 -0500)
for mke2fs and resize2fs, and use this function so that filesystem size
inputs to e2fsprogs command line programs are parsed consistently.

lib/e2p/ChangeLog
lib/e2p/Makefile.in
lib/e2p/e2p.h
lib/e2p/parse_num.c [new file with mode: 0644]
misc/ChangeLog
misc/mke2fs.c
resize/ChangeLog
resize/Makefile.in
resize/main.c

index 9cb7485..1df48da 100644 (file)
@@ -1,3 +1,9 @@
+2005-01-05  Theodore Ts'o  <tytso@mit.edu>
+
+       * parse_num.c (parse_num_blocks): New file which adds a standard
+               function for parsing the number of blocks by programs such
+               as mke2fs and resize2fs.
+
 2004-12-23  Theodore Ts'o  <tytso@mit.edu>
 
        * ls.c (list_super2): Print the s_reserved_gdt_blocks field if it
index 9d9bc26..4b11a61 100644 (file)
@@ -18,15 +18,15 @@ all::
 
 OBJS=          feature.o fgetflags.o fsetflags.o fgetversion.o fsetversion.o \
                getflags.o getversion.o hashstr.o iod.o ls.o mntopts.o \
-               pe.o pf.o ps.o setflags.o setversion.o uuid.o 
+               parse_num.o pe.o pf.o ps.o setflags.o setversion.o uuid.o 
 
 SRCS=          $(srcdir)/feature.c $(srcdir)/fgetflags.c \
                $(srcdir)/fsetflags.c $(srcdir)/fgetversion.c \
                $(srcdir)/fsetversion.c $(srcdir)/getflags.c \
                $(srcdir)/getversion.c $(srcdir)/hashstr.c $(srcdir)/iod.c \
-               $(srcdir)/ls.c $(srcdir)/mntopts.c $(srcdir)/pe.c \
-               $(srcdir)/pf.c $(srcdir)/ps.c $(srcdir)/setflags.c \
-               $(srcdir)/setversion.c $(srcdir)/uuid.c
+               $(srcdir)/ls.c $(srcdir)/mntopts.c $(srcdir)/parse_num.c \
+               $(srcdir)/pe.c $(srcdir)/pf.c $(srcdir)/ps.c \
+               $(srcdir)/setflags.c $(srcdir)/setversion.c $(srcdir)/uuid.c
 
 HFILES= e2p.h
 
index ebd30ab..048014f 100644 (file)
@@ -45,3 +45,5 @@ int e2p_string2hash(char *string);
 const char *e2p_mntopt2string(unsigned int mask);
 int e2p_string2mntopt(char *string, unsigned int *mask);
 int e2p_edit_mntopts(const char *str, __u32 *mntopts, __u32 ok);
+
+unsigned long parse_num_blocks(const char *arg, int log_block_size);
diff --git a/lib/e2p/parse_num.c b/lib/e2p/parse_num.c
new file mode 100644 (file)
index 0000000..3910e70
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * parse_num.c         - Parse the number of blocks 
+ *
+ * Copyright (C) 2004,2005  Theodore Ts'o <tytso@mit.edu>
+ * 
+ * This file can be redistributed under the terms of the GNU Library General
+ * Public License
+ */
+
+#include "e2p.h"
+
+#include <stdlib.h>
+
+unsigned long parse_num_blocks(const char *arg, int log_block_size)
+{
+       char *p;
+       unsigned long long num;
+
+       num = strtoull(arg, &p, 0);
+
+       if (p[0] && p[1]) 
+               return 0;
+
+       switch (*p) {           /* Using fall-through logic */
+       case 'T': case 't': 
+               num <<= 10;
+       case 'G': case 'g': 
+               num <<= 10;
+       case 'M': case 'm': 
+               num <<= 10;
+       case 'K': case 'k': 
+               num >>= log_block_size; 
+               break;
+       case 's': 
+               num >>= 1;
+               break;
+       case '\0':
+               break;
+       default: 
+               return 0;
+       }
+       return num;
+}
+
+#ifdef DEBUG
+#include <unistd.h>
+#include <stdio.h>
+
+main(int argc, char **argv)
+{
+       unsigned long num;
+       int log_block_size = 0;
+
+       if (argc != 2) {
+               fprintf(stderr, "Usage: %s arg\n", argv[0]);
+               exit(1);
+       }
+
+       num = parse_num_blocks(argv[1], log_block_size);
+
+       printf("Parsed number: %lu\n", num);
+       exit(0);
+}
+#endif
index e040632..7b288e7 100644 (file)
@@ -1,3 +1,9 @@
+2005-01-05  Theodore Ts'o  <tytso@mit.edu>
+
+       * mke2fs.c (PRS, parse_r_opts): Use parse_num_blocks() from the
+               e2p library to parse the number of blocks from the command
+               line.
+
 2004-12-23  Theodore Ts'o  <tytso@mit.edu>
 
        * dumpe2fs.c (list_desc): If reserved GDT blocks are present,
index 8bf5c18..2365fbb 100644 (file)
@@ -1,7 +1,8 @@
 /*
  * mke2fs.c - Make a ext2fs filesystem.
  * 
- * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
+ * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
+ *     2003, 2004, 2005 by Theodore Ts'o.
  *
  * %Begin-Header%
  * This file may be redistributed under the terms of the GNU Public
@@ -820,36 +821,13 @@ static void parse_r_opts(struct ext2_super_block *param, const char *opts)
                                continue;
                        }
 
-                       p = &arg[strlen(arg) - 1];
-
-                       switch(*p++) {
-                       case 'T':
-                       case 't': resize <<= 10; /* no break */
-                       case 'G':
-                       case 'g': resize <<= 10; /* no break */
-                       case 'M':
-                       case 'm': resize <<= 10; /* no break */
-                       case 'K':
-                       case 'k': resize >>= param->s_log_block_size -10; *p = 0; break;
-                       case 'b': resize >>= param->s_log_block_size - 9; *p = 0; break;
-                       case '0': break;
-                       case '1': break;
-                       case '2': break;
-                       case '3': break;
-                       case '4': break;
-                       case '5': break;
-                       case '6': break;
-                       case '7': break;
-                       case '8': break;
-                       case '9': break;
-                       default: r_usage++; continue;
-                       }
-
-                       resize *= strtoul(arg, NULL, 0);
+                       resize = parse_num_blocks(arg, 
+                                                 param->s_log_block_size);
 
                        if (resize == 0) {
-                               fprintf(stderr,
-                                       _("Invalid resize parameter.\n"));
+                               fprintf(stderr, 
+                                       _("Invalid resize parameter: %s\n"),
+                                       arg);
                                r_usage++;
                                continue;
                        }
@@ -978,7 +956,7 @@ static void PRS(int argc, char *argv[])
        }
 
        while ((c = getopt (argc, argv,
-                   "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
+                   "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
                switch (c) {
                case 'b':
                        blocksize = strtol(optarg, &tmp, 0);
@@ -1149,22 +1127,10 @@ static void PRS(int argc, char *argv[])
                default:
                        usage();
                }
-       if ((optind == argc) && !show_version_only)
-               usage();
-       device_name = argv[optind];
-       optind++;
-       if (optind < argc) {
-               unsigned long long tmp2 = strtoull(argv[optind++], &tmp, 0);
-
-               if ((*tmp) || (tmp2 > 0xfffffffful)) {
-                       com_err(program_name, 0, _("bad blocks count - %s"),
-                               argv[optind - 1]);
-                       exit(1);
-               }
-               param.s_blocks_count = tmp2;
        }
-       if (optind < argc)
+       if ((optind == argc) && !show_version_only)
                usage();
+       device_name = argv[optind++];
 
        if (!quiet || show_version_only)
                fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION, 
@@ -1229,6 +1195,18 @@ static void PRS(int argc, char *argv[])
                        "blocksizes greater than 4096 \n\tusing ext3."
                        "  Use -b 4096 if this is an issue for you.\n\n");
 
+       if (optind < argc) {
+               param.s_blocks_count = parse_num_blocks(argv[optind++], 
+                               param.s_log_block_size);
+               if (!param.s_blocks_count) {
+                       com_err(program_name, 0, _("bad blocks count - %s"),
+                               argv[optind - 1]);
+                       exit(1);
+               }
+       }
+       if (optind < argc)
+               usage();
+
        if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
                if (!fs_type)
                        fs_type = "journal";
index 838abaf..bed3fbf 100644 (file)
@@ -1,3 +1,8 @@
+2005-01-05  Theodore Ts'o  <tytso@mit.edu>
+
+       * main.c (main): Use parse_num_blocks() from the e2p library to
+               parse the number of blocks from the command line.
+
 2004-12-16  Theodore Ts'o  <tytso@mit.edu>
 
        * resize2fs.c (resize_fs): Call ext2fs_create_resize_inode to
index 4abefe0..0061850 100644 (file)
@@ -25,11 +25,12 @@ SRCS= $(srcdir)/extent.c \
        $(srcdir)/main.c \
        $(srcdir)/sim_progress.c
 
-LIBS= $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBINTL)
-DEPLIBS= $(LIBEXT2FS) $(LIBCOM_ERR)
+LIBS= $(LIBE2P) $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBINTL)
+DEPLIBS= $(LIBE2P) $(LIBEXT2FS) $(LIBCOM_ERR)
 
-STATIC_LIBS= $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) $(LIBINTL)
-STATIC_DEPLIBS= $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) 
+STATIC_LIBS= $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) \
+       $(LIBINTL)
+STATIC_DEPLIBS= $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) 
 
 .c.o:
        @echo " CC $<"
index 3d137ef..46c2d6c 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 1997, 1998 by Theodore Ts'o and
  *     PowerQuest, Inc.
  *
- * Copyright (C) 1999, 2000, 2001 by Theosore Ts'o
+ * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o
  * 
  * %Begin-Header%
  * This file may be redistributed under the terms of the GNU Public
@@ -21,6 +21,8 @@ extern int optind;
 #include <fcntl.h>
 #include <sys/stat.h>
 
+#include "e2p/e2p.h"
+
 #include "resize2fs.h"
 
 #include "../version.h"
@@ -106,23 +108,6 @@ static void check_mount(char *device)
        exit(1);
 }
 
-static int get_units(const char *s)
-{
-       if (strlen(s) != 1)
-               return -1;
-       switch(s[0]) {
-       case 's':
-               return 512;
-       case 'K':
-               return 1024;
-       case 'M':
-               return 1024*1024;
-       case 'G':
-               return 1024*1024*1024;
-       }
-       return -1;
-}
-
 int main (int argc, char ** argv)
 {
        errcode_t       retval;
@@ -134,9 +119,9 @@ int main (int argc, char ** argv)
        int             fd;
        blk_t           new_size = 0;
        blk_t           max_size = 0;
-       int             units = 0;
        io_manager      io_ptr;
        char            *tmp;
+       char            *new_size_str = 0;
        struct stat     st_buf;
        unsigned int    sys_page_size = 4096;
        long            sysval;
@@ -180,18 +165,8 @@ int main (int argc, char ** argv)
                usage(program_name);
 
        device_name = argv[optind++];
-       if (optind < argc) {
-               new_size = strtoul(argv[optind++], &tmp, 0);
-               if (*tmp) {
-                       units = get_units(tmp);
-                       if (units < 0) {
-                               com_err(program_name, 0, 
-                                       _("bad filesystem size - %s"),
-                                       argv[optind - 1]);
-                               exit(1);
-                       }
-               }
-       }
+       if (optind < argc)
+               new_size_str = argv[optind++];
        if (optind < argc)
                usage(program_name);
        
@@ -268,13 +243,15 @@ int main (int argc, char ** argv)
                        _("while trying to determine filesystem size"));
                exit(1);
        }
-       if (units) {
-               if ((unsigned) units < fs->blocksize)
-                       new_size = (new_size * units) / fs->blocksize;
-               else if ((unsigned) units > fs->blocksize)
-                       new_size = new_size * (units / fs->blocksize);
-       }
-       if (!new_size) {
+       if (new_size_str) {
+               new_size = parse_num_blocks(new_size_str, 
+                                           fs->super->s_log_block_size);
+               if (!new_size) {
+                       com_err(program_name, 0, _("bad filesystem size - %s"),
+                               new_size_str);
+                       exit(1);
+               }
+       } else {
                new_size = max_size;
                /* Round down to an even multiple of a pagesize */
                if (sys_page_size > fs->blocksize)