From 44c81eeffe9c8820b1009a7a5c728782aa5ebf40 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 3 Mar 2020 19:43:54 +0100 Subject: Add a generic copying mechanism to sqfs_object_t This patch adds a deep-copy callback to sqfs_object_t and removes the copying mechanism from sqfs_compressor_t. This is also interesting for other types. Signed-off-by: David Oberhollenzer --- lib/sqfs/frag_table.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'lib/sqfs/frag_table.c') diff --git a/lib/sqfs/frag_table.c b/lib/sqfs/frag_table.c index bf1c109..51164e6 100644 --- a/lib/sqfs/frag_table.c +++ b/lib/sqfs/frag_table.c @@ -58,6 +58,44 @@ static void frag_table_destroy(sqfs_object_t *obj) free(tbl); } +static sqfs_object_t *frag_table_copy(const sqfs_object_t *obj) +{ + const sqfs_frag_table_t *tbl = (const sqfs_frag_table_t *)obj; + sqfs_frag_table_t *copy; + const chunk_info_t *it; + chunk_info_t *last; + size_t i; + + copy = malloc(sizeof(*copy)); + if (copy == NULL) + return NULL; + + memcpy(copy, tbl, sizeof(*tbl)); + for (i = 0; i < NUM_BUCKETS; ++i) + copy->chunks[i].next = NULL; + + for (i = 0; i < NUM_BUCKETS; ++i) { + last = &(copy->chunks[i]); + it = tbl->chunks[i].next; + + while (it != NULL) { + last->next = malloc(sizeof(*it)); + if (last->next == NULL) + goto fail; + + memcpy(last->next, it, sizeof(*it)); + last = last->next; + last->next = NULL; + it = it->next; + } + } + + return (sqfs_object_t *)copy; +fail: + frag_table_destroy((sqfs_object_t *)copy); + return NULL; +} + sqfs_frag_table_t *sqfs_frag_table_create(sqfs_u32 flags) { sqfs_frag_table_t *tbl; @@ -69,6 +107,7 @@ sqfs_frag_table_t *sqfs_frag_table_create(sqfs_u32 flags) if (tbl == NULL) return NULL; + ((sqfs_object_t *)tbl)->copy = frag_table_copy; ((sqfs_object_t *)tbl)->destroy = frag_table_destroy; return tbl; } -- cgit v1.2.3