From 85e36aab1258d8d9ec7b61ce013f167ef8e03ae0 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 5 Mar 2020 22:38:15 +0100 Subject: Change the signature of sqfs_compressor_create to return an error code Make sure the function has a way of telling the caller *why* it failed. This way, the function can convey whether it had an internal error, an allocation failure, whether the arguments are totaly nonsensical, or simply that the compressor *or specific configuration* is not supported. Signed-off-by: David Oberhollenzer --- lib/common/comp_lzo.c | 17 ++++++++++------- lib/common/writer.c | 8 ++++---- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'lib/common') diff --git a/lib/common/comp_lzo.c b/lib/common/comp_lzo.c index 24f4009..0956e0e 100644 --- a/lib/common/comp_lzo.c +++ b/lib/common/comp_lzo.c @@ -217,25 +217,26 @@ static void lzo_destroy(sqfs_object_t *base) free(base); } -sqfs_compressor_t *lzo_compressor_create(const sqfs_compressor_config_t *cfg) +int lzo_compressor_create(const sqfs_compressor_config_t *cfg, + sqfs_compressor_t **out) { sqfs_compressor_t *base; lzo_compressor_t *lzo; size_t scratch_size; if (cfg->flags & ~SQFS_COMP_FLAG_GENERIC_ALL) - return NULL; + return SQFS_ERROR_UNSUPPORTED; if (cfg->opt.lzo.algorithm >= LZO_NUM_ALGS || lzo_algs[cfg->opt.lzo.algorithm].compress == NULL) { - return NULL; + return SQFS_ERROR_UNSUPPORTED; } if (cfg->opt.lzo.algorithm == SQFS_LZO1X_999) { if (cfg->opt.lzo.level > SQFS_LZO_MAX_LEVEL) - return NULL; + return SQFS_ERROR_UNSUPPORTED; } else if (cfg->opt.lzo.level != 0) { - return NULL; + return SQFS_ERROR_UNSUPPORTED; } /* XXX: liblzo does not do bounds checking internally, @@ -257,7 +258,7 @@ sqfs_compressor_t *lzo_compressor_create(const sqfs_compressor_config_t *cfg) base = (sqfs_compressor_t *)lzo; if (lzo == NULL) - return NULL; + return SQFS_ERROR_ALLOC; lzo->block_size = cfg->block_size; lzo->algorithm = cfg->opt.lzo.algorithm; @@ -272,5 +273,7 @@ sqfs_compressor_t *lzo_compressor_create(const sqfs_compressor_config_t *cfg) base->read_options = lzo_read_options; ((sqfs_object_t *)base)->copy = lzo_create_copy; ((sqfs_object_t *)base)->destroy = lzo_destroy; - return base; + + *out = base; + return 0; } diff --git a/lib/common/writer.c b/lib/common/writer.c index 4762b82..84a98a5 100644 --- a/lib/common/writer.c +++ b/lib/common/writer.c @@ -85,19 +85,19 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) if (fstree_init(&sqfs->fs, wrcfg->fs_defaults)) goto fail_file; - sqfs->cmp = sqfs_compressor_create(&cfg); + ret = sqfs_compressor_create(&cfg, &sqfs->cmp); #ifdef WITH_LZO if (cfg.id == SQFS_COMP_LZO) { if (sqfs->cmp != NULL) sqfs_destroy(sqfs->cmp); - sqfs->cmp = lzo_compressor_create(&cfg); + ret = lzo_compressor_create(&cfg, &sqfs->cmp); } #endif - if (sqfs->cmp == NULL) { - fputs("Error creating compressor\n", stderr); + if (ret != 0) { + sqfs_perror(wrcfg->filename, "creating compressor", ret); goto fail_fs; } -- cgit v1.2.3