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