blob: 7d7bb7aa5306b19a7fa6ea7eb52b472d0ef57a30 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
Theodore Ts'o57dca852000-07-04 19:20:25 +00002 * read_bb_file.c --- read a list of bad blocks from a FILE *
Theodore Ts'o3839e651997-04-26 13:21:57 +00003 *
Theodore Ts'o57dca852000-07-04 19:20:25 +00004 * Copyright (C) 1994, 1995, 2000 Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00005 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000013#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <fcntl.h>
19#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000020#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000022#endif
23#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000026
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000027#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include "ext2fs.h"
29
30/*
31 * Reads a list of bad blocks from a FILE *
32 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -040033errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
Theodore Ts'o57dca852000-07-04 19:20:25 +000034 ext2_badblocks_list *bb_list,
Theodore Ts'o50cd7e02002-07-14 16:00:50 -040035 void *priv_data,
Theodore Ts'o57dca852000-07-04 19:20:25 +000036 void (*invalid)(ext2_filsys fs,
37 blk_t blk,
38 char *badstr,
Theodore Ts'o50cd7e02002-07-14 16:00:50 -040039 void *priv_data))
Theodore Ts'o3839e651997-04-26 13:21:57 +000040{
41 errcode_t retval;
42 blk_t blockno;
43 int count;
Theodore Ts'of3db3561997-04-26 13:34:30 +000044 char buf[128];
45
Theodore Ts'o57dca852000-07-04 19:20:25 +000046 if (fs)
47 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'o3839e651997-04-26 13:21:57 +000048
49 if (!*bb_list) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000050 retval = ext2fs_badblocks_list_create(bb_list, 10);
Theodore Ts'o3839e651997-04-26 13:21:57 +000051 if (retval)
52 return retval;
53 }
54
55 while (!feof (f)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +000056 if (fgets(buf, sizeof(buf), f) == NULL)
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 break;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000058 count = sscanf(buf, "%u", &blockno);
Theodore Ts'of3db3561997-04-26 13:34:30 +000059 if (count <= 0)
60 continue;
Theodore Ts'o57dca852000-07-04 19:20:25 +000061 if (fs &&
62 ((blockno < fs->super->s_first_data_block) ||
Valerie Aurora Henson6d8b37f2010-06-13 11:00:00 -040063 (blockno >= ext2fs_blocks_count(fs->super)))) {
Theodore Ts'o3839e651997-04-26 13:21:57 +000064 if (invalid)
Theodore Ts'o50cd7e02002-07-14 16:00:50 -040065 (invalid)(fs, blockno, buf, priv_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000066 continue;
67 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000068 retval = ext2fs_badblocks_list_add(*bb_list, blockno);
Theodore Ts'of3db3561997-04-26 13:34:30 +000069 if (retval)
70 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000071 }
72 return 0;
73}
74
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050075struct compat_struct {
76 void (*invalid)(ext2_filsys, blk_t);
77};
78
Theodore Ts'o57dca852000-07-04 19:20:25 +000079static void call_compat_invalid(ext2_filsys fs, blk_t blk,
Theodore Ts'oefc6f622008-08-27 23:07:54 -040080 char *badstr EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -050081 void *priv_data)
Theodore Ts'o57dca852000-07-04 19:20:25 +000082{
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050083 struct compat_struct *st;
Theodore Ts'o57dca852000-07-04 19:20:25 +000084
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050085 st = (struct compat_struct *) priv_data;
86 if (st->invalid)
87 (st->invalid)(fs, blk);
Theodore Ts'o57dca852000-07-04 19:20:25 +000088}
89
90
91/*
92 * Reads a list of bad blocks from a FILE *
93 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -040094errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
Theodore Ts'o57dca852000-07-04 19:20:25 +000095 ext2_badblocks_list *bb_list,
96 void (*invalid)(ext2_filsys fs, blk_t blk))
97{
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050098 struct compat_struct st;
99
100 st.invalid = invalid;
101
102 return ext2fs_read_bb_FILE2(fs, f, bb_list, &st,
Theodore Ts'o57dca852000-07-04 19:20:25 +0000103 call_compat_invalid);
104}
105
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106