read_bb_file.c 2.1 KB
Newer Older
Theodore Ts'o's avatar
Theodore Ts'o committed
1
/*
Theodore Ts'o's avatar
Theodore Ts'o committed
2
 * read_bb_file.c --- read a list of bad blocks from a FILE *
Theodore Ts'o's avatar
Theodore Ts'o committed
3
 *
Theodore Ts'o's avatar
Theodore Ts'o committed
4
 * Copyright (C) 1994, 1995, 2000 Theodore Ts'o.
Theodore Ts'o's avatar
Theodore Ts'o committed
5 6
 *
 * %Begin-Header%
7 8
 * This file may be redistributed under the terms of the GNU Library
 * General Public License, version 2.
Theodore Ts'o's avatar
Theodore Ts'o committed
9
 * %End-Header%
Theodore Ts'o's avatar
Theodore Ts'o committed
10 11 12 13
 */

#include <stdio.h>
#include <string.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
14
#if HAVE_UNISTD_H
Theodore Ts'o's avatar
Theodore Ts'o committed
15
#include <unistd.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
16
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
17 18
#include <fcntl.h>
#include <time.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
19
#if HAVE_SYS_STAT_H
Theodore Ts'o's avatar
Theodore Ts'o committed
20
#include <sys/stat.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
21 22
#endif
#if HAVE_SYS_TYPES_H
Theodore Ts'o's avatar
Theodore Ts'o committed
23
#include <sys/types.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
24
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
25

Theodore Ts'o's avatar
Theodore Ts'o committed
26
#include "ext2_fs.h"
Theodore Ts'o's avatar
Theodore Ts'o committed
27 28 29 30 31
#include "ext2fs.h"

/*
 * Reads a list of bad blocks from  a FILE *
 */
32
errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
Theodore Ts'o's avatar
Theodore Ts'o committed
33
			       ext2_badblocks_list *bb_list,
34
			       void *priv_data,
Theodore Ts'o's avatar
Theodore Ts'o committed
35 36 37
			       void (*invalid)(ext2_filsys fs,
					       blk_t blk,
					       char *badstr,
38
					       void *priv_data))
Theodore Ts'o's avatar
Theodore Ts'o committed
39 40
{
	errcode_t	retval;
41
	blk_t		blockno;
Theodore Ts'o's avatar
Theodore Ts'o committed
42
	int		count;
Theodore Ts'o's avatar
Theodore Ts'o committed
43 44
	char		buf[128];

Theodore Ts'o's avatar
Theodore Ts'o committed
45 46
	if (fs)
		EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'o's avatar
Theodore Ts'o committed
47 48

	if (!*bb_list) {
Theodore Ts'o's avatar
Theodore Ts'o committed
49
		retval = ext2fs_badblocks_list_create(bb_list, 10);
Theodore Ts'o's avatar
Theodore Ts'o committed
50 51 52 53 54
		if (retval)
			return retval;
	}

	while (!feof (f)) {
Theodore Ts'o's avatar
Theodore Ts'o committed
55
		if (fgets(buf, sizeof(buf), f) == NULL)
Theodore Ts'o's avatar
Theodore Ts'o committed
56
			break;
57
		count = sscanf(buf, "%u", &blockno);
Theodore Ts'o's avatar
Theodore Ts'o committed
58 59
		if (count <= 0)
			continue;
Theodore Ts'o's avatar
Theodore Ts'o committed
60 61
		if (fs &&
		    ((blockno < fs->super->s_first_data_block) ||
62
		    (blockno >= fs->super->s_blocks_count))) {
Theodore Ts'o's avatar
Theodore Ts'o committed
63
			if (invalid)
64
				(invalid)(fs, blockno, buf, priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
65 66
			continue;
		}
Theodore Ts'o's avatar
Theodore Ts'o committed
67
		retval = ext2fs_badblocks_list_add(*bb_list, blockno);
Theodore Ts'o's avatar
Theodore Ts'o committed
68 69
		if (retval)
			return retval;
Theodore Ts'o's avatar
Theodore Ts'o committed
70 71 72 73
	}
	return 0;
}

74 75 76 77
struct compat_struct {
	void (*invalid)(ext2_filsys, blk_t);
};

Theodore Ts'o's avatar
Theodore Ts'o committed
78
static void call_compat_invalid(ext2_filsys fs, blk_t blk,
79
				char *badstr EXT2FS_ATTR((unused)),
Theodore Ts'o's avatar
Theodore Ts'o committed
80
				void *priv_data)
Theodore Ts'o's avatar
Theodore Ts'o committed
81
{
82
	struct compat_struct *st;
Theodore Ts'o's avatar
Theodore Ts'o committed
83

84 85 86
	st = (struct compat_struct *) priv_data;
	if (st->invalid)
		(st->invalid)(fs, blk);
Theodore Ts'o's avatar
Theodore Ts'o committed
87 88 89 90 91 92
}


/*
 * Reads a list of bad blocks from  a FILE *
 */
93
errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
Theodore Ts'o's avatar
Theodore Ts'o committed
94 95 96
			      ext2_badblocks_list *bb_list,
			      void (*invalid)(ext2_filsys fs, blk_t blk))
{
97 98 99 100 101
	struct compat_struct st;

	st.invalid = invalid;

	return ext2fs_read_bb_FILE2(fs, f, bb_list, &st,
Theodore Ts'o's avatar
Theodore Ts'o committed
102 103 104
				    call_compat_invalid);
}

Theodore Ts'o's avatar
Theodore Ts'o committed
105