blob: 5e8cd43659426d2ad52dea1d09740269c6e4e36e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/*
* badblocks.c --- routines to manipulate the bad block structure
*
* Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
* under the terms of the GNU Public License.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/fs.h>
#include <linux/ext2_fs.h>
#include "ext2fs.h"
/*
* This procedure create an empty badblocks list.
*/
errcode_t badblocks_list_create(badblocks_list *ret, int size)
{
badblocks_list bb;
bb = malloc(sizeof(struct struct_badblocks_list));
if (!bb)
return ENOMEM;
memset(bb, 0, sizeof(struct struct_badblocks_list));
bb->size = size ? size : 10;
bb->list = malloc(bb->size * sizeof(blk_t));
if (!bb->list) {
free(bb);
return ENOMEM;
}
*ret = bb;
return 0;
}
/*
* This procedure frees a badblocks list.
*/
void badblocks_list_free(badblocks_list bb)
{
if (bb->list)
free(bb->list);
bb->list = 0;
free(bb);
}
/*
* This procedure adds a block to a badblocks list.
*/
errcode_t badblocks_list_add(badblocks_list bb, blk_t blk)
{
int i;
for (i=0; i < bb->num; i++)
if (bb->list[i] == blk)
return 0;
if (bb->num >= bb->size) {
bb->size += 10;
bb->list = realloc(bb->list, bb->size * sizeof(blk_t));
if (!bb->list) {
bb->size = 0;
bb->num = 0;
return ENOMEM;
}
}
bb->list[bb->num++] = blk;
return 0;
}
/*
* This procedure tests to see if a particular block is on a badblocks
* list.
*/
int badblocks_list_test(badblocks_list bb, blk_t blk)
{
int i;
for (i=0; i < bb->num; i++)
if (bb->list[i] == blk)
return 1;
return 0;
}
errcode_t badblocks_list_iterate_begin(badblocks_list bb,
badblocks_iterate *ret)
{
badblocks_iterate iter;
iter = malloc(sizeof(struct struct_badblocks_iterate));
if (!iter)
return ENOMEM;
iter->bb = bb;
iter->ptr = 0;
*ret = iter;
return 0;
}
int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk)
{
badblocks_list bb = iter->bb;
if (iter->ptr < bb->num) {
*blk = bb->list[iter->ptr++];
return 1;
}
*blk = 0;
return 0;
}
void badblocks_list_iterate_end(badblocks_iterate iter)
{
iter->bb = 0;
free(iter);
}
|