read_bb_file.c revision 31dbecd482405e0d3a67eb58e1a1c8cb9f2ad83e
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#if EXT2_FLAT_INCLUDES
27#include "ext2_fs.h"
28#else
29#include <linux/ext2_fs.h>
30#endif
31
32#include "ext2fs.h"
33
34/*
35 * Reads a list of bad blocks from  a FILE *
36 */
37errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
38			       ext2_badblocks_list *bb_list,
39			       void *private,
40			       void (*invalid)(ext2_filsys fs,
41					       blk_t blk,
42					       char *badstr,
43					       void *private))
44{
45	errcode_t	retval;
46	blk_t		blockno;
47	int		count;
48	char		buf[128];
49
50	if (fs)
51		EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
52
53	if (!*bb_list) {
54		retval = ext2fs_badblocks_list_create(bb_list, 10);
55		if (retval)
56			return retval;
57	}
58
59	while (!feof (f)) {
60		if (fgets(buf, sizeof(buf), f) == NULL)
61			break;
62		count = sscanf(buf, "%u", &blockno);
63		if (count <= 0)
64			continue;
65		if (fs &&
66		    ((blockno < fs->super->s_first_data_block) ||
67		    (blockno >= fs->super->s_blocks_count))) {
68			if (invalid)
69				(invalid)(fs, blockno, buf, private);
70			continue;
71		}
72		retval = ext2fs_badblocks_list_add(*bb_list, blockno);
73		if (retval)
74			return retval;
75	}
76	return 0;
77}
78
79static void call_compat_invalid(ext2_filsys fs, blk_t blk,
80				char *badstr, void *private)
81{
82	void (*invalid)(ext2_filsys, blk_t);
83
84	invalid = (void (*)(ext2_filsys, blk_t)) private;
85	if (invalid)
86		invalid(fs, blk);
87}
88
89
90/*
91 * Reads a list of bad blocks from  a FILE *
92 */
93errcode_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	return ext2fs_read_bb_FILE2(fs, f, bb_list, (void *) invalid,
98				    call_compat_invalid);
99}
100
101
102