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 Library
8 * General Public License, version 2.
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 */
32errcode_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	blk64_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, "%llu", &blockno);
58		if (count <= 0)
59			continue;
60		/* Badblocks isn't going to be updated for 64bit */
61		if (blockno >> 32)
62			return EOVERFLOW;
63		if (fs &&
64		    ((blockno < fs->super->s_first_data_block) ||
65		     (blockno >= ext2fs_blocks_count(fs->super)))) {
66			if (invalid)
67				(invalid)(fs, blockno, buf, priv_data);
68			continue;
69		}
70		retval = ext2fs_badblocks_list_add(*bb_list, blockno);
71		if (retval)
72			return retval;
73	}
74	return 0;
75}
76
77struct compat_struct {
78	void (*invalid)(ext2_filsys, blk_t);
79};
80
81static void call_compat_invalid(ext2_filsys fs, blk_t blk,
82				char *badstr EXT2FS_ATTR((unused)),
83				void *priv_data)
84{
85	struct compat_struct *st;
86
87	st = (struct compat_struct *) priv_data;
88	if (st->invalid)
89		(st->invalid)(fs, blk);
90}
91
92
93/*
94 * Reads a list of bad blocks from  a FILE *
95 */
96errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
97			      ext2_badblocks_list *bb_list,
98			      void (*invalid)(ext2_filsys fs, blk_t blk))
99{
100	struct compat_struct st;
101
102	st.invalid = invalid;
103
104	return ext2fs_read_bb_FILE2(fs, f, bb_list, &st,
105				    call_compat_invalid);
106}
107
108
109