Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / badblocks.c
index 38e9f6e..195c8be 100644 (file)
@@ -14,7 +14,6 @@
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#include <stdlib.h>
 #include <fcntl.h>
 #include <time.h>
 #if HAVE_SYS_STAT_H
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
-#if HAVE_ERRNO_H
-#include <errno.h>
-#endif
 
+#if EXT2_FLAT_INCLUDES
+#include "ext2_fs.h"
+#else
 #include <linux/ext2_fs.h>
+#endif
 
 #include "ext2fsP.h"
 
@@ -38,18 +38,20 @@ static errcode_t make_badblocks_list(int size, int num, blk_t *list,
                                     ext2_badblocks_list *ret)
 {
        ext2_badblocks_list     bb;
+       errcode_t               retval;
        
-       bb = malloc(sizeof(struct ext2_struct_badblocks_list));
-       if (!bb)
-               return ENOMEM;
+       retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_list),
+                               (void **) &bb);
+       if (retval)
+               return retval;
        memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
        bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
        bb->size = size ? size : 10;
        bb->num = num;
-       bb->list = malloc(bb->size * sizeof(blk_t));
+       retval = ext2fs_get_mem(bb->size * sizeof(blk_t), (void **) &bb->list);
        if (!bb->list) {
-               free(bb);
-               return ENOMEM;
+               ext2fs_free_mem((void **) &bb);
+               return retval;
        }
        if (list)
                memcpy(bb->list, list, bb->size * sizeof(blk_t));
@@ -97,17 +99,32 @@ errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
  */
 errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
 {
-       int     i, j;
-       blk_t   *new_list;
+       errcode_t       retval;
+       int             i, j;
+       unsigned long   old_size;
 
        EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
 
        if (bb->num >= bb->size) {
-               bb->size += 10;
-               new_list = realloc(bb->list, bb->size * sizeof(blk_t));
-               if (!new_list)
-                       return ENOMEM;
-               bb->list = new_list;
+               old_size = bb->size * sizeof(blk_t);
+               bb->size += 100;
+               retval = ext2fs_resize_mem(old_size, bb->size * sizeof(blk_t),
+                                          (void **) &bb->list);
+               if (retval) {
+                       bb->size -= 100;
+                       return retval;
+               }
+       }
+
+       /*
+        * Add special case code for appending to the end of the list
+        */
+       i = bb->num-1;
+       if ((bb->num != 0) && (bb->list[i] == blk))
+               return 0;
+       if ((bb->num == 0) || (bb->list[i] < blk)) {
+               bb->list[bb->num++] = blk;
+               return 0;
        }
 
        j = bb->num;
@@ -165,12 +182,14 @@ errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
                                              ext2_badblocks_iterate *ret)
 {
        ext2_badblocks_iterate iter;
+       errcode_t               retval;
 
        EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
 
-       iter = malloc(sizeof(struct ext2_struct_badblocks_iterate));
-       if (!iter)
-               return ENOMEM;
+       retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_iterate),
+                             (void **) &iter);
+       if (retval)
+               return retval;
 
        iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
        iter->bb = bb;
@@ -205,10 +224,18 @@ void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
                return;
 
        iter->bb = 0;
-       free(iter);
+       ext2fs_free_mem((void **) &iter);
 }
 
+int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
+{
+       EXT2_CHECK_MAGIC(bb1, EXT2_ET_MAGIC_BADBLOCKS_LIST);
+       EXT2_CHECK_MAGIC(bb2, EXT2_ET_MAGIC_BADBLOCKS_LIST);
 
+       if (bb1->num != bb2->num)
+               return 0;
 
-
-               
+       if (memcmp(bb1->list, bb2->list, bb1->num * sizeof(blk_t)) != 0)
+               return 0;
+       return 1;
+}