1 /* 2 * read_bb_file.c --- read a list of bad blocks from a FILE * 3 * 4 * Copyright (C) 1994, 1995, 2000 Theodore Ts'o. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Library 8 * General Public License, version 2. 9 * %End-Header% 10 */ 11 12 #include <stdio.h> 13 #include <string.h> 14 #if HAVE_UNISTD_H 15 #include <unistd.h> 16 #endif 17 #include <fcntl.h> 18 #include <time.h> 19 #if HAVE_SYS_STAT_H 20 #include <sys/stat.h> 21 #endif 22 #if HAVE_SYS_TYPES_H 23 #include <sys/types.h> 24 #endif 25 26 #include "ext2_fs.h" 27 #include "ext2fs.h" 28 29 /* 30 * Reads a list of bad blocks from a FILE * 31 */ 32 errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f, 33 ext2_badblocks_list *bb_list, 34 void *priv_data, 35 void (*invalid)(ext2_filsys fs, 36 blk_t blk, 37 char *badstr, 38 void *priv_data)) 39 { 40 errcode_t retval; 41 blk_t blockno; 42 int count; 43 char buf[128]; 44 45 if (fs) 46 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 47 48 if (!*bb_list) { 49 retval = ext2fs_badblocks_list_create(bb_list, 10); 50 if (retval) 51 return retval; 52 } 53 54 while (!feof (f)) { 55 if (fgets(buf, sizeof(buf), f) == NULL) 56 break; 57 count = sscanf(buf, "%u", &blockno); 58 if (count <= 0) 59 continue; 60 if (fs && 61 ((blockno < fs->super->s_first_data_block) || 62 (blockno >= fs->super->s_blocks_count))) { 63 if (invalid) 64 (invalid)(fs, blockno, buf, priv_data); 65 continue; 66 } 67 retval = ext2fs_badblocks_list_add(*bb_list, blockno); 68 if (retval) 69 return retval; 70 } 71 return 0; 72 } 73 74 struct compat_struct { 75 void (*invalid)(ext2_filsys, blk_t); 76 }; 77 78 static void call_compat_invalid(ext2_filsys fs, blk_t blk, 79 char *badstr EXT2FS_ATTR((unused)), 80 void *priv_data) 81 { 82 struct compat_struct *st; 83 84 st = (struct compat_struct *) priv_data; 85 if (st->invalid) 86 (st->invalid)(fs, blk); 87 } 88 89 90 /* 91 * Reads a list of bad blocks from a FILE * 92 */ 93 errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, 94 ext2_badblocks_list *bb_list, 95 void (*invalid)(ext2_filsys fs, blk_t blk)) 96 { 97 struct compat_struct st; 98 99 st.invalid = invalid; 100 101 return ext2fs_read_bb_FILE2(fs, f, bb_list, &st, 102 call_compat_invalid); 103 } 104 105 106