From de9f0a79850df74078b8c104ce1232d40ec9cc1f Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 1 May 2019 01:41:30 +0200 Subject: Add id table code stub Signed-off-by: David Oberhollenzer --- include/id_table.h | 20 ++++++++++++++++++++ lib/Makemodule.am | 1 + lib/sqfs/id_table.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mkfs/mksquashfs.c | 7 ++++++- mkfs/mksquashfs.h | 3 +++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 include/id_table.h create mode 100644 lib/sqfs/id_table.c diff --git a/include/id_table.h b/include/id_table.h new file mode 100644 index 0000000..6d5d416 --- /dev/null +++ b/include/id_table.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#ifndef ID_TABLE_H +#define ID_TABLE_H + +#include +#include + +typedef struct { + uint32_t *ids; + size_t num_ids; + size_t max_ids; +} id_table_t; + +int id_table_init(id_table_t *tbl); + +void id_table_cleanup(id_table_t *tbl); + +int id_table_id_to_index(id_table_t *tbl, uint32_t id, uint16_t *out); + +#endif /* ID_TABLE_H */ diff --git a/lib/Makemodule.am b/lib/Makemodule.am index 86f1766..b12367d 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -8,6 +8,7 @@ libcompress_a_CPPFLAGS = $(AM_CPPFLAGS) libsquashfs_a_SOURCES = include/meta_writer.h include/squashfs.h libsquashfs_a_SOURCES += lib/sqfs/meta_writer.c lib/sqfs/super.c +libsquashfs_a_SOURCES += lib/sqfs/id_table.c include/id_table.h libutil_a_SOURCES = lib/util/canonicalize_name.c lib/util/write_retry.c libutil_a_SOURCES += lib/util/read_retry.c include/util.h diff --git a/lib/sqfs/id_table.c b/lib/sqfs/id_table.c new file mode 100644 index 0000000..cd31360 --- /dev/null +++ b/lib/sqfs/id_table.c @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "id_table.h" + +#include +#include +#include + +int id_table_init(id_table_t *tbl) +{ + memset(tbl, 0, sizeof(*tbl)); + return 0; +} + +void id_table_cleanup(id_table_t *tbl) +{ + free(tbl->ids); +} + +int id_table_id_to_index(id_table_t *tbl, uint32_t id, uint16_t *out) +{ + size_t i, sz; + void *ptr; + + for (i = 0; i < tbl->num_ids; ++i) { + if (tbl->ids[i] == id) { + *out = i; + return 0; + } + } + + if (tbl->num_ids == 0x10000) { + fputs("Too many unique UIDs/GIDs (more than 64k)!\n", stderr); + return -1; + } + + if (tbl->num_ids == tbl->max_ids) { + sz = (tbl->max_ids ? tbl->max_ids * 2 : 16); + ptr = realloc(tbl->ids, sizeof(tbl->ids[0]) * sz); + + if (ptr == NULL) { + perror("growing ID table"); + return -1; + } + + tbl->ids = ptr; + tbl->max_ids = sz; + } + + *out = tbl->num_ids; + tbl->ids[tbl->num_ids++] = id; + return 0; +} diff --git a/mkfs/mksquashfs.c b/mkfs/mksquashfs.c index 7503978..3272dbc 100644 --- a/mkfs/mksquashfs.c +++ b/mkfs/mksquashfs.c @@ -84,10 +84,13 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + if (id_table_init(&info.idtbl)) + return EXIT_FAILURE; + info.outfd = open(info.opt.outfile, info.opt.outmode, 0644); if (info.outfd < 0) { perror(info.opt.outfile); - return EXIT_FAILURE; + goto out_idtbl; } if (sqfs_super_write(&info.super, info.outfd)) @@ -129,5 +132,7 @@ out_fstree: fstree_cleanup(&info.fs); out_outfd: close(info.outfd); +out_idtbl: + id_table_cleanup(&info.idtbl); return status; } diff --git a/mkfs/mksquashfs.h b/mkfs/mksquashfs.h index 2e0183e..5522268 100644 --- a/mkfs/mksquashfs.h +++ b/mkfs/mksquashfs.h @@ -4,6 +4,7 @@ #include "squashfs.h" #include "compress.h" +#include "id_table.h" #include "fstree.h" #include "config.h" @@ -43,6 +44,8 @@ typedef struct { file_info_t *frag_list; size_t frag_offset; + id_table_t idtbl; + compressor_t *cmp; } sqfs_info_t; -- cgit v1.2.3