Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / read_bb_file.c
1 /*
2  * read_bb_file.c --- read a list of bad blocks for a FILE *
3  *
4  * Copyright (C) 1994, 1995 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 #include <unistd.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <time.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20
21 #include <linux/ext2_fs.h>
22
23 #include "ext2fs.h"
24
25 /*
26  * Reads a list of bad blocks from  a FILE *
27  */
28 errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, 
29                               ext2_badblocks_list *bb_list,
30                               void (*invalid)(ext2_filsys fs, blk_t blk))
31 {
32         errcode_t       retval;
33         blk_t           blockno;
34         int             count;
35         char            buf[128];
36
37         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
38
39         if (!*bb_list) {
40                 retval = ext2fs_badblocks_list_create(bb_list, 10);
41                 if (retval)
42                         return retval;
43         }
44
45         while (!feof (f)) {
46                 if (fgets(buf, sizeof(buf), f) == NULL)
47                         break;
48                 count = sscanf(buf, "%u", &blockno);
49                 if (count <= 0)
50                         continue;
51                 if ((blockno < fs->super->s_first_data_block) ||
52                     (blockno >= fs->super->s_blocks_count)) {
53                         if (invalid)
54                                 (invalid)(fs, blockno);
55                         continue;
56                 }
57                 retval = ext2fs_badblocks_list_add(*bb_list, blockno);
58                 if (retval)
59                         return retval;
60         }
61         return 0;
62 }
63
64