From 57dca85467cf3fc61565e916a5f2e35db8020d88 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Tue, 4 Jul 2000 19:20:25 +0000 Subject: [PATCH] Many files: tst_badblocks.c: Update test program to test ext2fs_read_bb_FILE2 and ext2fs_write_FILE. write_bb_file.c (ext2fs_write_bb_FILE): New function which writes out bad blocks list to a file. read_bb_file.c (ext2fs_read_bb_FILE2): Add new function which changes the callback function to take two additional arguments; a private blind pointer supplied by the caller, and pointer to a char * containing a pointer to the invalid string. badblocks.c (ext2fs_badblocks_equal): Add new function which returns true if two badblocks list are equal. Makefile.in: Remove explicit link of -lc in the shared library. (It shouldn't be necessary, and is harmful in some cases). jump.funcs: dll/jump.funcs: Add new jumptable entries for ext2fs_write_bb_FILE, ext2fs_read_bb_FILE2, and ext2fs_badblocks_equal. --- lib/ext2fs/ChangeLog | 24 ++++++++++ lib/ext2fs/Makefile.in | 14 ++++-- lib/ext2fs/badblocks.c | 13 +++++ lib/ext2fs/dll/jump.funcs | 3 ++ lib/ext2fs/ext2fs.h | 9 ++++ lib/ext2fs/read_bb_file.c | 46 ++++++++++++++---- lib/ext2fs/tst_badblocks.c | 117 +++++++++++++++++++++++++++++++++++++-------- lib/ext2fs/write_bb_file.c | 39 +++++++++++++++ 8 files changed, 230 insertions(+), 35 deletions(-) create mode 100644 lib/ext2fs/write_bb_file.c diff --git a/lib/ext2fs/ChangeLog b/lib/ext2fs/ChangeLog index 2f07788..af3df72 100644 --- a/lib/ext2fs/ChangeLog +++ b/lib/ext2fs/ChangeLog @@ -1,3 +1,27 @@ +2000-07-04 Theodore Ts'o + + * dll/jump.funcs: Add new jumptable entries for + ext2fs_write_bb_FILE, ext2fs_read_bb_FILE2, and + ext2fs_badblocks_equal. + + * tst_badblocks.c: Update test program to test + ext2fs_read_bb_FILE2 and ext2fs_write_FILE. + + * write_bb_file.c (ext2fs_write_bb_FILE): New function which + writes out bad blocks list to a file. + + * read_bb_file.c (ext2fs_read_bb_FILE2): Add new function which + changes the callback function to take two additional + arguments; a private blind pointer supplied by the caller, + and pointer to a char * containing a pointer to the + invalid string. + + * badblocks.c (ext2fs_badblocks_equal): Add new function which + returns true if two badblocks list are equal. + + * Makefile.in: Remove explicit link of -lc in the shared library. + (It shouldn't be necessary, and is harmful in some cases). + 2000-06-10 Theodore Ts'o * getsize.c (main): Add debugging code under #ifdef DEBUG diff --git a/lib/ext2fs/Makefile.in b/lib/ext2fs/Makefile.in index b8e67fc..7854159 100644 --- a/lib/ext2fs/Makefile.in +++ b/lib/ext2fs/Makefile.in @@ -52,7 +52,8 @@ OBJS= ext2_err.o \ unix_io.o \ unlink.o \ valid_blk.o \ - version.o + version.o \ + write_bb_file.o SRCS= ext2_err.c \ $(srcdir)/alloc.c \ @@ -99,7 +100,8 @@ SRCS= ext2_err.c \ $(srcdir)/unix_io.c \ $(srcdir)/unlink.c \ $(srcdir)/valid_blk.c \ - $(srcdir)/version.c + $(srcdir)/version.c \ + $(srcdir)/write_bb_file.c HFILES= bitops.h ext2fs.h ext2_io.h @@ -121,7 +123,7 @@ ELF_SO_VERSION = 2 ELF_IMAGE = libext2fs ELF_MYDIR = ext2fs ELF_INSTALL_DIR = $(root_libdir) -ELF_OTHER_LIBS = -lc -L../.. -lcom_err +ELF_OTHER_LIBS = -L../.. -lcom_err BSDLIB_VERSION = 2.1 BSDLIB_IMAGE = libext2fs @@ -154,8 +156,10 @@ ext2_err.et: $(DEP_SUBSTITUTE) $(srcdir)/ext2_err.et.in ext2_err.c ext2_err.h: ext2_err.et $(COMPILE_ET) ext2_err.et -tst_badblocks: tst_badblocks.o badblocks.o freefs.o - $(CC) -o tst_badblocks tst_badblocks.o badblocks.o freefs.o $(LIBCOM_ERR) +tst_badblocks: tst_badblocks.o freefs.o \ + read_bb_file.o write_bb_file.o badblocks.o + $(CC) -o tst_badblocks tst_badblocks.o freefs.o \ + read_bb_file.o write_bb_file.o badblocks.o $(LIBCOM_ERR) tst_iscan: tst_iscan.o inode.o $(STATIC_LIBEXT2FS) $(CC) -o tst_iscan tst_iscan.o inode.o $(STATIC_LIBEXT2FS) \ diff --git a/lib/ext2fs/badblocks.c b/lib/ext2fs/badblocks.c index 8c09079..195c8be 100644 --- a/lib/ext2fs/badblocks.c +++ b/lib/ext2fs/badblocks.c @@ -226,3 +226,16 @@ void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter) iter->bb = 0; ext2fs_free_mem((void **) &iter); } + +int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2) +{ + EXT2_CHECK_MAGIC(bb1, EXT2_ET_MAGIC_BADBLOCKS_LIST); + EXT2_CHECK_MAGIC(bb2, EXT2_ET_MAGIC_BADBLOCKS_LIST); + + if (bb1->num != bb2->num) + return 0; + + if (memcmp(bb1->list, bb2->list, bb1->num * sizeof(blk_t)) != 0) + return 0; + return 1; +} diff --git a/lib/ext2fs/dll/jump.funcs b/lib/ext2fs/dll/jump.funcs index 092f225..cc7acaa 100644 --- a/lib/ext2fs/dll/jump.funcs +++ b/lib/ext2fs/dll/jump.funcs @@ -170,3 +170,6 @@ 00000000 T _ext2fs_get_mem libext2fs inline 00000000 T _ext2fs_free_mem libext2fs inline 00000000 T _ext2fs_resize_mem libext2fs inline +00000000 T _ext2fs_read_bb_FILE2 libext2fs read_bb_file +00000000 T _ext2fs_write_bb_FILE libext2fs write_bb_file +00000000 T _ext2fs_badblocks_equal libext2fs badblocks diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h index 7d0018d..4b1fe6c 100644 --- a/lib/ext2fs/ext2fs.h +++ b/lib/ext2fs/ext2fs.h @@ -543,6 +543,8 @@ extern int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, extern void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter); extern errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src, ext2_badblocks_list *dest); +extern int ext2fs_badblocks_equal(ext2_badblocks_list bb1, + ext2_badblocks_list bb2); /* bb_compat */ extern errcode_t badblocks_list_create(badblocks_list *ret, int size); @@ -804,6 +806,13 @@ extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list); /* read_bb_file.c */ +extern errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f, + ext2_badblocks_list *bb_list, + void *private, + void (*invalid)(ext2_filsys fs, + blk_t blk, + char *badstr, + void *private)); extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, ext2_badblocks_list *bb_list, void (*invalid)(ext2_filsys fs, diff --git a/lib/ext2fs/read_bb_file.c b/lib/ext2fs/read_bb_file.c index dc62257..20696a8 100644 --- a/lib/ext2fs/read_bb_file.c +++ b/lib/ext2fs/read_bb_file.c @@ -1,7 +1,7 @@ /* - * read_bb_file.c --- read a list of bad blocks for a FILE * + * read_bb_file.c --- read a list of bad blocks from a FILE * * - * Copyright (C) 1994, 1995 Theodore Ts'o. + * Copyright (C) 1994, 1995, 2000 Theodore Ts'o. * * %Begin-Header% * This file may be redistributed under the terms of the GNU Public @@ -34,16 +34,21 @@ /* * Reads a list of bad blocks from a FILE * */ -errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, - ext2_badblocks_list *bb_list, - void (*invalid)(ext2_filsys fs, blk_t blk)) +errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f, + ext2_badblocks_list *bb_list, + void *private, + void (*invalid)(ext2_filsys fs, + blk_t blk, + char *badstr, + void *private)) { errcode_t retval; blk_t blockno; int count; char buf[128]; - EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); + if (fs) + EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); if (!*bb_list) { retval = ext2fs_badblocks_list_create(bb_list, 10); @@ -57,10 +62,11 @@ errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, count = sscanf(buf, "%u", &blockno); if (count <= 0) continue; - if ((blockno < fs->super->s_first_data_block) || - (blockno >= fs->super->s_blocks_count)) { + if (fs && + ((blockno < fs->super->s_first_data_block) || + (blockno >= fs->super->s_blocks_count))) { if (invalid) - (invalid)(fs, blockno); + (invalid)(fs, blockno, buf, private); continue; } retval = ext2fs_badblocks_list_add(*bb_list, blockno); @@ -70,4 +76,26 @@ errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, return 0; } +static void call_compat_invalid(ext2_filsys fs, blk_t blk, + char *badstr, void *private) +{ + void (*invalid)(ext2_filsys fs, blk_t blk); + + invalid = (void (*)(ext2_filsys, blk_t)) private; + if (invalid) + invalid(fs, blk); +} + + +/* + * Reads a list of bad blocks from a FILE * + */ +errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, + ext2_badblocks_list *bb_list, + void (*invalid)(ext2_filsys fs, blk_t blk)) +{ + return ext2fs_read_bb_FILE2(fs, f, bb_list, invalid, + call_compat_invalid); +} + diff --git a/lib/ext2fs/tst_badblocks.c b/lib/ext2fs/tst_badblocks.c index effcca7..f463bc1 100644 --- a/lib/ext2fs/tst_badblocks.c +++ b/lib/ext2fs/tst_badblocks.c @@ -31,7 +31,7 @@ #include "ext2fs.h" blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 }; -blk_t test2[] = { 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 0 }; +blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1 }; blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 }; blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 }; blk_t test4a[] = { @@ -123,45 +123,120 @@ static void validate_test_seq(badblocks_list bb, blk_t *vec) } } +int file_test(badblocks_list bb) +{ + char tmp_filename[20] = "#testXXXXXX"; + badblocks_list new_bb = 0; + errcode_t retval; + FILE *f; + + mktemp(tmp_filename); + + unlink(tmp_filename); + f = fopen(tmp_filename, "w"); + if (!f) { + fprintf(stderr, "Error opening temp file %s: %s\n", + tmp_filename, error_message(errno)); + return 1; + } + retval = ext2fs_write_bb_FILE(bb, 0, f); + if (retval) { + com_err("file_test", retval, "while writing bad blocks"); + return 1; + } + fclose(f); + + f = fopen(tmp_filename, "r"); + if (!f) { + fprintf(stderr, "Error re-opening temp file %s: %s\n", + tmp_filename, error_message(errno)); + return 1; + } + retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0); + if (retval) { + com_err("file_test", retval, "while reading bad blocks"); + return 1; + } + fclose(f); + + if (ext2fs_badblocks_equal(bb, new_bb)) { + printf("Block bitmap matched after reading and writing.\n"); + } else { + printf("Block bitmap NOT matched.\n"); + test_fail++; + } + +} + + int main(int argc, char *argv) { - badblocks_list bb; + badblocks_list bb1, bb2, bb3, bb4; + int equal; errcode_t retval; + bb1 = bb2 = bb3 = bb4 = 0; + printf("test1: "); - retval = create_test_list(test1, &bb); - if (retval == 0) { - print_list(bb, 1); - ext2fs_badblocks_list_free(bb); - } + retval = create_test_list(test1, &bb1); + if (retval == 0) + print_list(bb1, 1); printf("\n"); printf("test2: "); - retval = create_test_list(test2, &bb); - if (retval == 0) { - print_list(bb, 1); - ext2fs_badblocks_list_free(bb); - } + retval = create_test_list(test2, &bb2); + if (retval == 0) + print_list(bb2, 1); printf("\n"); printf("test3: "); - retval = create_test_list(test3, &bb); - if (retval == 0) { - print_list(bb, 1); - ext2fs_badblocks_list_free(bb); - } + retval = create_test_list(test3, &bb3); + if (retval == 0) + print_list(bb3, 1); printf("\n"); printf("test4: "); - retval = create_test_list(test4, &bb); + retval = create_test_list(test4, &bb4); if (retval == 0) { - print_list(bb, 0); + print_list(bb4, 0); printf("\n"); - validate_test_seq(bb, test4a); - ext2fs_badblocks_list_free(bb); + validate_test_seq(bb4, test4a); } printf("\n"); + + if (bb1 && bb2 && bb3 && bb4) { + printf("Comparison tests:\n"); + equal = ext2fs_badblocks_equal(bb1, bb2); + printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT "); + if (equal) + test_fail++; + + equal = ext2fs_badblocks_equal(bb1, bb3); + printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT "); + if (!equal) + test_fail++; + + equal = ext2fs_badblocks_equal(bb1, bb4); + printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT "); + if (equal) + test_fail++; + printf("\n"); + } + if (test_fail == 0) printf("ext2fs library badblocks tests checks out OK!\n"); + + file_test(bb4); + + if (bb1) + ext2fs_badblocks_list_free(bb1); + if (bb2) + ext2fs_badblocks_list_free(bb2); + if (bb3) + ext2fs_badblocks_list_free(bb3); + if (bb4) + ext2fs_badblocks_list_free(bb4); + return test_fail; + } diff --git a/lib/ext2fs/write_bb_file.c b/lib/ext2fs/write_bb_file.c new file mode 100644 index 0000000..5550765 --- /dev/null +++ b/lib/ext2fs/write_bb_file.c @@ -0,0 +1,39 @@ +/* + * write_bb_file.c --- write a list of bad blocks to a FILE * + * + * Copyright (C) 1994, 1995 Theodore Ts'o. + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Public + * License. + * %End-Header% + */ + +#include + +#if EXT2_FLAT_INCLUDES +#include "ext2_fs.h" +#else +#include +#endif + +#include "ext2fs.h" + +errcode_t ext2fs_write_bb_FILE(ext2_badblocks_list bb_list, + unsigned int flags, + FILE *f) +{ + badblocks_iterate bb_iter; + blk_t blk; + errcode_t retval; + + retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); + if (retval) + return retval; + + while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) { + fprintf(f, "%d\n", blk); + } + ext2fs_badblocks_list_iterate_end(bb_iter); + return 0; +} -- 1.8.3.1