diff mbox series

[RFCv1,06/72] dblist: add dblist merge logic

Message ID 99ecce2af8609cc7bbe79de273472dcb42cca8f5.1667822611.git.ritesh.list@gmail.com
State Under Review
Delegated to: Theodore Ts'o
Headers show
Series e2fsprogs: Parallel fsck support | expand

Commit Message

Ritesh Harjani (IBM) Nov. 7, 2022, 12:20 p.m. UTC
From: Li Xi <lixi@ddn.com>

This adds dblist merge logic.

TODO: Add a unit test for core operations of dblist. Currently there is
no such test for this.

Signed-off-by: Li Xi <lixi@ddn.com>
Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
---
 lib/ext2fs/dblist.c | 36 ++++++++++++++++++++++++++++++++++++
 lib/ext2fs/ext2fs.h |  1 +
 2 files changed, 37 insertions(+)

Comments

Andreas Dilger Nov. 18, 2022, 1:34 p.m. UTC | #1
On Nov 7, 2022, at 06:22, Ritesh Harjani (IBM) <ritesh.list@gmail.com> wrote:
> 
> ´╗┐From: Li Xi <lixi@ddn.com>
> 
> This adds dblist merge logic.
> 
> TODO: Add a unit test for core operations of dblist. Currently there is
> no such test for this.
> 
> Signed-off-by: Li Xi <lixi@ddn.com>
> Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>

Reviewed-by: Andreas Dilger <adilger@dilger.ca>

> ---
> lib/ext2fs/dblist.c | 36 ++++++++++++++++++++++++++++++++++++
> lib/ext2fs/ext2fs.h |  1 +
> 2 files changed, 37 insertions(+)
> 
> diff --git a/lib/ext2fs/dblist.c b/lib/ext2fs/dblist.c
> index bbdb221d..5568b8ec 100644
> --- a/lib/ext2fs/dblist.c
> +++ b/lib/ext2fs/dblist.c
> @@ -119,6 +119,42 @@ errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest)
>    return 0;
> }
> 
> +/*
> + * Merge a directory block list @src to @dest
> + */
> +errcode_t ext2fs_merge_dblist(ext2_dblist src, ext2_dblist dest)
> +{
> +   unsigned long long src_count = src->count;
> +   unsigned long long dest_count = dest->count;
> +   unsigned long long size = src_count + dest_count;
> +   size_t size_entry = sizeof(struct ext2_db_entry2);
> +   struct ext2_db_entry2 *array, *array2;
> +   errcode_t retval;
> +
> +   if (src_count == 0)
> +       return 0;
> +
> +   if (src->sorted || (dest->sorted && dest_count != 0))
> +       return EINVAL;
> +
> +   retval = ext2fs_get_array(size, size_entry, &array);
> +   if (retval)
> +       return retval;
> +
> +   array2 = array;
> +   memcpy(array, src->list, src_count * size_entry);
> +   array += src_count;
> +   memcpy(array, dest->list, dest_count * size_entry);
> +   ext2fs_free_mem(&dest->list);
> +
> +   dest->list = array2;
> +   dest->count = src_count + dest_count;
> +   dest->size = size;
> +   dest->sorted = 0;
> +
> +   return 0;
> +}
> +
> /*
>  * Close a directory block list
>  *
> diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
> index 18dddc2c..443f93d2 100644
> --- a/lib/ext2fs/ext2fs.h
> +++ b/lib/ext2fs/ext2fs.h
> @@ -1143,6 +1143,7 @@ extern errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino,
>                      blk_t blk, int blockcnt);
> extern errcode_t ext2fs_add_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
>                       blk64_t blk, e2_blkcnt_t blockcnt);
> +extern errcode_t ext2fs_merge_dblist(ext2_dblist src, ext2_dblist dest);
> extern void ext2fs_dblist_sort(ext2_dblist dblist,
>                   EXT2_QSORT_TYPE (*sortfunc)(const void *,
>                               const void *));
> -- 
> 2.37.3
>
diff mbox series

Patch

diff --git a/lib/ext2fs/dblist.c b/lib/ext2fs/dblist.c
index bbdb221d..5568b8ec 100644
--- a/lib/ext2fs/dblist.c
+++ b/lib/ext2fs/dblist.c
@@ -119,6 +119,42 @@  errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest)
 	return 0;
 }
 
+/*
+ * Merge a directory block list @src to @dest
+ */
+errcode_t ext2fs_merge_dblist(ext2_dblist src, ext2_dblist dest)
+{
+   unsigned long long src_count = src->count;
+   unsigned long long dest_count = dest->count;
+   unsigned long long size = src_count + dest_count;
+   size_t size_entry = sizeof(struct ext2_db_entry2);
+   struct ext2_db_entry2 *array, *array2;
+   errcode_t retval;
+
+   if (src_count == 0)
+       return 0;
+
+   if (src->sorted || (dest->sorted && dest_count != 0))
+       return EINVAL;
+
+   retval = ext2fs_get_array(size, size_entry, &array);
+   if (retval)
+       return retval;
+
+   array2 = array;
+   memcpy(array, src->list, src_count * size_entry);
+   array += src_count;
+   memcpy(array, dest->list, dest_count * size_entry);
+   ext2fs_free_mem(&dest->list);
+
+   dest->list = array2;
+   dest->count = src_count + dest_count;
+   dest->size = size;
+   dest->sorted = 0;
+
+   return 0;
+}
+
 /*
  * Close a directory block list
  *
diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index 18dddc2c..443f93d2 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -1143,6 +1143,7 @@  extern errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino,
 				      blk_t blk, int blockcnt);
 extern errcode_t ext2fs_add_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
 				       blk64_t blk, e2_blkcnt_t blockcnt);
+extern errcode_t ext2fs_merge_dblist(ext2_dblist src, ext2_dblist dest);
 extern void ext2fs_dblist_sort(ext2_dblist dblist,
 			       EXT2_QSORT_TYPE (*sortfunc)(const void *,
 							   const void *));