From 55f4cbd96e0029f0ff67c4913192d87bf52fd149 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Wed, 5 Jan 2005 03:01:06 -0500 Subject: [PATCH] Add new function in e2p for parsing the number of blocks on the command line for mke2fs and resize2fs, and use this function so that filesystem size inputs to e2fsprogs command line programs are parsed consistently. --- lib/e2p/ChangeLog | 6 +++++ lib/e2p/Makefile.in | 8 +++---- lib/e2p/e2p.h | 2 ++ lib/e2p/parse_num.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ misc/ChangeLog | 6 +++++ misc/mke2fs.c | 66 ++++++++++++++++++----------------------------------- resize/ChangeLog | 5 ++++ resize/Makefile.in | 9 ++++---- resize/main.c | 53 ++++++++++++------------------------------ 9 files changed, 129 insertions(+), 90 deletions(-) create mode 100644 lib/e2p/parse_num.c diff --git a/lib/e2p/ChangeLog b/lib/e2p/ChangeLog index 9cb7485..1df48da 100644 --- a/lib/e2p/ChangeLog +++ b/lib/e2p/ChangeLog @@ -1,3 +1,9 @@ +2005-01-05 Theodore Ts'o + + * 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 * ls.c (list_super2): Print the s_reserved_gdt_blocks field if it diff --git a/lib/e2p/Makefile.in b/lib/e2p/Makefile.in index 9d9bc26..4b11a61 100644 --- a/lib/e2p/Makefile.in +++ b/lib/e2p/Makefile.in @@ -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 diff --git a/lib/e2p/e2p.h b/lib/e2p/e2p.h index ebd30ab..048014f 100644 --- a/lib/e2p/e2p.h +++ b/lib/e2p/e2p.h @@ -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 index 0000000..3910e70 --- /dev/null +++ b/lib/e2p/parse_num.c @@ -0,0 +1,64 @@ +/* + * parse_num.c - Parse the number of blocks + * + * Copyright (C) 2004,2005 Theodore Ts'o + * + * This file can be redistributed under the terms of the GNU Library General + * Public License + */ + +#include "e2p.h" + +#include + +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 +#include + +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 diff --git a/misc/ChangeLog b/misc/ChangeLog index e040632..7b288e7 100644 --- a/misc/ChangeLog +++ b/misc/ChangeLog @@ -1,3 +1,9 @@ +2005-01-05 Theodore Ts'o + + * 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 * dumpe2fs.c (list_desc): If reserved GDT blocks are present, diff --git a/misc/mke2fs.c b/misc/mke2fs.c index 8bf5c18..2365fbb 100644 --- a/misc/mke2fs.c +++ b/misc/mke2fs.c @@ -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"; diff --git a/resize/ChangeLog b/resize/ChangeLog index 838abaf..bed3fbf 100644 --- a/resize/ChangeLog +++ b/resize/ChangeLog @@ -1,3 +1,8 @@ +2005-01-05 Theodore Ts'o + + * 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 * resize2fs.c (resize_fs): Call ext2fs_create_resize_inode to diff --git a/resize/Makefile.in b/resize/Makefile.in index 4abefe0..0061850 100644 --- a/resize/Makefile.in +++ b/resize/Makefile.in @@ -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 $<" diff --git a/resize/main.c b/resize/main.c index 3d137ef..46c2d6c 100644 --- a/resize/main.c +++ b/resize/main.c @@ -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 #include +#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) -- 1.8.3.1