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