From 303680ebcd5adaac2934b63a0edc2d9d1a36d7fb Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 12 Feb 2020 02:22:31 +0100 Subject: Implement a more explicit object system Make every dynamically allocated, opaque data structure inherit from a common sqfs_object_t structure with common entry points (e.g. destroy). This removes tons of public API functions and replaces them with a simple sqfs_destroy instead. If semantics of the (until now implicit) object system need to be extended, it can be much more conveniantely done this way. Signed-off-by: David Oberhollenzer --- difftool/sqfsdiff.c | 20 ++++---- extras/browse.c | 10 ++-- extras/list_files.c | 8 +-- extras/mk42sqfs.c | 12 ++--- extras/mknastyfs.c | 12 ++--- include/sqfs/block_processor.h | 11 +---- include/sqfs/block_writer.h | 11 +---- include/sqfs/compressor.h | 9 ++-- include/sqfs/data_reader.h | 11 +---- include/sqfs/dir_reader.h | 9 +--- include/sqfs/dir_writer.h | 11 +---- include/sqfs/frag_table.h | 9 +--- include/sqfs/id_table.h | 11 +---- include/sqfs/io.h | 9 ++-- include/sqfs/meta_reader.h | 11 +---- include/sqfs/meta_writer.h | 11 +---- include/sqfs/predef.h | 27 ++++++++++ include/sqfs/xattr_reader.h | 11 +---- include/sqfs/xattr_writer.h | 11 +---- lib/common/comp_lzo.c | 4 +- lib/common/io_stdin.c | 4 +- lib/common/writer.c | 42 ++++++++-------- lib/sqfs/block_processor/internal.h | 2 + lib/sqfs/block_processor/serial.c | 11 +++-- lib/sqfs/block_processor/winpthread.c | 93 ++++++++++++++++++----------------- lib/sqfs/block_writer.c | 14 +++--- lib/sqfs/comp/gzip.c | 4 +- lib/sqfs/comp/lz4.c | 4 +- lib/sqfs/comp/lzma.c | 4 +- lib/sqfs/comp/xz.c | 4 +- lib/sqfs/comp/zstd.c | 4 +- lib/sqfs/data_reader.c | 21 +++++--- lib/sqfs/dir_reader.c | 21 +++++--- lib/sqfs/dir_writer.c | 19 ++++--- lib/sqfs/frag_table.c | 19 ++++--- lib/sqfs/id_table.c | 24 ++++++--- lib/sqfs/meta_reader.c | 13 +++-- lib/sqfs/meta_writer.c | 30 ++++++----- lib/sqfs/read_table.c | 4 +- lib/sqfs/unix/io_file.c | 4 +- lib/sqfs/win32/io_file.c | 4 +- lib/sqfs/write_table.c | 2 +- lib/sqfs/xattr_reader.c | 31 +++++++----- lib/sqfs/xattr_writer.c | 38 +++++++------- mkfs/mkfs.c | 6 +-- tar/sqfs2tar.c | 12 ++--- tar/tar2sqfs.c | 2 +- unpack/rdsquashfs.c | 12 ++--- 48 files changed, 340 insertions(+), 336 deletions(-) diff --git a/difftool/sqfsdiff.c b/difftool/sqfsdiff.c index 58ddb4b..c5d2bf2 100644 --- a/difftool/sqfsdiff.c +++ b/difftool/sqfsdiff.c @@ -103,28 +103,28 @@ static int open_sfqs(sqfs_state_t *state, const char *path) return 0; fail_data: - sqfs_data_reader_destroy(state->data); + sqfs_destroy(state->data); fail_tree: sqfs_dir_tree_destroy(state->root); fail_dr: - sqfs_dir_reader_destroy(state->dr); + sqfs_destroy(state->dr); fail_id: - sqfs_id_table_destroy(state->idtbl); + sqfs_destroy(state->idtbl); fail_cmp: - state->cmp->destroy(state->cmp); + sqfs_destroy(state->cmp); fail_file: - state->file->destroy(state->file); + sqfs_destroy(state->file); return -1; } static void close_sfqs(sqfs_state_t *state) { - sqfs_data_reader_destroy(state->data); + sqfs_destroy(state->data); sqfs_dir_tree_destroy(state->root); - sqfs_dir_reader_destroy(state->dr); - sqfs_id_table_destroy(state->idtbl); - state->cmp->destroy(state->cmp); - state->file->destroy(state->file); + sqfs_destroy(state->dr); + sqfs_destroy(state->idtbl); + sqfs_destroy(state->cmp); + sqfs_destroy(state->file); } int main(int argc, char **argv) diff --git a/extras/browse.c b/extras/browse.c index 49ddb7e..0bca183 100644 --- a/extras/browse.c +++ b/extras/browse.c @@ -599,16 +599,16 @@ int main(int argc, char **argv) status = EXIT_SUCCESS; free(buffer); out: - sqfs_data_reader_destroy(data); + sqfs_destroy(data); out_dir: if (working_dir != NULL) free(working_dir); - sqfs_dir_reader_destroy(dr); + sqfs_destroy(dr); out_id: - sqfs_id_table_destroy(idtbl); + sqfs_destroy(idtbl); out_cmp: - cmp->destroy(cmp); + sqfs_destroy(cmp); out_fd: - file->destroy(file); + sqfs_destroy(file); return status; } diff --git a/extras/list_files.c b/extras/list_files.c index de390f0..5e181f2 100644 --- a/extras/list_files.c +++ b/extras/list_files.c @@ -134,12 +134,12 @@ int main(int argc, char **argv) out: if (root != NULL) sqfs_dir_tree_destroy(root); - sqfs_dir_reader_destroy(dr); + sqfs_destroy(dr); out_id: - sqfs_id_table_destroy(idtbl); + sqfs_destroy(idtbl); out_cmp: - cmp->destroy(cmp); + sqfs_destroy(cmp); out_fd: - file->destroy(file); + sqfs_destroy(file); return status; } diff --git a/extras/mk42sqfs.c b/extras/mk42sqfs.c index 0dbcc4e..53cf2f0 100644 --- a/extras/mk42sqfs.c +++ b/extras/mk42sqfs.c @@ -189,16 +189,16 @@ int main(void) /* cleanup */ status = EXIT_SUCCESS; - sqfs_id_table_destroy(idtbl); + sqfs_destroy(idtbl); out_dirwr: - sqfs_dir_writer_destroy(dirwr); + sqfs_destroy(dirwr); out_dm: - sqfs_meta_writer_destroy(dir_m); + sqfs_destroy(dir_m); out_im: - sqfs_meta_writer_destroy(inode_m); + sqfs_destroy(inode_m); out_cmp: - cmp->destroy(cmp); + sqfs_destroy(cmp); out_file: - file->destroy(file); + sqfs_destroy(file); return status; } diff --git a/extras/mknastyfs.c b/extras/mknastyfs.c index 2a66bc9..b11f8f9 100644 --- a/extras/mknastyfs.c +++ b/extras/mknastyfs.c @@ -168,16 +168,16 @@ int main(void) /* cleanup */ status = EXIT_SUCCESS; - sqfs_id_table_destroy(idtbl); + sqfs_destroy(idtbl); out_dirwr: - sqfs_dir_writer_destroy(dirwr); + sqfs_destroy(dirwr); out_dm: - sqfs_meta_writer_destroy(dir_m); + sqfs_destroy(dir_m); out_im: - sqfs_meta_writer_destroy(inode_m); + sqfs_destroy(inode_m); out_cmp: - cmp->destroy(cmp); + sqfs_destroy(cmp); out_file: - file->destroy(file); + sqfs_destroy(file); return status; } diff --git a/include/sqfs/block_processor.h b/include/sqfs/block_processor.h index 41cb3d3..cef4459 100644 --- a/include/sqfs/block_processor.h +++ b/include/sqfs/block_processor.h @@ -33,6 +33,8 @@ * * @brief Abstracts generating of file data and fragment blocks. * + * @implements sqfs_object_t + * * This data structure provides a simple begin/append/end interface * to generate file data blocks (see @ref sqfs_block_processor_begin_file, * @ref sqfs_block_processor_append and @ref sqfs_block_processor_end @@ -129,15 +131,6 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_block_writer_t *wr, sqfs_frag_table_t *tbl); -/** - * @brief Destroy a data writer and free all memory used by it. - * - * @memberof sqfs_block_processor_t - * - * @param proc A pointer to a data writer object. - */ -SQFS_API void sqfs_block_processor_destroy(sqfs_block_processor_t *proc); - /** * @brief Start writing a file. * diff --git a/include/sqfs/block_writer.h b/include/sqfs/block_writer.h index e921141..bd1ddfc 100644 --- a/include/sqfs/block_writer.h +++ b/include/sqfs/block_writer.h @@ -31,6 +31,8 @@ /** * @struct sqfs_block_writer_t * + * @implements sqfs_object_t + * * @brief Abstracts writing and deduplicating of data and fragment blocks. */ @@ -189,15 +191,6 @@ SQFS_API int sqfs_block_writer_set_hooks(sqfs_block_writer_t *wr, void *user_ptr, const sqfs_block_hooks_t *hooks); -/** - * @brief Destroy a block writer object. - * - * @memberof sqfs_block_writer_t - * - * @param wr A pointer to a block writer object. - */ -SQFS_API void sqfs_block_writer_destroy(sqfs_block_writer_t *wr); - /** * @brief Submit a data block to a blokc writer. * diff --git a/include/sqfs/compressor.h b/include/sqfs/compressor.h index 5e0be5d..c0fafda 100644 --- a/include/sqfs/compressor.h +++ b/include/sqfs/compressor.h @@ -32,16 +32,13 @@ /** * @interface sqfs_compressor_t * + * @extends sqfs_object_t + * * @brief Encapsultes a compressor with a simple interface to compress or * extract chunks of data. */ struct sqfs_compressor_t { - /** - * @brief Destroy a compressor and free all memory used by it. - * - * @param cmp A pointer to a compressor object. - */ - void (*destroy)(sqfs_compressor_t *cmp); + sqfs_object_t base; /** * @brief Write compressor options to disk if non-default settings diff --git a/include/sqfs/data_reader.h b/include/sqfs/data_reader.h index 0c4918c..e058017 100644 --- a/include/sqfs/data_reader.h +++ b/include/sqfs/data_reader.h @@ -31,6 +31,8 @@ /** * @struct sqfs_data_reader_t * + * @implements sqfs_object_t + * * @brief Abstracts access to data blocks stored in a SquashFS image. * * A SquashFS image can contain a series of file data blocks between the @@ -68,15 +70,6 @@ SQFS_API sqfs_data_reader_t *sqfs_data_reader_create(sqfs_file_t *file, size_t block_size, sqfs_compressor_t *cmp); -/** - * @brief Destroy a data reader instance and free all memory used by it. - * - * @memberof sqfs_data_reader_t - * - * @param data A pointer to a data reader object. - */ -SQFS_API void sqfs_data_reader_destroy(sqfs_data_reader_t *data); - /** * @brief Read and decode the fragment table from disk. * diff --git a/include/sqfs/dir_reader.h b/include/sqfs/dir_reader.h index 82121ed..7b55eb0 100644 --- a/include/sqfs/dir_reader.h +++ b/include/sqfs/dir_reader.h @@ -31,6 +31,8 @@ /** * @struct sqfs_dir_reader_t * + * @implements sqfs_object_t + * * @brief Abstracts reading of directory entries * * SquashFS stores directory listings and inode structures separated from @@ -166,13 +168,6 @@ SQFS_API sqfs_dir_reader_t *sqfs_dir_reader_create(const sqfs_super_t *super, sqfs_compressor_t *cmp, sqfs_file_t *file); -/** - * @brief Cleanup a directory reader and free all its memory. - * - * @memberof sqfs_dir_reader_t - */ -SQFS_API void sqfs_dir_reader_destroy(sqfs_dir_reader_t *rd); - /** * @brief Navigate a directory reader to the location of a directory * represented by an inode. diff --git a/include/sqfs/dir_writer.h b/include/sqfs/dir_writer.h index 05d054b..e4a26c7 100644 --- a/include/sqfs/dir_writer.h +++ b/include/sqfs/dir_writer.h @@ -31,6 +31,8 @@ /** * @struct sqfs_dir_writer_t * + * @implements sqfs_object_t + * * @brief Abstracts generating of directory entries * * SquashFS stores directory entries and inodes separated from each other. The @@ -101,15 +103,6 @@ extern "C" { SQFS_API sqfs_dir_writer_t *sqfs_dir_writer_create(sqfs_meta_writer_t *dm, sqfs_u32 flags); -/** - * @brief Destroy a directory writer and free all its memory. - * - * @memberof sqfs_dir_writer_t - * - * @param writer A pointer to a directory writer object. - */ -SQFS_API void sqfs_dir_writer_destroy(sqfs_dir_writer_t *writer); - /** * @brief Begin writing a directory, i.e. reset and initialize all internal * state neccessary. diff --git a/include/sqfs/frag_table.h b/include/sqfs/frag_table.h index 45ba645..9a66692 100644 --- a/include/sqfs/frag_table.h +++ b/include/sqfs/frag_table.h @@ -31,6 +31,8 @@ /** * @struct sqfs_frag_table_t * + * @implements sqfs_object_t + * * @brief Abstracts reading, writing and management of the fragment table. */ @@ -49,13 +51,6 @@ extern "C" { */ SQFS_API sqfs_frag_table_t *sqfs_frag_table_create(sqfs_u32 flags); -/** - * @brief Destroy a fragment table and release all associated memory. - * - * @memberof sqfs_frag_table_t - */ -SQFS_API void sqfs_frag_table_destroy(sqfs_frag_table_t *tbl); - /** * @brief Load a fragment table from a SquashFS image. * diff --git a/include/sqfs/id_table.h b/include/sqfs/id_table.h index 67990db..706687f 100644 --- a/include/sqfs/id_table.h +++ b/include/sqfs/id_table.h @@ -31,6 +31,8 @@ /** * @struct sqfs_id_table_t * + * @implements sqfs_object_t + * * @brief A simple data structure that encapsulates ID to index mapping for * user and group IDs. * @@ -56,15 +58,6 @@ extern "C" { */ SQFS_API sqfs_id_table_t *sqfs_id_table_create(sqfs_u32 flags); -/** - * @brief Destroy an ID table object and free all memory used by it. - * - * @memberof sqfs_id_table_t - * - * @param tbl A pointer to an ID table object. - */ -SQFS_API void sqfs_id_table_destroy(sqfs_id_table_t *tbl); - /** * @brief Resolve a 32 bit ID to a unique 16 bit index. * diff --git a/include/sqfs/io.h b/include/sqfs/io.h index 1108a61..578c4fb 100644 --- a/include/sqfs/io.h +++ b/include/sqfs/io.h @@ -60,15 +60,12 @@ typedef enum { /** * @interface sqfs_file_t * + * @extends sqfs_object_t + * * @brief Abstracts file I/O to make it easy to embedd SquashFS. */ struct sqfs_file_t { - /** - * @brief Close the file and destroy the interface implementation. - * - * @param file A pointer to the file object. - */ - void (*destroy)(sqfs_file_t *file); + sqfs_object_t base; /** * @brief Read a chunk of data from an absolute position. diff --git a/include/sqfs/meta_reader.h b/include/sqfs/meta_reader.h index e2203de..e8b40d4 100644 --- a/include/sqfs/meta_reader.h +++ b/include/sqfs/meta_reader.h @@ -31,6 +31,8 @@ /** * @struct sqfs_meta_reader_t * + * @implements sqfs_object_t + * * @brief Abstracts reading of meta data blocks. * * SquashFS stores meta data by dividing it into fixed size (8k) chunks @@ -69,15 +71,6 @@ SQFS_API sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file, sqfs_u64 start, sqfs_u64 limit); -/** - * @brief Destroy a meta data reader and free all memory used by it. - * - * @memberof sqfs_meta_reader_t - * - * @param m A pointer to a meta data reader. - */ -SQFS_API void sqfs_meta_reader_destroy(sqfs_meta_reader_t *m); - /** * @brief Seek to a specific meta data block and offset. * diff --git a/include/sqfs/meta_writer.h b/include/sqfs/meta_writer.h index cf8e16c..be151cc 100644 --- a/include/sqfs/meta_writer.h +++ b/include/sqfs/meta_writer.h @@ -31,6 +31,8 @@ /** * @struct sqfs_meta_writer_t * + * @implements sqfs_object_t + * * @brief Abstracts generating of meta data blocks, either in memory or * directly on disk. * @@ -87,15 +89,6 @@ SQFS_API sqfs_meta_writer_t *sqfs_meta_writer_create(sqfs_file_t *file, sqfs_compressor_t *cmp, sqfs_u32 flags); -/** - * @brief Destroy a meta data writer and free all memory used by it. - * - * @memberof sqfs_meta_writer_t - * - * @param m A pointer to a meta data writer. - */ -SQFS_API void sqfs_meta_writer_destroy(sqfs_meta_writer_t *m); - /** * @brief Finish the current block, even if it isn't full yet. * diff --git a/include/sqfs/predef.h b/include/sqfs/predef.h index d8cc293..6ca3503 100644 --- a/include/sqfs/predef.h +++ b/include/sqfs/predef.h @@ -57,6 +57,12 @@ #endif #endif +#ifdef _MSC_VER + #define SQFS_INLINE __forceinline +#else + #define SQFS_INLINE __inline__ __attribute__((always_inline)) +#endif + typedef uint8_t sqfs_u8; typedef uint16_t sqfs_u16; typedef uint32_t sqfs_u32; @@ -108,4 +114,25 @@ typedef struct sqfs_xattr_value_t sqfs_xattr_value_t; typedef struct sqfs_xattr_id_t sqfs_xattr_id_t; typedef struct sqfs_xattr_id_table_t sqfs_xattr_id_table_t; +/** + * @interface sqfs_object_t + * + * @brief Base interface for all libsquashfs in-memory data structures. + */ +typedef struct sqfs_object_t { + void (*destroy)(struct sqfs_object_t *instance); +} sqfs_object_t; + +/** + * @brief Destroy an object and free all its memory + * + * @memberof sqfs_object_t + * + * @param obj A pointer to an object + */ +static SQFS_INLINE void sqfs_destroy(void *obj) +{ + ((sqfs_object_t *)obj)->destroy(obj); +} + #endif /* SQFS_PREDEF_H */ diff --git a/include/sqfs/xattr_reader.h b/include/sqfs/xattr_reader.h index 6544a21..7912de4 100644 --- a/include/sqfs/xattr_reader.h +++ b/include/sqfs/xattr_reader.h @@ -31,6 +31,8 @@ /** * @struct sqfs_xattr_reader_t * + * @implements sqfs_object_t + * * @brief Abstracts read access to extended attributes in a SquashFS filesystem * * SquashFS stores extended attributes using multiple levels of indirection. @@ -94,15 +96,6 @@ SQFS_API sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file, sqfs_super_t *super, sqfs_compressor_t *cmp); -/** - * @brief Destroy an xattr reader and free all memory used by it - * - * @memberof sqfs_xattr_reader_t - * - * @param xr A pointer to an xattr reader instance - */ -SQFS_API void sqfs_xattr_reader_destroy(sqfs_xattr_reader_t *xr); - /** * @brief Load the locations of the xattr meta data blocks into memory * diff --git a/include/sqfs/xattr_writer.h b/include/sqfs/xattr_writer.h index 5b18659..b25215e 100644 --- a/include/sqfs/xattr_writer.h +++ b/include/sqfs/xattr_writer.h @@ -31,6 +31,8 @@ /** * @struct sqfs_xattr_writer_t * + * @implements sqfs_object_t + * * @brief Abstracts writing of extended attributes to a SquashFS filesystem. * * This data structure provides a simple, abstract interface to recording @@ -63,15 +65,6 @@ extern "C" { */ SQFS_API sqfs_xattr_writer_t *sqfs_xattr_writer_create(void); -/** - * @brief Destroy an xattr writer instance and free all memory it used. - * - * @memberof sqfs_xattr_writer_t - * - * @param xwr A pointer to an xattr writer instance. - */ -SQFS_API void sqfs_xattr_writer_destroy(sqfs_xattr_writer_t *xwr); - /** * @brief Begin recording a block of key-value pairs. * diff --git a/lib/common/comp_lzo.c b/lib/common/comp_lzo.c index f371fb6..a0c838a 100644 --- a/lib/common/comp_lzo.c +++ b/lib/common/comp_lzo.c @@ -195,7 +195,7 @@ static sqfs_compressor_t *lzo_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)lzo; } -static void lzo_destroy(sqfs_compressor_t *base) +static void lzo_destroy(sqfs_object_t *base) { free(base); } @@ -247,11 +247,11 @@ sqfs_compressor_t *lzo_compressor_create(const sqfs_compressor_config_t *cfg) lzo->buf_size = scratch_size; lzo->work_size = lzo_algs[cfg->opt.lzo.algorithm].bufsize; - base->destroy = lzo_destroy; base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ? lzo_uncomp_block : lzo_comp_block; base->write_options = lzo_write_options; base->read_options = lzo_read_options; base->create_copy = lzo_create_copy; + ((sqfs_object_t *)base)->destroy = lzo_destroy; return base; } diff --git a/lib/common/io_stdin.c b/lib/common/io_stdin.c index ca6bf49..469e300 100644 --- a/lib/common/io_stdin.c +++ b/lib/common/io_stdin.c @@ -21,7 +21,7 @@ typedef struct { } sqfs_file_stdinout_t; -static void stdinout_destroy(sqfs_file_t *base) +static void stdinout_destroy(sqfs_object_t *base) { free(base); } @@ -159,7 +159,7 @@ sqfs_file_t *sqfs_get_stdin_file(FILE *fp, const sparse_map_t *map, file->map = map; file->fp = fp; - base->destroy = stdinout_destroy; + ((sqfs_object_t *)base)->destroy = stdinout_destroy; base->write_at = stdin_write_at; base->get_size = stdinout_get_size; base->truncate = stdinout_truncate; diff --git a/lib/common/writer.c b/lib/common/writer.c index 84a40d2..4762b82 100644 --- a/lib/common/writer.c +++ b/lib/common/writer.c @@ -90,7 +90,7 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) #ifdef WITH_LZO if (cfg.id == SQFS_COMP_LZO) { if (sqfs->cmp != NULL) - sqfs->cmp->destroy(sqfs->cmp); + sqfs_destroy(sqfs->cmp); sqfs->cmp = lzo_compressor_create(&cfg); } @@ -187,26 +187,26 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) return 0; fail_dm: - sqfs_meta_writer_destroy(sqfs->dm); + sqfs_destroy(sqfs->dm); fail_im: - sqfs_meta_writer_destroy(sqfs->im); + sqfs_destroy(sqfs->im); fail_xwr: if (sqfs->xwr != NULL) - sqfs_xattr_writer_destroy(sqfs->xwr); + sqfs_destroy(sqfs->xwr); fail_id: - sqfs_id_table_destroy(sqfs->idtbl); + sqfs_destroy(sqfs->idtbl); fail_data: - sqfs_block_processor_destroy(sqfs->data); + sqfs_destroy(sqfs->data); fail_fragtbl: - sqfs_frag_table_destroy(sqfs->fragtbl); + sqfs_destroy(sqfs->fragtbl); fail_blkwr: - sqfs_block_writer_destroy(sqfs->blkwr); + sqfs_destroy(sqfs->blkwr); fail_cmp: - sqfs->cmp->destroy(sqfs->cmp); + sqfs_destroy(sqfs->cmp); fail_fs: fstree_cleanup(&sqfs->fs); fail_file: - sqfs->outfile->destroy(sqfs->outfile); + sqfs_destroy(sqfs->outfile); return -1; } @@ -299,16 +299,16 @@ int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg) void sqfs_writer_cleanup(sqfs_writer_t *sqfs) { if (sqfs->xwr != NULL) - sqfs_xattr_writer_destroy(sqfs->xwr); - - sqfs_dir_writer_destroy(sqfs->dirwr); - sqfs_meta_writer_destroy(sqfs->dm); - sqfs_meta_writer_destroy(sqfs->im); - sqfs_id_table_destroy(sqfs->idtbl); - sqfs_block_processor_destroy(sqfs->data); - sqfs_block_writer_destroy(sqfs->blkwr); - sqfs_frag_table_destroy(sqfs->fragtbl); - sqfs->cmp->destroy(sqfs->cmp); + sqfs_destroy(sqfs->xwr); + + sqfs_destroy(sqfs->dirwr); + sqfs_destroy(sqfs->dm); + sqfs_destroy(sqfs->im); + sqfs_destroy(sqfs->idtbl); + sqfs_destroy(sqfs->data); + sqfs_destroy(sqfs->blkwr); + sqfs_destroy(sqfs->fragtbl); + sqfs_destroy(sqfs->cmp); fstree_cleanup(&sqfs->fs); - sqfs->outfile->destroy(sqfs->outfile); + sqfs_destroy(sqfs->outfile); } diff --git a/lib/sqfs/block_processor/internal.h b/lib/sqfs/block_processor/internal.h index b3d76ba..71f8fab 100644 --- a/lib/sqfs/block_processor/internal.h +++ b/lib/sqfs/block_processor/internal.h @@ -50,6 +50,8 @@ typedef struct sqfs_block_t { } sqfs_block_t; struct sqfs_block_processor_t { + sqfs_object_t obj; + /* synchronization primitives */ #ifdef WITH_PTHREAD pthread_mutex_t mtx; diff --git a/lib/sqfs/block_processor/serial.c b/lib/sqfs/block_processor/serial.c index e18b67e..469388c 100644 --- a/lib/sqfs/block_processor/serial.c +++ b/lib/sqfs/block_processor/serial.c @@ -7,6 +7,11 @@ #define SQFS_BUILDING_DLL #include "internal.h" +static void block_processor_destroy(sqfs_object_t *obj) +{ + block_processor_cleanup((sqfs_block_processor_t *)obj); +} + sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_compressor_t *cmp, unsigned int num_workers, @@ -27,14 +32,10 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, return NULL; } + ((sqfs_object_t *)proc)->destroy = block_processor_destroy; return proc; } -void sqfs_block_processor_destroy(sqfs_block_processor_t *proc) -{ - block_processor_cleanup(proc); -} - int test_and_set_status(sqfs_block_processor_t *proc, int status) { if (proc->status == 0) diff --git a/lib/sqfs/block_processor/winpthread.c b/lib/sqfs/block_processor/winpthread.c index 85416ab..a91f001 100644 --- a/lib/sqfs/block_processor/winpthread.c +++ b/lib/sqfs/block_processor/winpthread.c @@ -94,6 +94,35 @@ static THREAD_TYPE worker_proc(THREAD_ARG arg) } #if defined(_WIN32) || defined(__WINDOWS__) +static void block_processor_destroy(sqfs_object_t *obj) +{ + sqfs_block_processor_t *proc = (sqfs_block_processor_t *)obj; + unsigned int i; + + EnterCriticalSection(&proc->mtx); + proc->status = -1; + WakeAllConditionVariable(&proc->queue_cond); + LeaveCriticalSection(&proc->mtx); + + for (i = 0; i < proc->num_workers; ++i) { + if (proc->workers[i] == NULL) + continue; + + if (proc->workers[i]->thread != NULL) { + WaitForSingleObject(proc->workers[i]->thread, INFINITE); + CloseHandle(proc->workers[i]->thread); + } + + if (proc->workers[i]->cmp != NULL) + sqfs_destroy(proc->workers[i]->cmp); + + free(proc->workers[i]); + } + + DeleteCriticalSection(&proc->mtx); + block_processor_cleanup(proc); +} + sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_compressor_t *cmp, unsigned int num_workers, @@ -112,6 +141,8 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, if (proc == NULL) return NULL; + ((sqfs_object_t *)obj)->destroy = block_processor_destroy; + InitializeCriticalSection(&proc->mtx); InitializeConditionVariable(&proc->queue_cond); InitializeConditionVariable(&proc->done_cond); @@ -142,38 +173,34 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, return proc; fail: - sqfs_block_processor_destroy(proc); + block_processor_destroy(proc); return NULL; } - -void sqfs_block_processor_destroy(sqfs_block_processor_t *proc) +#else +static void block_processor_destroy(sqfs_object_t *obj) { + sqfs_block_processor_t *proc = (sqfs_block_processor_t *)obj; unsigned int i; - EnterCriticalSection(&proc->mtx); + pthread_mutex_lock(&proc->mtx); proc->status = -1; - WakeAllConditionVariable(&proc->queue_cond); - LeaveCriticalSection(&proc->mtx); + pthread_cond_broadcast(&proc->queue_cond); + pthread_mutex_unlock(&proc->mtx); for (i = 0; i < proc->num_workers; ++i) { - if (proc->workers[i] == NULL) - continue; - - if (proc->workers[i]->thread != NULL) { - WaitForSingleObject(proc->workers[i]->thread, INFINITE); - CloseHandle(proc->workers[i]->thread); - } - - if (proc->workers[i]->cmp != NULL) - proc->workers[i]->cmp->destroy(proc->workers[i]->cmp); + pthread_join(proc->workers[i]->thread, NULL); + sqfs_destroy(proc->workers[i]->cmp); free(proc->workers[i]); } - DeleteCriticalSection(&proc->mtx); + pthread_cond_destroy(&proc->done_cond); + pthread_cond_destroy(&proc->queue_cond); + pthread_mutex_destroy(&proc->mtx); + block_processor_cleanup(proc); } -#else + sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_compressor_t *cmp, unsigned int num_workers, @@ -194,6 +221,7 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, if (proc == NULL) return NULL; + ((sqfs_object_t *)proc)->destroy = block_processor_destroy; proc->mtx = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; proc->queue_cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; proc->done_cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; @@ -246,10 +274,8 @@ fail_thread: fail_init: for (i = 0; i < num_workers; ++i) { if (proc->workers[i] != NULL) { - if (proc->workers[i]->cmp != NULL) { - proc->workers[i]->cmp-> - destroy(proc->workers[i]->cmp); - } + if (proc->workers[i]->cmp != NULL) + sqfs_destroy(proc->workers[i]->cmp); free(proc->workers[i]); } @@ -260,29 +286,6 @@ fail_init: block_processor_cleanup(proc); return NULL; } - -void sqfs_block_processor_destroy(sqfs_block_processor_t *proc) -{ - unsigned int i; - - pthread_mutex_lock(&proc->mtx); - proc->status = -1; - pthread_cond_broadcast(&proc->queue_cond); - pthread_mutex_unlock(&proc->mtx); - - for (i = 0; i < proc->num_workers; ++i) { - pthread_join(proc->workers[i]->thread, NULL); - - proc->workers[i]->cmp->destroy(proc->workers[i]->cmp); - free(proc->workers[i]); - } - - pthread_cond_destroy(&proc->done_cond); - pthread_cond_destroy(&proc->queue_cond); - pthread_mutex_destroy(&proc->mtx); - - block_processor_cleanup(proc); -} #endif int append_to_work_queue(sqfs_block_processor_t *proc, sqfs_block_t *block, diff --git a/lib/sqfs/block_writer.c b/lib/sqfs/block_writer.c index f77fcf8..63f8c61 100644 --- a/lib/sqfs/block_writer.c +++ b/lib/sqfs/block_writer.c @@ -26,6 +26,7 @@ typedef struct { } blk_info_t; struct sqfs_block_writer_t { + sqfs_object_t *base; sqfs_file_t *file; size_t num_blocks; @@ -115,6 +116,12 @@ static int align_file(sqfs_block_writer_t *wr) return store_block_location(wr, size, 0, 0); } +static void block_writer_destroy(sqfs_object_t *wr) +{ + free(((sqfs_block_writer_t *)wr)->blocks); + free(wr); +} + sqfs_block_writer_t *sqfs_block_writer_create(sqfs_file_t *file, size_t devblksz, sqfs_u32 flags) { @@ -127,6 +134,7 @@ sqfs_block_writer_t *sqfs_block_writer_create(sqfs_file_t *file, if (wr == NULL) return NULL; + ((sqfs_object_t *)wr)->destroy = block_writer_destroy; wr->file = file; wr->devblksz = devblksz; wr->max_blocks = INIT_BLOCK_COUNT; @@ -153,12 +161,6 @@ int sqfs_block_writer_set_hooks(sqfs_block_writer_t *wr, void *user_ptr, return 0; } -void sqfs_block_writer_destroy(sqfs_block_writer_t *wr) -{ - free(wr->blocks); - free(wr); -} - int sqfs_block_writer_write(sqfs_block_writer_t *wr, sqfs_u32 size, sqfs_u32 checksum, sqfs_u32 flags, const sqfs_u8 *data, sqfs_u64 *location) diff --git a/lib/sqfs/comp/gzip.c b/lib/sqfs/comp/gzip.c index 7183e5a..37491bb 100644 --- a/lib/sqfs/comp/gzip.c +++ b/lib/sqfs/comp/gzip.c @@ -31,7 +31,7 @@ typedef struct { gzip_options_t opt; } gzip_compressor_t; -static void gzip_destroy(sqfs_compressor_t *base) +static void gzip_destroy(sqfs_object_t *base) { gzip_compressor_t *gzip = (gzip_compressor_t *)base; @@ -264,10 +264,10 @@ sqfs_compressor_t *gzip_compressor_create(const sqfs_compressor_config_t *cfg) gzip->compress = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) == 0; gzip->block_size = cfg->block_size; base->do_block = gzip_do_block; - base->destroy = gzip_destroy; base->write_options = gzip_write_options; base->read_options = gzip_read_options; base->create_copy = gzip_create_copy; + ((sqfs_object_t *)base)->destroy = gzip_destroy; if (gzip->compress) { ret = deflateInit2(&gzip->strm, cfg->opt.gzip.level, diff --git a/lib/sqfs/comp/lz4.c b/lib/sqfs/comp/lz4.c index dd9b6ac..7656d67 100644 --- a/lib/sqfs/comp/lz4.c +++ b/lib/sqfs/comp/lz4.c @@ -110,7 +110,7 @@ static sqfs_compressor_t *lz4_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)lz4; } -static void lz4_destroy(sqfs_compressor_t *base) +static void lz4_destroy(sqfs_object_t *base) { free(base); } @@ -132,11 +132,11 @@ sqfs_compressor_t *lz4_compressor_create(const sqfs_compressor_config_t *cfg) lz4->high_compression = (cfg->flags & SQFS_COMP_FLAG_LZ4_HC) != 0; - base->destroy = lz4_destroy; base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ? lz4_uncomp_block : lz4_comp_block; base->write_options = lz4_write_options; base->read_options = lz4_read_options; base->create_copy = lz4_create_copy; + ((sqfs_object_t *)base)->destroy = lz4_destroy; return base; } diff --git a/lib/sqfs/comp/lzma.c b/lib/sqfs/comp/lzma.c index f7ab428..900cc14 100644 --- a/lib/sqfs/comp/lzma.c +++ b/lib/sqfs/comp/lzma.c @@ -153,7 +153,7 @@ static sqfs_compressor_t *lzma_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)copy; } -static void lzma_destroy(sqfs_compressor_t *base) +static void lzma_destroy(sqfs_object_t *base) { free(base); } @@ -176,11 +176,11 @@ sqfs_compressor_t *lzma_compressor_create(const sqfs_compressor_config_t *cfg) if (lzma->block_size < SQFS_META_BLOCK_SIZE) lzma->block_size = SQFS_META_BLOCK_SIZE; - base->destroy = lzma_destroy; base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ? lzma_uncomp_block : lzma_comp_block; base->write_options = lzma_write_options; base->read_options = lzma_read_options; base->create_copy = lzma_create_copy; + ((sqfs_object_t *)base)->destroy = lzma_destroy; return base; } diff --git a/lib/sqfs/comp/xz.c b/lib/sqfs/comp/xz.c index 91d48da..d612729 100644 --- a/lib/sqfs/comp/xz.c +++ b/lib/sqfs/comp/xz.c @@ -207,7 +207,7 @@ static sqfs_compressor_t *xz_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)xz; } -static void xz_destroy(sqfs_compressor_t *base) +static void xz_destroy(sqfs_object_t *base) { free(base); } @@ -233,11 +233,11 @@ sqfs_compressor_t *xz_compressor_create(const sqfs_compressor_config_t *cfg) xz->flags = cfg->flags; xz->dict_size = cfg->opt.xz.dict_size; xz->block_size = cfg->block_size; - base->destroy = xz_destroy; base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ? xz_uncomp_block : xz_comp_block; base->write_options = xz_write_options; base->read_options = xz_read_options; base->create_copy = xz_create_copy; + ((sqfs_object_t *)base)->destroy = xz_destroy; return base; } diff --git a/lib/sqfs/comp/zstd.c b/lib/sqfs/comp/zstd.c index a850033..4bd9456 100644 --- a/lib/sqfs/comp/zstd.c +++ b/lib/sqfs/comp/zstd.c @@ -110,7 +110,7 @@ static sqfs_compressor_t *zstd_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)zstd; } -static void zstd_destroy(sqfs_compressor_t *base) +static void zstd_destroy(sqfs_object_t *base) { zstd_compressor_t *zstd = (zstd_compressor_t *)base; @@ -142,11 +142,11 @@ sqfs_compressor_t *zstd_compressor_create(const sqfs_compressor_config_t *cfg) return NULL; } - base->destroy = zstd_destroy; base->do_block = cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS ? zstd_uncomp_block : zstd_comp_block; base->write_options = zstd_write_options; base->read_options = zstd_read_options; base->create_copy = zstd_create_copy; + ((sqfs_object_t *)base)->destroy = zstd_destroy; return base; } diff --git a/lib/sqfs/data_reader.c b/lib/sqfs/data_reader.c index c8886c5..8c9bb2e 100644 --- a/lib/sqfs/data_reader.c +++ b/lib/sqfs/data_reader.c @@ -21,6 +21,8 @@ #include struct sqfs_data_reader_t { + sqfs_object_t obj; + sqfs_frag_table_t *frag_tbl; sqfs_compressor_t *cmp; sqfs_file_t *file; @@ -125,6 +127,16 @@ static int precache_fragment_block(sqfs_data_reader_t *data, size_t idx) &data->frag_blk_size, &data->frag_block); } +static void data_reader_destroy(sqfs_object_t *obj) +{ + sqfs_data_reader_t *data = (sqfs_data_reader_t *)obj; + + sqfs_destroy(data->frag_tbl); + free(data->data_block); + free(data->frag_block); + free(data); +} + sqfs_data_reader_t *sqfs_data_reader_create(sqfs_file_t *file, size_t block_size, sqfs_compressor_t *cmp) @@ -140,6 +152,7 @@ sqfs_data_reader_t *sqfs_data_reader_create(sqfs_file_t *file, return NULL; } + ((sqfs_object_t *)data)->destroy = data_reader_destroy; data->file = file; data->block_size = block_size; data->cmp = cmp; @@ -164,14 +177,6 @@ int sqfs_data_reader_load_fragment_table(sqfs_data_reader_t *data, return 0; } -void sqfs_data_reader_destroy(sqfs_data_reader_t *data) -{ - sqfs_frag_table_destroy(data->frag_tbl); - free(data->data_block); - free(data->frag_block); - free(data); -} - int sqfs_data_reader_get_block(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, size_t index, size_t *size, sqfs_u8 **out) diff --git a/lib/sqfs/dir_reader.c b/lib/sqfs/dir_reader.c index 8eae5bd..39e3e57 100644 --- a/lib/sqfs/dir_reader.c +++ b/lib/sqfs/dir_reader.c @@ -20,6 +20,8 @@ #include struct sqfs_dir_reader_t { + sqfs_object_t base; + sqfs_meta_reader_t *meta_dir; sqfs_meta_reader_t *meta_inode; const sqfs_super_t *super; @@ -34,6 +36,15 @@ struct sqfs_dir_reader_t { sqfs_u16 inode_offset; }; +static void dir_reader_destroy(sqfs_object_t *obj) +{ + sqfs_dir_reader_t *rd = (sqfs_dir_reader_t *)obj; + + sqfs_destroy(rd->meta_inode); + sqfs_destroy(rd->meta_dir); + free(rd); +} + sqfs_dir_reader_t *sqfs_dir_reader_create(const sqfs_super_t *super, sqfs_compressor_t *cmp, sqfs_file_t *file) @@ -66,22 +77,16 @@ sqfs_dir_reader_t *sqfs_dir_reader_create(const sqfs_super_t *super, rd->meta_dir = sqfs_meta_reader_create(file, cmp, start, limit); if (rd->meta_dir == NULL) { - sqfs_meta_reader_destroy(rd->meta_inode); + sqfs_destroy(rd->meta_inode); free(rd); return NULL; } + ((sqfs_object_t *)rd)->destroy = dir_reader_destroy; rd->super = super; return rd; } -void sqfs_dir_reader_destroy(sqfs_dir_reader_t *rd) -{ - sqfs_meta_reader_destroy(rd->meta_inode); - sqfs_meta_reader_destroy(rd->meta_dir); - free(rd); -} - int sqfs_dir_reader_open_dir(sqfs_dir_reader_t *rd, const sqfs_inode_generic_t *inode) { diff --git a/lib/sqfs/dir_writer.c b/lib/sqfs/dir_writer.c index 53e5800..9dd7ad2 100644 --- a/lib/sqfs/dir_writer.c +++ b/lib/sqfs/dir_writer.c @@ -37,6 +37,8 @@ typedef struct index_ent_t { } index_ent_t; struct sqfs_dir_writer_t { + sqfs_object_t base; + dir_entry_t *list; dir_entry_t *list_end; @@ -136,6 +138,15 @@ static int add_export_table_entry(sqfs_dir_writer_t *writer, return 0; } +static void dir_writer_destroy(sqfs_object_t *obj) +{ + sqfs_dir_writer_t *writer = (sqfs_dir_writer_t *)obj; + + writer_reset(writer); + free(writer->export_tbl); + free(writer); +} + sqfs_dir_writer_t *sqfs_dir_writer_create(sqfs_meta_writer_t *dm, sqfs_u32 flags) { @@ -162,17 +173,11 @@ sqfs_dir_writer_t *sqfs_dir_writer_create(sqfs_meta_writer_t *dm, sizeof(writer->export_tbl[0]) * writer->export_tbl_max); } + ((sqfs_object_t *)writer)->destroy = dir_writer_destroy; writer->dm = dm; return writer; } -void sqfs_dir_writer_destroy(sqfs_dir_writer_t *writer) -{ - writer_reset(writer); - free(writer->export_tbl); - free(writer); -} - int sqfs_dir_writer_begin(sqfs_dir_writer_t *writer, sqfs_u32 flags) { sqfs_u32 offset; diff --git a/lib/sqfs/frag_table.c b/lib/sqfs/frag_table.c index 200f3d5..58851c5 100644 --- a/lib/sqfs/frag_table.c +++ b/lib/sqfs/frag_table.c @@ -27,6 +27,8 @@ typedef struct { struct sqfs_frag_table_t { + sqfs_object_t base; + size_t capacity; size_t used; sqfs_fragment_t *table; @@ -37,6 +39,15 @@ struct sqfs_frag_table_t { chunk_info_t *chunk_list; }; +static void frag_table_destroy(sqfs_object_t *obj) +{ + sqfs_frag_table_t *tbl = (sqfs_frag_table_t *)obj; + + free(tbl->chunk_list); + free(tbl->table); + free(tbl); +} + sqfs_frag_table_t *sqfs_frag_table_create(sqfs_u32 flags) { sqfs_frag_table_t *tbl; @@ -48,16 +59,10 @@ sqfs_frag_table_t *sqfs_frag_table_create(sqfs_u32 flags) if (tbl == NULL) return NULL; + ((sqfs_object_t *)tbl)->destroy = frag_table_destroy; return tbl; } -void sqfs_frag_table_destroy(sqfs_frag_table_t *tbl) -{ - free(tbl->chunk_list); - free(tbl->table); - free(tbl); -} - int sqfs_frag_table_read(sqfs_frag_table_t *tbl, sqfs_file_t *file, const sqfs_super_t *super, sqfs_compressor_t *cmp) { diff --git a/lib/sqfs/id_table.c b/lib/sqfs/id_table.c index 8c30964..86f2809 100644 --- a/lib/sqfs/id_table.c +++ b/lib/sqfs/id_table.c @@ -17,23 +17,35 @@ #include struct sqfs_id_table_t { + sqfs_object_t base; + sqfs_u32 *ids; size_t num_ids; size_t max_ids; }; +static void id_table_destroy(sqfs_object_t *obj) +{ + sqfs_id_table_t *tbl = (sqfs_id_table_t *)obj; + + free(tbl->ids); + free(tbl); +} + sqfs_id_table_t *sqfs_id_table_create(sqfs_u32 flags) { + sqfs_id_table_t *tbl; + if (flags != 0) return NULL; - return calloc(1, sizeof(sqfs_id_table_t)); -} + tbl = calloc(1, sizeof(sqfs_id_table_t)); -void sqfs_id_table_destroy(sqfs_id_table_t *tbl) -{ - free(tbl->ids); - free(tbl); + if (tbl != NULL) { + ((sqfs_object_t *)tbl)->destroy = id_table_destroy; + } + + return tbl; } int sqfs_id_table_id_to_index(sqfs_id_table_t *tbl, sqfs_u32 id, sqfs_u16 *out) diff --git a/lib/sqfs/meta_reader.c b/lib/sqfs/meta_reader.c index 19e856a..8fd7f21 100644 --- a/lib/sqfs/meta_reader.c +++ b/lib/sqfs/meta_reader.c @@ -18,6 +18,8 @@ #include struct sqfs_meta_reader_t { + sqfs_object_t base; + sqfs_u64 start; sqfs_u64 limit; size_t data_used; @@ -44,6 +46,11 @@ struct sqfs_meta_reader_t { sqfs_u8 scratch[SQFS_META_BLOCK_SIZE]; }; +static void meta_reader_destroy(sqfs_object_t *m) +{ + free(m); +} + sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file, sqfs_compressor_t *cmp, sqfs_u64 start, sqfs_u64 limit) @@ -53,6 +60,7 @@ sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file, if (m == NULL) return NULL; + ((sqfs_object_t *)m)->destroy = meta_reader_destroy; m->start = start; m->limit = limit; m->file = file; @@ -60,11 +68,6 @@ sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file, return m; } -void sqfs_meta_reader_destroy(sqfs_meta_reader_t *m) -{ - free(m); -} - int sqfs_meta_reader_seek(sqfs_meta_reader_t *m, sqfs_u64 block_start, size_t offset) { diff --git a/lib/sqfs/meta_writer.c b/lib/sqfs/meta_writer.c index acb2235..46a67cd 100644 --- a/lib/sqfs/meta_writer.c +++ b/lib/sqfs/meta_writer.c @@ -25,6 +25,8 @@ typedef struct meta_block_t { } meta_block_t; struct sqfs_meta_writer_t { + sqfs_object_t base; + /* A byte offset into the uncompressed data of the current block */ size_t offset; @@ -53,6 +55,20 @@ static int write_block(sqfs_file_t *file, meta_block_t *outblk) return file->write_at(file, off, outblk->data, count + 2); } +static void meta_writer_destroy(sqfs_object_t *obj) +{ + sqfs_meta_writer_t *m = (sqfs_meta_writer_t *)obj; + meta_block_t *blk; + + while (m->list != NULL) { + blk = m->list; + m->list = blk->next; + free(blk); + } + + free(m); +} + sqfs_meta_writer_t *sqfs_meta_writer_create(sqfs_file_t *file, sqfs_compressor_t *cmp, sqfs_u32 flags) @@ -66,25 +82,13 @@ sqfs_meta_writer_t *sqfs_meta_writer_create(sqfs_file_t *file, if (m == NULL) return NULL; + ((sqfs_object_t *)m)->destroy = meta_writer_destroy; m->cmp = cmp; m->file = file; m->flags = flags; return m; } -void sqfs_meta_writer_destroy(sqfs_meta_writer_t *m) -{ - meta_block_t *blk; - - while (m->list != NULL) { - blk = m->list; - m->list = blk->next; - free(blk); - } - - free(m); -} - int sqfs_meta_writer_flush(sqfs_meta_writer_t *m) { meta_block_t *outblk; diff --git a/lib/sqfs/read_table.c b/lib/sqfs/read_table.c index 096ac1a..1c9c8ad 100644 --- a/lib/sqfs/read_table.c +++ b/lib/sqfs/read_table.c @@ -76,12 +76,12 @@ int sqfs_read_table(sqfs_file_t *file, sqfs_compressor_t *cmp, table_size -= diff; } - sqfs_meta_reader_destroy(m); + sqfs_destroy(m); free(locations); *out = data; return 0; fail: - sqfs_meta_reader_destroy(m); + sqfs_destroy(m); fail_idx: free(locations); fail_data: diff --git a/lib/sqfs/unix/io_file.c b/lib/sqfs/unix/io_file.c index f93af4d..1c181ab 100644 --- a/lib/sqfs/unix/io_file.c +++ b/lib/sqfs/unix/io_file.c @@ -25,7 +25,7 @@ typedef struct { } sqfs_file_stdio_t; -static void stdio_destroy(sqfs_file_t *base) +static void stdio_destroy(sqfs_object_t *base) { sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; @@ -154,10 +154,10 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) file->size = sb.st_size; - base->destroy = stdio_destroy; base->read_at = stdio_read_at; base->write_at = stdio_write_at; base->get_size = stdio_get_size; base->truncate = stdio_truncate; + ((sqfs_object_t *)base)->destroy = stdio_destroy; return base; } diff --git a/lib/sqfs/win32/io_file.c b/lib/sqfs/win32/io_file.c index 951a0fe..c1849a8 100644 --- a/lib/sqfs/win32/io_file.c +++ b/lib/sqfs/win32/io_file.c @@ -24,7 +24,7 @@ typedef struct { } sqfs_file_stdio_t; -static void stdio_destroy(sqfs_file_t *base) +static void stdio_destroy(sqfs_object_t *base) { sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; @@ -161,10 +161,10 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) } file->size = size.QuadPart; - base->destroy = stdio_destroy; base->read_at = stdio_read_at; base->write_at = stdio_write_at; base->get_size = stdio_get_size; base->truncate = stdio_truncate; + ((sqfs_object_t *)base)->destroy = stdio_destroy; return base; } diff --git a/lib/sqfs/write_table.c b/lib/sqfs/write_table.c index 1931b83..b198941 100644 --- a/lib/sqfs/write_table.c +++ b/lib/sqfs/write_table.c @@ -74,7 +74,7 @@ int sqfs_write_table(sqfs_file_t *file, sqfs_compressor_t *cmp, /* cleanup */ ret = 0; out: - sqfs_meta_writer_destroy(m); + sqfs_destroy(m); out_idx: free(locations); return ret; diff --git a/lib/sqfs/xattr_reader.c b/lib/sqfs/xattr_reader.c index d5b003b..97cf821 100644 --- a/lib/sqfs/xattr_reader.c +++ b/lib/sqfs/xattr_reader.c @@ -21,6 +21,8 @@ #include struct sqfs_xattr_reader_t { + sqfs_object_t base; + sqfs_u64 xattr_start; size_t num_id_blocks; @@ -34,6 +36,20 @@ struct sqfs_xattr_reader_t { sqfs_file_t *file; }; +static void xattr_reader_destroy(sqfs_object_t *obj) +{ + sqfs_xattr_reader_t *xr = (sqfs_xattr_reader_t *)obj; + + if (xr->kvrd != NULL) + sqfs_destroy(xr->kvrd); + + if (xr->idrd != NULL) + sqfs_destroy(xr->idrd); + + free(xr->id_block_starts); + free(xr); +} + int sqfs_xattr_reader_load_locations(sqfs_xattr_reader_t *xr) { sqfs_xattr_id_table_t idtbl; @@ -242,18 +258,6 @@ int sqfs_xattr_reader_get_desc(sqfs_xattr_reader_t *xr, sqfs_u32 idx, return 0; } -void sqfs_xattr_reader_destroy(sqfs_xattr_reader_t *xr) -{ - if (xr->kvrd != NULL) - sqfs_meta_reader_destroy(xr->kvrd); - - if (xr->idrd != NULL) - sqfs_meta_reader_destroy(xr->idrd); - - free(xr->id_block_starts); - free(xr); -} - sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file, sqfs_super_t *super, sqfs_compressor_t *cmp) @@ -263,6 +267,7 @@ sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file, if (xr == NULL) return NULL; + ((sqfs_object_t *)xr)->destroy = xattr_reader_destroy; xr->file = file; xr->super = super; @@ -286,6 +291,6 @@ sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file, return xr; fail: - sqfs_xattr_reader_destroy(xr); + sqfs_destroy(xr); return NULL; } diff --git a/lib/sqfs/xattr_writer.c b/lib/sqfs/xattr_writer.c index 047e411..827d0c5 100644 --- a/lib/sqfs/xattr_writer.c +++ b/lib/sqfs/xattr_writer.c @@ -99,6 +99,8 @@ typedef struct kv_block_desc_t { } kv_block_desc_t; struct sqfs_xattr_writer_t { + sqfs_object_t base; + str_table_t keys; str_table_t values; @@ -113,6 +115,23 @@ struct sqfs_xattr_writer_t { }; +static void xattr_writer_destroy(sqfs_object_t *obj) +{ + sqfs_xattr_writer_t *xwr = (sqfs_xattr_writer_t *)obj; + kv_block_desc_t *blk; + + while (xwr->kv_blocks != NULL) { + blk = xwr->kv_blocks; + xwr->kv_blocks = xwr->kv_blocks->next; + free(blk); + } + + free(xwr->kv_pairs); + str_table_cleanup(&xwr->values); + str_table_cleanup(&xwr->keys); + free(xwr); +} + sqfs_xattr_writer_t *sqfs_xattr_writer_create(void) { sqfs_xattr_writer_t *xwr = calloc(1, sizeof(*xwr)); @@ -129,6 +148,7 @@ sqfs_xattr_writer_t *sqfs_xattr_writer_create(void) if (xwr->kv_pairs == NULL) goto fail_pairs; + ((sqfs_object_t *)xwr)->destroy = xattr_writer_destroy; return xwr; fail_pairs: str_table_cleanup(&xwr->values); @@ -139,22 +159,6 @@ fail_keys: return NULL; } -void sqfs_xattr_writer_destroy(sqfs_xattr_writer_t *xwr) -{ - kv_block_desc_t *blk; - - while (xwr->kv_blocks != NULL) { - blk = xwr->kv_blocks; - xwr->kv_blocks = xwr->kv_blocks->next; - free(blk); - } - - free(xwr->kv_pairs); - str_table_cleanup(&xwr->values); - str_table_cleanup(&xwr->keys); - free(xwr); -} - int sqfs_xattr_writer_begin(sqfs_xattr_writer_t *xwr) { xwr->kv_start = xwr->num_pairs; @@ -603,6 +607,6 @@ int sqfs_xattr_writer_flush(sqfs_xattr_writer_t *xwr, sqfs_file_t *file, locations, count); out: free(locations); - sqfs_meta_writer_destroy(mw); + sqfs_destroy(mw); return err; } diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index 5e350c8..357dad7 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -96,7 +96,7 @@ static int pack_files(sqfs_block_processor_t *data, fstree_t *fs, SZ_ADD_OV(sizeof(*inode), size, &size)) { fputs("creating file inode: too many blocks\n", stderr); - file->destroy(file); + sqfs_destroy(file); free(node_path); return -1; } @@ -104,7 +104,7 @@ static int pack_files(sqfs_block_processor_t *data, fstree_t *fs, inode = calloc(1, size); if (inode == NULL) { perror("creating file inode"); - file->destroy(file); + sqfs_destroy(file); free(node_path); return -1; } @@ -121,7 +121,7 @@ static int pack_files(sqfs_block_processor_t *data, fstree_t *fs, flags |= SQFS_BLK_DONT_FRAGMENT; ret = write_data_from_file(path, data, inode, file, flags); - file->destroy(file); + sqfs_destroy(file); free(node_path); if (ret) diff --git a/tar/sqfs2tar.c b/tar/sqfs2tar.c index 2fb30cb..5a786dd 100644 --- a/tar/sqfs2tar.c +++ b/tar/sqfs2tar.c @@ -688,17 +688,17 @@ out_tree: sqfs_dir_tree_destroy(root); out_xr: if (xr != NULL) - sqfs_xattr_reader_destroy(xr); + sqfs_destroy(xr); out_dr: - sqfs_dir_reader_destroy(dr); + sqfs_destroy(dr); out_data: - sqfs_data_reader_destroy(data); + sqfs_destroy(data); out_id: - sqfs_id_table_destroy(idtbl); + sqfs_destroy(idtbl); out_cmp: - cmp->destroy(cmp); + sqfs_destroy(cmp); out_fd: - file->destroy(file); + sqfs_destroy(file); out_dirs: for (i = 0; i < num_subdirs; ++i) free(subdirs[i]); diff --git a/tar/tar2sqfs.c b/tar/tar2sqfs.c index 856a1f4..51987fe 100644 --- a/tar/tar2sqfs.c +++ b/tar/tar2sqfs.c @@ -303,7 +303,7 @@ static int write_file(tar_header_decoded_t *hdr, file_info_t *fi, flags |= SQFS_BLK_DONT_FRAGMENT; ret = write_data_from_file(hdr->name, sqfs.data, inode, file, 0); - file->destroy(file); + sqfs_destroy(file); if (ret) return -1; diff --git a/unpack/rdsquashfs.c b/unpack/rdsquashfs.c index 14a88ae..99e51fb 100644 --- a/unpack/rdsquashfs.c +++ b/unpack/rdsquashfs.c @@ -179,18 +179,18 @@ int main(int argc, char **argv) out: sqfs_dir_tree_destroy(n); out_data: - sqfs_data_reader_destroy(data); + sqfs_destroy(data); out_dr: - sqfs_dir_reader_destroy(dirrd); + sqfs_destroy(dirrd); out_id: - sqfs_id_table_destroy(idtbl); + sqfs_destroy(idtbl); out_xr: if (xattr != NULL) - sqfs_xattr_reader_destroy(xattr); + sqfs_destroy(xattr); out_cmp: - cmp->destroy(cmp); + sqfs_destroy(cmp); out_file: - file->destroy(file); + sqfs_destroy(file); out_cmd: free(opt.cmdpath); return status; -- cgit v1.2.3