summaryrefslogtreecommitdiff
path: root/lib/sqfs/frag_table.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-03-03 19:43:54 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-03-04 00:58:53 +0100
commit44c81eeffe9c8820b1009a7a5c728782aa5ebf40 (patch)
tree544eceb6dc5633018e5147c88ca9b07dd0fb54d9 /lib/sqfs/frag_table.c
parent6f47796dc0761fac359ec59ce26c7af5154e538d (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/frag_table.c')
-rw-r--r--lib/sqfs/frag_table.c39
1 files changed, 39 insertions, 0 deletions
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;
}