Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame^] | 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 Public |
| 8 | * License. |
| 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 | static void call_compat_invalid(ext2_filsys fs, blk_t blk, |
| 75 | char *badstr EXT2FS_ATTR((unused)), |
| 76 | void *priv_data) |
| 77 | { |
| 78 | void (*invalid)(ext2_filsys, blk_t); |
| 79 | |
| 80 | invalid = (void (*)(ext2_filsys, blk_t)) priv_data; |
| 81 | if (invalid) |
| 82 | invalid(fs, blk); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | * Reads a list of bad blocks from a FILE * |
| 88 | */ |
| 89 | errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, |
| 90 | ext2_badblocks_list *bb_list, |
| 91 | void (*invalid)(ext2_filsys fs, blk_t blk)) |
| 92 | { |
| 93 | return ext2fs_read_bb_FILE2(fs, f, bb_list, (void *) invalid, |
| 94 | call_compat_invalid); |
| 95 | } |
| 96 | |
| 97 | |