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