• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #if HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 
27 #include "ext2_fs.h"
28 #include "ext2fs.h"
29 
30 /*
31  * Reads a list of bad blocks from  a FILE *
32  */
ext2fs_read_bb_FILE2(ext2_filsys fs,FILE * f,ext2_badblocks_list * bb_list,void * priv_data,void (* invalid)(ext2_filsys fs,blk_t blk,char * badstr,void * priv_data))33 errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
34 			       ext2_badblocks_list *bb_list,
35 			       void *priv_data,
36 			       void (*invalid)(ext2_filsys fs,
37 					       blk_t blk,
38 					       char *badstr,
39 					       void *priv_data))
40 {
41 	errcode_t	retval;
42 	blk64_t		blockno;
43 	int		count;
44 	char		buf[128];
45 
46 	if (fs)
47 		EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
48 
49 	if (!*bb_list) {
50 		retval = ext2fs_badblocks_list_create(bb_list, 10);
51 		if (retval)
52 			return retval;
53 	}
54 
55 	while (!feof (f)) {
56 		if (fgets(buf, sizeof(buf), f) == NULL)
57 			break;
58 		count = sscanf(buf, "%llu", &blockno);
59 		if (count <= 0)
60 			continue;
61 		/* Badblocks isn't going to be updated for 64bit */
62 		if (blockno >> 32)
63 			return EOVERFLOW;
64 		if (fs &&
65 		    ((blockno < fs->super->s_first_data_block) ||
66 		     (blockno >= ext2fs_blocks_count(fs->super)))) {
67 			if (invalid)
68 				(invalid)(fs, blockno, buf, priv_data);
69 			continue;
70 		}
71 		retval = ext2fs_badblocks_list_add(*bb_list, blockno);
72 		if (retval)
73 			return retval;
74 	}
75 	return 0;
76 }
77 
78 struct compat_struct {
79 	void (*invalid)(ext2_filsys, blk_t);
80 };
81 
call_compat_invalid(ext2_filsys fs,blk_t blk,char * badstr EXT2FS_ATTR ((unused)),void * priv_data)82 static void call_compat_invalid(ext2_filsys fs, blk_t blk,
83 				char *badstr EXT2FS_ATTR((unused)),
84 				void *priv_data)
85 {
86 	struct compat_struct *st;
87 
88 	st = (struct compat_struct *) priv_data;
89 	if (st->invalid)
90 		(st->invalid)(fs, blk);
91 }
92 
93 
94 /*
95  * Reads a list of bad blocks from  a FILE *
96  */
ext2fs_read_bb_FILE(ext2_filsys fs,FILE * f,ext2_badblocks_list * bb_list,void (* invalid)(ext2_filsys fs,blk_t blk))97 errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
98 			      ext2_badblocks_list *bb_list,
99 			      void (*invalid)(ext2_filsys fs, blk_t blk))
100 {
101 	struct compat_struct st;
102 
103 	st.invalid = invalid;
104 
105 	return ext2fs_read_bb_FILE2(fs, f, bb_list, &st,
106 				    call_compat_invalid);
107 }
108 
109 
110