Whamcloud - gitweb
libext2fs/ismounted.c: check device id in advance to skip false device names
[tools/e2fsprogs.git] / lib / ext2fs / badblocks.c
index 4e94e8c..0f23983 100644 (file)
@@ -1,14 +1,15 @@
 /*
  * badblocks.c --- routines to manipulate the bad block structure
- * 
+ *
  * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
 /*
  * Helper function for making a badblocks list
  */
-static errcode_t make_badblocks_list(int size, int num, blk_t *list,
-                                    ext2_badblocks_list *ret)
+static errcode_t make_u32_list(int size, int num, __u32 *list,
+                              ext2_u32_list *ret)
 {
-       ext2_badblocks_list     bb;
-       errcode_t               retval;
-       
-       retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_list),
-                               (void **) &bb);
+       ext2_u32_list   bb;
+       errcode_t       retval;
+
+       retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_list), &bb);
        if (retval)
                return retval;
-       memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
+       memset(bb, 0, sizeof(struct ext2_struct_u32_list));
        bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
        bb->size = size ? size : 10;
        bb->num = num;
-       retval = ext2fs_get_mem(bb->size * sizeof(blk_t), (void **) &bb->list);
-       if (!bb->list) {
-               ext2fs_free_mem((void **) &bb);
+       retval = ext2fs_get_array(bb->size, sizeof(blk_t), &bb->list);
+       if (retval) {
+               ext2fs_free_mem(&bb);
                return retval;
        }
        if (list)
@@ -55,32 +55,45 @@ static errcode_t make_badblocks_list(int size, int num, blk_t *list,
        *ret = bb;
        return 0;
 }
-       
+
+
+/*
+ * This procedure creates an empty u32 list.
+ */
+errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size)
+{
+       return make_u32_list(size, 0, 0, ret);
+}
 
 /*
  * This procedure creates an empty badblocks list.
  */
 errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
 {
-       return make_badblocks_list(size, 0, 0, ret);
+       return make_u32_list(size, 0, 0, (ext2_badblocks_list *) ret);
 }
 
+
 /*
  * This procedure copies a badblocks list
  */
-errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
-                               ext2_badblocks_list *dest)
+errcode_t ext2fs_u32_copy(ext2_u32_list src, ext2_u32_list *dest)
 {
        errcode_t       retval;
-       
-       retval = make_badblocks_list(src->size, src->num, src->list,
-                                    dest);
+
+       retval = make_u32_list(src->size, src->num, src->list, dest);
        if (retval)
                return retval;
        (*dest)->badblocks_flags = src->badblocks_flags;
        return 0;
 }
 
+errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
+                               ext2_badblocks_list *dest)
+{
+       return ext2fs_u32_copy((ext2_u32_list) src,
+                              (ext2_u32_list *) dest);
+}
 
 /*
  * This procedure frees a badblocks list.
@@ -92,7 +105,7 @@ errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
 /*
  * This procedure adds a block to a badblocks list.
  */
-errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
+errcode_t ext2fs_u32_list_add(ext2_u32_list bb, __u32 blk)
 {
        errcode_t       retval;
        int             i, j;
@@ -101,10 +114,10 @@ errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
        EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
 
        if (bb->num >= bb->size) {
-               old_size = bb->size * sizeof(blk_t);
+               old_size = bb->size * sizeof(__u32);
                bb->size += 100;
-               retval = ext2fs_resize_mem(old_size, bb->size * sizeof(blk_t),
-                                          (void **) &bb->list);
+               retval = ext2fs_resize_mem(old_size, bb->size * sizeof(__u32),
+                                          &bb->list);
                if (retval) {
                        bb->size -= 100;
                        return retval;
@@ -138,51 +151,98 @@ errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
        return 0;
 }
 
+errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
+{
+       return ext2fs_u32_list_add((ext2_u32_list) bb, (__u32) blk);
+}
+
 /*
- * This procedure tests to see if a particular block is on a badblocks
+ * This procedure finds a particular block is on a badblocks
  * list.
  */
-int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
+int ext2fs_u32_list_find(ext2_u32_list bb, __u32 blk)
 {
        int     low, high, mid;
 
        if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
-               return 0;
+               return -1;
 
        if (bb->num == 0)
-               return 0;
+               return -1;
 
        low = 0;
        high = bb->num-1;
        if (blk == bb->list[low])
-               return 1;
+               return low;
        if (blk == bb->list[high])
-               return 1;
+               return high;
 
        while (low < high) {
-               mid = (low+high)/2;
+               mid = ((unsigned)low + (unsigned)high)/2;
                if (mid == low || mid == high)
                        break;
                if (blk == bb->list[mid])
-                       return 1;
+                       return mid;
                if (blk < bb->list[mid])
                        high = mid;
                else
                        low = mid;
        }
+       return -1;
+}
+
+/*
+ * This procedure tests to see if a particular block is on a badblocks
+ * list.
+ */
+int ext2fs_u32_list_test(ext2_u32_list bb, __u32 blk)
+{
+       if (ext2fs_u32_list_find(bb, blk) < 0)
+               return 0;
+       else
+               return 1;
+}
+
+int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
+{
+       return ext2fs_u32_list_test((ext2_u32_list) bb, (__u32) blk);
+}
+
+
+/*
+ * Remove a block from the badblock list
+ */
+int ext2fs_u32_list_del(ext2_u32_list bb, __u32 blk)
+{
+       int     remloc, i;
+
+       if (bb->num == 0)
+               return -1;
+
+       remloc = ext2fs_u32_list_find(bb, blk);
+       if (remloc < 0)
+               return -1;
+
+       for (i = remloc ; i < bb->num-1; i++)
+               bb->list[i] = bb->list[i+1];
+       bb->num--;
        return 0;
 }
 
-errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
-                                             ext2_badblocks_iterate *ret)
+void ext2fs_badblocks_list_del(ext2_u32_list bb, __u32 blk)
+{
+       ext2fs_u32_list_del(bb, blk);
+}
+
+errcode_t ext2fs_u32_list_iterate_begin(ext2_u32_list bb,
+                                       ext2_u32_iterate *ret)
 {
-       ext2_badblocks_iterate iter;
+       ext2_u32_iterate iter;
        errcode_t               retval;
 
        EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
 
-       retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_iterate),
-                             (void **) &iter);
+       retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_iterate), &iter);
        if (retval)
                return retval;
 
@@ -193,9 +253,17 @@ errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
        return 0;
 }
 
-int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
+errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
+                                             ext2_badblocks_iterate *ret)
+{
+       return ext2fs_u32_list_iterate_begin((ext2_u32_list) bb,
+                                             (ext2_u32_iterate *) ret);
+}
+
+
+int ext2fs_u32_list_iterate(ext2_u32_iterate iter, __u32 *blk)
 {
-       ext2_badblocks_list     bb;
+       ext2_u32_list   bb;
 
        if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
                return 0;
@@ -204,25 +272,38 @@ int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
 
        if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
                return 0;
-       
+
        if (iter->ptr < bb->num) {
                *blk = bb->list[iter->ptr++];
                return 1;
-       } 
+       }
        *blk = 0;
        return 0;
 }
 
-void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
+int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
+{
+       return ext2fs_u32_list_iterate((ext2_u32_iterate) iter,
+                                      (__u32 *) blk);
+}
+
+
+void ext2fs_u32_list_iterate_end(ext2_u32_iterate iter)
 {
        if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
                return;
 
        iter->bb = 0;
-       ext2fs_free_mem((void **) &iter);
+       ext2fs_free_mem(&iter);
 }
 
-int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
+void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
+{
+       ext2fs_u32_list_iterate_end((ext2_u32_iterate) iter);
+}
+
+
+int ext2fs_u32_list_equal(ext2_u32_list bb1, ext2_u32_list bb2)
 {
        EXT2_CHECK_MAGIC(bb1, EXT2_ET_MAGIC_BADBLOCKS_LIST);
        EXT2_CHECK_MAGIC(bb2, EXT2_ET_MAGIC_BADBLOCKS_LIST);
@@ -234,3 +315,14 @@ int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
                return 0;
        return 1;
 }
+
+int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
+{
+       return ext2fs_u32_list_equal((ext2_u32_list) bb1,
+                                    (ext2_u32_list) bb2);
+}
+
+int ext2fs_u32_list_count(ext2_u32_list bb)
+{
+       return bb->num;
+}